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