made a copy
[atutor.git] / include / classes / UrlRewrite / GlossaryUrl.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 * UrlParser
16 * Class for rewriting pretty urls on forums.
17 * @access       public
18 * @author       Harris Wong
19 * @package      UrlParser
20 */
21 class GlossaryUrl {
22         // constructor
23         function GlossaryUrl() {
24                 $this->rule = array(0=>'p');
25         }
26
27         /**
28          * Construct pretty url by the given query string.
29          */
30         function constructPrettyUrl($query){
31                 if (empty($query)){
32                         return '';
33                 }
34
35                 //Take out bookmark
36                 if (($pos = strpos($query, '#'))!==FALSE){
37                         $bookmark = substr($query, $pos);
38                         $query = substr($query, 0, $pos);
39                 }
40
41                 //If this is already a pretty url,but without mod_apache rule
42                 //unwrap it and reconstruct
43                 if (is_array($query)){
44                         $new_query = '';
45                         foreach($query as $fk=>$fv){
46                                 if      (preg_match('/\.php/', $fv)==1){
47                                         continue;       //skip the php file
48                                 }
49
50                                 //check if this is part of the rule, if so,add it, o/w ignore
51                                 if (array_search($fv, $this->rule)!==FALSE){
52                                         $new_query .= $fv . '=' . $query[$fk+1] . SEP;
53                                 } elseif (preg_match('/([0-9]+)\.html/', $fv, $matches)==1){
54                                         $new_query .= 'page=' . $matches[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
72                 //construct pretty url on mapping
73                 foreach ($this->rule as $key=>$value){
74
75                         //if this value is empty, the url construction should quit.
76                         if ($query_parts[$value] ==''){
77                                 break;
78                         }
79                         $url .= $query_parts[$value].'/';
80
81                         //if the query parts are not in the defined rules, set it back to query string again
82                         if ($query_parts[$this->rule[$key]]!=''){
83                                 $query_parts[$this->rule[$key]] = '';
84                         }
85                 }
86
87                 //Go through the query_parts again, and for those values that are not empty
88                 // add it to the querystring
89                 foreach($query_parts as $key=>$value){
90                         //paginator are handle differently
91                         if ($value!='' && $key!='page'){
92                                 $query_string .= $key.'='.$value.SEP;
93                         }
94                 }
95                 //take out the last sep.
96                 $query_string = substr($query_string, 0, -1);
97
98                 //handle paginators
99                 if ($query_parts['page']!=''){
100                         $url .= $query_parts['page'].'.html';
101                 }
102
103                 //append query string at the back
104                 if ($query_string!=''){
105                         $url .= '?'.$query_string;
106                 }
107
108                 //finally, append bookmark if not emptied
109                 if ($bookmark!=''){
110                         $url .= $bookmark;
111                 }
112
113                 return $url;
114         }
115 }
116 ?>