| 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 | // }}} |
| 128 | |
| 129 | // {{{ class PFO_Random |
| 130 | class 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 | |
| 140 | protected function getSortTokens(PlFilter $pf) |
| 141 | { |
| 142 | if ($this->seed == null) { |
| 143 | return 'RAND()'; |
| 144 | } else { |
| 145 | return XDB::format('RAND({?})', $this->seed); |
| 146 | } |
| 147 | } |
| 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 | } |
| 156 | } |
| 157 | // }}} |
| 158 | |
| 159 | // {{{ interface PlFilterCondition |
| 160 | interface PlFilterCondition extends PlExportable |
| 161 | { |
| 162 | const COND_TRUE = 'TRUE'; |
| 163 | const COND_FALSE = 'FALSE'; |
| 164 | |
| 165 | public function buildCondition(PlFilter $pf); |
| 166 | } |
| 167 | // }}} |
| 168 | |
| 169 | // {{{ class PFC_OneChild |
| 170 | abstract class PFC_OneChild implements PlFilterCondition |
| 171 | { |
| 172 | protected $child; |
| 173 | |
| 174 | public function __construct($child = null) |
| 175 | { |
| 176 | if (!is_null($child) && ($child instanceof PlFilterCondition)) { |
| 177 | $this->setChild($child); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public function setChild(PlFilterCondition $cond) |
| 182 | { |
| 183 | $this->child = $cond; |
| 184 | } |
| 185 | |
| 186 | public function export() |
| 187 | { |
| 188 | return array('child' => $child->export()); |
| 189 | } |
| 190 | } |
| 191 | // }}} |
| 192 | |
| 193 | // {{{ class PFC_NChildren |
| 194 | abstract class PFC_NChildren implements PlFilterCondition |
| 195 | { |
| 196 | protected $children = array(); |
| 197 | |
| 198 | public function __construct() |
| 199 | { |
| 200 | $this->addChildren(pl_flatten(func_get_args())); |
| 201 | } |
| 202 | |
| 203 | public function addChildren(array $conds) |
| 204 | { |
| 205 | foreach ($conds as $cond) { |
| 206 | if (!is_null($cond) && ($cond instanceof PlFilterCondition)) { |
| 207 | $this->addChild($cond); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public function addChild(PlFilterCondition $cond) |
| 213 | { |
| 214 | $this->children[] = $cond; |
| 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 | } |
| 227 | |
| 228 | public function export() |
| 229 | { |
| 230 | $export = array(); |
| 231 | foreach ($this->children as $child) { |
| 232 | $export[] = $child->export(); |
| 233 | } |
| 234 | return array('children' => $export); |
| 235 | } |
| 236 | } |
| 237 | // }}} |
| 238 | |
| 239 | // {{{ class PFC_True |
| 240 | class PFC_True implements PlFilterCondition |
| 241 | { |
| 242 | public function buildCondition(PlFilter $uf) |
| 243 | { |
| 244 | return self::COND_TRUE; |
| 245 | } |
| 246 | |
| 247 | public function export() |
| 248 | { |
| 249 | return array('type' => 'true'); |
| 250 | } |
| 251 | } |
| 252 | // }}} |
| 253 | |
| 254 | // {{{ class PFC_False |
| 255 | class PFC_False implements PlFilterCondition |
| 256 | { |
| 257 | public function buildCondition(PlFilter $uf) |
| 258 | { |
| 259 | return self::COND_FALSE; |
| 260 | } |
| 261 | |
| 262 | public function export() |
| 263 | { |
| 264 | return array('type' => 'false'); |
| 265 | } |
| 266 | } |
| 267 | // }}} |
| 268 | |
| 269 | // {{{ class PFC_Not |
| 270 | class PFC_Not extends PFC_OneChild |
| 271 | { |
| 272 | public function buildCondition(PlFilter $uf) |
| 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 | } |
| 283 | |
| 284 | public function export() |
| 285 | { |
| 286 | $export = parent::export(); |
| 287 | $export['type'] = 'not'; |
| 288 | return $export; |
| 289 | } |
| 290 | } |
| 291 | // }}} |
| 292 | |
| 293 | // {{{ class PFC_And |
| 294 | class PFC_And extends PFC_NChildren |
| 295 | { |
| 296 | public function buildCondition(PlFilter $uf) |
| 297 | { |
| 298 | if (empty($this->children)) { |
| 299 | return self::COND_FALSE; |
| 300 | } else { |
| 301 | $true = self::COND_FALSE; |
| 302 | $conds = array(); |
| 303 | foreach ($this->children as $child) { |
| 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 | } |
| 316 | |
| 317 | public function export() { |
| 318 | $export = parent::export(); |
| 319 | $export['type'] = 'and'; |
| 320 | return $export; |
| 321 | } |
| 322 | } |
| 323 | // }}} |
| 324 | |
| 325 | // {{{ class PFC_Or |
| 326 | class PFC_Or extends PFC_NChildren |
| 327 | { |
| 328 | public function buildCondition(PlFilter $uf) |
| 329 | { |
| 330 | if (empty($this->children)) { |
| 331 | return self::COND_TRUE; |
| 332 | } else { |
| 333 | $true = self::COND_TRUE; |
| 334 | $conds = array(); |
| 335 | foreach ($this->children as $child) { |
| 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 | } |
| 348 | |
| 349 | public function export() { |
| 350 | $export = parent::export(); |
| 351 | $export['type'] = 'or'; |
| 352 | return $export; |
| 353 | } |
| 354 | } |
| 355 | // }}} |
| 356 | |
| 357 | // {{{ class PlFilter |
| 358 | abstract class PlFilter implements PlExportable |
| 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 | */ |
| 364 | public abstract function filter(array $objects, $limit = null); |
| 365 | |
| 366 | public abstract function setCondition(PlFilterCondition $cond); |
| 367 | |
| 368 | public abstract function addSort(PlFilterOrder $sort); |
| 369 | |
| 370 | public abstract function getTotalCount(); |
| 371 | |
| 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 | |
| 382 | /** Get objects, selecting only those within a limit |
| 383 | * @param $limit The portion of the matching objects to select |
| 384 | */ |
| 385 | public abstract function get($limit = null); |
| 386 | |
| 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 | |
| 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 | */ |
| 403 | protected $joinMetas = array(); |
| 404 | |
| 405 | protected $joinMethods = array(); |
| 406 | |
| 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 | */ |
| 411 | protected function buildJoins() |
| 412 | { |
| 413 | $joins = array(); |
| 414 | foreach ($this->joinMethods as $method) { |
| 415 | $joins = array_merge($joins, $this->$method()); |
| 416 | } |
| 417 | return PlSqlJoin::formatJoins($joins, $this->joinMetas); |
| 418 | } |
| 419 | |
| 420 | } |
| 421 | // }}} |
| 422 | |
| 423 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 424 | ?> |