XOOPS  2.6.0
CriteriaCompo.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 
12 namespace Xoops\Core\Kernel;
13 
16 
31 {
37  protected $criteriaElements = array();
38 
44  protected $conditions = array();
45 
52  public function __construct(CriteriaElement $ele = null, $condition = 'AND')
53  {
54  if (isset($ele)) {
55  $this->add($ele, $condition);
56  }
57  }
58 
67  public function add(CriteriaElement $criteriaElement, $condition = 'AND')
68  {
69  $this->criteriaElements[] = $criteriaElement;
70  $this->conditions[] = $condition;
71  return $this;
72  }
73 
79  public function render()
80  {
81  $ret = '';
82  foreach ($this->criteriaElements as $i => $element) {
83  if (!is_object($element)) {
84  continue;
85  }
86  /* @var $element CriteriaElement */
87  if ($i == 0) {
88  $ret = $element->render();
89  } else {
90  if (!$render = $element->render()) {
91  continue;
92  }
93  $ret .= ' ' . $this->conditions[$i] . ' (' . $render . ')';
94  }
95  $ret = "({$ret})";
96  }
97  $ret = ($ret=='()') ? '(1)' : $ret;
98  return $ret;
99  }
100 
106  public function renderWhere()
107  {
108  $ret = $this->render();
109  $ret = ($ret != '') ? 'WHERE ' . $ret : $ret;
110  return $ret;
111  }
112 
119  public function renderLdap()
120  {
121  $ret = '';
122  foreach ($this->criteriaElements as $i => $element) {
123  /* @var $element CriteriaElement */
124  if ($i == 0) {
125  $ret = $element->renderLdap();
126  } else {
127  $cond = strtoupper($this->conditions[$i]);
128  $op = ($cond == "OR") ? "|" : "&";
129  $ret = "({$op}{$ret}" . $element->renderLdap() . ")";
130  }
131  }
132  return $ret;
133  }
134 
144  public function renderQb(QueryBuilder $qb = null, $whereMode = '')
145  {
146  if ($qb==null) {
147  $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder();
148  $whereMode = ''; // first entry in new instance must be where
149  }
150 
151  $expr = '';
152  foreach ($this->criteriaElements as $i => $element) {
153  $expr_part = $element->buildExpressionQb($qb);
154  if ($expr_part !== false) {
155  if ($i == 0) {
156  $expr = $expr_part;
157  } else {
158  $expr .= ' ' . strtoupper($this->conditions[$i]) . ' ' . $expr_part;
159  }
160  }
161  }
162 
163  if (!empty($expr)) {
164  $expr = '(' . $expr . ')'; // group all condtions in this compo
165 
166  switch (strtolower($whereMode)) {
167  case 'and':
168  $qb->andWhere($expr);
169  break;
170  case 'or':
171  $qb->orWhere($expr);
172  break;
173  case '':
174  $qb->where($expr);
175  break;
176  }
177  }
178 
179  if ($this->limit!=0 || $this->start!=0) {
180  $qb->setFirstResult($this->start)
181  ->setMaxResults($this->limit);
182  }
183 
184  if (!empty($this->groupby)) {
185  $qb->groupBy($this->groupby);
186  }
187 
188  if (!empty($this->sort)) {
189  $qb->orderBy($this->sort, $this->order);
190  }
191  return $qb;
192  }
193 
206  public function buildExpressionQb(QueryBuilder $qb)
207  {
208  $expr = false;
209  foreach ($this->criteriaElements as $i => $element) {
210  $expr_part = $element->buildExpressionQb($qb);
211  if ($expr_part !== false) {
212  if ($i == 0) {
213  $expr = $expr_part;
214  } else {
215  $expr .= ' ' . strtoupper($this->conditions[$i]) . ' ' . $expr_part;
216  }
217  }
218  }
219 
220  if (!empty($expr)) {
221  $expr = '(' . $expr . ')'; // group all condtions in this compo
222  }
223  return $expr;
224  }
225 }
$i
Definition: dialog.php:68
__construct(CriteriaElement $ele=null, $condition= 'AND')
static getInstance()
Definition: Xoops.php:160
renderQb(QueryBuilder $qb=null, $whereMode= '')
add(CriteriaElement $criteriaElement, $condition= 'AND')
buildExpressionQb(QueryBuilder $qb)
$op