Merge file:///home/habarnam/public_html/vsc_rewrite/ into friendly_url
[vsc.git] / _res / _libs / tsurl.class.php
blobd9730d3008f43b0c9013054066f062f5e4a4975f
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 __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 return str_replace ($varName . ':', '', $string);
75 return $string;
78 static function getVarName ($string) {
79 if (stristr ($string, ':')) {
80 return str_replace (stristr ($string,':'), '', $string);
82 return $string;
85 static private function getFriendlyParams () {
86 $sizeof = 1;
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
94 if ($retArr[ACT_VAR])
95 $sizeof = 2;
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;
106 return $retArr;
109 static public function composeUrl ($incArr) {
110 $retStr = '';
111 // FIXME: find a way around hard-coding the $glue and $separator
112 if (defined('METHOD') && METHOD == FRIENDLY) {
113 $glue = ':';
114 $separator = '/';
115 } else {
116 $glue = '=';
117 $separator = '&amp;';
120 foreach ($incArr as $key => $val) {
121 if (!empty($key) && !empty($val)) {
122 $retStr .= (($separator == '&amp;') || (($key != 'to') && ($key != 'do')) ? $key . $glue : '') . $val . $separator;
126 if ($separator == '&amp;') {
127 // to remove the final '&amp;'
128 $retStr = '/?'.substr ($retStr, 0, -5);
129 } else {
130 $retStr = '/' . $retStr;
133 return $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];
141 return $_GET;
144 static public function getRequest ($varName = null) {
145 if (defined('METHOD') && METHOD == FRIENDLY) {
146 $params = tsUrl::getFriendlyParams ();
147 } else {
148 $params = tsUrl::getRegularParams ();
151 if (!$varName) {
152 return $params;
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);