3 * TODO: if (stristr ($topic, '~')) {
6 * $title = substr($topic, 1);
8 * FIXME: 1. the whole static idea sucks - no way around it so far
9 * 2. I need to think of a method like the Zend router objects for getting/setting urls
12 // will generate : http://base_url/topic/major/title/minor1/1/minor2/2
13 define ('FRIENDLY' , 1);
14 // will generate : http://base_url/?NAV_VAR=topic&major=title&minor1=1&minor2=2
15 define ('REGULAR' , 2);
16 define ('METHOD' , FRIENDLY
);
19 * Object to generate URLs
23 static public $instance;
25 public $options = array ();
30 protected $to, // REGULAR : NAV_VAR=$topic | FRIENDLY : /$topic/
31 $action, // REGULAR : do=$action | FRIENDLY : /$topic/$action/
32 $major, // REGULAR : &$major=$title
33 $title, // FRIENDLY : ../$major/$title
36 public function __construct ($opt = null) {
38 if (defined('BASE_URL'))
39 $options['base'] = BASE_URL
;
41 $options['base'] = '';
46 public function __destruct () {
51 * method to return the url as a string
54 public function __toString () {
59 * method to generate an friendly URL slug
61 static public function buildSlug () {
65 static function getValue ($string, $varName = '') {
66 if (stristr ($string, ':')) {
67 if (!stristr ($string, $varName)) {
70 if (stristr ($string,'/'))
71 substr ($string, 0, -1);
72 // returns what's right of $glue=: in $string
73 return str_replace ($varName . ':', '', $string);
75 // we have no var name on the left of glue
79 static function getVarName ($string) {
80 if (stristr ($string, ':')) {
81 // returns what's left of $glue=: in $string (ie, the var name)
82 return str_replace (stristr ($string,':'), '', $string);
87 static public function getController () {
88 return tsUrl
::getRequest (NAV_VAR
);
91 static private function getFriendlyParams () {
93 // var_dump(substr(stristr($_SERVER['REDIRECT_URI'], 'index.php/'), 10));die;
94 // this only works for lighttpd, for Apache we need to use $_SERVER['REDIRECT_URL']
95 if (stristr($_SERVER['SERVER_SOFTWARE'], 'Apache'))
96 $redirectUri = 'REDIRECT_URL';
97 elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'lighttpd'))
98 $redirectUri = 'REDIRECT_URI';
100 $redirectUri = 'REDIRECT_URI';
102 $parArr = explode ('/', substr(stristr($_SERVER[$redirectUri], 'index.php/'), 10));
103 // array_shift($parArr);
104 $size = sizeof ($parArr);
105 // var_dump(explode ('/', substr(stristr($_SERVER['REDIRECT_URI'], 'index.php/'), 10)));
107 foreach ($parArr as $k => $cont) {
108 if (!empty ($k) ||
!empty ($cont)) {
109 $key = tsUrl
::getVarName ($cont);
110 $retArr[$key ?
$key : $k] = tsUrl
::getValue ($cont, $key);
116 // navigation variable - logically first
117 if (!empty ($retArr[0])) {
118 $retArr[NAV_VAR
] = $retArr[0];
121 // action variable - right after
122 if (!empty ($retArr[1])) {
123 $retArr[ACT_VAR
] = $retArr[1];
129 static public function composeUrl ($incArr) {
131 if (!is_array ($incArr)) {
134 // FIXME: find a way around hard-coding the $glue and $separator
135 if (defined('METHOD') && METHOD
== FRIENDLY
) {
140 $separator = '&';
143 foreach ($incArr as $key => $val) {
144 if (!empty($key) && !empty($val)) {
145 $retStr .= (($separator == '&') ||
(($key != 'to') && ($key != 'do')) ?
$key . $glue : '') . $val . $separator;
149 if ($separator == '&') {
150 // to remove the final '&'
151 $retStr = '/?'.substr ($retStr, 0, -5);
153 $retStr = '/' . $retStr;
159 static private function getRegularParams () {
160 // $this->to = $_GET[NAV_VAR];
161 // dumb - this contradicts my previous idea of letting the $to object
162 // decide what to do with the URL params = less flexibility
163 // $this->action = $_GET[ACT_VAR];
167 static public function getRequest ($varName = null) {
168 if (defined('METHOD') && METHOD
== FRIENDLY
) {
169 $params = tsUrl
::getFriendlyParams ();
171 $params = tsUrl
::getRegularParams ();
176 } elseif (!empty ($_REQUEST[$varName])) {
177 return $_REQUEST[$varName];
178 } elseif (!empty ($params[$varName])) {
179 return $params[$varName];
184 * method to return the URL based on $method
186 * TODO: find a method to overlook folders
187 * - done by hardcoding folder names in the rewrite rule
189 static public function setRequest ($whereTo = null, $varVal = array()) {
190 // var_dump($varVal);
191 // generating friendly URLs
192 // if (is_null ($whereTo))
193 // $to = tsUrl::getController ();
195 if (!is_array ($varVal)) {
199 // var_dump($varVal);
200 $paramArr = array_merge (array ('to' => $whereTo) , $varVal);
201 return URL
. tsUrl
::composeUrl ($paramArr);