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