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:
14: use Doctrine\DBAL\Query\QueryBuilder;
15:
16: /**
17: * A criteria (grammar?) for a database query.
18: *
19: * This abstract base class should never be instantiated directly.
20: *
21: * @category Xoops\Core\Kernel\CriteriaElement
22: * @package Xoops\Core\Kernel
23: * @author Kazumi Ono <onokazu@xoops.org>
24: * @author Nathan Dial <ndial@trillion21.com>
25: * @author Taiwen Jiang <phppp@users.sourceforge.net>
26: * @copyright 2000-2013 XOOPS Project (http://xoops.org)
27: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28: * @link http://xoops.org
29: * @since 2.0.0
30: */
31: abstract class CriteriaElement
32: {
33: /**
34: * Sort order
35: *
36: * @var string
37: */
38: protected $order = 'ASC';
39:
40: /**
41: * @var string
42: */
43: protected $sort = '';
44:
45: /**
46: * Number of records to retrieve
47: *
48: * @var int
49: */
50: protected $limit = 0;
51:
52: /**
53: * Offset of first record
54: *
55: * @var int
56: */
57: protected $start = 0;
58:
59: /**
60: * @var string
61: */
62: protected $groupBy = '';
63:
64:
65: /**
66: * Render the criteria element
67: *
68: * @return string
69: */
70: abstract public function render();
71:
72: /**
73: * Make the criteria into a SQL "WHERE" clause
74: *
75: * @return string
76: */
77: abstract public function renderWhere();
78:
79: /**
80: * Generate an LDAP filter from criteria
81: *
82: * @return string
83: */
84: abstract public function renderLdap();
85:
86: /**
87: * Render as Doctrine QueryBuilder instructions
88: *
89: * @param QueryBuilder $qb query builder instance
90: * @param string $whereMode how does this fit in the passed in QueryBuilder?
91: * '' = as where,'and'= as andWhere, 'or' = as orWhere
92: *
93: * @return QueryBuilder query builder instance
94: */
95: abstract public function renderQb(QueryBuilder $qb = null, $whereMode = '');
96:
97: /**
98: * Build an expression to be included in a Doctrine QueryBuilder instance.
99: *
100: * This method will build an expression, adding any parameters to the query,
101: * but the caller is responsible for adding the expression to the query, for
102: * example as where() parameter. This allows the caller to handle all context,
103: * such as parenthetical groupings.
104: *
105: * @param QueryBuilder $qb query builder instance
106: *
107: * @return string expression
108: */
109: abstract public function buildExpressionQb(QueryBuilder $qb);
110:
111: /**
112: * set sort column
113: *
114: * @param string $sort sort column
115: *
116: * @return void
117: */
118: public function setSort($sort)
119: {
120: $this->sort = $sort;
121: }
122:
123: /**
124: * get sort column
125: *
126: * @return string sort column
127: */
128: public function getSort()
129: {
130: return $this->sort;
131: }
132:
133: /**
134: * set sort order
135: *
136: * @param string $order sort order ASC or DESC
137: *
138: * @return void
139: */
140: public function setOrder($order)
141: {
142: if (is_string($order)) {
143: $order = strtoupper($order);
144: if (in_array($order, array('ASC', 'DESC'))) {
145: $this->order = $order;
146: }
147: }
148: }
149:
150: /**
151: * get sort order
152: *
153: * @return string sort order
154: */
155: public function getOrder()
156: {
157: return $this->order;
158: }
159:
160: /**
161: * set row limit
162: *
163: * @param int $limit row limit
164: *
165: * @return void
166: */
167: public function setLimit($limit = 0)
168: {
169: $this->limit = (int)($limit);
170: }
171:
172: /**
173: * get row limit
174: *
175: * @return int row limit
176: */
177: public function getLimit()
178: {
179: return $this->limit;
180: }
181:
182: /**
183: * set first row offset
184: *
185: * @param int $start offset of first row
186: *
187: * @return void
188: */
189: public function setStart($start = 0)
190: {
191: $this->start = (int)($start);
192: }
193:
194: /**
195: * get first row offset
196: *
197: * @return int start row offset
198: */
199: public function getStart()
200: {
201: return $this->start;
202: }
203:
204: /**
205: * set group by
206: *
207: * @param string $group group by
208: *
209: * @return void
210: */
211: public function setGroupBy($group)
212: {
213: $this->groupBy = $group;
214: }
215:
216: /**
217: * get group by
218: *
219: * @return string group by
220: */
221: public function getGroupBy()
222: {
223: return isset($this->groupBy) ? $this->groupBy : "";
224: }
225: }
226: