PlFilterOrder is an abstract class, put a default implementation for
[platal.git] / classes / plfilter.php
CommitLineData
285fb262
RB
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
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
e05c0f54
FB
65 public function export()
66 {
67 throw new Exception("This instance is not exportable");
68 }
69
f66f26e2
RB
70 public function toggleDesc()
71 {
067a59f4 72 $this->desc = !$this->desc;
f66f26e2
RB
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);
7c44b8bc 83 $this->_tokens = $sel;
f66f26e2
RB
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
7c44b8bc
RB
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 */
0d78a96b 99 abstract protected function getSortTokens(PlFilter &$pf);
f66f26e2 100}
0d78a96b
RB
101// }}}
102
7c44b8bc
RB
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 */
85bb0c6f 107abstract class PlFilterGroupableOrder extends PlFilterOrder
7c44b8bc
RB
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
0d78a96b
RB
120// {{{ class PFO_Random
121class 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 }
8e87570c
R
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 }
0d78a96b
RB
147}
148// }}}
f66f26e2
RB
149
150// {{{ interface PlFilterCondition
8e87570c 151interface PlFilterCondition extends PlExportable
f66f26e2
RB
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
161abstract 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 }
8e87570c
R
176
177 public function export()
178 {
179 return array('child' => $child->export());
180 }
f66f26e2
RB
181}
182// }}}
183
184// {{{ class PFC_NChildren
185abstract class PFC_NChildren implements PlFilterCondition
186{
187 protected $children = array();
188
189 public function __construct()
190 {
bf0ebb51
FB
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);
f66f26e2
RB
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 }
8e87570c
R
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 }
f66f26e2
RB
226}
227// }}}
228
229// {{{ class PFC_True
230class PFC_True implements PlFilterCondition
231{
232 public function buildCondition(PlFilter &$uf)
233 {
234 return self::COND_TRUE;
235 }
8e87570c
R
236
237 public function export()
238 {
239 return array('type' => 'true');
240 }
f66f26e2
RB
241}
242// }}}
243
244// {{{ class PFC_False
245class PFC_False implements PlFilterCondition
246{
247 public function buildCondition(PlFilter &$uf)
248 {
249 return self::COND_FALSE;
250 }
8e87570c
R
251
252 public function export()
253 {
254 return array('type' => 'false');
255 }
f66f26e2
RB
256}
257// }}}
258
259// {{{ class PFC_Not
260class 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 }
8e87570c
R
273
274 public function export()
275 {
276 $export = parent::export();
277 $export['type'] = 'not';
278 return $export;
279 }
f66f26e2
RB
280}
281// }}}
282
283// {{{ class PFC_And
284class 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 }
8e87570c
R
306
307 public function export() {
308 $export = parent::export();
309 $export['type'] = 'and';
310 return $export;
311 }
f66f26e2
RB
312}
313// }}}
314
315// {{{ class PFC_Or
316class 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 }
8e87570c
R
338
339 public function export() {
340 $export = parent::export();
341 $export['type'] = 'or';
342 return $export;
343 }
f66f26e2
RB
344}
345// }}}
346
0d78a96b 347// {{{ class PlFilter
8e87570c 348abstract class PlFilter implements PlExportable
bd9b36fe
RB
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 */
e1746810 354 public abstract function filter(array $objects, $limit = null);
bd9b36fe
RB
355
356 public abstract function setCondition(PlFilterCondition &$cond);
357
358 public abstract function addSort(PlFilterOrder &$sort);
359
360 public abstract function getTotalCount();
361
7c44b8bc
RB
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
bd9b36fe
RB
372 /** Get objects, selecting only those within a limit
373 * @param $limit The portion of the matching objects to select
374 */
e1746810 375 public abstract function get($limit = null);
bd9b36fe
RB
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 */
9a122520
RB
385 protected $joinMetas = array();
386
387 protected $joinMethods = array();
285fb262 388
bd9b36fe
RB
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 */
9a122520 393 protected function buildJoins()
285fb262
RB
394 {
395 $joins = array();
9a122520 396 foreach ($this->joinMethods as $method) {
285fb262
RB
397 $joins = array_merge($joins, $this->$method());
398 }
9a122520 399 return PlSqlJoin::formatJoins($joins, $this->joinMetas);
285fb262
RB
400 }
401
402}
0d78a96b 403// }}}
285fb262 404
ae3effcc 405// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
285fb262 406?>