Revert "* initial work towards factorizing the controllers (yeah, now it's tsControll...
[vsc.git] / _res / _libs / tsurl.class.php
blob1feeedff0a459ed8ed0b11ae483053389163c5a8
1 <?php
2 /**
3 * TODO: if (stristr ($topic, '~')) {
4 * $topic = ??; // TBD
5 * $major = 'user';
6 * $title = substr($topic, 1);
7 * }
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);
17 define ('METHOD' , REGULAR);
19 /**
20 * Object to generate URLs
23 class tsUrl {
24 static public $instance;
26 public $options = array ();
28 /**
29 * The URL components
31 protected $to, // REGULAR : NAV_VAR=$topic | FRIENDLY : /$topic/
32 $action, // REGULAR : do=$action | FRIENDLY : /$topic/$action/
33 $major, // REGULAR : &amp;$major=$title
34 $title, // FRIENDLY : ../$major/$title
35 $minors; // TBD
37 public function __construct ($opt = null) {
38 if (is_null ($opt)) {
39 if (defined('BASE_URL'))
40 $options['base'] = BASE_URL;
41 else
42 $options['base'] = '';
47 // public function getInstance () {
48 // if (tsUrl::$instance instanceof tsUrl) {
49 // tsUrl::$instance = new tsUrl ();
50 // }
51 //
52 // return tsUrl::$instance;
53 // }
55 public function __destruct () {
59 /**
60 * method to return the url as a string
63 public function __toString () {
67 /**
68 * method to generate an friendly URL slug
70 static public function buildSlug () {
71 // TODO
74 static function getValue ($string, $varName = '') {
75 if (stristr ($string, ':')) {
76 if (!stristr ($string, $varName)) {
77 return false;
79 if (stristr ($string,'/'))
80 substr ($string, 0, -1);
81 return str_replace ($varName . ':', '', $string);
83 return $string;
86 static function getVarName ($string) {
87 if (stristr ($string, ':')) {
88 return str_replace (stristr ($string,':'), '', $string);
90 return $string;
93 static private function getFriendlyParams () {
94 $sizeof = 1;
95 list($urlStr) = array_keys ($_GET);
96 $parArr = explode ('/', $urlStr); // fixme: $separator needed
98 $retArr[NAV_VAR] = tsUrl::getValue ($parArr[0], NAV_VAR);
99 $retArr[ACT_VAR] = tsUrl::getValue ($parArr[1], ACT_VAR);
100 // var_dump($retArr);die;
101 // we have a 'do' action
102 if ($retArr[ACT_VAR])
103 $sizeof = 2;
105 if (sizeof ($parArr) > $sizeof) {
106 $others = array_slice ($parArr, $sizeof);
108 foreach ($others as $cont) {
109 $key = tsUrl::getVarName ($cont);
110 $val = tsUrl::getValue ($cont, $key);
111 $retArr[$key] = $val;
115 return $retArr;
118 static public function composeUrl ($incArr) {
119 $retStr = '';
120 // FIXME: find a way around hard-coding the $glue and $separator
121 if (defined('METHOD') && METHOD == FRIENDLY) {
122 $glue = ':';
123 $separator = '/';
124 } else {
125 $glue = '=';
126 $separator = '&amp;';
129 foreach ($incArr as $key => $val) {
130 if (!empty($key) && !empty($val)) {
131 $retStr .= (($separator == '&amp;') || (($key != 'to') && ($key != 'do')) ? $key . $glue : '') . $val . $separator;
135 if ($separator == '&amp;') {
136 // to remove the final '&amp;'
137 $retStr = '/?'.substr ($retStr, 0, -5);
138 } else {
139 $retStr = '/' . $retStr;
142 return $retStr;
145 static private function getRegularParams () {
146 // $this->to = $_GET[NAV_VAR];
147 // dumb - this contradicts my previous idea of letting the $to object
148 // decide what to do with the URL params = less flexibility
149 // $this->action = $_GET[ACT_VAR];
150 return $_GET;
153 static public function getRequest ($varName = null) {
154 if (defined('METHOD') && METHOD == FRIENDLY) {
155 $params = tsUrl::getFriendlyParams ();
156 } else {
157 $params = tsUrl::getRegularParams ();
160 if (!$varName) {
161 return $params;
162 } else {
163 if (!empty ($_REQUEST[$varName]))
164 return $_REQUEST[$varName];
165 return $params[$varName];
170 * method to return the URL based on $method
172 * TODO: find a method to overlook folders
173 * - done by hardcoding folder names in the rewrite rule
175 static public function setRequest ($whereTo = null, $varVal = null) {
176 // var_dump($varVal);
177 // generating friendly URLs
178 if (!is_array ($varVall)) {
179 $varVal[] = $varVall;
181 $paramArr = array_merge (array ('to'=>$whereTo) , $varVal);
182 return URL . tsUrl::composeUrl ($paramArr);
183 // if (defined('METHOD') && METHOD == FRIENDLY) {
184 // if (!empty($whereTo))
185 // $whereTo = $whereTo . '/';
187 // if (is_array($varVal)) {
188 // foreach ($varVal as $key => $val){
189 // if (is_numeric($key) && stristr ($val, '#')) {
190 // // probably an href with id in it
191 // $end = $val;
192 // } else {
193 // $retUrl .= $key . (!empty($val) ? ':' . $val . '/' : '');
194 // }
195 // }
196 // }
198 // $outStr = URL;
200 // $outStr .= '/' . $whereTo . $retUrl;
202 // return $outStr;
204 // } else {
205 // if (!empty($whereTo))
206 // $whereTo = NAV_VAR . '=' . $whereTo;
208 // if (is_array($varVal)) {
209 // foreach ($varVal as $key => $val){
210 // if (is_numeric($key) && stristr ($val, '#')) {
211 // // probably an href with id in it
212 // $end = $val;
213 // } else {
214 // $tArr[] = $key.(!empty($val) ? '='.$val : '');
215 // }
216 // }
217 // $retUrl = implode ('&amp;', $tArr).$end;
218 // } elseif (is_string($varVal)){
219 // $retUrl = $varVal;
220 // }
222 // $outStr = URL;
224 // if ($method == 'get'){
225 // if (!empty ($whereTo) && !empty ($retUrl) )
226 // $whereTo .= '&amp;';
227 // $outStr .= '/?' . $whereTo . $retUrl;
228 // }
230 // return $outStr;
231 // }