Dotclear

source: inc/admin/class.dc.filter.php @ 580:a381af6f89f9

Revision 580:a381af6f89f9, 13.5 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Typos diverses et variées

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

Sites map