made a copy
[atutor.git] / include / classes / UrlRewrite / ForumsUrl.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 ForumsUrl {
22         // constructor
23         function ForumsUrl() {
24                 $this->rule = array(0=>'fid', 1=>'pid');
25         }
26
27
28         /**
29          * Construct pretty url by the given query string.
30          */
31         function constructPrettyUrl($query){
32                 if (empty($query)){
33                         return '';
34                 }
35
36                 //If this is already a pretty url,but without mod_apache rule
37                 //unwrap it and reconstruct
38                 if (is_array($query)){
39                         $new_query = '';
40                         foreach($query as $fk=>$fv){
41                                 if      (preg_match('/\.php/', $fv)==1){
42                                         continue;       //skip the php file
43                                 }
44
45                                 //check if this is part of the rule, if so,add it, o/w ignore
46                                 if (array_search($fv, $this->rule)!==FALSE){
47                                         $new_query .= $fv . '=' . $query[$fk+1] . SEP;
48                                 } elseif (preg_match('/([0-9]+)\.html/', $fv, $matches)==1){
49                                         $new_query .= 'page=' . $matches[1] . SEP;
50                                 }
51                         }
52                         $query = $new_query;    //done
53                 }
54
55                 $temp = explode(SEP, $query);
56                 foreach ($temp as $index=>$attributes){
57                         if(empty($attributes)){
58                                 //skip the ones that are empty.
59                                 continue;
60                         }
61                         list($key, $value) = preg_split('/\=/', $attributes, 2);
62                         $query_parts[$key] = $value;
63                 }
64
65                 $query_string = '';
66
67                 //construct pretty url on mapping
68                 foreach ($this->rule as $key=>$value){
69
70                         //if this value is empty, the url construction should quit.
71                         if ($query_parts[$value] ==''){
72                                 break;
73                         }
74                         $url .= $query_parts[$value].'/';
75
76                         //if the query parts are not in the defined rules, set it back to query string again
77                         if ($query_parts[$this->rule[$key]]!=''){
78                                 $query_parts[$this->rule[$key]] = '';
79                         }
80                 }
81
82                 //Go through the query_parts again, and for those values that are not empty
83                 // add it to the querystring
84                 foreach($query_parts as $key=>$value){
85                         //paginator are handle differently
86                         if ($value!='' && $key!='page'){
87                                 $query_string .= $key.'='.$value.SEP;
88                         }
89                 }
90                 //take out the last sep.
91                 $query_string = substr($query_string, 0, -1);
92
93                 //handle paginators
94                 if ($query_parts['page']!=''){
95                         $url .= $query_parts['page'].'.html';
96                 }
97
98                 //append query string at the back
99                 if ($query_string!=''){
100                         $url .= '?'.$query_string;
101                 }
102
103                 return $url;
104         }
105 }
106 ?>