remove old readme
[atutor.git] / include / classes / UrlRewrite / GlossaryUrl.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 * UrlParser
15 * Class for rewriting pretty urls on forums.
16 * @access       public
17 * @author       Harris Wong
18 * @package      UrlParser
19 */
20 class GlossaryUrl {
21         // constructor
22         function GlossaryUrl() {
23                 $this->rule = array(0=>'p');
24         }
25
26         /**
27          * Construct pretty url by the given query string.
28          */
29         function constructPrettyUrl($query){
30                 if (empty($query)){
31                         return '';
32                 }
33
34                 //Take out bookmark
35                 if (($pos = strpos($query, '#'))!==FALSE){
36                         $bookmark = substr($query, $pos);
37                         $query = substr($query, 0, $pos);
38                 }
39
40                 //If this is already a pretty url,but without mod_apache rule
41                 //unwrap it and reconstruct
42                 if (is_array($query)){
43                         $new_query = '';
44                         foreach($query as $fk=>$fv){
45                                 if      (preg_match('/\.php/', $fv)==1){
46                                         continue;       //skip the php file
47                                 }
48
49                                 //check if this is part of the rule, if so,add it, o/w ignore
50                                 if (array_search($fv, $this->rule)!==FALSE){
51                                         $new_query .= $fv . '=' . $query[$fk+1] . SEP;
52                                 } elseif (preg_match('/([0-9]+)\.html/', $fv, $matches)==1){
53                                         $new_query .= 'page=' . $matches[1] . SEP;
54                                 }
55                         }
56                         $query = $new_query;    //done
57                 }
58
59                 $temp = explode(SEP, $query);
60                 foreach ($temp as $index=>$attributes){
61                         if(empty($attributes)){
62                                 //skip the ones that are empty.
63                                 continue;
64                         }
65                         list($key, $value) = preg_split('/\=/', $attributes, 2);
66                         $query_parts[$key] = $value;
67                 }
68
69                 $query_string = '';
70
71                 //construct pretty url on mapping
72                 foreach ($this->rule as $key=>$value){
73
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 ?>