1: <?php
2: // $Id: criteria.php,v 1.1 2007/05/15 02:34:21 minahito Exp $
3: // ------------------------------------------------------------------------ //
4: // XOOPS - PHP Content Management System //
5: // Copyright (c) 2000 XOOPS.org //
6: // <http://www.xoops.org/> //
7: // ------------------------------------------------------------------------ //
8: // This program is free software; you can redistribute it and/or modify //
9: // it under the terms of the GNU General Public License as published by //
10: // the Free Software Foundation; either version 2 of the License, or //
11: // (at your option) any later version. //
12: // //
13: // You may not change or alter any portion of this comment or credits //
14: // of supporting developers from this source code or any supporting //
15: // source code which is considered copyrighted (c) material of the //
16: // original comment or credit authors. //
17: // //
18: // This program is distributed in the hope that it will be useful, //
19: // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21: // GNU General Public License for more details. //
22: // //
23: // You should have received a copy of the GNU General Public License //
24: // along with this program; if not, write to the Free Software //
25: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
26: // ------------------------------------------------------------------------ //
27: // Author: Kazumi Ono (AKA onokazu) //
28: // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29: // Project: The XOOPS Project //
30: // ------------------------------------------------------------------------- //
31: // Modified by: Nathan Dial //
32: // Date: 20 March 2003 //
33: // Desc: added experimental LDAP filter generation code //
34: // also refactored to remove about 20 lines of redundant code. //
35: // ------------------------------------------------------------------------- //
36:
37: /**
38: *
39: *
40: * @package kernel
41: * @subpackage database
42: *
43: * @author Kazumi Ono <onokazu@xoops.org>
44: * @copyright copyright (c) 2000-2003 XOOPS.org
45: */
46:
47: define('XOOPS_CRITERIA_ASC', 'ASC');
48: define('XOOPS_CRITERIA_DESC', 'DESC');
49: define('XOOPS_CRITERIA_STARTWITH', 1);
50: define('XOOPS_CRITERIA_ENDWITH', 2);
51: define('XOOPS_CRITERIA_CONTAIN', 3);
52:
53: /**
54: * A criteria (grammar?) for a database query.
55: *
56: * Abstract base class should never be instantiated directly.
57: *
58: * @abstract
59: *
60: * @package kernel
61: * @subpackage database
62: *
63: * @author Kazumi Ono <onokazu@xoops.org>
64: * @copyright copyright (c) 2000-2003 XOOPS.org
65: */
66: class CriteriaElement
67: {
68: /**
69: * Sort order
70: * @var string
71: */
72: var $order = array();
73:
74: /**
75: * @var string
76: */
77: var $sort = array();
78:
79: /**
80: * Number of records to retrieve
81: * @var int
82: */
83: var $limit = 0;
84:
85: /**
86: * Offset of first record
87: * @var int
88: */
89: var $start = 0;
90:
91: /**
92: * @var string
93: */
94: var $groupby = '';
95:
96: /**
97: * Constructor
98: **/
99: function CriteriaElement()
100: {
101:
102: }
103:
104: /**
105: * Render the criteria element
106: * @deprecated
107: */
108: function render()
109: {
110:
111: }
112:
113: /**
114: * Return true if this object has child elements.
115: */
116: function hasChildElements()
117: {
118: return false;
119: }
120:
121: function getCountChildElements()
122: {
123: return 0;
124: }
125:
126: /**
127: * Return child element.
128: */
129: function getChildElement($idx)
130: {
131: return null;
132: }
133:
134: /**
135: * Return condition string.
136: */
137: function getCondition($idx)
138: {
139: return null;
140: }
141:
142: function getName()
143: {
144: return null;
145: }
146:
147: function getValue()
148: {
149: return null;
150: }
151:
152: function getOperator()
153: {
154: return null;
155: }
156:
157: /**#@+
158: * Accessor
159: */
160: /**
161: * @param string $sort
162: * @param string $order
163: */
164: function setSort($sort, $order = null)
165: {
166: $this->sort[0] = $sort;
167:
168: if (!isset($this->order[0])) {
169: $this->order[0] = 'ASC';
170: }
171:
172: if ($order != null) {
173: if (strtoupper($order) == 'ASC') {
174: $this->order[0] = 'ASC';
175: }
176: elseif (strtoupper($order) == 'DESC') {
177: $this->order[0] = 'DESC';
178: }
179: }
180: }
181:
182: /**
183: * Add sort and order condition to this object.
184: */
185: function addSort($sort, $order = 'ASC')
186: {
187: $this->sort[] = $sort;
188: if (strtoupper($order) == 'ASC') {
189: $this->order[] = 'ASC';
190: }
191: elseif (strtoupper($order) == 'DESC') {
192: $this->order[] = 'DESC';
193: }
194: }
195:
196: /**
197: * @return string
198: */
199: function getSort()
200: {
201: if (isset($this->sort[0])) {
202: return $this->sort[0];
203: }
204: else {
205: return '';
206: }
207: }
208:
209: /**
210: * Return sort and order condition as hashmap array.
211: *
212: * @return hashmap 'sort' ... sort string/key'order' order string.
213: */
214: function getSorts()
215: {
216: $ret = array();
217: $max = count($this->sort);
218:
219: for ($i = 0; $i < $max; $i++) {
220: $ret[$i]['sort'] = $this->sort[$i];
221: if (isset($this->order[$i])) {
222: $ret[$i]['order'] = $this->order[$i];
223: }
224: else {
225: $ret[$i]['order'] = 'ASC';
226: }
227: }
228:
229: return $ret;
230: }
231:
232: /**
233: * @param string $order
234: * @deprecated
235: */
236: function setOrder($order)
237: {
238: if (strtoupper($order) == 'ASC') {
239: $this->order[0] = 'ASC';
240: }
241: elseif (strtoupper($order) == 'DESC') {
242: $this->order[0] = 'DESC';
243: }
244: }
245:
246: /**
247: * @return string
248: */
249: function getOrder()
250: {
251: if (isset($this->order[0])) {
252: return $this->order[0];
253: }
254: else {
255: return 'ASC';
256: }
257: }
258:
259: /**
260: * @param int $limit
261: */
262: function setLimit($limit=0)
263: {
264: $this->limit = intval($limit);
265: }
266:
267: /**
268: * @return int
269: */
270: function getLimit()
271: {
272: return $this->limit;
273: }
274:
275: /**
276: * @param int $start
277: */
278: function setStart($start=0)
279: {
280: $this->start = intval($start);
281: }
282:
283: /**
284: * @return int
285: */
286: function getStart()
287: {
288: return $this->start;
289: }
290:
291: /**
292: * @param string $group
293: * @deprecated
294: */
295: function setGroupby($group){
296: $this->groupby = $group;
297: }
298:
299: /**
300: * @return string
301: * @deprecated
302: */
303: function getGroupby(){
304: return ' GROUP BY '.$this->groupby;
305: }
306: /**#@-*/
307: }
308:
309: /**
310: * Collection of multiple {@link CriteriaElement}s
311: *
312: * @package kernel
313: * @subpackage database
314: *
315: * @author Kazumi Ono <onokazu@xoops.org>
316: * @copyright copyright (c) 2000-2003 XOOPS.org
317: */
318: class CriteriaCompo extends CriteriaElement
319: {
320:
321: /**
322: * The elements of the collection
323: * @var array Array of {@link CriteriaElement} objects
324: */
325: var $criteriaElements = array();
326:
327: /**
328: * Conditions
329: * @var array
330: */
331: var $conditions = array();
332:
333: /**
334: * Constructor
335: *
336: * @param object $ele
337: * @param string $condition
338: **/
339: function CriteriaCompo($ele=null, $condition='AND')
340: {
341: if (isset($ele) && is_object($ele)) {
342: $this->add($ele, $condition);
343: }
344: }
345:
346: function hasChildElements()
347: {
348: return count($this->criteriaElements) > 0;
349: }
350:
351: function getCountChildElements()
352: {
353: return count($this->criteriaElements);
354: }
355:
356: function getChildElement($idx)
357: {
358: return $this->criteriaElements[$idx];
359: }
360:
361: function getCondition($idx)
362: {
363: return $this->conditions[$idx];
364: }
365:
366: /**
367: * Add an element
368: *
369: * @param object &$criteriaElement
370: * @param string $condition
371: *
372: * @return object reference to this collection
373: **/
374: function &add(&$criteriaElement, $condition='AND')
375: {
376: $this->criteriaElements[] =& $criteriaElement;
377: $this->conditions[] = $condition;
378: return $this;
379: }
380:
381: /**
382: * Make the criteria into a query string
383: *
384: * @return string
385: * @deprecated XoopsObjectGenericHandler::_makeCriteriaElement4sql()
386: */
387: function render()
388: {
389: $ret = '';
390: $count = count($this->criteriaElements);
391: if ($count > 0) {
392: $elems =& $this->criteriaElements;
393: $conds =& $this->conditions;
394: $ret = '('. $elems[0]->render();
395: for ($i = 1; $i < $count; $i++) {
396: $ret .= ' '.$conds[$i].' '.$elems[$i]->render();
397: }
398: $ret .= ')';
399: }
400: return $ret;
401: }
402:
403: /**
404: * Make the criteria into a SQL "WHERE" clause
405: *
406: * @return string
407: * @deprecated
408: */
409: function renderWhere()
410: {
411: $ret = $this->render();
412: $ret = ($ret != '') ? 'WHERE ' . $ret : $ret;
413: return $ret;
414: }
415:
416: /**
417: * Generate an LDAP filter from criteria
418: *
419: * @return string
420: * @author Nathan Dial ndial@trillion21.com
421: * @deprecated
422: */
423: function renderLdap(){
424: $retval = '';
425: $count = count($this->criteriaElements);
426: if ($count > 0) {
427: $retval = $this->criteriaElements[0]->renderLdap();
428: for ($i = 1; $i < $count; $i++) {
429: $cond = $this->conditions[$i];
430: if(strtoupper($cond) == 'AND'){
431: $op = '&';
432: } elseif (strtoupper($cond)=='OR'){
433: $op = '|';
434: }
435: $retval = "($op$retval" . $this->criteriaElements[$i]->renderLdap().")";
436: }
437: }
438: return $retval;
439: }
440: }
441:
442:
443: /**
444: * A single criteria
445: *
446: * @package kernel
447: * @subpackage database
448: *
449: * @author Kazumi Ono <onokazu@xoops.org>
450: * @copyright copyright (c) 2000-2003 XOOPS.org
451: */
452: class Criteria extends CriteriaElement
453: {
454:
455: /**
456: * @var string
457: */
458: var $prefix;
459: var $function;
460: var $column;
461: var $operator;
462: var $value;
463:
464: var $dtype = 0;
465:
466: /**
467: * Constructor
468: *
469: * @param string $column
470: * @param string $value
471: * @param string $operator
472: **/
473: function Criteria($column, $value='', $operator='=', $prefix = '', $function = '') {
474: $this->prefix = $prefix;
475: $this->function = $function;
476: $this->column = $column;
477: $this->operator = $operator;
478:
479: //
480: // Recive DTYPE. This is a prolongation of criterion life operation.
481: //
482: if (is_array($value) && count($value)==2 && $operator!='IN' && $operator!='NOT IN')
483: {
484: $this->dtype = intval($value[0]);
485: $this->value = $value[1];
486: }
487: else
488: {
489: $this->value = $value;
490: }
491: }
492:
493: function getName()
494: {
495: return $this->column;
496: }
497:
498: function getValue()
499: {
500: return $this->value;
501: }
502:
503: function getOperator()
504: {
505: return $this->operator;
506: }
507:
508: /**
509: * Make a sql condition string
510: *
511: * @return string
512: * @deprecated XoopsObjectGenericHandler::_makeCriteriaElement4sql()
513: **/
514: function render() {
515: $value = $this->value;
516: if (!in_array(strtoupper($this->operator), array('IN', 'NOT IN'))) {
517: $value = "'$value'";
518: }
519: $clause = (!empty($this->prefix) ? $this->prefix.'.' : '') . $this->column;
520: if ( !empty($this->function) ) {
521: $clause = sprintf($this->function, $clause);
522: }
523: $clause .= ' '.$this->operator.' '.$value;
524: return $clause;
525: }
526:
527: /**
528: * Generate an LDAP filter from criteria
529: *
530: * @return string
531: * @author Nathan Dial ndial@trillion21.com
532: * @deprecated
533: */
534: function renderLdap(){
535: $clause = "(" . $this->column . $this->operator . $this->value . ")";
536: return $clause;
537: }
538:
539: /**
540: * Make a SQL "WHERE" clause
541: *
542: * @return string
543: * @deprecated
544: */
545: function renderWhere() {
546: $cond = $this->render();
547: return empty($cond) ? '' : "WHERE $cond";
548: }
549: }
550:
551: ?>
552: