* simplifications of tsUrl and tsRouter - still not working fully
[vsc.git] / _res / _libs / tsurl.class.php
blob2298476617527844660f3b18c91bb516547c2a1d
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' , FRIENDLY);
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 __destruct () {
51 /**
52 * method to return the url as a string
55 public function __toString () {
59 /**
60 * method to generate an friendly URL slug
62 static public function buildSlug () {
63 // TODO
66 static function getValue ($string, $varName = '') {
67 if (stristr ($string, ':')) {
68 if (!stristr ($string, $varName)) {
69 return false;
71 if (stristr ($string,'/'))
72 substr ($string, 0, -1);
73 // returns what's right of $glue=: in $string
74 return str_replace ($varName . ':', '', $string);
76 // we have no var name on the left of glue
77 return $string;
80 static function getVarName ($string) {
81 if (stristr ($string, ':')) {
82 // returns what's left of $glue=: in $string (ie, the var name)
83 return str_replace (stristr ($string,':'), '', $string);
85 return null;
88 static private function getFriendlyParams () {
89 $parArr = explode ('/', $_SERVER['REQUEST_URI']); // fixme: $separator needed
90 array_shift($parArr);
91 $size = sizeof ($parArr);
93 while ($size > 0) {
94 foreach ($parArr as $cont) {
95 $key = tsUrl::getVarName ($cont);
96 $retArr[] = tsUrl::getValue ($cont, $key);
97 $size--;
101 // navigation variable - logically first
102 if (!empty ($retArr[0])) {
103 $retArr[NAV_VAR] = $retArr[0];
104 unset ($retArr[0]);
106 // action variable - right after
107 if (!empty ($retArr[1])) {
108 $retArr[ACT_VAR] = $retArr[1];
109 unset ($retArr[1]);
112 // var_dump($retArr);
113 return $retArr;
116 static public function composeUrl ($incArr) {
117 $retStr = '';
118 // FIXME: find a way around hard-coding the $glue and $separator
119 if (defined('METHOD') && METHOD == FRIENDLY) {
120 $glue = ':';
121 $separator = '/';
122 } else {
123 $glue = '=';
124 $separator = '&amp;';
127 foreach ($incArr as $key => $val) {
128 if (!empty($key) && !empty($val)) {
129 $retStr .= (($separator == '&amp;') || (($key != 'to') && ($key != 'do')) ? $key . $glue : '') . $val . $separator;
133 if ($separator == '&amp;') {
134 // to remove the final '&amp;'
135 $retStr = '/?'.substr ($retStr, 0, -5);
136 } else {
137 $retStr = '/' . $retStr;
140 return $retStr;
143 static private function getRegularParams () {
144 // $this->to = $_GET[NAV_VAR];
145 // dumb - this contradicts my previous idea of letting the $to object
146 // decide what to do with the URL params = less flexibility
147 // $this->action = $_GET[ACT_VAR];
148 return $_GET;
151 static public function getRequest ($varName = null) {
152 if (defined('METHOD') && METHOD == FRIENDLY) {
153 $params = tsUrl::getFriendlyParams ();
154 } else {
155 $params = tsUrl::getRegularParams ();
158 if (!$varName) {
159 return $params;
160 } elseif (!empty ($_REQUEST[$varName])) {
161 return $_REQUEST[$varName];
162 } elseif (!empty ($params[$varName])) {
163 return $params[$varName];
168 * method to return the URL based on $method
170 * TODO: find a method to overlook folders
171 * - done by hardcoding folder names in the rewrite rule
173 static public function setRequest ($whereTo = null, $varVal = null) {
174 // var_dump($varVal);
175 // generating friendly URLs
176 if (is_null($varVal) && !is_array ($varVal)) {
177 $varVal[] = (int)$varVal;
180 $paramArr = array_merge (array ('to' => $whereTo) , $varVal);
181 return URL . tsUrl::composeUrl ($paramArr);