remove old readme
[atutor.git] / docs / include / classes / UrlRewrite / ContentUrl.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13 /**
14 * ContentUrl
15 * Class for rewriting pretty urls.
16 * @access       public
17 * @author       Harris Wong
18 * @package      UrlParser
19 */
20 class ContentUrl {
21         // constructor
22         function ContentUrl() {
23                 $this->rule = array(0=>'cid');
24         }
25
26         /**
27          * Construct pretty url by the given query string.
28          */
29         function constructPrettyUrl($query){
30                 $url = '';  //url to be returned
31                 $bookmark = '';         //html bookmark
32
33                 if (empty($query)){
34                         return '';
35                 }
36
37                 //Take out bookmark
38                 if (($pos = strpos($query, '#'))!==FALSE){
39                         $bookmark = substr($query, $pos);
40                         $query = substr($query, 0, $pos);
41                 }
42
43                 //If this is already a pretty url,but without mod_apache rule
44                 //unwrap it and reconstruct
45                 if (is_array($query)){
46                         $new_query = '';
47                         foreach($query as $fk=>$fv){
48                                 if      (preg_match('/\.php/', $fv)==1){
49                                         continue;       //skip the php file
50                                 }
51
52                                 //check if this is part of the rule, if so,add it, o/w ignore
53                                 if (array_search($fv, $this->rule)!==FALSE){
54                                         $new_query .= $fv . '=' . $query[$fk+1] . SEP;
55                                 }
56                         }
57                         $query = $new_query;    //done
58                 }
59
60                 $temp = explode(SEP, $query);
61                 foreach ($temp as $index=>$attributes){
62                         if(empty($attributes)){
63                                 //skip the ones that are empty.
64                                 continue;
65                         }
66                         list($key, $value) = preg_split('/\=/', $attributes, 2);
67                         $query_parts[$key] = $value;
68                 }
69
70                 $query_string = '';
71                 //construct pretty url on mapping
72                 foreach ($this->rule as $key=>$value){
73                         //if this value is empty, the url construction should quit.
74                         if ($query_parts[$value] ==''){
75                                 break;
76                         }
77                         $url .= $query_parts[$value].'/';
78
79                         //if the query parts are not in the defined rules, set it back to query string again
80                         if ($query_parts[$this->rule[$key]]!=''){
81                                 $query_parts[$this->rule[$key]] = '';
82                         }
83                 }
84
85                 //Go through the query_parts again, and for those values that are not empty
86                 // add it to the querystring
87                 foreach($query_parts as $key=>$value){
88                         //paginator are handle differently
89                         if ($value!='' && $key!='page'){
90                                 $query_string .= $key.'='.$value.SEP;
91                         }
92                 }
93                 //take out the last sep.
94                 $query_string = substr($query_string, 0, -1);
95
96                 //handle paginators
97 //              if ($query_parts['page']!=''){
98 //                      $url .= '/'.$query_parts['page'].'.html';
99 //              }
100
101                 //append query string at the back
102                 if ($query_string!=''){
103                         $url .= '?'.$query_string;
104                 }
105
106                 //finally, append bookmark if not emptied
107                 if ($bookmark!=''){
108                         $url .= $bookmark;
109                 }
110
111                 return $url;
112         }
113 }
114 ?>