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);
17 define ('METHOD' , REGULAR
);
20 * Object to generate URLs
24 static public $instance;
26 public $options = array ();
31 protected $to, // REGULAR : NAV_VAR=$topic | FRIENDLY : /$topic/
32 $action, // REGULAR : do=$action | FRIENDLY : /$topic/$action/
33 $major, // REGULAR : &$major=$title
34 $title, // FRIENDLY : ../$major/$title
37 public function __construct ($opt = null) {
39 if (defined('BASE_URL'))
40 $options['base'] = BASE_URL
;
42 $options['base'] = '';
47 // public function getInstance () {
48 // if (tsUrl::$instance instanceof tsUrl) {
49 // tsUrl::$instance = new tsUrl ();
52 // return tsUrl::$instance;
55 public function __destruct () {
60 * method to return the url as a string
63 public function __toString () {
68 * method to generate an friendly URL slug
70 static public function buildSlug () {
74 static function getValue ($string, $varName = '') {
75 if (stristr ($string, ':')) {
76 if (!stristr ($string, $varName)) {
79 if (stristr ($string,'/'))
80 substr ($string, 0, -1);
81 return str_replace ($varName . ':', '', $string);
86 static function getVarName ($string) {
87 if (stristr ($string, ':')) {
88 return str_replace (stristr ($string,':'), '', $string);
93 static private function getFriendlyParams () {
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
])
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;
118 static public function composeUrl ($incArr) {
120 // FIXME: find a way around hard-coding the $glue and $separator
121 if (defined('METHOD') && METHOD
== FRIENDLY
) {
126 $separator = '&';
129 foreach ($incArr as $key => $val) {
130 if (!empty($key) && !empty($val)) {
131 $retStr .= (($separator == '&') ||
(($key != 'to') && ($key != 'do')) ?
$key . $glue : '') . $val . $separator;
135 if ($separator == '&') {
136 // to remove the final '&'
137 $retStr = '/?'.substr ($retStr, 0, -5);
139 $retStr = '/' . $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];
153 static public function getRequest ($varName = null) {
154 if (defined('METHOD') && METHOD
== FRIENDLY
) {
155 $params = tsUrl
::getFriendlyParams ();
157 $params = tsUrl
::getRegularParams ();
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
193 // $retUrl .= $key . (!empty($val) ? ':' . $val . '/' : '');
200 // $outStr .= '/' . $whereTo . $retUrl;
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
214 // $tArr[] = $key.(!empty($val) ? '='.$val : '');
217 // $retUrl = implode ('&', $tArr).$end;
218 // } elseif (is_string($varVal)){
219 // $retUrl = $varVal;
224 // if ($method == 'get'){
225 // if (!empty ($whereTo) && !empty ($retUrl) )
226 // $whereTo .= '&';
227 // $outStr .= '/?' . $whereTo . $retUrl;