f36a8035bf2584fb152b09b70bff90513eeaf6ca
[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 protected function buildExport($type)
66 {
67 $export = array('type' => $type);
68 if ($this->desc) {
69 $export['order'] = 'desc';
70 }
71 return $export;
72 }
73
74 public function export()
75 {
76 throw new Exception("This instance is not exportable");
77 }
78
79 public function toggleDesc()
80 {
81 $this->desc = !$this->desc;
82 }
83
84 public function setDescending($desc = true)
85 {
86 $this->desc = $desc;
87 }
88
89 public function buildSort(PlFilter $pf)
90 {
91 $sel = $this->getSortTokens($pf);
92 $this->_tokens = $sel;
93 if (!is_array($sel)) {
94 $sel = array($sel);
95 }
96 if ($this->desc) {
97 foreach ($sel as $k => $s) {
98 $sel[$k] = $s . ' DESC';
99 }
100 }
101 return $sel;
102 }
103
104 /** This function must return the tokens to use for ordering
105 * @param $pf The PlFilter whose results must be ordered
106 * @return The name of the field to use for ordering results
107 */
108 abstract protected function getSortTokens(PlFilter $pf);
109 }
110 // }}}
111
112 // {{{ class PlFilterGroupableOrder
113 /** Extension of a PlFilterOrder, for orders where the value on which ordering
114 * is done could be used for grouping results (promo, country, ...)
115 */
116 abstract class PlFilterGroupableOrder extends PlFilterOrder
117 {
118 /** This function will be called when trying to retrieve groups;
119 * the returned token will be used to group the values.
120 * It will always be called AFTER getSortTokens().
121 */
122 public function getGroupToken(PlFilter $pf)
123 {
124 return $this->_tokens;
125 }
126 }
127 // }}}
128
129 // {{{ class PFO_Random
130 class PFO_Random extends PlFilterOrder
131 {
132 private $seed = null;
133
134 public function __construct($seed = null, $desc = false)
135 {
136 parent::__construct($desc);
137 $this->seed = $seed;
138 }
139
140 protected function getSortTokens(PlFilter $pf)
141 {
142 if ($this->seed == null) {
143 return 'RAND()';
144 } else {
145 return XDB::format('RAND({?})', $this->seed);
146 }
147 }
148
149 public function export()
150 {
151 $export = array('type' => 'random',);
152 if ($this->seed !== null)
153 $export['seed'] = $this->seed;
154 return $export;
155 }
156 }
157 // }}}
158
159 // {{{ interface PlFilterCondition
160 interface PlFilterCondition extends PlExportable
161 {
162 const COND_TRUE = 'TRUE';
163 const COND_FALSE = 'FALSE';
164
165 public function buildCondition(PlFilter $pf);
166 }
167 // }}}
168
169 // {{{ class PFC_OneChild
170 abstract class PFC_OneChild implements PlFilterCondition
171 {
172 protected $child;
173
174 public function __construct($child = null)
175 {
176 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
177 $this->setChild($child);
178 }
179 }
180
181 public function setChild(PlFilterCondition $cond)
182 {
183 $this->child = $cond;
184 }
185
186 public function export()
187 {
188 return array('child' => $child->export());
189 }
190 }
191 // }}}
192
193 // {{{ class PFC_NChildren
194 abstract class PFC_NChildren implements PlFilterCondition
195 {
196 protected $children = array();
197
198 public function __construct()
199 {
200 $this->addChildren(pl_flatten(func_get_args()));
201 }
202
203 public function addChildren(array $conds)
204 {
205 foreach ($conds as $cond) {
206 if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
207 $this->addChild($cond);
208 }
209 }
210 }
211
212 public function addChild(PlFilterCondition $cond)
213 {
214 $this->children[] = $cond;
215 }
216
217 protected function catConds(array $cond, $op, $fallback)
218 {
219 if (count($cond) == 0) {
220 return $fallback;
221 } else if (count($cond) == 1) {
222 return $cond[0];
223 } else {
224 return '(' . implode(') ' . $op . ' (', $cond) . ')';
225 }
226 }
227
228 public function export()
229 {
230 $export = array();
231 foreach ($this->children as $child)
232 $export[] = $child->export();
233 return array('children' => $export);
234 }
235 }
236 // }}}
237
238 // {{{ class PFC_True
239 class PFC_True implements PlFilterCondition
240 {
241 public function buildCondition(PlFilter $uf)
242 {
243 return self::COND_TRUE;
244 }
245
246 public function export()
247 {
248 return array('type' => 'true');
249 }
250 }
251 // }}}
252
253 // {{{ class PFC_False
254 class PFC_False implements PlFilterCondition
255 {
256 public function buildCondition(PlFilter $uf)
257 {
258 return self::COND_FALSE;
259 }
260
261 public function export()
262 {
263 return array('type' => 'false');
264 }
265 }
266 // }}}
267
268 // {{{ class PFC_Not
269 class PFC_Not extends PFC_OneChild
270 {
271 public function buildCondition(PlFilter $uf)
272 {
273 $val = $this->child->buildCondition($uf);
274 if ($val == self::COND_TRUE) {
275 return self::COND_FALSE;
276 } else if ($val == self::COND_FALSE) {
277 return self::COND_TRUE;
278 } else {
279 return 'NOT (' . $val . ')';
280 }
281 }
282
283 public function export()
284 {
285 $export = parent::export();
286 $export['type'] = 'not';
287 return $export;
288 }
289 }
290 // }}}
291
292 // {{{ class PFC_And
293 class PFC_And extends PFC_NChildren
294 {
295 public function buildCondition(PlFilter $uf)
296 {
297 if (empty($this->children)) {
298 return self::COND_FALSE;
299 } else {
300 $true = self::COND_FALSE;
301 $conds = array();
302 foreach ($this->children as $child) {
303 $val = $child->buildCondition($uf);
304 if ($val == self::COND_TRUE) {
305 $true = self::COND_TRUE;
306 } else if ($val == self::COND_FALSE) {
307 return self::COND_FALSE;
308 } else {
309 $conds[] = $val;
310 }
311 }
312 return $this->catConds($conds, 'AND', $true);
313 }
314 }
315
316 public function export() {
317 $export = parent::export();
318 $export['type'] = 'and';
319 return $export;
320 }
321 }
322 // }}}
323
324 // {{{ class PFC_Or
325 class PFC_Or extends PFC_NChildren
326 {
327 public function buildCondition(PlFilter $uf)
328 {
329 if (empty($this->children)) {
330 return self::COND_TRUE;
331 } else {
332 $true = self::COND_TRUE;
333 $conds = array();
334 foreach ($this->children as $child) {
335 $val = $child->buildCondition($uf);
336 if ($val == self::COND_TRUE) {
337 return self::COND_TRUE;
338 } else if ($val == self::COND_FALSE) {
339 $true = self::COND_FALSE;
340 } else {
341 $conds[] = $val;
342 }
343 }
344 return $this->catConds($conds, 'OR', $true);
345 }
346 }
347
348 public function export() {
349 $export = parent::export();
350 $export['type'] = 'or';
351 return $export;
352 }
353 }
354 // }}}
355
356 // {{{ class PlFilter
357 abstract class PlFilter implements PlExportable
358 {
359 /** Filters objects matching the PlFilter
360 * @param $objects The objects to filter
361 * @param $limit The portion of the matching objects to show
362 */
363 public abstract function filter(array $objects, $limit = null);
364
365 public abstract function setCondition(PlFilterCondition $cond);
366
367 public abstract function addSort(PlFilterOrder $sort);
368
369 public abstract function getTotalCount();
370
371 /** Whether this PlFilter can return grouped results through
372 * $this->getGroups();
373 */
374 public abstract function hasGroups();
375
376 /** Used to retrieve value/amount resulting from grouping by the first
377 * given order.
378 */
379 public abstract function getGroups();
380
381 /** Get objects, selecting only those within a limit
382 * @param $limit The portion of the matching objects to select
383 */
384 public abstract function get($limit = null);
385
386 /** PRIVATE FUNCTIONS
387 */
388
389 /** List of metas to replace in joins:
390 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
391 *
392 * "$ME" => "joined table alias" is always added to these.
393 */
394 protected $joinMetas = array();
395
396 protected $joinMethods = array();
397
398 /** Build the 'join' part of the query
399 * This function will call all methods declared in self::$joinMethods
400 * to get an array of PlSqlJoin objects to merge
401 */
402 protected function buildJoins()
403 {
404 $joins = array();
405 foreach ($this->joinMethods as $method) {
406 $joins = array_merge($joins, $this->$method());
407 }
408 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
409 }
410
411 }
412 // }}}
413
414 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
415 ?>