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