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