XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
criteria.php
1<?php
14
15
16define('XOOPS_CRITERIA_ASC', 'ASC');
17define('XOOPS_CRITERIA_DESC', 'DESC');
18define('XOOPS_CRITERIA_STARTWITH', 1);
19define('XOOPS_CRITERIA_ENDWITH', 2);
20define('XOOPS_CRITERIA_CONTAIN', 3);
21
22
24{
29 public $order = [];
30
34 public $sort = [];
35
40 public $limit = 0;
41
46 public $start = 0;
47
51 public $groupby = '';
52
56 public function __construct()
57 {
58 }
59
64 public function render()
65 {
66 }
67
71 public function hasChildElements()
72 {
73 return false;
74 }
75
76 public function getCountChildElements()
77 {
78 return 0;
79 }
80
86 public function getChildElement($idx)
87 {
88 return null;
89 }
90
96 public function getCondition($idx)
97 {
98 return null;
99 }
100
101 public function getName()
102 {
103 return null;
104 }
105
106 public function getValue()
107 {
108 return null;
109 }
110
111 public function getOperator()
112 {
113 return null;
114 }
115
123 public function setSort($sort, $order = null)
124 {
125 $this->sort[0] = $sort;
126
127 if (!isset($this->order[0])) {
128 $this->order[0] = 'ASC';
129 }
130
131 if (null != $order) {
132 if ('ASC' == strtoupper($order)) {
133 $this->order[0] = 'ASC';
134 } elseif ('DESC' == strtoupper($order)) {
135 $this->order[0] = 'DESC';
136 }
137 }
138 }
139
145 public function addSort($sort, $order = 'ASC')
146 {
147 $this->sort[] = $sort;
148 if ('ASC' == strtoupper($order)) {
149 $this->order[] = 'ASC';
150 } elseif ('DESC' == strtoupper($order)) {
151 $this->order[] = 'DESC';
152 }
153 }
154
158 public function getSort()
159 {
160 if (isset($this->sort[0])) {
161 return $this->sort[0];
162 }
163
164 return '';
165 }
166
172 public function getSorts()
173 {
174 $ret = [];
175 $max = count($this->sort);
176
177 for ($i = 0; $i < $max; $i++) {
178 $ret[$i]['sort'] = $this->sort[$i];
179 $ret[$i]['order'] = $this->order[$i] ?? 'ASC';
180 }
181
182 return $ret;
183 }
184
189 public function setOrder($order)
190 {
191 if ('ASC' == strtoupper($order)) {
192 $this->order[0] = 'ASC';
193 } elseif ('DESC' === strtoupper($order)) {
194 $this->order[0] = 'DESC';
195 }
196 }
197
201 public function getOrder()
202 {
203 if (isset($this->order[0])) {
204 return $this->order[0];
205 }
206
207 return 'ASC';
208 }
209
213 public function setLimit($limit=0)
214 {
215 $this->limit = (int)$limit;
216 }
217
221 public function getLimit()
222 {
223 return $this->limit;
224 }
225
229 public function setStart($start=0)
230 {
231 $this->start = (int)$start;
232 }
233
237 public function getStart()
238 {
239 return $this->start;
240 }
241
246 public function setGroupby($group)
247 {
248 $this->groupby = $group;
249 }
250
255 public function getGroupby()
256 {
257 return ' GROUP BY '.$this->groupby;
258 }
259
260}
261
267{
268
273 public $criteriaElements = [];
274
279 public $conditions = [];
280
287 public function __construct($ele=null, $condition='AND')
288 {
289 if (isset($ele) && is_object($ele)) {
290 $this->add($ele, $condition);
291 }
292 }
293
294 public function hasChildElements()
295 {
296 return count($this->criteriaElements) > 0;
297 }
298
299 public function getCountChildElements()
300 {
301 return count($this->criteriaElements);
302 }
303
304 public function getChildElement($idx)
305 {
306 return $this->criteriaElements[$idx];
307 }
308
309 public function getCondition($idx)
310 {
311 return $this->conditions[$idx];
312 }
313
322 public function &add($criteriaElement, $condition='AND')
323 {
324 $this->criteriaElements[] =& $criteriaElement;
325 $this->conditions[] = $condition;
326 return $this;
327 }
328
335 public function render()
336 {
337 $ret = '';
338 $count = count($this->criteriaElements);
339 if ($count > 0) {
340 $elems =& $this->criteriaElements;
341 $conds =& $this->conditions;
342 $ret = '('. $elems[0]->render();
343 for ($i = 1; $i < $count; $i++) {
344 $ret .= ' '.$conds[$i].' '.$elems[$i]->render();
345 }
346 $ret .= ')';
347 }
348 return $ret;
349 }
350
357 public function renderWhere()
358 {
359 $ret = $this->render();
360 $ret = ('' !== $ret) ? 'WHERE ' . $ret : $ret;
361 return $ret;
362 }
363
371 public function renderLdap()
372 {
373 $retval = '';
374 $count = count($this->criteriaElements);
375 if ($count > 0) {
376 $retval = $this->criteriaElements[0]->renderLdap();
377 for ($i = 1; $i < $count; $i++) {
378 $cond = $this->conditions[$i];
379 if ('AND' == strtoupper($cond)) {
380 $op = '&';
381 } elseif ('OR' == strtoupper($cond)) {
382 $op = '|';
383 }
384 $retval = "($op$retval" . $this->criteriaElements[$i]->renderLdap() . ')';
385 }
386 }
387 return $retval;
388 }
389}
390
391
402{
403
407 public $prefix;
408 public $function;
409 public $column;
410 public $operator;
411 public $value;
412
413 public $dtype = 0;
414
424 public function __construct($column, $value='', $operator='=', $prefix = '', $function = '')
425 {
426 $this->prefix = $prefix;
427 $this->function = $function;
428 $this->column = $column;
429 $this->operator = $operator;
430
431 //
432 // Recive DTYPE. This is a prolongation of criterion life operation.
433 //
434 if (is_array($value) && 2 == count($value) && 'IN' !== $operator && 'NOT IN' !== $operator) {
435 $this->dtype = (int)$value[0];
436 $this->value = $value[1];
437 } else {
438 $this->value = $value;
439 }
440 }
441
442 public function getName()
443 {
444 return $this->column;
445 }
446
447 public function getValue()
448 {
449 return $this->value;
450 }
451
452 public function getOperator()
453 {
454 return $this->operator;
455 }
456
463 public function render()
464 {
465 $value = $this->value;
466 if (in_array(strtoupper($this->operator), ['IN', 'NOT IN'])) {
467 $value = is_array($value) ? implode(',', $value) : trim($value, " ()\t"); // [Compat] allow value '("a", "b", "c")'
468 if (isset($value)) {
469 $value = '('.$value.')';
470 } else {
471 $value = '("")';
472 }
473 } else {
474 $value = "'$value'";
475 }
476 $clause = (!empty($this->prefix) ? $this->prefix.'.' : '') . $this->column;
477 if (!empty($this->function)) {
478 $clause = sprintf($this->function, $clause);
479 }
480 $clause .= ' '.$this->operator.' '.$value;
481 return $clause;
482 }
483
491 public function renderLdap()
492 {
493 return '(' . $this->column . $this->operator . $this->value . ')';
494 }
495
502 public function renderWhere()
503 {
504 $cond = $this->render();
505 return empty($cond) ? '' : "WHERE $cond";
506 }
507}
getChildElement($idx)
Definition criteria.php:304
__construct($ele=null, $condition='AND')
Definition criteria.php:287
getCondition($idx)
Definition criteria.php:309
& add($criteriaElement, $condition='AND')
Definition criteria.php:322
setGroupby($group)
Definition criteria.php:246
setOrder($order)
Definition criteria.php:189
getChildElement($idx)
Definition criteria.php:86
setLimit($limit=0)
Definition criteria.php:213
setStart($start=0)
Definition criteria.php:229
setSort($sort, $order=null)
Definition criteria.php:123
addSort($sort, $order='ASC')
Definition criteria.php:145
getCondition($idx)
Definition criteria.php:96
renderWhere()
Definition criteria.php:502
__construct($column, $value='', $operator='=', $prefix='', $function='')
Definition criteria.php:424