1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* URL Class |
4
|
|
|
* |
5
|
|
|
* @package TheyWorkForYou |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace MySociety\TheyWorkForYou; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* URL Class |
12
|
|
|
* |
13
|
|
|
* The URL class is used for generating URLs and other related things. |
14
|
|
|
* Relies on there being a get_http_var() function. |
15
|
|
|
* |
16
|
|
|
* This is probably how you'll use it most: |
17
|
|
|
* |
18
|
|
|
* ```php |
19
|
|
|
* $URL = new \MySociety\TheyWorkForYou\Url("YOURPAGENAME"); |
20
|
|
|
* print $URL->generate(); |
21
|
|
|
* ``` |
22
|
|
|
* |
23
|
|
|
* In the metadata you should set a session_vars variable, an array. |
24
|
|
|
* The default page session_vars may be just array("debug"). |
25
|
|
|
* These can then be overridden on a per-page basis. |
26
|
|
|
* Session vars are GET/POST vars that will be passed by default to that page. |
27
|
|
|
* ie, if "foo=bar" is in the current URL and you generate a URL to a page that has "foo" |
28
|
|
|
* as a session_var, "foo=bar" will be automatically added to the generated URL. |
29
|
|
|
* You can modify the session vars that will be included in the URL generated using the functions below. |
30
|
|
|
* |
31
|
|
|
* @author Phil Gyford <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
class Url { |
35
|
|
|
|
36
|
25 |
|
public function __construct($pagename) { |
37
|
|
|
// Initialise. |
38
|
25 |
|
global $DATA; |
39
|
|
|
|
40
|
|
|
// The page we're going to be generating URL(s) for. |
41
|
25 |
|
$this->destinationpage = $pagename; |
|
|
|
|
42
|
|
|
|
43
|
|
|
// These stores an associative array of key/value pairs that |
44
|
|
|
// we'll want passed on to other pages. |
45
|
25 |
|
$this->session_vars = array (); |
|
|
|
|
46
|
|
|
|
47
|
|
|
// Prevent things using $DATA running if it hasn't been set, ie in testing |
48
|
25 |
|
if (isset($DATA)) { |
49
|
|
|
|
50
|
|
|
// Set the contents of $this->session_vars. |
51
|
|
|
// session_vars are variables we generally want to pass between pages, if any. |
52
|
|
|
// Will only be added as vars if they have values. |
53
|
|
|
|
54
|
25 |
|
$keys = $DATA->page_metadata($this->destinationpage, "session_vars"); |
55
|
25 |
|
foreach ($keys as $key) { |
56
|
25 |
|
if (get_http_var($key) != "") { |
57
|
25 |
|
$this->session_vars[$key] = get_http_var($key); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Some pages have the same URL, modified by a "pg" variable. |
62
|
|
|
// See if this page is one such, and add the variable if so. |
63
|
25 |
|
if ($pg = $DATA->page_metadata($this->destinationpage, "pg")) { |
64
|
|
|
$this->session_vars["pg"] = $pg; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// So we can restore the originals. |
70
|
25 |
|
$this->original_session_vars = $this->session_vars; |
|
|
|
|
71
|
|
|
|
72
|
25 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Restore Session Variables |
76
|
|
|
* |
77
|
|
|
* Restores the session variables to their state when the object was |
78
|
|
|
* instantiated. |
79
|
|
|
*/ |
80
|
|
|
|
81
|
|
|
public function restore() { |
82
|
|
|
// Call this to reset the session vars to how they were when |
83
|
|
|
// the object was instantiated. |
84
|
|
|
$this->session_vars = $this->original_session_vars; |
|
|
|
|
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Reset Session Variables |
90
|
|
|
* |
91
|
|
|
* Removes all session variables. |
92
|
|
|
*/ |
93
|
|
|
|
94
|
1 |
|
public function reset() { |
95
|
|
|
// Call this to remove all the session_vars. |
96
|
1 |
|
$this->session_vars = array (); |
|
|
|
|
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Insert Session Key/Value Pairs |
101
|
|
|
* |
102
|
|
|
* @param array $arr An associative array of key/value pairs |
103
|
|
|
*/ |
104
|
|
|
|
105
|
19 |
|
public function insert($arr) { |
106
|
|
|
// $arr is an associative array of key/value pairs. |
107
|
|
|
// These will be used as session_vars in addition to any that |
108
|
|
|
// already exist. |
109
|
19 |
|
foreach ($arr as $key => $val) { |
110
|
19 |
|
$this->session_vars[$key] = $val; |
|
|
|
|
111
|
|
|
} |
112
|
19 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove Session Key/Value Pair |
116
|
|
|
* |
117
|
|
|
* @param array $arr A list array of key names to remove |
118
|
|
|
*/ |
119
|
|
|
|
120
|
5 |
|
public function remove($arr) { |
121
|
|
|
// $arr is a list array of key names. Any key/value pairs |
122
|
|
|
// in session_vars with keys found in $arr will be removed. |
123
|
5 |
|
foreach ($arr as $key) { |
124
|
5 |
|
if (isset($this->session_vars[$key])) { |
125
|
5 |
|
unset($this->session_vars[$key]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
5 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Update Values |
132
|
|
|
* |
133
|
|
|
* Any keys in session_vars that are also in $arr |
134
|
|
|
* will have their values overwritten by those in $arr. |
135
|
|
|
* Other session_var key/vals are not affected. |
136
|
|
|
* |
137
|
|
|
* @param array $arr An associative array of key/value pairs. |
138
|
|
|
*/ |
139
|
|
|
|
140
|
8 |
|
public function update($arr) { |
141
|
|
|
// |
142
|
|
|
// |
143
|
8 |
|
foreach ($arr as $key => $val) { |
144
|
8 |
|
if (isset($this->session_vars[$key])) { |
145
|
8 |
|
$this->session_vars[$key] = $arr[$key]; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
8 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Generate URL |
152
|
|
|
* |
153
|
|
|
* Generate a URL to the page specified with session vars. |
154
|
|
|
* |
155
|
|
|
* @param string encode "html" will make the URL suitable for inclusion |
|
|
|
|
156
|
|
|
* in a page directly, "none" will leave it alone, |
157
|
|
|
* "url" will do exactly the same. |
158
|
|
|
* @param array overrideVars A key=>value mapping which allows some specific |
|
|
|
|
159
|
|
|
* variable/value pairs to be overridden/inserted |
160
|
|
|
* into the query. Use this when you want to keep |
161
|
|
|
* the standard 'session vars' in a url, but |
162
|
|
|
* override just one or two of them. |
163
|
|
|
*/ |
164
|
|
|
|
165
|
25 |
|
public function generate($encode = "html", $overrideVars=array()) { |
166
|
|
|
|
167
|
25 |
|
global $DATA; |
168
|
|
|
|
169
|
25 |
|
$url_args = array (); |
170
|
|
|
|
171
|
25 |
|
foreach (array_merge($this->session_vars, $overrideVars) as $key => $var) { |
172
|
19 |
|
if (is_array($var)) { |
173
|
|
|
foreach ($var as $v) { |
174
|
|
|
$url_args[] = "$key=" . urlencode(stripslashes($v)); |
175
|
|
|
} |
176
|
19 |
|
} elseif ($var != null) |
177
|
19 |
|
$url_args[] = "$key=" . urlencode(stripslashes($var)); |
178
|
|
|
} |
179
|
|
|
|
180
|
25 |
|
$page_url = WEBPATH . $DATA->page_metadata($this->destinationpage, "url"); |
181
|
|
|
|
182
|
25 |
|
if (sizeof($url_args) == 0) { |
183
|
10 |
|
return $page_url; |
184
|
|
|
} else { |
185
|
19 |
|
if ($encode == "html") { |
186
|
11 |
|
return $page_url . "?" . implode("&", $url_args); |
187
|
8 |
|
} elseif ($encode == "none" || $encode == "url") { |
188
|
8 |
|
return $page_url . "?" . implode("&", $url_args); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|