/
filter.php
386 lines (339 loc) · 8.14 KB
/
filter.php
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Filter
*
* Sets up a filtering system where keys from `$_GET` and `$_POST` can be stored
* in the session to be used to filter data. The advantage to storing these in
* the session is that the filters will remain after a user leaves the page,
* so that when they come back they won't have to refilter anything.
*
* @package Filter
* @author Corey Worrell
* @homepage http://coreyworrell.com
* @version 1.4
*/
class Filter {
/**
* @var string session key to store filters in
*/
public static $session_key = 'filters';
/**
* @var array holds all the filters
*/
protected $_filters = array();
/**
* @var array holds the defaults for filters
*/
protected $_originals = array();
/**
* @var array holds the keys that have changed since the last page load
*/
protected $_changed = array();
/**
* @var string path to current request
*/
protected $_path;
/**
* @var array `$_GET` and `$_POST` merged
*/
protected $_globals;
/**
* @var Filter instances of Filter
*/
protected static $_instances;
/**
* Return an instance of Filter for current request or for the path given
*
* // Gets filters for current request
* $filters = Filter::instance(array(
* 'page' => 1,
* 'per_page' => 20,
* 'search' => '',
* 'status' => 'published',
* ));
*
* // Get filters for a different request
* $filters = Filter::instance('admin/blog/archives');
*
* @chainable
* @param array $filters filters to keep track of
* @return Filter
*/
public static function instance($path = NULL, $filters = NULL)
{
if (is_array($path))
{
$filters = $path;
$path = NULL;
}
if ($path === NULL)
{
$directory = Request::$current->directory();
$controller = Request::$current->controller();
$action = Request::$current->action();
$path = trim("$directory/$controller/$action", '/');
unset($directory, $controller, $action);
}
if ( ! isset(Filter::$_instances[$path]))
{
Filter::$_instances[$path] = new Filter($filters, $path);
}
return Filter::$_instances[$path];
}
/**
* Create a new instance of Filter
*
* Sets up globals and adds keys to keep track of
*
* @param array $filters global keys to keep track of
* @param string $path path to request (if not getting current request)
* @return Filter
*/
public function __construct($filters = NULL, $path = NULL)
{
$this->_globals = array_merge($_POST, $_GET);
$this->_path = $path;
// Get filters from session
$this->_filters = Session::instance()->get(Filter::$session_key, array());
// Make sure paths exist
if ( ! isset($this->_filters[$this->_path]))
{
$this->_filters[$this->_path] = array();
}
if ( ! isset($this->_changed[$this->_path]))
{
$this->_changed[$this->_path] = array();
}
// If filters are passed, keep track of them
if (is_array($filters))
{
$this->track($filters);
}
}
/**
* Keep track of global keys
*
* Can be called instead of passing filters to [Filter::instance]
*
* $filters = Filter::instance()
* ->track(array(
* 'page' => 1,
* 'per_page' => 20,
* ));
*
* @chainable
* @param array $filters global keys to keep track of
* @return Filter
*/
public function track(array $filters)
{
$this->_originals += $filters;
foreach ($filters as $key => $default)
{
if (array_key_exists($key, $this->_globals))
{
$value = $this->_globals[$key];
if (array_key_exists($key, $this->_filters[$this->_path]) AND $this->_filters[$this->_path][$key] !== $value)
{
$this->_changed[$this->_path][$key] = $key;
}
$this->_filters[$this->_path][$key] = $value;
}
elseif ( ! array_key_exists($key, $this->_filters[$this->_path]))
{
// NULL doesn't exist for incoming data, as all params are strings
if ($default === NULL)
{
$default = '';
}
$this->_filters[$this->_path][$key] = $default;
}
}
$this->_save();
return $this;
}
/**
* Set a filter manually, instead of letting Filter get it from the globals
*
* $filters->set('page', 10);
*
* $filters->set(array(
* 'status' => 'draft',
* 'order' => 'date',
* ));
*
* @chainable
* @param mixed $filters array of key => values, or just the key
* @param mixed $value value of key if first param is a string
* @return Filter
*/
public function set($filters, $value = NULL)
{
if ( ! is_array($filters))
{
$filters = array($filters => $value);
}
foreach ($filters as $key => $value)
{
$this->_filters[$this->_path][$key] = $value;
}
$this->_save();
return $this;
}
/**
* Get the value of a stored filter, or all filters
*
* $page = $filters->get('page', 1);
*
* $as_array = $filters->get();
*
* @param string $key filter to return
* @param mixed $default default value if key is empty or doesn't exist
* @return mixed filter value(s)
*/
public function get($key = NULL, $default = NULL)
{
if ($key === NULL)
{
return $this->_filters[$this->_path];
}
return Arr::get($this->_filters[$this->_path], $key, $default);
}
/**
* Removes a filter entirely
*
* [!!] Note: This only affects the current page request
*
* $filters->delete('page');
*
* $filters->delete(array('page', 'per_page', 'search'));
*
* @chainable
* @param mixed $keys key or array of keys to delete
* @return Filter
*/
public function delete($keys = NULL)
{
if ($keys === NULL)
{
$this->_filters[$this->_path] = array();
}
else
{
foreach ((array) $keys as $key)
{
unset($this->_filters[$this->_path][$key]);
}
}
$this->_save();
return $this;
}
/**
* Resets a key, or all keys, to their defaults
*
* // Set the page back to its default (1)
* $filters->reset('page');
*
* // Reset a group of filters
* $filters->reset(array('page', 'per_page', 'search'));
*
* // Reset all the filters
* $filters->reset();
*
* @chainable
* @param mixed $keys key or array of keys to reset
* @return Filter
*/
public function reset($keys = NULL)
{
if ($keys === NULL)
{
$keys = array_keys($this->_originals);
}
foreach ((array) $keys as $key)
{
$this->_filters[$this->_path][$key] = $this->_originals[$key];
}
$this->_save();
return $this;
}
/**
* Check whether a filter has changed since the last page load
*
* if ($filters->changed('search'))
* {
* $filters->reset('page');
* }
*
* @param string $key key to check for, or NULL to return array of changed keys
* @return mixed boolean or array depending on first param passed
*/
public function changed($key = NULL)
{
if ($key === NULL)
{
return $this->_changed[$this->_path];
}
return isset($this->_changed[$this->_path][$key]);
}
/**
* Sets a filter
*
* $filters->page = 10;
*
* @param string $key filter key
* @param string $value filter value
* @return void
*/
public function __set($key, $value)
{
$this->set($key, $value);
}
/**
* Gets a filter value
*
* $page = $filters->page
*
* @param string $key filter key
* @return mixed filter value
*/
public function __get($key)
{
return $this->get($key);
}
/**
* Check if a filter is set
*
* if (isset($filters->page)) ...
*
* @param string $key filter key
* @return boolean
*/
public function __isset($key)
{
return isset($this->_filters[$this->_path][$key]);
}
/**
* Unset/remove a filter
*
* [!!] Note: This only affects the current request
*
* unset($filters->page);
*
* @param string $key filter key
* @return void
*/
public function __unset($key)
{
$this->delete($key);
}
/**
* Writes the filters to the session
*
* @return Session
*/
protected function _save()
{
return Session::instance()->set(Filter::$session_key, $this->_filters);
}
}