Dotclear

source: inc/admin/class.dc.filter.php @ 517:7a334cf98e87

Revision 517:7a334cf98e87, 14.1 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

Added filter name, fixed php notice in filters.

Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# This file is part of DotClear "MyPostTypes" plugin.
4# Copyright (c) 2010 Bruno Hondelatte, and contributors.
5# Many, many thanks to Olivier Meunier and the Dotclear Team.
6# All rights reserved.
7#
8# MyPostTypes plugin for DC2 is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# DotClear is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with DotClear; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21#
22# ***** END LICENSE BLOCK *****
23
24/**
25@ingroup DC_CORE
26@nosubgrouping
27@brief Dotclear FilterSet class.
28
29Dotclear FilterSet handles filters and columns when displaying items lists.
30*/
31class dcFilterSet {
32
33     protected $filters;           /// <b>array</b> lists of defined filters
34     protected $form_prefix;       /// <b>string</b> displayed form prefix
35     protected $action;            /// <b>string</b> form action page
36     protected $hideform;          /// <b>boolean</b> start form display hidden by default or not
37     protected $columns_form; /// <b>string</b> columns form
38     protected $name;              /// <b>string</b> fieldset name
39     /**
40     Inits dcFilterSet object
41     
42     @param    core      <b>dcCore</b>       Dotclear core reference
43     @param    form_prefix    <b>string</b>       form prefix to use for parameters
44     */
45     public function __construct($name,$action,$form_prefix="f_") {
46          $this->name = $name;
47          $this->form_prefix=$form_prefix;
48          $this->filters = array();
49          $this->action = $action;
50     }
51
52     /**
53     Adds a new filter to list
54     
55     @param    filter         <b>dcFilter</b>          the filter to add
56     */
57     public function addFilter (Filter $filter) {
58          $filter->setFormPrefix($this->form_prefix);
59          $this->filters[$filter->id] = $filter;
60          return $this;
61     }
62     
63     /**
64     Saves user filters to preferences
65     */
66     protected function saveFilters() {
67          $ser = array();
68          $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters');
69          foreach($this->filters as $filter) {
70               $ser[$filter->id]=$filter->serialize();
71          }
72          $ws->put($this->name,serialize($ser),'string');
73     }
74     
75     /**
76     Loads user filters from preferences
77     */
78     protected function loadFilters() {
79          $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters');
80         
81          $settings = !is_null($ws->{$this->name}) ? unserialize($ws->{$this->name}) : array();
82          foreach($settings as $k => $v) {
83               $this->filters[$k]->unserialize($v);
84          }
85     }
86     
87     /**
88     Updates filters values according to form_data
89     To be called before any call to display() or getForm()
90     
91     @param    form_data <b>array</b>   form values (usually $_GET or $_POST)
92     */
93     public function setFormValues ($form_data) {
94          $this->hideform = true;
95          if (isset($form_data['clear_filters'])) {
96               $this->saveFilters();
97               return;
98          }
99          if (!isset($form_data['apply'])) {
100               $this->loadFilters();
101          }
102          foreach ($this->filters as $filter) {
103               $filter->setFormValues ($form_data);
104               if ($filter->isEnabled()) {
105                    $this->hideform=false;
106               }
107          }
108          if (isset($form_data['apply'])) {
109               if (trim($form_data['apply']) == '+'
110                    && isset($form_data['add_filter']) 
111                    && isset($this->filters[$form_data['add_filter']])) {
112                    $this->filters[$form_data['add_filter']]->add();
113                    $this->hideform=false;
114               }
115          }
116          $this->saveFilters();
117     }
118     
119     /**
120     Defines additional form in layout (right column)
121     
122     @param    html <b>string</b>       the code to add
123     */
124     public function setColumnsForm($html)
125     {
126          $this->columns_form = $html;
127     }
128     
129     /**
130     Returns form fields as hidden fields
131     
132     @return   <b>string</b>  the corresponding html code
133     */
134     public function getFormFieldsAsHidden() {
135          $ret='';
136          foreach ($this->filters as $filter) {
137               $ret.= $filter->getFormFieldAsHidden();
138          }
139          return $ret;
140     }
141
142     /**
143     Retrieves filterset generated form
144     
145     @param    method    <b>string</b>       form method to use (default: "get")
146     */
147     public function getForm($method="get") {
148          $ret = '';
149         
150          if ($this->hideform) {
151               $formclass = ' class="hidden"';
152          } else {
153               $formclass='';
154          }
155          $ret .= '<p><img alt="" src="images/minus.png" /> <a href="#" id="toggle-filters">'.__('Toggle filters and display options').'</a></p>';
156          $ret .=
157               '<div class="two-cols">'.
158               '<form id="filters" action="'.$this->action.'" method="'.$method.'"'.$formclass.'>'.
159               '<div class="col70">'.
160               '<h3>'.__('Entries filters').'</h3>';
161               
162          $count=0;
163          $form_combo=array();
164          $form_combo['-']='';
165          if (count($this->filters)) {
166               $ret .= '<ul>';
167               foreach ($this->filters as $filter) {
168                    if ($filter->isEnabled()) {
169                         $ret .= $filter->getFormLine();
170                    }
171                    $form_combo[$filter->name]=$filter->id;
172                    $count++;
173               }
174               $ret .= '</ul>';
175          }
176          $ret .= 
177               '<p class="clear"><input class="delete" type="submit" value="'.__('Delete all filters').'" name="clear_filters" /></p>'.
178               '<h3 class="margintop">'.__('Add a filter').'</h3>'.
179               '<p id="available_filters">'.
180               form::combo("add_filter",$form_combo).
181               '<input type="submit" value=" + " title="'.__('Add this filter').'" name="apply" />'.
182               '</p>'.
183               '</div>'.
184               '<div class="col30">'.
185               $this->columns_form.
186               '</div>'.
187               '<p class="clear margintop"><input type="submit" value="'.__('Apply filters and display options').'" name="apply" /></p>'.
188
189               '</form></div>';
190          return $ret;
191     }
192     
193     /**
194     Displays required fieldset http header
195     To be called in page header, of course.
196     */
197     public function header() {
198          return dcPage::jsLoad('js/filters.js');
199     }
200     
201     
202     /**
203     Displays the fieldset
204     */
205     public function display() {
206          echo $this->getForm();
207     }
208
209     /**
210     Applies fieldset and return resulting parameters for request
211     
212     @param    method    <b>string</b>       form method to use (default: "get")
213     @param    method    <b>string</b>       form method to use (default: "get")
214     
215     */
216     public function applyFilters($params) {
217          $filtered = false;
218          foreach ($this->filters as $filter) {
219               if ($filter->isEnabled()) {
220                    $filter->applyFilter($params);
221                    $filtered = true;
222               }
223          }
224          return $filtered;
225     }
226     
227}
228
229
230/**
231@ingroup DC_CORE
232@nosubgrouping
233@brief abstract filter class.
234
235Dotclear Filter handles administration filters for each list
236A filter fills in a parameter array, as defined in dcBlog class
237*/
238abstract class Filter {
239     public $id;                        ///<b>string</b> field id (local to fieldset)
240     public $name;                 ///<b>string</b> filter name
241     public $desc;                 ///<b>string</b> field description
242     protected $request_param;     ///<b>string</b> resulting parameter array key
243     protected $enabled;           ///<b>string</b> true if filter is enabled
244     protected $values;            ///<b>array</b> possible filter values
245     public $field_id;             ///<b>string</b> field id (global to the page)
246     
247     /**
248     Inits Filter object
249     
250     @param    id        <b>string</b>  field id
251     @param    form_prefix    <b>string</b>       form prefix to use for parameters
252     */
253     public function __construct ($id,$name,$desc,$request_param) {
254          $this->id = $id;
255          $this->name=$name;
256          $this->desc = $desc;
257          $this->request_param = $request_param;
258          $this->enabled=false;
259          $this->values = array();
260          $this->field_id = $this->id;
261     }
262     
263     /**
264     Get a field id
265     
266     @param    pos       <b>integer</b> position of field, in case of multiple field (0 if only 1 field set, default value)
267     @return   <b>string</b> The field ID
268     */
269     protected function getFieldId($pos=0) {
270          if ($pos == 0) {
271               return $this->field_id;
272          } else {
273               return $this->field_id.'_'.$pos;
274          }
275     }
276     
277     /**
278     Tells whether the filter is enabled or not
279     
280     @return   <b>boolean</b> true if enabled, false otherwise
281     */
282     public function isEnabled() {
283          return $this->enabled;
284     }
285     
286     /**
287     Adds the current filter to the list
288     */
289     public function add() {
290          // By default here, only 1 value allowed. Simply enable the filter
291          $this->enabled = true;
292     }
293     
294     /**
295     Defines form prefix for filter
296     
297     @param    prefix         <b>string</b>  the form prefix
298     */
299     public function setFormPrefix($prefix) {
300          $this->field_id = $prefix.$this->id;
301     }
302     
303     
304     /**
305     Returns HTML code for form field
306     
307     @param    pos       <b>integer</b> position of the field to display (in case of multiple values)
308     @return <b>string</b> the html code
309     */
310     public function getFormFields($pos=0) {
311          return '';
312     }
313     
314     /**
315     Returns filter values il a serialized way (array)
316     
317     @return        <b>array</b>   serialized data
318     */
319     public function serialize() {
320          return array(
321               'values' => $this->values,
322               'enabled' => $this->enabled
323          );
324     }
325     
326     /**
327     Defines filter values from serialized data (array)
328     To be used in conjunction with serialize method
329     
330     @param    $data     <b>array</b>   serialized data to retrieve
331     */
332     public function unserialize ($data) {
333          $this->values = $data['values'];
334          $this->enabled = $data['enabled'];
335     }
336     
337     /**
338     Set filter values from form_data (usually $_GET) 
339     @param    $form_data     <b>array</b>   form data
340     */
341     public function setFormValues($form_data) {
342          $count=0;
343          while (isset($form_data[$this->getFieldId($count)])) {
344               if (!isset($form_data['del_'.$this->getFieldId($count)])) {
345                    $this->values[$count] = $form_data[$this->getFieldId($count)];
346               } elseif (isset($this->values[$count])) {
347                    unset($this->values[$count]);
348               }
349               $count++;
350
351          }
352          $this->values = array_values($this->values);
353          $this->enabled = (count($this->values)!=0);
354     }
355     
356          /**
357     Returns form fields as hidden fields
358     
359     @return   <b>string</b>  the corresponding html code
360     */   
361     public function getFormFieldAsHidden () {
362          $ret='';
363          for ($cur=0; $cur < count($this->values); $cur++) {
364               $ret .= form::hidden($this->getFieldId($cur), $this->values[$cur]);
365          }
366     }
367     /**
368     Returns HTML code for the hole filter lines
369     
370     @return <b>string</b> the html code
371     */
372     
373     public function getFormLine() {
374          $ret="";
375          for ($cur=0; $cur < count($this->values); $cur++) {
376               $ret .= '<li id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'.
377                    $this->getFormFields($cur).
378                    '<input id="del_'.$this->getFieldId($cur).'" class="delete" '.
379                    'type="submit" title="Delete the following filter : " value=" - " name="del_'.$this->getFieldId($cur).'"/>'.
380                    '</li>';
381          }
382          return $ret;
383     }
384     
385     /**
386     Convert filter values into a $param filter, used for the upcoming SQL request
387     
388     @param <b>ArrayObject</b> the parameters array to enrich
389     */
390     public function applyFilter($params) {
391     }
392     
393     public function setValues($value) {
394          $this->values = $value;
395     }
396     
397     public function getValue() {
398          return $this->values;
399     }
400     
401}
402
403/**
404@ingroup DC_CORE
405@nosubgrouping
406@brief abstract filter class.
407
408Handle combo filter on admin side. Can be single or multi-valued
409*/
410class comboFilter extends Filter {
411     protected $options;
412     protected $default;
413     protected $no_value;
414     protected $verb;
415     protected $extra;
416     
417     public function __construct($id,$name,$desc,$request_param,$options,$extra=array()) {
418          parent::__construct($id,$name,$desc,$request_param);
419          $this->options = $options;
420          $this->extra = $extra;
421          $this->desc = $desc;
422          $this->verb = "is";
423          $this->values=array();
424     }
425     
426     public function add() {
427          parent::add();
428          if (isset($this->extra['singleval']) && (count($this->values) > 0))
429               return;
430          $this->values[]=current($this->options);
431     }
432     
433     public function getType() {
434          return "combo";
435     }
436
437     public function serialize() {
438          $data = parent::serialize();
439          $data['verb'] = $this->verb;
440          return $data;
441     }
442     
443     public function unserialize ($data) {
444          parent::unserialize($data);
445          $this->verb = $data['verb'];
446     }
447     
448     public function setFormValues($form_data) {
449          parent::setFormValues($form_data);
450          if (isset($form_data[$this->field_id."_v"])) {
451               $this->verb = ($form_data[$this->field_id."_v"] == 'is') ? 'is' : 'isnot';
452          }
453     }
454
455     public function getFormFieldAsHidden () {
456          return parent::getFormFieldAsHidden().form::hidden($this->field_id."_v",$this->verb);
457     }
458
459     public function getFormFields($pos=0) {
460         
461          if ($pos == 0) {
462               $desc = $this->desc.' : ';
463               $labelclass="filter-title";
464          } else {
465               $desc = __('or');
466               $labelclass = 'or';
467          };
468          return '<span class="'.$labelclass.'">'.$desc.'</span>'.
469               (($pos == 0) 
470                    ?form::combo($this->field_id.'_v',
471                         array(__('is')=>'is',__('is not')=>'isnot'),$this->verb,'','',
472                         false,'title="'.sprintf(__('%s is or is not'),$this->desc).'"') 
473                    :'').
474               form::combo($this->getFieldId($pos),$this->options,$this->values[$pos],
475                    '','',false,'title="'.__('Choose an option').'"');
476     }
477     
478     public function applyFilter($params) {
479          $attr = $this->request_param;
480          if ($this->verb != "is") {
481               $params[$attr."_not"] = true;
482          }
483          if (isset($this->extra['singleval']))
484               $params[$attr]=$this->values[0];
485          else
486               $params[$attr]=$this->values;
487     }
488}
489
490/**
491@ingroup DC_CORE
492@nosubgrouping
493@brief abstract filter class.
494
495Handle boolean filter on admin side.
496*/
497class booleanFilter extends Filter {
498     protected $options;
499     
500     public function __construct($id,$name,$desc,$request_param,$options,$extra=array()) {
501          parent::__construct($id,$name,$desc,$request_param);
502          $this->options = $options;
503          $this->values=array();
504     }
505     
506     
507     public function getType() {
508          return "boolean";
509     }
510     public function add() {
511          parent::add();
512          $this->values=current($this->options);
513     }
514
515     public function getFormFields($pos=0) {
516          return '<span class="'.$labelclass.'">'.$this->desc.'</span>'.
517               form::combo($this->getFieldId($pos),$this->options,$this->values[$pos],
518                    '','',false,'title="'.__('Choose an option').'"');
519     }
520     
521     public function applyFilter($params) {
522          $params[$this->request_param]=$this->values[0];
523     }
524}
525
526
527class textFilter extends Filter {
528     protected $size;
529     protected $max;
530     
531     public function __construct($id,$name,$desc,$request_param,$size,$max) {
532          parent::__construct($id,$name,$desc,$request_param);
533          $this->options = $options;
534          $this->values=array();
535          $this->size = $size;
536          $this->max = $max;
537     }
538     
539     
540     public function getType() {
541          return "text";
542     }
543     public function add() {
544          parent::add();
545          $this->values[]='';
546     }
547
548     public function getFormFields($pos=0) {
549          return '<span class="'.$labelclass.'">'.$this->desc.'</span>'.
550               form::field($this->getFieldId($pos),$this->size,$this->max,html::escapeHTML($this->values[0]));
551     }
552     
553     public function applyFilter($params) {
554          $params[$this->request_param]=$this->values[0];
555     }
556     
557     public function setValues($value) {
558          parent::setValues(array($value));
559     }
560
561     public function getValue() {
562          $v = parent::getValue();
563          return $v[0];
564     }
565     
566}
567?>
Note: See TracBrowser for help on using the repository browser.

Sites map