XOOPS  2.6.0
Criteria.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
13 
16 
31 {
35  public $prefix;
36 
40  public $function;
41 
45  public $column;
46 
50  public $operator;
51 
55  public $value;
56 
66  public function __construct($column, $value = '', $operator = '=', $prefix = '', $function = '')
67  {
68  $this->prefix = $prefix;
69  $this->function = $function;
70  $this->column = $column;
71  $this->value = $value;
72  $this->operator = $operator;
73  }
74 
80  public function render()
81  {
82  $clause = (!empty($this->prefix) ? "{$this->prefix}." : "") . $this->column;
83  if (!empty($this->function)) {
84  $clause = sprintf($this->function, $clause);
85  }
86  if (in_array(strtoupper($this->operator), array('IS NULL', 'IS NOT NULL'))) {
87  $clause .= ' ' . $this->operator;
88  } else {
89  if ('' === ($value = trim($this->value))) {
90  return '';
91  }
92  if (!in_array(strtoupper($this->operator), array('IN', 'NOT IN'))) {
93  if ((substr($value, 0, 1) != '`') && (substr($value, -1) != '`')) {
94  $value = "'{$value}'";
95  } else {
96  if (!preg_match('/^[a-zA-Z0-9_\.\-`]*$/', $value)) {
97  $value = '``';
98  }
99  }
100  }
101  $clause .= " {$this->operator} {$value}";
102  }
103 
104  return $clause;
105  }
106 
113  public function renderLdap()
114  {
115  $clause = '';
116  if ($this->operator == '>') {
117  $this->operator = '>=';
118  }
119  if ($this->operator == '<') {
120  $this->operator = '<=';
121  }
122 
123  if ($this->operator == '!=' || $this->operator == '<>') {
124  $operator = '=';
125  $clause = "(!(" . $this->column . $operator . $this->value . "))";
126  } else {
127  if ($this->operator == 'IN') {
128  $newvalue = str_replace(array('(', ')'), '', $this->value);
129  $tab = explode(',', $newvalue);
130  foreach ($tab as $uid) {
131  $clause .= "({$this->column}={$uid})";
132  }
133  $clause = '(|' . $clause . ')';
134  } else {
135  $clause = "(" . $this->column . ' ' . $this->operator . ' ' . $this->value . ")";
136  }
137  }
138  return $clause;
139  }
140 
146  public function renderWhere()
147  {
148  $cond = $this->render();
149  return empty($cond) ? '' : "WHERE {$cond}";
150  }
151 
161  public function renderQb(QueryBuilder $qb = null, $whereMode = '')
162  {
163  if ($qb==null) { // initialize querybuilder if not passed in
164  $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder();
165  $whereMode = ''; // first entry in new instance must be where
166  }
167  $expr = $this->buildExpressionQb($qb);
168  /* $eb = $qb->expr();
169 
170  $column = (empty($this->prefix) ? "" : $this->prefix.'.') . $this->column;
171 
172  // this should be done using portability functions
173  if (!empty($this->function)) {
174  $column = sprintf($this->function, $column);
175  }
176 
177  $value=trim($this->value);
178 
179  $operator = strtolower($this->operator);
180  $expr = '';
181 
182  // handle special case of value
183  if (in_array($operator, array('is null', 'is not null', 'in', 'not in'))) {
184  switch ($operator) {
185  case 'is null':
186  $expr = $eb->isNull($column);
187  break;
188  case 'is not null':
189  $expr = $eb->isNotNull($column);
190  break;
191  case 'in':
192  $expr = $column . ' IN ' . $value;
193  break;
194  case 'not in':
195  $expr = $column . ' NOT IN '.$value;
196  break;
197  }
198 
199  } elseif (empty($column)) { // no value is a nop (bug: this should be a valid value)
200  $whereMode='none';
201  $expr = '2=2';
202  } else {
203  $colvalue = $qb->createNamedParameter($value);
204  switch ($operator) {
205  case '=':
206  case 'eq':
207  $expr = $eb->eq($column, $colvalue);
208  break;
209  case '!=':
210  case '<>':
211  case 'neq':
212  $expr = $eb->neq($column, $colvalue);
213  break;
214  case '<':
215  case 'lt':
216  $expr = $eb->lt($column, $colvalue);
217  break;
218  case '<=':
219  case 'lte':
220  $expr = $eb->lte($column, $colvalue);
221  break;
222  case '>':
223  case 'gt':
224  $expr = $eb->gt($column, $colvalue);
225  break;
226  case '>=':
227  case 'gte':
228  $expr = $eb->gte($column, $colvalue);
229  break;
230  case 'like':
231  $expr = $eb->like($column, $colvalue);
232  break;
233  case 'not like':
234  $expr = $eb->notLike($column, $colvalue);
235  break;
236  default:
237  $expr = $eb->comparison($column, strtoupper($operator), $colvalue);
238  break;
239  }
240  }
241  */
242 
243  switch (strtolower($whereMode)) {
244  case 'and':
245  $qb->andWhere($expr);
246  break;
247  case 'or':
248  $qb->orWhere($expr);
249  break;
250  case '':
251  $qb->where($expr);
252  break;
253  }
254 
255  if ($this->limit!=0 || $this->start!=0) {
256  $qb->setFirstResult($this->start)
257  ->setMaxResults($this->limit);
258  }
259 
260  if (!empty($this->groupby)) {
261  $qb->groupBy($this->groupby);
262  }
263 
264  if (!empty($this->sort)) {
265  $qb->orderBy($this->sort, $this->order);
266  }
267 
268  return $qb;
269  }
270 
283  public function buildExpressionQb(QueryBuilder $qb)
284  {
285  $eb = $qb->expr();
286 
287  $column = (empty($this->prefix) ? "" : $this->prefix.'.') . $this->column;
288 
289  // this should be done using portability functions
290  if (!empty($this->function)) {
291  $column = sprintf($this->function, $column);
292  }
293 
294  $value=trim($this->value);
295 
296  $operator = strtolower($this->operator);
297  $expr = '';
298 
299  // handle special case of value
300  if (in_array($operator, array('is null', 'is not null', 'in', 'not in'))) {
301  switch ($operator) {
302  case 'is null':
303  $expr = $eb->isNull($column);
304  break;
305  case 'is not null':
306  $expr = $eb->isNotNull($column);
307  break;
308  case 'in':
309  if (!empty($value) && $value!='()') {
310  $expr = $column . ' IN ' . $value;
311  } else {
312  // odd case of a null set - this won't match anything
313  $expr = $eb->neq($column, $column);
314  }
315  break;
316  case 'not in':
317  if (!empty($value) && $value!='()') {
318  $expr = $column . ' NOT IN ' . $value;
319  }
320  break;
321  }
322  } elseif (!empty($column)) { // no value is a nop (bug: this should be a valid value)
323  $colvalue = $qb->createNamedParameter($value);
324  switch ($operator) {
325  case '=':
326  case 'eq':
327  $expr = $eb->eq($column, $colvalue);
328  break;
329  case '!=':
330  case '<>':
331  case 'neq':
332  $expr = $eb->neq($column, $colvalue);
333  break;
334  case '<':
335  case 'lt':
336  $expr = $eb->lt($column, $colvalue);
337  break;
338  case '<=':
339  case 'lte':
340  $expr = $eb->lte($column, $colvalue);
341  break;
342  case '>':
343  case 'gt':
344  $expr = $eb->gt($column, $colvalue);
345  break;
346  case '>=':
347  case 'gte':
348  $expr = $eb->gte($column, $colvalue);
349  break;
350  case 'like':
351  $expr = $eb->like($column, $colvalue);
352  break;
353  case 'not like':
354  $expr = $eb->notLike($column, $colvalue);
355  break;
356  default:
357  $expr = $eb->comparison($column, strtoupper($operator), $colvalue);
358  break;
359  }
360  } else {
361  $expr = '(1)';
362  }
363  return $expr;
364  }
365 }
static getInstance()
Definition: Xoops.php:160
renderQb(QueryBuilder $qb=null, $whereMode= '')
Definition: Criteria.php:161
if(!$xoops->isUser()) $uid
Definition: index.php:31
__construct($column, $value= '', $operator= '=', $prefix= '', $function= '')
Definition: Criteria.php:66
buildExpressionQb(QueryBuilder $qb)
Definition: Criteria.php:283