Commit | Line | Data |
---|---|---|
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 |
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 | { | |
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 | 56 | abstract 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 | 116 | abstract 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 | } | |
852e6400 RB |
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 | } | |
7c44b8bc RB |
136 | } |
137 | // }}} | |
138 | ||
0d78a96b RB |
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 | ||
7da01959 | 150 | protected function getSortTokens(PlFilter $pf) |
0d78a96b RB |
151 | { |
152 | if ($this->seed == null) { | |
153 | return 'RAND()'; | |
154 | } else { | |
155 | return XDB::format('RAND({?})', $this->seed); | |
156 | } | |
157 | } | |
8e87570c R |
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 | } | |
0d78a96b RB |
166 | } |
167 | // }}} | |
f66f26e2 RB |
168 | |
169 | // {{{ interface PlFilterCondition | |
8e87570c | 170 | interface PlFilterCondition extends PlExportable |
f66f26e2 RB |
171 | { |
172 | const COND_TRUE = 'TRUE'; | |
173 | const COND_FALSE = 'FALSE'; | |
174 | ||
7da01959 | 175 | public function buildCondition(PlFilter $pf); |
f66f26e2 RB |
176 | } |
177 | // }}} | |
178 | ||
179 | // {{{ class PFC_OneChild | |
180 | abstract class PFC_OneChild implements PlFilterCondition | |
181 | { | |
182 | protected $child; | |
183 | ||
7da01959 | 184 | public function __construct($child = null) |
f66f26e2 RB |
185 | { |
186 | if (!is_null($child) && ($child instanceof PlFilterCondition)) { | |
187 | $this->setChild($child); | |
188 | } | |
189 | } | |
190 | ||
7da01959 | 191 | public function setChild(PlFilterCondition $cond) |
f66f26e2 | 192 | { |
7da01959 | 193 | $this->child = $cond; |
f66f26e2 | 194 | } |
8e87570c R |
195 | |
196 | public function export() | |
197 | { | |
8623e0ef | 198 | return array('child' => $this->child->export()); |
8e87570c | 199 | } |
f66f26e2 RB |
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 | { | |
bf0ebb51 FB |
210 | $this->addChildren(pl_flatten(func_get_args())); |
211 | } | |
212 | ||
213 | public function addChildren(array $conds) | |
214 | { | |
7da01959 | 215 | foreach ($conds as $cond) { |
bf0ebb51 FB |
216 | if (!is_null($cond) && ($cond instanceof PlFilterCondition)) { |
217 | $this->addChild($cond); | |
f66f26e2 RB |
218 | } |
219 | } | |
220 | } | |
221 | ||
7da01959 | 222 | public function addChild(PlFilterCondition $cond) |
f66f26e2 | 223 | { |
7da01959 | 224 | $this->children[] = $cond; |
f66f26e2 RB |
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 | } | |
8e87570c R |
237 | |
238 | public function export() | |
239 | { | |
240 | $export = array(); | |
23749c61 | 241 | foreach ($this->children as $child) { |
8e87570c | 242 | $export[] = $child->export(); |
23749c61 | 243 | } |
8e87570c R |
244 | return array('children' => $export); |
245 | } | |
f66f26e2 RB |
246 | } |
247 | // }}} | |
248 | ||
249 | // {{{ class PFC_True | |
250 | class PFC_True implements PlFilterCondition | |
251 | { | |
7da01959 | 252 | public function buildCondition(PlFilter $uf) |
f66f26e2 RB |
253 | { |
254 | return self::COND_TRUE; | |
255 | } | |
8e87570c R |
256 | |
257 | public function export() | |
258 | { | |
259 | return array('type' => 'true'); | |
260 | } | |
f66f26e2 RB |
261 | } |
262 | // }}} | |
263 | ||
264 | // {{{ class PFC_False | |
265 | class PFC_False implements PlFilterCondition | |
266 | { | |
7da01959 | 267 | public function buildCondition(PlFilter $uf) |
f66f26e2 RB |
268 | { |
269 | return self::COND_FALSE; | |
270 | } | |
8e87570c R |
271 | |
272 | public function export() | |
273 | { | |
274 | return array('type' => 'false'); | |
275 | } | |
f66f26e2 RB |
276 | } |
277 | // }}} | |
278 | ||
279 | // {{{ class PFC_Not | |
280 | class PFC_Not extends PFC_OneChild | |
281 | { | |
7da01959 | 282 | public function buildCondition(PlFilter $uf) |
f66f26e2 RB |
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 | } | |
8e87570c R |
293 | |
294 | public function export() | |
295 | { | |
296 | $export = parent::export(); | |
297 | $export['type'] = 'not'; | |
298 | return $export; | |
299 | } | |
f66f26e2 RB |
300 | } |
301 | // }}} | |
302 | ||
303 | // {{{ class PFC_And | |
304 | class PFC_And extends PFC_NChildren | |
305 | { | |
7da01959 | 306 | public function buildCondition(PlFilter $uf) |
f66f26e2 RB |
307 | { |
308 | if (empty($this->children)) { | |
309 | return self::COND_FALSE; | |
310 | } else { | |
311 | $true = self::COND_FALSE; | |
312 | $conds = array(); | |
7da01959 | 313 | foreach ($this->children as $child) { |
f66f26e2 RB |
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 | } | |
8e87570c R |
326 | |
327 | public function export() { | |
328 | $export = parent::export(); | |
329 | $export['type'] = 'and'; | |
330 | return $export; | |
331 | } | |
f66f26e2 RB |
332 | } |
333 | // }}} | |
334 | ||
335 | // {{{ class PFC_Or | |
336 | class PFC_Or extends PFC_NChildren | |
337 | { | |
7da01959 | 338 | public function buildCondition(PlFilter $uf) |
f66f26e2 RB |
339 | { |
340 | if (empty($this->children)) { | |
341 | return self::COND_TRUE; | |
342 | } else { | |
343 | $true = self::COND_TRUE; | |
344 | $conds = array(); | |
7da01959 | 345 | foreach ($this->children as $child) { |
f66f26e2 RB |
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 | } | |
8e87570c R |
358 | |
359 | public function export() { | |
360 | $export = parent::export(); | |
361 | $export['type'] = 'or'; | |
362 | return $export; | |
363 | } | |
f66f26e2 RB |
364 | } |
365 | // }}} | |
366 | ||
0d78a96b | 367 | // {{{ class PlFilter |
8e87570c | 368 | abstract class PlFilter implements PlExportable |
bd9b36fe RB |
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 | */ | |
e1746810 | 374 | public abstract function filter(array $objects, $limit = null); |
bd9b36fe | 375 | |
7da01959 | 376 | public abstract function setCondition(PlFilterCondition $cond); |
bd9b36fe | 377 | |
7da01959 | 378 | public abstract function addSort(PlFilterOrder $sort); |
bd9b36fe RB |
379 | |
380 | public abstract function getTotalCount(); | |
381 | ||
7c44b8bc RB |
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 | ||
bd9b36fe RB |
392 | /** Get objects, selecting only those within a limit |
393 | * @param $limit The portion of the matching objects to select | |
394 | */ | |
e1746810 | 395 | public abstract function get($limit = null); |
bd9b36fe | 396 | |
4f4f166c SJ |
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 | ||
bd9b36fe RB |
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 | */ | |
9a122520 RB |
413 | protected $joinMetas = array(); |
414 | ||
415 | protected $joinMethods = array(); | |
285fb262 | 416 | |
bd9b36fe RB |
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 | */ | |
9a122520 | 421 | protected function buildJoins() |
285fb262 RB |
422 | { |
423 | $joins = array(); | |
9a122520 | 424 | foreach ($this->joinMethods as $method) { |
285fb262 RB |
425 | $joins = array_merge($joins, $this->$method()); |
426 | } | |
9a122520 | 427 | return PlSqlJoin::formatJoins($joins, $this->joinMetas); |
285fb262 RB |
428 | } |
429 | ||
430 | } | |
0d78a96b | 431 | // }}} |
285fb262 | 432 | |
ae3effcc | 433 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
285fb262 | 434 | ?> |