Release plat/al core v1.1.13
[platal.git] / classes / plfilter.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 /** This function is called when trying to restrict to one of the
128 * specific values of the group token.
129 *
130 * @return A PlFilterCondition.
131 */
132 public function getCondition($group_value)
133 {
134 return null;
135 }
136 }
137 // }}}
138
139 // {{{ class PFO_Random
140 class PFO_Random extends PlFilterOrder
141 {
142 private $seed = null;
143
144 public function __construct($seed = null, $desc = false)
145 {
146 parent::__construct($desc);
147 $this->seed = $seed;
148 }
149
150 protected function getSortTokens(PlFilter $pf)
151 {
152 if ($this->seed == null) {
153 return 'RAND()';
154 } else {
155 return XDB::format('RAND({?})', $this->seed);
156 }
157 }
158
159 public function export()
160 {
161 $export = array('type' => 'random',);
162 if ($this->seed !== null)
163 $export['seed'] = $this->seed;
164 return $export;
165 }
166 }
167 // }}}
168
169 // {{{ interface PlFilterCondition
170 interface PlFilterCondition extends PlExportable
171 {
172 const COND_TRUE = 'TRUE';
173 const COND_FALSE = 'FALSE';
174
175 public function buildCondition(PlFilter $pf);
176 }
177 // }}}
178
179 // {{{ class PFC_OneChild
180 abstract class PFC_OneChild implements PlFilterCondition
181 {
182 protected $child;
183
184 public function __construct($child = null)
185 {
186 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
187 $this->setChild($child);
188 }
189 }
190
191 public function setChild(PlFilterCondition $cond)
192 {
193 $this->child = $cond;
194 }
195
196 public function export()
197 {
198 return array('child' => $this->child->export());
199 }
200 }
201 // }}}
202
203 // {{{ class PFC_NChildren
204 abstract class PFC_NChildren implements PlFilterCondition
205 {
206 protected $children = array();
207
208 public function __construct()
209 {
210 $this->addChildren(pl_flatten(func_get_args()));
211 }
212
213 public function addChildren(array $conds)
214 {
215 foreach ($conds as $cond) {
216 if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
217 $this->addChild($cond);
218 }
219 }
220 }
221
222 public function addChild(PlFilterCondition $cond)
223 {
224 $this->children[] = $cond;
225 }
226
227 protected function catConds(array $cond, $op, $fallback)
228 {
229 if (count($cond) == 0) {
230 return $fallback;
231 } else if (count($cond) == 1) {
232 return $cond[0];
233 } else {
234 return '(' . implode(') ' . $op . ' (', $cond) . ')';
235 }
236 }
237
238 public function export()
239 {
240 $export = array();
241 foreach ($this->children as $child) {
242 $export[] = $child->export();
243 }
244 return array('children' => $export);
245 }
246 }
247 // }}}
248
249 // {{{ class PFC_True
250 class PFC_True implements PlFilterCondition
251 {
252 public function buildCondition(PlFilter $uf)
253 {
254 return self::COND_TRUE;
255 }
256
257 public function export()
258 {
259 return array('type' => 'true');
260 }
261 }
262 // }}}
263
264 // {{{ class PFC_False
265 class PFC_False implements PlFilterCondition
266 {
267 public function buildCondition(PlFilter $uf)
268 {
269 return self::COND_FALSE;
270 }
271
272 public function export()
273 {
274 return array('type' => 'false');
275 }
276 }
277 // }}}
278
279 // {{{ class PFC_Not
280 class PFC_Not extends PFC_OneChild
281 {
282 public function buildCondition(PlFilter $uf)
283 {
284 $val = $this->child->buildCondition($uf);
285 if ($val == self::COND_TRUE) {
286 return self::COND_FALSE;
287 } else if ($val == self::COND_FALSE) {
288 return self::COND_TRUE;
289 } else {
290 return 'NOT (' . $val . ')';
291 }
292 }
293
294 public function export()
295 {
296 $export = parent::export();
297 $export['type'] = 'not';
298 return $export;
299 }
300 }
301 // }}}
302
303 // {{{ class PFC_And
304 class PFC_And extends PFC_NChildren
305 {
306 public function buildCondition(PlFilter $uf)
307 {
308 if (empty($this->children)) {
309 return self::COND_FALSE;
310 } else {
311 $true = self::COND_FALSE;
312 $conds = array();
313 foreach ($this->children as $child) {
314 $val = $child->buildCondition($uf);
315 if ($val == self::COND_TRUE) {
316 $true = self::COND_TRUE;
317 } else if ($val == self::COND_FALSE) {
318 return self::COND_FALSE;
319 } else {
320 $conds[] = $val;
321 }
322 }
323 return $this->catConds($conds, 'AND', $true);
324 }
325 }
326
327 public function export() {
328 $export = parent::export();
329 $export['type'] = 'and';
330 return $export;
331 }
332 }
333 // }}}
334
335 // {{{ class PFC_Or
336 class PFC_Or extends PFC_NChildren
337 {
338 public function buildCondition(PlFilter $uf)
339 {
340 if (empty($this->children)) {
341 return self::COND_TRUE;
342 } else {
343 $true = self::COND_TRUE;
344 $conds = array();
345 foreach ($this->children as $child) {
346 $val = $child->buildCondition($uf);
347 if ($val == self::COND_TRUE) {
348 return self::COND_TRUE;
349 } else if ($val == self::COND_FALSE) {
350 $true = self::COND_FALSE;
351 } else {
352 $conds[] = $val;
353 }
354 }
355 return $this->catConds($conds, 'OR', $true);
356 }
357 }
358
359 public function export() {
360 $export = parent::export();
361 $export['type'] = 'or';
362 return $export;
363 }
364 }
365 // }}}
366
367 // {{{ class PlFilter
368 abstract class PlFilter implements PlExportable
369 {
370 /** Filters objects matching the PlFilter
371 * @param $objects The objects to filter
372 * @param $limit The portion of the matching objects to show
373 */
374 public abstract function filter(array $objects, $limit = null);
375
376 public abstract function setCondition(PlFilterCondition $cond);
377
378 public abstract function addSort(PlFilterOrder $sort);
379
380 public abstract function getTotalCount();
381
382 /** Whether this PlFilter can return grouped results through
383 * $this->getGroups();
384 */
385 public abstract function hasGroups();
386
387 /** Used to retrieve value/amount resulting from grouping by the first
388 * given order.
389 */
390 public abstract function getGroups();
391
392 /** Get objects, selecting only those within a limit
393 * @param $limit The portion of the matching objects to select
394 */
395 public abstract function get($limit = null);
396
397 /** Get ids, selecting only those within a limit
398 * @param $limit The portion of the matching objects to select
399 */
400 public function getIds($limit = null)
401 {
402 return $this->get($limit);
403 }
404
405 /** PRIVATE FUNCTIONS
406 */
407
408 /** List of metas to replace in joins:
409 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
410 *
411 * "$ME" => "joined table alias" is always added to these.
412 */
413 protected $joinMetas = array();
414
415 protected $joinMethods = array();
416
417 /** Build the 'join' part of the query
418 * This function will call all methods declared in self::$joinMethods
419 * to get an array of PlSqlJoin objects to merge
420 */
421 protected function buildJoins()
422 {
423 $joins = array();
424 foreach ($this->joinMethods as $method) {
425 $joins = array_merge($joins, $this->$method());
426 }
427 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
428 }
429
430 }
431 // }}}
432
433 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
434 ?>