move code up one directory
[atutor.git] / include / classes / UrlRewrite / TestsUrl.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 TestsUrl {
21         // local variables
22 //      var $rule;              //an array that maps [lvl->query parts]
23 //      var $className; //the name of this class
24
25         // constructor
26         function TestsUrl() {
27                 $this->rule = array(0=>'tid', 1=>'action');
28         }
29
30
31         /**
32          * Construct pretty url by the given query string.
33          */
34         function constructPrettyUrl($query){
35                 $url = '';  //url to be returned
36
37                 if (empty($query)){
38                         return '';
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                 return $url;
109         }
110 }
111 ?>