-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute_params.php
More file actions
331 lines (264 loc) · 9.44 KB
/
route_params.php
File metadata and controls
331 lines (264 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace SmartHistoryTourManager;
/**
* A poor man's router.
* This basically determines "routes" defined as GET params in urls.
*/
class RouteParams {
// a list of parameter keys order by names
const KEYS = [
'controller' => 'shtm_c',
'action' => 'shtm_a',
'id' => 'shtm_id',
'area_id' => 'shtm_area_id',
'tour_id' => 'shtm_tour_id',
'scene_id' => 'shtm_scene_id',
'back_params' => 'shtm_back_params'
];
// build GET parametrs for a query by adding all params to the current GET
// parameters
// The "route" build here is just a string of GET parameters
// If $remove others is given, all of our parameters, i.e. the ones of this
// plugin are removed before
private static function params_to_route($added_params = array(), $remove_others = true) {
if($remove_others) {
$get = self::current_get_params(array_values(self::KEYS));
} else {
$get = self::current_get_params();
}
return http_build_query(array_merge($get, $added_params));
}
public static function default_page() {
return self::index_tours();
}
// returns a copy of the current get parameters, omitting the given keys
public static function current_get_params($omit = array()) {
$get = $_GET;
foreach($omit as $omit_key) {
unset($get[$omit_key]);
}
return $get;
}
private static function make_route(
$controller,
$action,
$id = null,
$tour_id = null,
$area_id = null,
$scene_id = null)
{
$params = array(
self::KEYS['controller'] => $controller,
self::KEYS['action'] => $action
);
if(!is_null($id)) {
$params[self::KEYS['id']] = $id;
}
if(!is_null($tour_id)) {
$params[self::KEYS['tour_id']] = $tour_id;
}
if(!is_null($area_id)) {
$params[self::KEYS['area_id']] = $area_id;
}
if(!is_null($scene_id)) {
$params[self::KEYS['scene_id']] = $scene_id;
}
if(!is_null($back_params)) {
$params[self::KEYS['area_id']] = $area_id;
}
return self::params_to_route($params);
}
// parse the supplied string of query params and return an array
private static function params_from_str($str) {
$result;
parse_str($str, $result);
return $result;
}
// takes a param str generated by this class and outputs a link to the
// admin page using these params
public static function admin_link($param_str) {
return 'admin.php?' . $param_str;
}
// Areas
public static function index_areas() {
return self::make_route('area', 'index');
}
public static function new_area() {
return self::make_route('area', 'new');
}
public static function create_area() {
return self::make_route('area', 'create');
}
public static function edit_area($id) {
return self::make_route('area', 'edit', $id);
}
public static function update_area($id) {
return self::make_route('area', 'update', $id);
}
public static function delete_area($id) {
return self::make_route('area', 'delete', $id);
}
public static function destroy_area($id) {
return self::make_route('area', 'destroy', $id);
}
// Places
public static function index_places($area_id = null) {
return self::make_route('place', 'index', null, null, $area_id);
}
public static function new_place() {
return self::make_route('place', 'new');
}
public static function create_place() {
return self::make_route('place', 'create');
}
public static function edit_place($id) {
return self::make_route('place', 'edit', $id);
}
public static function update_place($id) {
return self::make_route('place', 'update', $id);
}
public static function delete_place($id) {
return self::make_route('place', 'delete', $id);
}
public static function destroy_place($id) {
return self::make_route('place', 'destroy', $id);
}
// Tours
public static function index_tours($area_id = null) {
return self::make_route('tour', 'index', null, null, $area_id);
}
public static function tour_report($id) {
return self::make_route('tour', 'report', $id);
}
public static function new_tour() {
return self::make_route('tour', 'new');
}
public static function create_tour() {
return self::make_route('tour', 'create');
}
public static function edit_tour($id) {
return self::make_route('tour', 'edit', $id);
}
public static function edit_tour_track($id) {
return self::make_route('tour', 'edit_track', $id);
}
public static function edit_tour_stops($id) {
return self::make_route('tour', 'edit_stops', $id);
}
public static function update_tour($id) {
return self::make_route('tour', 'update', $id);
}
public static function update_tour_stops($id, $scene_id = null) {
return self::make_route('tour', 'update_stops', $id, null, null, $scene_id);
}
public static function update_tour_scenes($id) {
return self::make_route('tour', 'update_scenes', $id);
}
public static function delete_tour($id) {
return self::make_route('tour', 'delete', $id);
}
public static function destroy_tour($id) {
return self::make_route('tour', 'destroy', $id);
}
// Mapstops
public static function new_mapstop($tour_id, $scene_id = null) {
return self::make_route('mapstop', 'new', null, $tour_id, null, $scene_id);
}
public static function create_mapstop($tour_id, $scene_id = null) {
return self::make_route('mapstop', 'create', null, $tour_id, null, $scene_id);
}
public static function edit_mapstop($id, $scene_id = null) {
return self::make_route('mapstop', 'edit', $id, null, null, $scene_id);
}
public static function update_mapstop($id, $scene_id = null) {
return self::make_route('mapstop', 'update', $id, null, null, $scene_id);
}
public static function delete_mapstop($id, $scene_id = null) {
return self::make_route('mapstop', 'delete', $id, null, null, $scene_id);
}
public static function destroy_mapstop($id, $scene_id = null) {
return self::make_route('mapstop', 'destroy', $id, null, null, $scene_id);
}
// Tour records
public static function index_tour_records($area_id = null) {
return self::make_route('tour_record', 'index', null, null, $area_id);
}
public static function view_tour_record($id) {
return self::make_route('tour_record', 'view', $id);
}
public static function new_tour_record($tour_id) {
return self::make_route('tour_record', 'new', null, $tour_id);
}
public static function create_tour_record($tour_id) {
return self::make_route('tour_record', 'create', null, $tour_id);
}
public static function deactivate_tour_record($id) {
return self::make_route('tour_record', 'deactivate', $id);
}
public static function delete_tour_record($id) {
return self::make_route('tour_record', 'delete', $id);
}
public static function destroy_tour_record($id) {
return self::make_route('tour_record', 'destroy', $id);
}
// Scenes
public static function new_scene($tour_id) {
return self::make_route('scene', 'new', null, $tour_id);
}
public static function add_scene($tour_id) {
return self::make_route('scene', 'add', null, $tour_id);
}
public static function delete_scene($id) {
return self::make_route('scene', 'delete', $id);
}
public static function destroy_scene($id) {
return self::make_route('scene', 'destroy', $id);
}
public static function new_scene_stop($scene_id) {
return self::make_route('scene', 'new_stop', $scene_id);
}
public static function set_marker($mapstop_id, $scene_id) {
return self::make_route('scene', 'set_marker', $mapstop_id, null, null, $scene_id);
}
/**
* Return true if the current page has all get parameters that are in the
* given parameter string, else false.
*
* @param string $params_str
*
* @return boolean
*/
public static function is_current_page($params_str) {
$params = self::params_from_str($params_str);
$result = true;
foreach ($params as $key => $value) {
if(!isset($_GET[$key]) || $_GET[$key] != $value) {
$result = false;
break;
}
}
return $result;
}
public static function get_controller_value() {
return $_GET[self::KEYS['controller']];
}
public static function get_action_value() {
return $_GET[self::KEYS['action']];
}
public static function get_id_value() {
return intval($_GET[self::KEYS['id']]);
}
public static function get_tour_id_value() {
return intval($_GET[self::KEYS['tour_id']]);
}
public static function get_area_id_value() {
return intval($_GET[self::KEYS['area_id']]);
}
public static function get_scene_id_value() {
return intval($_GET[self::KEYS['scene_id']]);
}
public static function get_back_params_value() {
return $_GET[self::KEYS['back_params']];
}
}
?>