1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | |
---|
13 | if (!defined('DC_RC_PATH')) { return; } |
---|
14 | |
---|
15 | |
---|
16 | /** |
---|
17 | * dcFilterSet -- filter handling object |
---|
18 | * |
---|
19 | * @uses dcForm |
---|
20 | * |
---|
21 | */ |
---|
22 | class dcFilterSet extends dcForm { |
---|
23 | /** @var array list of variable filters */ |
---|
24 | protected $filters; |
---|
25 | /** @var array list of static filters */ |
---|
26 | protected $static_filters; |
---|
27 | /** @var array list of all filters (union of the 2 previous props) */ |
---|
28 | protected $all_filters; |
---|
29 | /** @var string prefix to be used for all fields */ |
---|
30 | protected $form_prefix; |
---|
31 | /** @var string action to perform upon form submission */ |
---|
32 | protected $action; |
---|
33 | /** @var boolean start form display hidden by default or not */ |
---|
34 | protected $hide_filterset; |
---|
35 | /** @var string filterset name */ |
---|
36 | protected $name; |
---|
37 | /** @var dcCore dotclear core object */ |
---|
38 | protected $core; |
---|
39 | |
---|
40 | /** |
---|
41 | * __init__ - class static initialiser (called at the very bottom of this |
---|
42 | * page) |
---|
43 | * |
---|
44 | * @param mixed $env the twig environment. |
---|
45 | * |
---|
46 | * @access public |
---|
47 | * @static |
---|
48 | * |
---|
49 | */ |
---|
50 | public static function __init__($env) { |
---|
51 | // filterset widgets are defined in a separate file |
---|
52 | $env->getExtension('dc_form')->addTemplate( |
---|
53 | '@forms/formfilter_layout.html.twig' |
---|
54 | ); |
---|
55 | |
---|
56 | $env->addFunction( |
---|
57 | new Twig_SimpleFunction( |
---|
58 | 'filterset', |
---|
59 | 'dcFilterSet::renderFilterSet', |
---|
60 | array( |
---|
61 | 'is_safe' => array('html'), |
---|
62 | 'needs_context' => true, |
---|
63 | 'needs_environment' => true |
---|
64 | ))); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * __construct -- constructor |
---|
69 | * |
---|
70 | * @param dcCore $core dotclear core instance. |
---|
71 | * @param string $name filterset name. |
---|
72 | * @param string $action form action. |
---|
73 | * @param string $form_prefix form prefix. |
---|
74 | * |
---|
75 | * @access public |
---|
76 | * |
---|
77 | * @return mixed Value. |
---|
78 | */ |
---|
79 | public function __construct($core,$name,$action,$form_prefix="f_"){ |
---|
80 | $this->form_prefix=$form_prefix; |
---|
81 | $this->filters = new ArrayObject(); |
---|
82 | $this->static_filters = new ArrayObject(); |
---|
83 | $this->all_filters = new ArrayObject(); |
---|
84 | $this->action = $action; |
---|
85 | $this->filtered = false; |
---|
86 | parent::__construct($core,$name,$action,'POST'); |
---|
87 | $this->id = "filters"; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | /** |
---|
92 | * renderFilterSet -- binding to twig function "filterset" |
---|
93 | * renders a filterset given its name & context |
---|
94 | * @param mixed $env Twig environment (passed by Twig template). |
---|
95 | * @param mixed $context Context (passed by Twig template). |
---|
96 | * @param mixed $name filterset name. |
---|
97 | * @param array $attributes filterset attributes. |
---|
98 | * |
---|
99 | * @access public |
---|
100 | * @static |
---|
101 | * |
---|
102 | * @return mixed Value. |
---|
103 | */ |
---|
104 | public static function renderFilterSet($env,$context,$name, |
---|
105 | $attributes=array()) { |
---|
106 | $context['filtersetname']=$name; |
---|
107 | echo $env->getExtension('dc_form')->renderWidget( |
---|
108 | 'filterset', |
---|
109 | $context |
---|
110 | ); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /** |
---|
115 | * setup - sets up the form filter from http context |
---|
116 | * |
---|
117 | * @access public |
---|
118 | * |
---|
119 | * @return mixed Value. |
---|
120 | */ |
---|
121 | public function setup() { |
---|
122 | $form_combo = array(); |
---|
123 | $form_combo['-'] = ''; |
---|
124 | foreach ($this->all_filters as $filter) { |
---|
125 | $filter->init(); |
---|
126 | } |
---|
127 | foreach ($this->filters as $filter) { |
---|
128 | $form_combo[$filter->id]=$filter->name; |
---|
129 | } |
---|
130 | $p = $this->form_prefix; |
---|
131 | $this |
---|
132 | ->addField ( |
---|
133 | new dcFieldCombo ($p.'add_filter','',$form_combo, |
---|
134 | array())) |
---|
135 | ->addField ( |
---|
136 | new dcFieldSubmit($p.'add',__('Add this filter'), |
---|
137 | array())) |
---|
138 | ->addField ( |
---|
139 | new dcFieldSubmit($p.'clear_filters',__('Delete all filters'), |
---|
140 | array())) |
---|
141 | ->addField ( |
---|
142 | new dcFieldSubmit($p.'apply', |
---|
143 | __('Apply filters and display options'), |
---|
144 | array())) |
---|
145 | ->addField ( |
---|
146 | new dcFieldSubmit($p.'reset',__('Reset'), |
---|
147 | array())) |
---|
148 | ; |
---|
149 | $this->setupFields(); |
---|
150 | /* Since we have specific handling for actions |
---|
151 | (for instance "del_*" fields), we do not take advantage of |
---|
152 | submitfields features, actions are performed manually here. |
---|
153 | |
---|
154 | Use cases : |
---|
155 | (1) $_POST not empty for formfilter fields : |
---|
156 | * filters are set from $_POST |
---|
157 | * applied filters values are set from $_GET |
---|
158 | * keep filters div shown |
---|
159 | (2) $_POST empty : |
---|
160 | * both filters fields & applied values are set from $_GET |
---|
161 | * hide filter div |
---|
162 | */ |
---|
163 | $action = false; |
---|
164 | //$allowed_actions = array('clear_filters','add','del_.*','apply','reset'); |
---|
165 | $allowed_actions = '#^(clear_filters|add|del_.*|apply|reset)$#'; |
---|
166 | |
---|
167 | // Fetch each $_POST parameter to see whether filters are concerned. |
---|
168 | // Only 1 action at a time is allowed. |
---|
169 | foreach ($_POST as $k => $v) { |
---|
170 | if (strpos($k,$this->form_prefix)===0) { |
---|
171 | $tmp = substr($k,strlen($this->form_prefix)); |
---|
172 | $count = preg_match($allowed_actions,$tmp,$match); |
---|
173 | if ($count==1) { |
---|
174 | $action = $match[1]; |
---|
175 | break; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | $this->hide_filterset = true; |
---|
180 | if ($action !== false) { |
---|
181 | // Use case (1) |
---|
182 | if ($action != 'clear_filters' && $action != 'reset') { |
---|
183 | // initialize fields from $_POST |
---|
184 | $this->setupEditFilters($this->all_filters,$_POST); |
---|
185 | if ($action == 'add'){ |
---|
186 | // Add a new filter |
---|
187 | $fname = $p.'add_filter'; |
---|
188 | if (isset($_POST[$fname]) |
---|
189 | && isset($this->filters[$_POST[$fname]])) { |
---|
190 | $this->filters[$_POST[$fname]]->add(); |
---|
191 | } |
---|
192 | $this->hide_filterset = false; |
---|
193 | } elseif (strpos($action,'del_') === 0) { |
---|
194 | // Remove a filter |
---|
195 | $count = preg_match('#del_(.+)_([0-9]+)#',$action,$match); |
---|
196 | if (($count == 1) && isset($this->filters[$match[1]])) { |
---|
197 | $this->filters[$match[1]]->remove($match[2]); |
---|
198 | } |
---|
199 | $this->hide_filterset = false; |
---|
200 | } elseif ($action=="apply") { |
---|
201 | // Apply all filters |
---|
202 | // ==> store filter to preferences and redirect to |
---|
203 | // page with filter as $_GET attributes |
---|
204 | $data = $this->saveFilters(); |
---|
205 | $query = http_build_query($data,'','&'); |
---|
206 | if ($query != '') { |
---|
207 | $query = (strpos($this->action,'?') === false ? '?' : '&').$query; |
---|
208 | } |
---|
209 | http::redirect($this->action.$query); |
---|
210 | exit; |
---|
211 | } |
---|
212 | } |
---|
213 | // Form as been submitted with POST method, retrieve |
---|
214 | // applied filter values from "query" field |
---|
215 | if (isset($_POST[$p."query"])) { |
---|
216 | parse_str($_POST[$p."query"],$out); |
---|
217 | $this->setupAppliedValues($this->all_filters,$out); |
---|
218 | if ($action == 'reset') { |
---|
219 | // RESET action => set fields from query field |
---|
220 | $this->setupEditFilters($this->all_filters,$out); |
---|
221 | } elseif ($action == 'clear_filters') { |
---|
222 | $this->setupEditFilters($this->static_filters,$out); |
---|
223 | foreach ($this->filters as $f) { |
---|
224 | $f->cleanup(); |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | } else { |
---|
229 | // Use case (2) : GET method; set both filters and applied values |
---|
230 | // from GET args or from settings if no GET args. |
---|
231 | $load_from_settings = true; |
---|
232 | foreach($_GET as $k=>$v) { |
---|
233 | if (strpos($k,$this->form_prefix)===0) { |
---|
234 | $load_from_settings=false; |
---|
235 | break; |
---|
236 | } |
---|
237 | } |
---|
238 | $get = $_GET; |
---|
239 | if ($load_from_settings) { |
---|
240 | $get = array_merge($this->loadFilters(),$get); |
---|
241 | } |
---|
242 | $this->setupEditFilters($this->all_filters,$get); |
---|
243 | |
---|
244 | $this->setupAppliedValues($this->all_filters,$get); |
---|
245 | } |
---|
246 | foreach ($this->static_filters as $f) { |
---|
247 | if (!$f->isEnabled()) { |
---|
248 | $f->add(); |
---|
249 | } |
---|
250 | } |
---|
251 | $queryParams = $this->getAppliedFilterValues(); |
---|
252 | $this->addField( |
---|
253 | new dcFieldHidden($this->form_prefix.'query', |
---|
254 | http_build_query($queryParams))); |
---|
255 | // form context is set through a global twig variable |
---|
256 | $this->core->tpl->addGlobal( |
---|
257 | 'filterset_'.$this->name, |
---|
258 | $this->getContext()); |
---|
259 | } |
---|
260 | |
---|
261 | public function getURLParams() { |
---|
262 | return $this->getAppliedFilterValues()->getArrayCopy(); |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * saveFilters - save user defined filters into preferences |
---|
267 | * |
---|
268 | * @access protected |
---|
269 | */ |
---|
270 | protected function saveFilters() { |
---|
271 | $ser = array(); |
---|
272 | $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); |
---|
273 | $data= $this->serialize(); |
---|
274 | $ws->put($this->name,serialize($data->getArrayCopy()),'string'); |
---|
275 | return $data; |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * loadFilters - load user filters from preferences |
---|
280 | * |
---|
281 | * @access protected |
---|
282 | * |
---|
283 | * @return mixed Value. |
---|
284 | */ |
---|
285 | protected function loadFilters() { |
---|
286 | $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); |
---|
287 | $data = (!is_null($ws->{$this->name})) |
---|
288 | ? unserialize($ws->{$this->name}) |
---|
289 | : array(); |
---|
290 | if (is_array($data)) |
---|
291 | return $data; |
---|
292 | else |
---|
293 | return array(); |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * setupEditFilters - Updates filters fields according to form_data |
---|
298 | * To be called before any call to display() or getForm() |
---|
299 | * |
---|
300 | * @param array $filters list of filters to update |
---|
301 | * @param array $form_data form values (usually $_GET or $_POST) |
---|
302 | * |
---|
303 | * @access protected |
---|
304 | * |
---|
305 | * @return mixed Value. |
---|
306 | */ |
---|
307 | protected function setupEditFilters ($filters,$form_data) { |
---|
308 | foreach ($filters as $filter) { |
---|
309 | $filter->setupFields ($form_data); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * setupAppliedValues - Updates filters applied values according to |
---|
315 | * form_data |
---|
316 | * |
---|
317 | * @param array $filters list of filters to update |
---|
318 | * @param array $form_data form values (usually $_GET or $_POST) |
---|
319 | * |
---|
320 | * @access protected |
---|
321 | * |
---|
322 | * @return mixed Value. |
---|
323 | */ |
---|
324 | protected function setupAppliedValues ($filters,$form_data) { |
---|
325 | foreach ($filters as $filter) { |
---|
326 | $filter->setupAppliedValue ($form_data); |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * serialize - retrieves filter applied values in a serialized form |
---|
332 | * |
---|
333 | * @access protected |
---|
334 | * |
---|
335 | * @return ArrayObject serialized data. |
---|
336 | */ |
---|
337 | protected function serialize() { |
---|
338 | $arr = new ArrayObject(); |
---|
339 | foreach ($this->filters as $f) { |
---|
340 | if ($f->isEnabled()) { |
---|
341 | $f->serialize($arr); |
---|
342 | } |
---|
343 | } |
---|
344 | foreach ($this->static_filters as $f) { |
---|
345 | $f->serialize($arr); |
---|
346 | } |
---|
347 | return $arr; |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * addFilter - registers a new filter in filterset |
---|
352 | * |
---|
353 | * @param mixed \dcFilter the filter. |
---|
354 | * |
---|
355 | * @access public |
---|
356 | * |
---|
357 | * @return dcFilterSet the filterset (enabling to chain addFilter). |
---|
358 | */ |
---|
359 | public function addFilter (dcFilter $filter) { |
---|
360 | $filter->setFormPrefix($this->form_prefix); |
---|
361 | $filter->setFilterSet($this); |
---|
362 | $this->all_filters[$filter->id] = $filter; |
---|
363 | if ($filter->isStatic()) { |
---|
364 | $this->static_filters[$filter->id] = $filter; |
---|
365 | } else { |
---|
366 | $this->filters[$filter->id] = $filter; |
---|
367 | } |
---|
368 | return $this; |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * getContext - retrieves current filterset context |
---|
373 | * (to be given to twig template) |
---|
374 | * |
---|
375 | * @access public |
---|
376 | * |
---|
377 | * @return array the context |
---|
378 | */ |
---|
379 | public function getContext() { |
---|
380 | $fcontext = new ArrayObject(); |
---|
381 | $sfcontext = new ArrayObject(); |
---|
382 | foreach ($this->filters as $f) { |
---|
383 | if($f->isEnabled()) { |
---|
384 | $f->appendFilterContext($fcontext); |
---|
385 | } |
---|
386 | } |
---|
387 | foreach ($this->static_filters as $f) { |
---|
388 | $f->appendFilterContext($sfcontext); |
---|
389 | } |
---|
390 | return array( |
---|
391 | 'active_filters' => $fcontext, |
---|
392 | 'static_filters' => $sfcontext, |
---|
393 | 'hide_filters' => $this->hide_filterset, |
---|
394 | 'prefix' => $this->form_prefix); |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * getAppliedFilterValues - retrieves the list of applied filter values |
---|
399 | * |
---|
400 | * @access protected |
---|
401 | * |
---|
402 | * @return ArrayObject the list of applied values |
---|
403 | */ |
---|
404 | protected function getAppliedFilterValues() { |
---|
405 | $arr = new ArrayObject(); |
---|
406 | foreach ($this->all_filters as $f) { |
---|
407 | if ($f->isApplied()) |
---|
408 | $f->updateAppliedValues($arr); |
---|
409 | } |
---|
410 | return $arr; |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * applyFilters -- applies filterset |
---|
415 | * |
---|
416 | * @param mixed $params the parameters to update. |
---|
417 | * |
---|
418 | * @access public |
---|
419 | * |
---|
420 | * @return mixed true if at least 1 filter has been applied, false otherwise |
---|
421 | */ |
---|
422 | public function applyFilters($params) { |
---|
423 | foreach ($this->all_filters as $filter) { |
---|
424 | if ($filter->isApplied()) { |
---|
425 | $filter->applyFilter($params); |
---|
426 | $this->filtered = true; |
---|
427 | } |
---|
428 | } |
---|
429 | return $this->filtered; |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * buildFieldName -- builds a field name given a verb, an id and a position |
---|
434 | * takes the form prefix into consideration |
---|
435 | * @param mixed $verb the verb to use (ex : "del") |
---|
436 | * @param mixed $field_id the field id |
---|
437 | * @param mixed $pos the field position |
---|
438 | * |
---|
439 | * @access public |
---|
440 | * |
---|
441 | * @return mixed the field name |
---|
442 | */ |
---|
443 | public function buildFieldName($verb,$field_id,$pos) { |
---|
444 | return $this->form_prefix.$verb.'_'.$field_id.'_'.$pos; |
---|
445 | } |
---|
446 | |
---|
447 | } |
---|
448 | |
---|
449 | |
---|
450 | /** |
---|
451 | * dcFilter -- base filter class |
---|
452 | * |
---|
453 | * A filter can be edited while being applied with other values |
---|
454 | * that enables to keep the list of applied filters (to display a list of items) |
---|
455 | * while modifing the filter itself. |
---|
456 | * Therefore it contains : |
---|
457 | * * a Field that tracks the currently edited filter |
---|
458 | * * an applied values that tracks the currently applied filter |
---|
459 | * |
---|
460 | */ |
---|
461 | abstract class dcFilter { |
---|
462 | /** @var dcFilterSet filterset parent */ |
---|
463 | public $filterset; |
---|
464 | /** @var string filter id */ |
---|
465 | public $id; |
---|
466 | /** @var string filter name */ |
---|
467 | public $name; |
---|
468 | /** @var string filter description */ |
---|
469 | public $desc; |
---|
470 | /** @var string filter id (including form prefix) */ |
---|
471 | public $filter_id; |
---|
472 | /** @var string resulting parameter array key */ |
---|
473 | protected $request_param; |
---|
474 | /** @var dcField edited field */ |
---|
475 | protected $field; |
---|
476 | /** @var array currently applied values */ |
---|
477 | protected $avalues; |
---|
478 | /** @var boolean true if field is static */ |
---|
479 | protected $static; |
---|
480 | /** @var array generic field options */ |
---|
481 | protected $options; |
---|
482 | /** @var boolean true if field can have multiple values */ |
---|
483 | protected $multiple; |
---|
484 | |
---|
485 | /** |
---|
486 | * __construct -- filter constructor |
---|
487 | * |
---|
488 | * @param mixed $id filter id. |
---|
489 | * @param mixed $name filter name. |
---|
490 | * @param mixed $desc filter description. |
---|
491 | * @param mixed $request_param request parameter (see dcBlog::getPosts for |
---|
492 | * instance). |
---|
493 | * @param array $options filter options. |
---|
494 | * |
---|
495 | * @access public |
---|
496 | * |
---|
497 | * @return mixed Value. |
---|
498 | */ |
---|
499 | public function __construct ($id,$name,$desc,$request_param,$options=array()) { |
---|
500 | $this->id = $id; |
---|
501 | $this->name =$name; |
---|
502 | $this->desc = $desc; |
---|
503 | $this->request_param = $request_param; |
---|
504 | $this->avalues = array(); |
---|
505 | $this->filter_id = $this->id; |
---|
506 | $this->static = false; |
---|
507 | $this->multiple = false; |
---|
508 | $this->options = $options; |
---|
509 | if (isset($options['static']) && $options['static']) { |
---|
510 | $this->static = true; |
---|
511 | } |
---|
512 | if (isset($options['multiple']) && $options['multiple']) { |
---|
513 | $this->multiple = true; |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | |
---|
518 | /** |
---|
519 | * parseData - Extract values from data (data being an array, such as $_GET |
---|
520 | * or $_POST) ; does not update any field, only return parsed |
---|
521 | * data |
---|
522 | * |
---|
523 | * @param mixed $data input data. |
---|
524 | * |
---|
525 | * @access protected |
---|
526 | * |
---|
527 | * @return array an array containing parsed data. |
---|
528 | */ |
---|
529 | protected function parseData($data) { |
---|
530 | $arr = $this->field->parseValues($data); |
---|
531 | return array('values' => $arr); |
---|
532 | } |
---|
533 | |
---|
534 | /** |
---|
535 | * isStatic -- returns whether the filter is static or not |
---|
536 | * |
---|
537 | * @access public |
---|
538 | * |
---|
539 | * @return boolean true if the filter is static. |
---|
540 | */ |
---|
541 | public function isStatic() { |
---|
542 | return $this->static; |
---|
543 | } |
---|
544 | |
---|
545 | /** |
---|
546 | * setupFields -- sets up filter fielt from $_GET or $_POST |
---|
547 | * |
---|
548 | * @param mixed $data input data (either $_GET or $_POST). |
---|
549 | * |
---|
550 | * @access public |
---|
551 | * |
---|
552 | * @return mixed Value. |
---|
553 | */ |
---|
554 | public function setupFields($data) { |
---|
555 | $this->field->setup($data); |
---|
556 | } |
---|
557 | |
---|
558 | |
---|
559 | /** |
---|
560 | * init -- initializes filter (called after setup) |
---|
561 | * |
---|
562 | * @access public |
---|
563 | * |
---|
564 | * @return mixed Value. |
---|
565 | */ |
---|
566 | public function init() { |
---|
567 | } |
---|
568 | |
---|
569 | /** |
---|
570 | * cleanup - resets filter field to its default value |
---|
571 | * |
---|
572 | * @access public |
---|
573 | * |
---|
574 | * @return mixed Value. |
---|
575 | */ |
---|
576 | public function cleanup() { |
---|
577 | $this->field->setValues(array()); |
---|
578 | } |
---|
579 | |
---|
580 | /** |
---|
581 | * setupAppliedValue -- defines field applied values from data |
---|
582 | * |
---|
583 | * @param mixed $data data to retrieve values from ($_GET or $_POST). |
---|
584 | * |
---|
585 | * @access public |
---|
586 | * |
---|
587 | * @return mixed Value. |
---|
588 | */ |
---|
589 | public function setupAppliedValue($data) { |
---|
590 | $this->avalues = $this->parseData($data); |
---|
591 | } |
---|
592 | |
---|
593 | public function updateAppliedValues($arr) { |
---|
594 | $arr[$this->filter_id] = $this->avalues['values']; |
---|
595 | } |
---|
596 | |
---|
597 | /** |
---|
598 | * setFilterSet -- sets filterSet for filter |
---|
599 | * |
---|
600 | * @param mixed \dcFilterset Description. |
---|
601 | * |
---|
602 | * @access public |
---|
603 | * |
---|
604 | */ |
---|
605 | public function setFilterSet(dcFilterset $fs) { |
---|
606 | $this->filterset = $fs; |
---|
607 | } |
---|
608 | |
---|
609 | /** |
---|
610 | * setFormPrefix -- sets filter form prefix |
---|
611 | * |
---|
612 | * @param string $prefix the form prefix. |
---|
613 | * |
---|
614 | * @access public |
---|
615 | * |
---|
616 | * @return mixed Value. |
---|
617 | */ |
---|
618 | public function setFormPrefix($prefix) { |
---|
619 | $this->filter_id = $prefix.$this->id; |
---|
620 | } |
---|
621 | |
---|
622 | /** |
---|
623 | * isEnabled -- Tells whether the filter is enabled or not (ie field has |
---|
624 | * at least 1 value defined) |
---|
625 | * |
---|
626 | * @access public |
---|
627 | * |
---|
628 | * @return mixed true if the filter is enabled. |
---|
629 | */ |
---|
630 | public function isEnabled() { |
---|
631 | return count($this->field) != 0; |
---|
632 | } |
---|
633 | |
---|
634 | protected abstract function addValue($value=NULL); |
---|
635 | |
---|
636 | /** |
---|
637 | * add -- adds a value for the filter |
---|
638 | * |
---|
639 | * @access public |
---|
640 | * |
---|
641 | * @return mixed Value. |
---|
642 | */ |
---|
643 | public function add() { |
---|
644 | if (count($this->field) > 1 && !$this->multiple) |
---|
645 | return; |
---|
646 | $this->addValue(); |
---|
647 | } |
---|
648 | |
---|
649 | /** |
---|
650 | * remove -- Removes a value from filter |
---|
651 | * |
---|
652 | * @param mixed $pos value position. |
---|
653 | * |
---|
654 | * @access public |
---|
655 | * |
---|
656 | * @return mixed Value. |
---|
657 | */ |
---|
658 | public function remove($pos) { |
---|
659 | $values = $this->field->getValues(); |
---|
660 | if (isset($values[$pos])) { |
---|
661 | $this->field->delValue($pos); |
---|
662 | } |
---|
663 | |
---|
664 | } |
---|
665 | |
---|
666 | abstract protected function appendContextLine($ctx,$pos); |
---|
667 | |
---|
668 | /** |
---|
669 | * appendFilterContext -- appends current filter context to the given |
---|
670 | * context. |
---|
671 | * A filter context consists in a list of array elements, one for |
---|
672 | * each field line displayed. |
---|
673 | * If a field has multiple values, there will be as many lines as values |
---|
674 | * |
---|
675 | * The twig template then iterates through the array to display |
---|
676 | * each and every line |
---|
677 | * @param mixed $ctx the context to enrich |
---|
678 | * |
---|
679 | * @access public |
---|
680 | * |
---|
681 | * @return mixed Value. |
---|
682 | */ |
---|
683 | public function appendFilterContext($ctx) { |
---|
684 | foreach ($this->field->getValues() as $cur => $f) { |
---|
685 | /* |
---|
686 | * each line of context has the following properties : |
---|
687 | * * lineclass : <tr> class to use |
---|
688 | * * 'del_id' : delete input field name to delete current value |
---|
689 | * * other field-type specific values that are set from |
---|
690 | * appendContextLine method |
---|
691 | */ |
---|
692 | $line = new ArrayObject(); |
---|
693 | $line['lineclass'] = $this->id; |
---|
694 | $line['del_id'] = $this->filterset->buildFieldName('del',$this->id,$cur); |
---|
695 | // Create the delete field for this line |
---|
696 | $del = new dcFieldSubmit( |
---|
697 | $this->filterset->buildFieldName('del',$this->id,$cur), |
---|
698 | '-', |
---|
699 | array( |
---|
700 | 'attr' => array( |
---|
701 | 'title' => __('Delete the following filter'))) |
---|
702 | ); |
---|
703 | $this->filterset->addField($del); |
---|
704 | $this->appendContextLine($line,$cur); |
---|
705 | $ctx[]=$line; |
---|
706 | } |
---|
707 | } |
---|
708 | |
---|
709 | /** |
---|
710 | * serialize - serializes field value into given array |
---|
711 | * |
---|
712 | * @param mixed $arr the context to update. |
---|
713 | * |
---|
714 | * @access public |
---|
715 | * |
---|
716 | * @return mixed Value. |
---|
717 | */ |
---|
718 | public function serialize($arr) { |
---|
719 | if (count($this->fields) == 1) { |
---|
720 | $arr[$this->filter_id]=$this->field->getValue(); |
---|
721 | } else { |
---|
722 | $arr[$this->filter_id]=$this->field->getValues(); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | /** |
---|
727 | * isApplied -- returns true when the filter is applied |
---|
728 | * (ie. has at least 1 applied value) |
---|
729 | * @access public |
---|
730 | * |
---|
731 | * @return mixed Value. |
---|
732 | */ |
---|
733 | public function isApplied(){ |
---|
734 | return (count($this->avalues['values']) != 0); |
---|
735 | } |
---|
736 | |
---|
737 | /** |
---|
738 | * applyFilter -- Converts filter values into a $param filter, used for the |
---|
739 | * upcoming SQL request |
---|
740 | * |
---|
741 | * @param mixed $params Description. |
---|
742 | * |
---|
743 | * @access public |
---|
744 | * |
---|
745 | * @return mixed Value. |
---|
746 | */ |
---|
747 | public function applyFilter($params) { |
---|
748 | return false; |
---|
749 | } |
---|
750 | |
---|
751 | /** |
---|
752 | * header -- tbd |
---|
753 | * |
---|
754 | * @access public |
---|
755 | * |
---|
756 | * @return mixed Value. |
---|
757 | */ |
---|
758 | public function header() { |
---|
759 | return ''; |
---|
760 | } |
---|
761 | |
---|
762 | /** |
---|
763 | * getFields -- returns filter field(s) |
---|
764 | * |
---|
765 | * @access public |
---|
766 | * |
---|
767 | * @return dcField the filter field. |
---|
768 | */ |
---|
769 | public function getFields() { |
---|
770 | return $this->field; |
---|
771 | } |
---|
772 | |
---|
773 | } |
---|
774 | |
---|
775 | /** |
---|
776 | * dcFilterText - basic single field text filter |
---|
777 | * |
---|
778 | * @uses dcFilter |
---|
779 | * |
---|
780 | */ |
---|
781 | class dcFilterText extends dcFilter { |
---|
782 | |
---|
783 | /** |
---|
784 | * @see dcFilter::init() |
---|
785 | */ |
---|
786 | public function init() { |
---|
787 | $this->field = new dcFieldText( |
---|
788 | $this->filter_id, |
---|
789 | NULL); |
---|
790 | $this->filterset->addField($this->field); |
---|
791 | $this->multiple = false; |
---|
792 | } |
---|
793 | |
---|
794 | |
---|
795 | /** |
---|
796 | * @see dcFilter::appendContextLine() |
---|
797 | */ |
---|
798 | public function appendContextLine($line,$pos) { |
---|
799 | /* |
---|
800 | Extra data provided by this filter : |
---|
801 | * ffield : field name |
---|
802 | * display_inline : true if the field is static |
---|
803 | * fwidget : name of the widget (filter_text) |
---|
804 | * desc : filter description |
---|
805 | */ |
---|
806 | $line['ffield'] = $this->field->getName(); |
---|
807 | if ($this->static) { |
---|
808 | $line['display_inline'] = true; |
---|
809 | } |
---|
810 | |
---|
811 | if ($pos == 0) { |
---|
812 | $line['fwidget']='filter_text'; |
---|
813 | $line['desc']=$this->desc; |
---|
814 | }; |
---|
815 | } |
---|
816 | |
---|
817 | /** |
---|
818 | * @see dcFilter::addValue() |
---|
819 | */ |
---|
820 | protected function addValue($value=NULL) { |
---|
821 | if ($value === NULL) { |
---|
822 | $value = ''; |
---|
823 | } |
---|
824 | $this->field->addValue($value); |
---|
825 | } |
---|
826 | |
---|
827 | /** |
---|
828 | * @see dcFilter::applyFilter() |
---|
829 | */ |
---|
830 | public function applyFilter($params) { |
---|
831 | $params[$this->request_param]=$this->avalues['values'][0]; |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | /** |
---|
836 | * dcFilterCombo -- combo filter |
---|
837 | * |
---|
838 | * Enables to filter through a list of values, can be used to check |
---|
839 | * if a value is in a list of items |
---|
840 | * |
---|
841 | * @uses dcFilter |
---|
842 | * |
---|
843 | */ |
---|
844 | class dcFilterCombo extends dcFilter { |
---|
845 | /** @var combo the list of possible values in combo */ |
---|
846 | protected $combo; |
---|
847 | |
---|
848 | /** |
---|
849 | * @see dcFilter::__construct() |
---|
850 | */ |
---|
851 | public function __construct($id,$name,$desc,$request_param,$combo, |
---|
852 | $options=array()) { |
---|
853 | parent::__construct($id,$name,$desc,$request_param,$options); |
---|
854 | $this->combo = $combo; |
---|
855 | } |
---|
856 | |
---|
857 | /** |
---|
858 | * @see dcFilter::init() |
---|
859 | */ |
---|
860 | public function init() { |
---|
861 | $this->field = new dcFieldCombo( |
---|
862 | $this->filter_id, |
---|
863 | NULL, |
---|
864 | $this->combo); |
---|
865 | $this->filterset->addField($this->field); |
---|
866 | } |
---|
867 | |
---|
868 | /** |
---|
869 | * @see dcFilter::addValue() |
---|
870 | */ |
---|
871 | protected function addValue($value=NULL) { |
---|
872 | if ($value === NULL) { |
---|
873 | $value = current($this->combo); |
---|
874 | } |
---|
875 | $this->field->addValue($value); |
---|
876 | } |
---|
877 | |
---|
878 | /** |
---|
879 | * @see dcFilter::init() |
---|
880 | */ |
---|
881 | public function appendContextLine($line,$pos) { |
---|
882 | /* |
---|
883 | Extra data provided by this filter : |
---|
884 | * ffield : field name |
---|
885 | * display_inline : true if the field is static |
---|
886 | * fwidget : name of the widget (filter_combo or filter_combo_cont) |
---|
887 | * foffset : field value offset |
---|
888 | * desc : filter description |
---|
889 | Only the 1st item contains description. |
---|
890 | */ |
---|
891 | if ($this->static) { |
---|
892 | $line['display_inline'] = true; |
---|
893 | } |
---|
894 | $line['ffield'] = $this->field->getName(); |
---|
895 | $line['foffset'] = $pos; |
---|
896 | if ($pos == 0) { |
---|
897 | $line['fwidget']='filter_combo'; |
---|
898 | $line['desc']=$this->desc; |
---|
899 | } else { |
---|
900 | $line['fwidget']='filter_combo_cont'; |
---|
901 | }; |
---|
902 | } |
---|
903 | |
---|
904 | /** |
---|
905 | * @see dcFilter::applyFilter() |
---|
906 | */ |
---|
907 | public function applyFilter($params) { |
---|
908 | $attr = $this->request_param; |
---|
909 | if ($this->multiple) |
---|
910 | $params[$attr]=$this->avalues['values']; |
---|
911 | else |
---|
912 | $params[$attr]=$this->avalues['values'][0]; |
---|
913 | } |
---|
914 | |
---|
915 | } |
---|
916 | |
---|
917 | /** |
---|
918 | * dcFilterRichCombo -- rich combo filter |
---|
919 | * |
---|
920 | * Same as dcFilterCombo, with the possibility to exclude a list of values |
---|
921 | * |
---|
922 | * @uses dcFilter |
---|
923 | * |
---|
924 | */ |
---|
925 | class dcFilterRichCombo extends dcFilterCombo { |
---|
926 | /** @var verb verb field ('is' or 'is not') */ |
---|
927 | protected $verb; |
---|
928 | |
---|
929 | /** |
---|
930 | * @see dcFilter::init() |
---|
931 | */ |
---|
932 | public function init() { |
---|
933 | parent::init(); |
---|
934 | $this->verb = new dcFieldCombo( |
---|
935 | $this->filter_id.'_v', |
---|
936 | 'is', |
---|
937 | array( |
---|
938 | 'is'=>__('is'), |
---|
939 | 'isnot'=>__('is not')) |
---|
940 | ); |
---|
941 | $this->filterset->addField($this->verb); |
---|
942 | } |
---|
943 | |
---|
944 | /** |
---|
945 | * @see dcFilter::parseData() |
---|
946 | */ |
---|
947 | protected function parseData($data) { |
---|
948 | $val = parent::parseData($data); |
---|
949 | $v = $this->verb->parseValues($data); |
---|
950 | if (isset($v[0]) && $v[0] === 'isnot') |
---|
951 | $val['verb'] = 'isnot'; |
---|
952 | else |
---|
953 | $val['verb'] ='is'; |
---|
954 | return $val; |
---|
955 | } |
---|
956 | |
---|
957 | /** |
---|
958 | * @see dcFilter::setupFields() |
---|
959 | */ |
---|
960 | public function setupFields($data) { |
---|
961 | parent::setupFields($data); |
---|
962 | $this->verb->setup($data); |
---|
963 | } |
---|
964 | |
---|
965 | /** |
---|
966 | * @see dcFilter::updateAppliedValues() |
---|
967 | */ |
---|
968 | public function updateAppliedValues($arr) { |
---|
969 | parent::updateAppliedValues($arr); |
---|
970 | $arr[$this->verb->getName()] = $this->verb->getValue(); |
---|
971 | } |
---|
972 | |
---|
973 | /** |
---|
974 | * @see dcFilter::appendContextLine() |
---|
975 | */ |
---|
976 | public function appendContextLine($line,$pos) { |
---|
977 | parent::appendContextLine($line,$pos); |
---|
978 | if ($pos == 0) { |
---|
979 | $line['fverb'] = $this->verb->getName(); |
---|
980 | $line['fwidget']='filter_richcombo'; |
---|
981 | } |
---|
982 | } |
---|
983 | |
---|
984 | /** |
---|
985 | * @see dcFilter::serialize() |
---|
986 | */ |
---|
987 | public function serialize($arr) { |
---|
988 | parent::serialize($arr); |
---|
989 | $arr[$this->filter_id.'_v']=$this->verb->getValue(); |
---|
990 | } |
---|
991 | |
---|
992 | /** |
---|
993 | * @see dcFilter::applyFilter() |
---|
994 | */ |
---|
995 | public function applyFilter($params) { |
---|
996 | parent::applyFilter($params); |
---|
997 | $attr = $this->request_param; |
---|
998 | if ($this->avalues['verb'] != "is") { |
---|
999 | $params[$attr."_not"] = true; |
---|
1000 | } |
---|
1001 | } |
---|
1002 | |
---|
1003 | } |
---|
1004 | |
---|
1005 | // Static initializer |
---|
1006 | dcFilterSet::__init__($GLOBALS['core']->tpl); |
---|
1007 | ?> |
---|