Fixes variable use.
[platal.git] / classes / plfilter.php
CommitLineData
285fb262
RB
1<?php
2/***************************************************************************
e92ecb8c 3 * Copyright (C) 2003-2011 Polytechnique.org *
285fb262
RB
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
857a2b96
FB
22__autoload('xdb');
23
0d78a96b 24// {{{ class PlLimit
bd9b36fe
RB
25class 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 {
4aa4899b 38 if (!is_null($this->count) && $this->count != 0) {
bd9b36fe
RB
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}
0d78a96b 48// }}}
bd9b36fe 49
0d78a96b 50// {{{ class PlFilterOrder
7c44b8bc
RB
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 */
8e87570c 56abstract class PlFilterOrder implements PlExportable
f66f26e2
RB
57{
58 protected $desc = false;
59 public function __construct($desc = false)
60 {
61 $this->desc = $desc;
7c44b8bc 62 $this->_tokens = null;
f66f26e2
RB
63 }
64
778e3dbb
FB
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
e05c0f54
FB
74 public function export()
75 {
76 throw new Exception("This instance is not exportable");
77 }
78
f66f26e2
RB
79 public function toggleDesc()
80 {
067a59f4 81 $this->desc = !$this->desc;
f66f26e2
RB
82 }
83
84 public function setDescending($desc = true)
85 {
86 $this->desc = $desc;
87 }
88
7da01959 89 public function buildSort(PlFilter $pf)
f66f26e2
RB
90 {
91 $sel = $this->getSortTokens($pf);
7c44b8bc 92 $this->_tokens = $sel;
f66f26e2
RB
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
7c44b8bc 104 /** This function must return the tokens to use for ordering
7da01959 105 * @param $pf The PlFilter whose results must be ordered
7c44b8bc
RB
106 * @return The name of the field to use for ordering results
107 */
7da01959 108 abstract protected function getSortTokens(PlFilter $pf);
f66f26e2 109}
0d78a96b
RB
110// }}}
111
7c44b8bc
RB
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 */
85bb0c6f 116abstract class PlFilterGroupableOrder extends PlFilterOrder
7c44b8bc
RB
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 */
7da01959 122 public function getGroupToken(PlFilter $pf)
7c44b8bc
RB
123 {
124 return $this->_tokens;
125 }
126}
127// }}}
128
0d78a96b
RB
129// {{{ class PFO_Random
130class 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
7da01959 140 protected function getSortTokens(PlFilter $pf)
0d78a96b
RB
141 {
142 if ($this->seed == null) {
143 return 'RAND()';
144 } else {
145 return XDB::format('RAND({?})', $this->seed);
146 }
147 }
8e87570c
R
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 }
0d78a96b
RB
156}
157// }}}
f66f26e2
RB
158
159// {{{ interface PlFilterCondition
8e87570c 160interface PlFilterCondition extends PlExportable
f66f26e2
RB
161{
162 const COND_TRUE = 'TRUE';
163 const COND_FALSE = 'FALSE';
164
7da01959 165 public function buildCondition(PlFilter $pf);
f66f26e2
RB
166}
167// }}}
168
169// {{{ class PFC_OneChild
170abstract class PFC_OneChild implements PlFilterCondition
171{
172 protected $child;
173
7da01959 174 public function __construct($child = null)
f66f26e2
RB
175 {
176 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
177 $this->setChild($child);
178 }
179 }
180
7da01959 181 public function setChild(PlFilterCondition $cond)
f66f26e2 182 {
7da01959 183 $this->child = $cond;
f66f26e2 184 }
8e87570c
R
185
186 public function export()
187 {
8623e0ef 188 return array('child' => $this->child->export());
8e87570c 189 }
f66f26e2
RB
190}
191// }}}
192
193// {{{ class PFC_NChildren
194abstract class PFC_NChildren implements PlFilterCondition
195{
196 protected $children = array();
197
198 public function __construct()
199 {
bf0ebb51
FB
200 $this->addChildren(pl_flatten(func_get_args()));
201 }
202
203 public function addChildren(array $conds)
204 {
7da01959 205 foreach ($conds as $cond) {
bf0ebb51
FB
206 if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
207 $this->addChild($cond);
f66f26e2
RB
208 }
209 }
210 }
211
7da01959 212 public function addChild(PlFilterCondition $cond)
f66f26e2 213 {
7da01959 214 $this->children[] = $cond;
f66f26e2
RB
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 }
8e87570c
R
227
228 public function export()
229 {
230 $export = array();
23749c61 231 foreach ($this->children as $child) {
8e87570c 232 $export[] = $child->export();
23749c61 233 }
8e87570c
R
234 return array('children' => $export);
235 }
f66f26e2
RB
236}
237// }}}
238
239// {{{ class PFC_True
240class PFC_True implements PlFilterCondition
241{
7da01959 242 public function buildCondition(PlFilter $uf)
f66f26e2
RB
243 {
244 return self::COND_TRUE;
245 }
8e87570c
R
246
247 public function export()
248 {
249 return array('type' => 'true');
250 }
f66f26e2
RB
251}
252// }}}
253
254// {{{ class PFC_False
255class PFC_False implements PlFilterCondition
256{
7da01959 257 public function buildCondition(PlFilter $uf)
f66f26e2
RB
258 {
259 return self::COND_FALSE;
260 }
8e87570c
R
261
262 public function export()
263 {
264 return array('type' => 'false');
265 }
f66f26e2
RB
266}
267// }}}
268
269// {{{ class PFC_Not
270class PFC_Not extends PFC_OneChild
271{
7da01959 272 public function buildCondition(PlFilter $uf)
f66f26e2
RB
273 {
274 $val = $this->child->buildCondition($uf);
275 if ($val == self::COND_TRUE) {
276 return self::COND_FALSE;
277 } else if ($val == self::COND_FALSE) {
278 return self::COND_TRUE;
279 } else {
280 return 'NOT (' . $val . ')';
281 }
282 }
8e87570c
R
283
284 public function export()
285 {
286 $export = parent::export();
287 $export['type'] = 'not';
288 return $export;
289 }
f66f26e2
RB
290}
291// }}}
292
293// {{{ class PFC_And
294class PFC_And extends PFC_NChildren
295{
7da01959 296 public function buildCondition(PlFilter $uf)
f66f26e2
RB
297 {
298 if (empty($this->children)) {
299 return self::COND_FALSE;
300 } else {
301 $true = self::COND_FALSE;
302 $conds = array();
7da01959 303 foreach ($this->children as $child) {
f66f26e2
RB
304 $val = $child->buildCondition($uf);
305 if ($val == self::COND_TRUE) {
306 $true = self::COND_TRUE;
307 } else if ($val == self::COND_FALSE) {
308 return self::COND_FALSE;
309 } else {
310 $conds[] = $val;
311 }
312 }
313 return $this->catConds($conds, 'AND', $true);
314 }
315 }
8e87570c
R
316
317 public function export() {
318 $export = parent::export();
319 $export['type'] = 'and';
320 return $export;
321 }
f66f26e2
RB
322}
323// }}}
324
325// {{{ class PFC_Or
326class PFC_Or extends PFC_NChildren
327{
7da01959 328 public function buildCondition(PlFilter $uf)
f66f26e2
RB
329 {
330 if (empty($this->children)) {
331 return self::COND_TRUE;
332 } else {
333 $true = self::COND_TRUE;
334 $conds = array();
7da01959 335 foreach ($this->children as $child) {
f66f26e2
RB
336 $val = $child->buildCondition($uf);
337 if ($val == self::COND_TRUE) {
338 return self::COND_TRUE;
339 } else if ($val == self::COND_FALSE) {
340 $true = self::COND_FALSE;
341 } else {
342 $conds[] = $val;
343 }
344 }
345 return $this->catConds($conds, 'OR', $true);
346 }
347 }
8e87570c
R
348
349 public function export() {
350 $export = parent::export();
351 $export['type'] = 'or';
352 return $export;
353 }
f66f26e2
RB
354}
355// }}}
356
0d78a96b 357// {{{ class PlFilter
8e87570c 358abstract class PlFilter implements PlExportable
bd9b36fe
RB
359{
360 /** Filters objects matching the PlFilter
361 * @param $objects The objects to filter
362 * @param $limit The portion of the matching objects to show
363 */
e1746810 364 public abstract function filter(array $objects, $limit = null);
bd9b36fe 365
7da01959 366 public abstract function setCondition(PlFilterCondition $cond);
bd9b36fe 367
7da01959 368 public abstract function addSort(PlFilterOrder $sort);
bd9b36fe
RB
369
370 public abstract function getTotalCount();
371
7c44b8bc
RB
372 /** Whether this PlFilter can return grouped results through
373 * $this->getGroups();
374 */
375 public abstract function hasGroups();
376
377 /** Used to retrieve value/amount resulting from grouping by the first
378 * given order.
379 */
380 public abstract function getGroups();
381
bd9b36fe
RB
382 /** Get objects, selecting only those within a limit
383 * @param $limit The portion of the matching objects to select
384 */
e1746810 385 public abstract function get($limit = null);
bd9b36fe 386
4f4f166c
SJ
387 /** Get ids, selecting only those within a limit
388 * @param $limit The portion of the matching objects to select
389 */
390 public function getIds($limit = null)
391 {
392 return $this->get($limit);
393 }
394
bd9b36fe
RB
395 /** PRIVATE FUNCTIONS
396 */
397
398 /** List of metas to replace in joins:
399 * '$COIN' => 'pan.x' means 'replace $COIN with pan.x in the condition of the joins'
400 *
401 * "$ME" => "joined table alias" is always added to these.
402 */
9a122520
RB
403 protected $joinMetas = array();
404
405 protected $joinMethods = array();
285fb262 406
bd9b36fe
RB
407 /** Build the 'join' part of the query
408 * This function will call all methods declared in self::$joinMethods
409 * to get an array of PlSqlJoin objects to merge
410 */
9a122520 411 protected function buildJoins()
285fb262
RB
412 {
413 $joins = array();
9a122520 414 foreach ($this->joinMethods as $method) {
285fb262
RB
415 $joins = array_merge($joins, $this->$method());
416 }
9a122520 417 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
285fb262
RB
418 }
419
420}
0d78a96b 421// }}}
285fb262 422
ae3effcc 423// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
285fb262 424?>