Removed unconsistent reference passing in PlFilter
[platal.git] / classes / plfilter.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 __autoload('xdb');
23
24 // {{{ class PlLimit
25 class PlLimit
26 {
27 private $count = null;
28 private $from = null;
29
30 public function __construct($count = null, $from = null)
31 {
32 $this->count = $count;
33 $this->from = $from;
34 }
35
36 public function getSql()
37 {
38 if (!is_null($this->count) && $this->count != 0) {
39 if (!is_null($this->from) && $this->from != 0) {
40 return XDB::format('LIMIT {?}, {?}', (int)$this->from, (int)$this->count);
41 } else {
42 return XDB::format('LIMIT {?}', (int)$this->count);
43 }
44 }
45 return '';
46 }
47 }
48 // }}}
49
50 // {{{ class PlFilterOrder
51 /** Base class for ordering results of a query.
52 * Parameters for the ordering must be given to the constructor ($desc for a
53 * descending order).
54 * The getSortTokens function is used to get actual ordering part of the query.
55 */
56 abstract class PlFilterOrder implements PlExportable
57 {
58 protected $desc = false;
59 public function __construct($desc = false)
60 {
61 $this->desc = $desc;
62 $this->_tokens = null;
63 }
64
65 public function export()
66 {
67 throw new Exception("This instance is not exportable");
68 }
69
70 public function toggleDesc()
71 {
72 $this->desc = !$this->desc;
73 }
74
75 public function setDescending($desc = true)
76 {
77 $this->desc = $desc;
78 }
79
80 public function buildSort(PlFilter $pf)
81 {
82 $sel = $this->getSortTokens($pf);
83 $this->_tokens = $sel;
84 if (!is_array($sel)) {
85 $sel = array($sel);
86 }
87 if ($this->desc) {
88 foreach ($sel as $k => $s) {
89 $sel[$k] = $s . ' DESC';
90 }
91 }
92 return $sel;
93 }
94
95 /** This function must return the tokens to use for ordering
96 * @param $pf The PlFilter whose results must be ordered
97 * @return The name of the field to use for ordering results
98 */
99 abstract protected function getSortTokens(PlFilter $pf);
100 }
101 // }}}
102
103 // {{{ class PlFilterGroupableOrder
104 /** Extension of a PlFilterOrder, for orders where the value on which ordering
105 * is done could be used for grouping results (promo, country, ...)
106 */
107 abstract class PlFilterGroupableOrder extends PlFilterOrder
108 {
109 /** This function will be called when trying to retrieve groups;
110 * the returned token will be used to group the values.
111 * It will always be called AFTER getSortTokens().
112 */
113 public function getGroupToken(PlFilter $pf)
114 {
115 return $this->_tokens;
116 }
117 }
118 // }}}
119
120 // {{{ class PFO_Random
121 class PFO_Random extends PlFilterOrder
122 {
123 private $seed = null;
124
125 public function __construct($seed = null, $desc = false)
126 {
127 parent::__construct($desc);
128 $this->seed = $seed;
129 }
130
131 protected function getSortTokens(PlFilter $pf)
132 {
133 if ($this->seed == null) {
134 return 'RAND()';
135 } else {
136 return XDB::format('RAND({?})', $this->seed);
137 }
138 }
139
140 public function export()
141 {
142 $export = array('type' => 'random',);
143 if ($this->seed !== null)
144 $export['seed'] = $this->seed;
145 return $export;
146 }
147 }
148 // }}}
149
150 // {{{ interface PlFilterCondition
151 interface PlFilterCondition extends PlExportable
152 {
153 const COND_TRUE = 'TRUE';
154 const COND_FALSE = 'FALSE';
155
156 public function buildCondition(PlFilter $pf);
157 }
158 // }}}
159
160 // {{{ class PFC_OneChild
161 abstract class PFC_OneChild implements PlFilterCondition
162 {
163 protected $child;
164
165 public function __construct($child = null)
166 {
167 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
168 $this->setChild($child);
169 }
170 }
171
172 public function setChild(PlFilterCondition $cond)
173 {
174 $this->child = $cond;
175 }
176
177 public function export()
178 {
179 return array('child' => $child->export());
180 }
181 }
182 // }}}
183
184 // {{{ class PFC_NChildren
185 abstract class PFC_NChildren implements PlFilterCondition
186 {
187 protected $children = array();
188
189 public function __construct()
190 {
191 $this->addChildren(pl_flatten(func_get_args()));
192 }
193
194 public function addChildren(array $conds)
195 {
196 foreach ($conds as $cond) {
197 if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
198 $this->addChild($cond);
199 }
200 }
201 }
202
203 public function addChild(PlFilterCondition $cond)
204 {
205 $this->children[] = $cond;
206 }
207
208 protected function catConds(array $cond, $op, $fallback)
209 {
210 if (count($cond) == 0) {
211 return $fallback;
212 } else if (count($cond) == 1) {
213 return $cond[0];
214 } else {
215 return '(' . implode(') ' . $op . ' (', $cond) . ')';
216 }
217 }
218
219 public function export()
220 {
221 $export = array();
222 foreach ($this->children as $child)
223 $export[] = $child->export();
224 return array('children' => $export);
225 }
226 }
227 // }}}
228
229 // {{{ class PFC_True
230 class PFC_True implements PlFilterCondition
231 {
232 public function buildCondition(PlFilter $uf)
233 {
234 return self::COND_TRUE;
235 }
236
237 public function export()
238 {
239 return array('type' => 'true');
240 }
241 }
242 // }}}
243
244 // {{{ class PFC_False
245 class PFC_False implements PlFilterCondition
246 {
247 public function buildCondition(PlFilter $uf)
248 {
249 return self::COND_FALSE;
250 }
251
252 public function export()
253 {
254 return array('type' => 'false');
255 }
256 }
257 // }}}
258
259 // {{{ class PFC_Not
260 class PFC_Not extends PFC_OneChild
261 {
262 public function buildCondition(PlFilter $uf)
263 {
264 $val = $this->child->buildCondition($uf);
265 if ($val == self::COND_TRUE) {
266 return self::COND_FALSE;
267 } else if ($val == self::COND_FALSE) {
268 return self::COND_TRUE;
269 } else {
270 return 'NOT (' . $val . ')';
271 }
272 }
273
274 public function export()
275 {
276 $export = parent::export();
277 $export['type'] = 'not';
278 return $export;
279 }
280 }
281 // }}}
282
283 // {{{ class PFC_And
284 class PFC_And extends PFC_NChildren
285 {
286 public function buildCondition(PlFilter $uf)
287 {
288 if (empty($this->children)) {
289 return self::COND_FALSE;
290 } else {
291 $true = self::COND_FALSE;
292 $conds = array();
293 foreach ($this->children as $child) {
294 $val = $child->buildCondition($uf);
295 if ($val == self::COND_TRUE) {
296 $true = self::COND_TRUE;
297 } else if ($val == self::COND_FALSE) {
298 return self::COND_FALSE;
299 } else {
300 $conds[] = $val;
301 }
302 }
303 return $this->catConds($conds, 'AND', $true);
304 }
305 }
306
307 public function export() {
308 $export = parent::export();
309 $export['type'] = 'and';
310 return $export;
311 }
312 }
313 // }}}
314
315 // {{{ class PFC_Or
316 class PFC_Or extends PFC_NChildren
317 {
318 public function buildCondition(PlFilter $uf)
319 {
320 if (empty($this->children)) {
321 return self::COND_TRUE;
322 } else {
323 $true = self::COND_TRUE;
324 $conds = array();
325 foreach ($this->children as $child) {
326 $val = $child->buildCondition($uf);
327 if ($val == self::COND_TRUE) {
328 return self::COND_TRUE;
329 } else if ($val == self::COND_FALSE) {
330 $true = self::COND_FALSE;
331 } else {
332 $conds[] = $val;
333 }
334 }
335 return $this->catConds($conds, 'OR', $true);
336 }
337 }
338
339 public function export() {
340 $export = parent::export();
341 $export['type'] = 'or';
342 return $export;
343 }
344 }
345 // }}}
346
347 // {{{ class PlFilter
348 abstract class PlFilter implements PlExportable
349 {
350 /** Filters objects matching the PlFilter
351 * @param $objects The objects to filter
352 * @param $limit The portion of the matching objects to show
353 */
354 public abstract function filter(array $objects, $limit = null);
355
356 public abstract function setCondition(PlFilterCondition $cond);
357
358 public abstract function addSort(PlFilterOrder $sort);
359
360 public abstract function getTotalCount();
361
362 /** Whether this PlFilter can return grouped results through
363 * $this->getGroups();
364 */
365 public abstract function hasGroups();
366
367 /** Used to retrieve value/amount resulting from grouping by the first
368 * given order.
369 */
370 public abstract function getGroups();
371
372 /** Get objects, selecting only those within a limit
373 * @param $limit The portion of the matching objects to select
374 */
375 public abstract function get($limit = null);
376
377 /** PRIVATE FUNCTIONS
378 */
379
380 /** List of metas to replace in joins:
381 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
382 *
383 * "$ME" => "joined table alias" is always added to these.
384 */
385 protected $joinMetas = array();
386
387 protected $joinMethods = array();
388
389 /** Build the 'join' part of the query
390 * This function will call all methods declared in self::$joinMethods
391 * to get an array of PlSqlJoin objects to merge
392 */
393 protected function buildJoins()
394 {
395 $joins = array();
396 foreach ($this->joinMethods as $method) {
397 $joins = array_merge($joins, $this->$method());
398 }
399 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
400 }
401
402 }
403 // }}}
404
405 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
406 ?>