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 __destruct () {
52 * method to return the url as a string
55 public function __toString () {
60 * method to generate an friendly URL slug
62 static public function buildSlug () {
66 static function getValue ($string, $varName = '') {
67 if (stristr ($string, ':')) {
68 if (!stristr ($string, $varName)) {
71 if (stristr ($string,'/'))
72 substr ($string, 0, -1);
73 return str_replace ($varName . ':', '', $string);
78 static function getVarName ($string) {
79 if (stristr ($string, ':')) {
80 return str_replace (stristr ($string,':'), '', $string);
85 static private function getFriendlyParams () {
87 list($urlStr) = array_keys ($_GET);
88 $parArr = explode ('/', $urlStr); // fixme: $separator needed
90 $retArr[NAV_VAR
] = tsUrl
::getValue ($parArr[0], NAV_VAR
);
91 $retArr[ACT_VAR
] = tsUrl
::getValue ($parArr[1], ACT_VAR
);
93 // we have a 'do' action
97 if (sizeof ($parArr) > $sizeof) {
98 $others = array_slice ($parArr, $sizeof);
100 foreach ($others as $cont) {
101 $key = tsUrl
::getVarName ($cont);
102 $val = tsUrl
::getValue ($cont, $key);
103 $retArr[$key] = $val;
109 static public function composeUrl ($incArr) {
111 // FIXME: find a way around hard-coding the $glue and $separator
112 if (defined('METHOD') && METHOD
== FRIENDLY
) {
117 $separator = '&';
120 foreach ($incArr as $key => $val) {
121 if (!empty($key) && !empty($val)) {
122 $retStr .= (($separator == '&') ||
(($key != 'to') && ($key != 'do')) ?
$key . $glue : '') . $val . $separator;
126 if ($separator == '&') {
127 // to remove the final '&'
128 $retStr = '/?'.substr ($retStr, 0, -5);
130 $retStr = '/' . $retStr;
136 static private function getRegularParams () {
137 // $this->to = $_GET[NAV_VAR];
138 // dumb - this contradicts my previous idea of letting the $to object
139 // decide what to do with the URL params = less flexibility
140 // $this->action = $_GET[ACT_VAR];
144 static public function getRequest ($varName = null) {
145 if (defined('METHOD') && METHOD
== FRIENDLY
) {
146 $params = tsUrl
::getFriendlyParams ();
148 $params = tsUrl
::getRegularParams ();
153 } elseif (!empty ($_REQUEST[$varName])) {
154 return $_REQUEST[$varName];
155 } elseif (!empty ($params[$varName])) {
156 return $params[$varName];
161 * method to return the URL based on $method
163 * TODO: find a method to overlook folders
164 * - done by hardcoding folder names in the rewrite rule
166 static public function setRequest ($whereTo = null, $varVal = null) {
167 // var_dump($varVal);
168 // generating friendly URLs
169 if (is_null($varVal) && !is_array ($varVal)) {
170 $varVal[] = (int)$varVal;
173 $paramArr = array_merge (array ('to' => $whereTo) , $varVal);
174 return URL
. tsUrl
::composeUrl ($paramArr);