* refactored the tsPage::[get|set]Request to tsUrl::[get|set]Request
[vsc.git] / _res / _libs / tsurl.class.php
blob27b9d5f5494379eb309607c79201c79397e42b19
1 <?php
2 /**
3 * TODO: if (stristr ($topic, '~')) {
4 * $topic = ??; // TBD
5 * $major = 'user';
6 * $title = substr($topic, 1);
7 * }
8 */
9 // will generate : http://base_url/topic/major/title/minor1/1/minor2/2
10 define ('FRIENDLY' , 1);
11 // will generate : http://base_url/?NAV_VAR=topic&major=title&minor1=1&minor2=2
12 define ('REGULAR' , 2);
14 define ('METHOD' , FRIENDLY);
16 /**
17 * Object to generate URLs
20 class tsUrl {
21 public $options = array ();
23 /**
24 * The URL components
26 protected $topic, // REGULAR : NAV_VAR=$topic | FRIENDLY : /$topic/
27 $action,// REGULAR : do=$action | FRIENDLY : /$topic/$action/
28 $major, // REGULAR : &amp;$major=$title
29 $title, // FRIENDLY : ../$major/$title
30 $minors; // TBD
32 public function __construct ($opt = null) {
33 if (is_null ($opt)) {
34 if (defined('BASE_URL'))
35 $options['base'] = BASE_URL;
36 else
37 $options['base'] = '';
42 public function __destruct () {
46 /**
47 * method to return the url as a string
50 public function __toString () {
54 /**
55 * method to generate an friendly URL slug
57 public function buildSlug () {
61 public function getRequest ($varName) {
62 if (defined('METHOD') && METHOD == FRIENDLY) {
63 // TODO
64 list($urlStr) = array_keys ($_GET);
65 $parArr = explode ('/',$urlStr);
67 $to = $parArr[0];
68 $action = $parArr[1];
69 $title = $parArr[2];
71 if (sizeof($parArr) > 3) {
72 $minors = array_slice ($parArr, 3);
73 // foreach ( $minors as $key => $cont) {
74 // if ($key)
75 // }
78 } else {
79 if (is_null($varName))
80 return $_REQUEST;
81 if (isset($_REQUEST[$varName]))
82 return $_REQUEST[$varName];
83 else
84 return false;
88 /**
89 * method to return the URL based on $method
91 * TODO: find a method to overlook folders
92 * - done by hardcoding folder names in the rewrite rule
94 public function setRequest ($whereTo = null, $varVal = null) {
95 // generating friendly URLs
96 if ($this->method == FRIENDLY) {
97 // TODO
98 } else {
99 if (!empty($whereTo))
100 $whereTo = NAV_VAR . '=' . $whereTo;
102 if (is_array($varVal)) {
103 foreach ($varVal as $key => $val){
104 if (is_numeric($key) && stristr ($val, '#')) {
105 // probably an href with id in it
106 $end = $val;
107 } else {
108 $tArr[] = $key.(!empty($val) ? '='.$val : '');
111 $retUrl = implode ('&amp;', $tArr).$end;
112 } elseif (is_string($varVal)){
113 $retUrl = $varVal;
116 $outStr = URL;
118 if ($method == 'get'){
119 if (!empty ($whereTo) && !empty ($retUrl) )
120 $whereTo .= '&amp;';
121 $outStr .= '/?' . $whereTo . $retUrl;
124 return $outStr;