Dotclear

source: inc/admin/class.dc.filter.php @ 225:411ee50a8664

Revision 225:411ee50a8664, 8.5 KB checked in by Dsls <dsls@…>, 15 years ago (diff)

New branch formfilters, starting work on generic filters on admin side.

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
24class dcFilterSet {
25
26     protected $filters;
27     protected $form_prefix;
28     protected $action;
29     protected $hideform;
30     
31     public function __construct($action,$form_prefix="f_") {
32          $this->form_prefix=$form_prefix;
33          $this->filters = array();
34          $this->action = $action;
35     }
36
37     public function addFilter (Filter $filter) {
38          $filter->setFormPrefix($this->form_prefix);
39          $this->filters[$filter->id] = $filter;
40          return $this;
41     }
42
43     // Retrieves filter values from context
44     public function setValues ($form_data) {
45          $this->hideform = true;
46          if (isset($form_data['clear_filters']))
47               return;
48          foreach ($this->filters as $filter) {
49               $filter->setValues ($form_data);
50               if ($filter->isEnabled())
51                    $this->hideform=false;
52          }
53          if (isset($form_data['add_filter']) && isset($this->filters[$form_data['add_filter']])) {
54               $this->filters[$form_data['add_filter']]->add();
55          }
56     }
57     public function getFormFieldsAsHidden() {
58          $ret='';
59          foreach ($this->filters as $filter) {
60               $ret.= $filter->getFormFieldAsHidden();
61          }
62          return $ret;
63     }
64
65     public function getForm($action,$extra_content,$method="get",$nb_cols=3) {
66          $ret = '';
67          /*if ($this->hideform) {
68               $ret .= '<p><a id="filter-control" class="form-control" href="#">'.
69               __('Filters').'</a></p>';
70          }*/
71          $ret .= '<p><img alt="" src="minus.png"/> <a href="#" id="toggle-filters">'.__('Toggle filters and display options').'</a></p>';
72          $ret .=
73               '<div class="two-cols">'.
74               '<form id="filters" action="'.$this->action.'" method="get" id="filters-form">'.
75               '<div class="col70">'.
76               '<h3>'.__('Entries filters').'</h3>';
77               
78          $count=0;
79          $form_combo=array();
80          $form_combo['-']='';
81          foreach ($this->filters as $filter) {
82               if ($filter->isEnabled()) {
83                    $ret .= $filter->getFormLine();
84               }
85               $form_combo[$filter->desc]=$filter->id;
86               $count++;
87          }
88          $ret .= 
89               '<p class="clear"><input class="delete" type="submit" value="'.__('Delete all filters').'" name="clear_filters"></p>'.
90               '<h3 class="margintop">'.__('Add a filter').'</h3>'.
91               '<p id="available_filters">'.
92               form::combo("add_filter",$form_combo).
93               '<input type="submit" value=" + " title="'.__('Add this filter').'" name="apply">'.
94               '</p>'.
95               '</div>'.
96               '<div class="col30">'.
97               '<h3>'.__('Displayed information').'</h3>'.
98               '<ul>'.
99               '<li class="line"><label for="col_title">Titre</label><input type="checkbox" id="col_title" value="title" name="title" checked="checked"></li>'.
100
101               '</ul>'.
102               '</div>'.
103               '<p class="clear margintop"><input type="submit" value="'.__('Apply filters and display options').'" name="apply"></p>'.
104
105               '</form></div>';
106          return $ret;
107     }
108
109     public function header() {
110          return dcPage::jsLoad('js/filters.js');
111     }
112     public function display() {
113          echo $this->getForm("#","");
114     }
115
116     public function applyFilters($params) {
117          $filtered = false;
118          foreach ($this->filters as $filter) {
119               if ($filter->isEnabled()) {
120                    $filter->applyFilter($params);
121                    $filtered = true;
122               }
123          }
124          return $filtered;
125     }
126     
127}
128
129
130abstract class Filter {
131     public $id;
132     public $desc;
133     protected $request_param;
134     protected $enabled;
135     protected $values;
136     public $field_id;
137     
138     
139     public function __construct ($id,$desc,$request_param) {
140          $this->id = $id;
141          $this->desc = $desc;
142          $this->request_param = $request_param;
143          $this->enabled=false;
144          $this->values = array();
145          $this->field_id = $this->id;
146     }
147     
148     protected function getFieldId($pos=0) {
149          if ($pos == 0) {
150               return $this->field_id;
151          } else {
152               return $this->field_id.'_'.$pos;
153          }
154     }
155     
156     public function isEnabled() {
157          return $this->enabled;
158     }
159     
160     public function add() {
161          $this->enabled = true;
162     }
163     
164     public function setFormPrefix($prefix) {
165          $this->field_id = $prefix.$this->id;
166     }
167     
168     public abstract function getType();
169     
170     public function getFormFields() {
171          return '';
172     }
173
174     public function setValues($form_data) {
175/*        if (isset($form_data['c_'.$this->field_id])) {
176               $this->enabled = true;
177          }*/
178          $count=0;
179          while (isset($form_data[$this->getFieldId($count)])) {
180               if (!isset($form_data['del_'.$this->getFieldId($count)])) {
181                    $this->values[] = $form_data[$this->getFieldId($count)];
182                    $this->enabled = true;
183               }
184               $count++;
185          }
186     }
187     
188     public function getFormFieldAsHidden () {
189          $ret='';
190          for ($cur=0; $cur < count($this->values); $cur++) {
191               $ret .= form::hidden($this->getFieldId($cur), $this->values[$cur]);
192          }
193     }
194     public function getFormLine() {
195          $ret="";
196          for ($cur=0; $cur < count($this->values); $cur++) {
197               $ret .= '<p id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'.
198                    $this->getFormFields($cur).
199                    '<input id="del_'.$this->getFieldId($cur).'" class="delete" '.
200                    'type="submit" title="Delete this filter" value=" - " name="del_'.$this->getFieldId($cur).'"/>'.
201                    '</p>';
202          }
203          return $ret;
204     }
205     
206     public function applyFilter($params) {
207     }
208     
209}
210
211class comboFilter extends Filter {
212     protected $options;
213     protected $default;
214     protected $no_value;
215     protected $verb;
216     protected $extra;
217     
218     public function __construct($id,$desc,$request_param,$options,$extra=array()) {
219          parent::__construct($id,$desc,$request_param);
220          $this->options = $options;
221          $this->extra = $extra;
222          /*$this->default = $default;
223          $this->no_value = $no_value;*/
224          $this->desc = $desc;
225          $this->verb = "is";
226          $this->values=array();
227     }
228     
229     public function add() {
230          parent::add();
231          if (isset($this->extra['singleval']) && (count($this->values) > 0))
232               return;
233          $this->values[]=current($this->options);
234     }
235     
236     public function getType() {
237          return "combo";
238     }
239     
240     public function setValues($form_data) {
241          parent::setValues($form_data);
242          if (isset($form_data[$this->field_id."_v"])) {
243               $this->verb = $form_data[$this->field_id."_v"] == 'is' ? 'is' : 'isnot';
244          }
245     }
246
247     public function getFormFieldAsHidden () {
248          return parent::getFormFieldAsHidden().form::hidden($this->field_id."_v",$this->verb);
249     }
250
251     public function getFormFields($pos=0) {
252         
253          if ($pos == 0) {
254               $desc = $this->desc.' : ';
255               $labelclass="";
256          } else {
257               $desc = __('or');
258               $labelclass = ' class="or"';
259          };
260          return '<label for="'.$this->getFieldId($pos).'"'.$labelclass.'>'.$desc.'</label>'.
261               (($pos == 0) ?form::combo($this->field_id.'_v',array(__('is')=>'is',__('is not')=>'isnot'),$this->verb) : '').
262               form::combo($this->getFieldId($pos),$this->options,$this->values[$pos]);
263     }
264     
265     public function applyFilter($params) {
266          if (isset($this->extra['singleval']))
267               $params[$this->request_param]=$this->values[0];
268          else
269               $params[$this->request_param]=$this->values;
270     }
271}
272
273class booleanFilter extends Filter {
274     public function getType() {
275          return "boolean";
276     }
277}
278
279class valueFilter extends Filter {
280     protected $size;
281     protected $max;
282     protected $default;
283     protected $no_value;
284     
285     public function __construct($id,$desc,$request_param,$size, $max, $no_value='',$default=null) {
286          parent::__construct($id,$desc,$request_param);
287          $this->size = $size;
288          $this->max = $max;
289          $this->default_value = $default;
290          $this->no_value = $no_value;
291     }
292
293     public function getType() {
294          return "value";
295     }
296     
297     
298     public function getFormFields() {
299          return '<label for="'.$this->field_id.'">'.$this->desc.'</label>'.
300               form::field($this->field_id,$this->size,$this->max,$this->value);
301     
302     }
303     
304     public function setValues($form_data) {
305          parent::setValues($form_data);
306          if ($this->enabled && $this->value==$this->no_value) {
307               $this->enabled = false;
308               $this->value=null;
309          }
310     }
311}
312
313?>
Note: See TracBrowser for help on using the repository browser.

Sites map