[ Index ] |
|
Code source de XOOPS 2.0.17.1 |
1 <?php 2 // $Id: criteria.php 785 2006-11-06 06:11:17Z skalpa $ 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 /** 48 * A criteria (grammar?) for a database query. 49 * 50 * Abstract base class should never be instantiated directly. 51 * 52 * @abstract 53 * 54 * @package kernel 55 * @subpackage database 56 * 57 * @author Kazumi Ono <onokazu@xoops.org> 58 * @copyright copyright (c) 2000-2003 XOOPS.org 59 */ 60 class CriteriaElement 61 { 62 /** 63 * Sort order 64 * @var string 65 */ 66 var $order = 'ASC'; 67 68 /** 69 * @var string 70 */ 71 var $sort = ''; 72 73 /** 74 * Number of records to retrieve 75 * @var int 76 */ 77 var $limit = 0; 78 79 /** 80 * Offset of first record 81 * @var int 82 */ 83 var $start = 0; 84 85 /** 86 * @var string 87 */ 88 var $groupby = ''; 89 90 /** 91 * Constructor 92 **/ 93 function CriteriaElement() 94 { 95 96 } 97 98 /** 99 * Render the criteria element 100 */ 101 function render() 102 { 103 104 } 105 106 /**#@+ 107 * Accessor 108 */ 109 /** 110 * @param string $sort 111 */ 112 function setSort($sort) 113 { 114 $this->sort = $sort; 115 } 116 117 /** 118 * @return string 119 */ 120 function getSort() 121 { 122 return $this->sort; 123 } 124 125 /** 126 * @param string $order 127 */ 128 function setOrder($order) 129 { 130 if ('DESC' == strtoupper($order)) { 131 $this->order = 'DESC'; 132 } 133 } 134 135 /** 136 * @return string 137 */ 138 function getOrder() 139 { 140 return $this->order; 141 } 142 143 /** 144 * @param int $limit 145 */ 146 function setLimit($limit=0) 147 { 148 $this->limit = intval($limit); 149 } 150 151 /** 152 * @return int 153 */ 154 function getLimit() 155 { 156 return $this->limit; 157 } 158 159 /** 160 * @param int $start 161 */ 162 function setStart($start=0) 163 { 164 $this->start = intval($start); 165 } 166 167 /** 168 * @return int 169 */ 170 function getStart() 171 { 172 return $this->start; 173 } 174 175 /** 176 * @param string $group 177 */ 178 function setGroupby($group){ 179 $this->groupby = $group; 180 } 181 182 /** 183 * @return string 184 */ 185 function getGroupby(){ 186 return ' GROUP BY '.$this->groupby; 187 } 188 /**#@-*/ 189 } 190 191 /** 192 * Collection of multiple {@link CriteriaElement}s 193 * 194 * @package kernel 195 * @subpackage database 196 * 197 * @author Kazumi Ono <onokazu@xoops.org> 198 * @copyright copyright (c) 2000-2003 XOOPS.org 199 */ 200 class CriteriaCompo extends CriteriaElement 201 { 202 203 /** 204 * The elements of the collection 205 * @var array Array of {@link CriteriaElement} objects 206 */ 207 var $criteriaElements = array(); 208 209 /** 210 * Conditions 211 * @var array 212 */ 213 var $conditions = array(); 214 215 /** 216 * Constructor 217 * 218 * @param object $ele 219 * @param string $condition 220 **/ 221 function CriteriaCompo($ele=null, $condition='AND') 222 { 223 if (isset($ele) && is_object($ele)) { 224 $this->add($ele, $condition); 225 } 226 } 227 228 /** 229 * Add an element 230 * 231 * @param object &$criteriaElement 232 * @param string $condition 233 * 234 * @return object reference to this collection 235 **/ 236 function &add(&$criteriaElement, $condition='AND') 237 { 238 $this->criteriaElements[] =& $criteriaElement; 239 $this->conditions[] = $condition; 240 return $this; 241 } 242 243 /** 244 * Make the criteria into a query string 245 * 246 * @return string 247 */ 248 function render() 249 { 250 $ret = ''; 251 $count = count($this->criteriaElements); 252 if ($count > 0) { 253 $ret = '('. $this->criteriaElements[0]->render(); 254 for ($i = 1; $i < $count; $i++) { 255 $ret .= ' '.$this->conditions[$i].' '.$this->criteriaElements[$i]->render(); 256 } 257 $ret .= ')'; 258 } 259 return $ret; 260 } 261 262 /** 263 * Make the criteria into a SQL "WHERE" clause 264 * 265 * @return string 266 */ 267 function renderWhere() 268 { 269 $ret = $this->render(); 270 $ret = ($ret != '') ? 'WHERE ' . $ret : $ret; 271 return $ret; 272 } 273 274 /** 275 * Generate an LDAP filter from criteria 276 * 277 * @return string 278 * @author Nathan Dial ndial@trillion21.com 279 */ 280 function renderLdap(){ 281 $retval = ''; 282 $count = count($this->criteriaElements); 283 if ($count > 0) { 284 $retval = $this->criteriaElements[0]->renderLdap(); 285 for ($i = 1; $i < $count; $i++) { 286 $cond = $this->conditions[$i]; 287 if(strtoupper($cond) == 'AND'){ 288 $op = '&'; 289 } elseif (strtoupper($cond)=='OR'){ 290 $op = '|'; 291 } 292 $retval = "($op$retval" . $this->criteriaElements[$i]->renderLdap().")"; 293 } 294 } 295 return $retval; 296 } 297 } 298 299 300 /** 301 * A single criteria 302 * 303 * @package kernel 304 * @subpackage database 305 * 306 * @author Kazumi Ono <onokazu@xoops.org> 307 * @copyright copyright (c) 2000-2003 XOOPS.org 308 */ 309 class Criteria extends CriteriaElement 310 { 311 312 /** 313 * @var string 314 */ 315 var $prefix; 316 var $function; 317 var $column; 318 var $operator; 319 var $value; 320 321 /** 322 * Constructor 323 * 324 * @param string $column 325 * @param string $value 326 * @param string $operator 327 **/ 328 function Criteria($column, $value='', $operator='=', $prefix = '', $function = '') { 329 $this->prefix = $prefix; 330 $this->function = $function; 331 $this->column = $column; 332 $this->value = $value; 333 $this->operator = $operator; 334 } 335 336 /** 337 * Make a sql condition string 338 * 339 * @return string 340 **/ 341 function render() { 342 $clause = (!empty($this->prefix) ? "{$this->prefix}." : "") . $this->column; 343 if ( !empty($this->function) ) { 344 $clause = sprintf($this->function, $clause); 345 } 346 if ( in_array( strtoupper( $this->operator ), array( 'IS NULL', 'IS NOT NULL' ) ) ) { 347 $clause .= ' ' . $this->operator; 348 } else { 349 if ( '' === ($value = trim($this->value) ) ) { 350 return ''; 351 } 352 if ( !in_array( strtoupper($this->operator), array('IN', 'NOT IN') ) ) { 353 if ( ( substr( $value, 0, 1 ) != '`' ) && ( substr( $value, -1 ) != '`' ) ) { 354 $value = "'$value'"; 355 } elseif ( !preg_match( '/^[a-zA-Z0-9_\.\-`]*$/', $value ) ) { 356 $value = '``'; 357 } 358 } 359 $clause .= " {$this->operator} $value"; 360 } 361 return $clause; 362 } 363 364 /** 365 * Generate an LDAP filter from criteria 366 * 367 * @return string 368 * @author Nathan Dial ndial@trillion21.com, improved by Pierre-Eric MENUET pemen@sourceforge.net 369 */ 370 function renderLdap(){ 371 if ($this->operator == '>') { 372 $this->operator = '>='; 373 } 374 if ($this->operator == '<') { 375 $this->operator = '<='; 376 } 377 378 if ($this->operator == '!=' || $this->operator == '<>') { 379 $operator = '='; 380 $clause = "(!(" . $this->column . $operator . $this->value . "))"; 381 } 382 else { 383 if ($this->operator == 'IN') { 384 $newvalue = str_replace(array('(',')'),'', 385 $this->value); 386 $tab = explode(',',$newvalue); 387 foreach ($tab as $uid) 388 { 389 $clause .= '(' . $this->column . '=' . $uid 390 .')'; 391 } 392 $clause = '(|' . $clause . ')'; 393 } 394 else { 395 $clause = "(" . $this->column . $this->operator . $this->value . ")"; 396 } 397 } 398 return $clause; 399 } 400 401 /** 402 * Make a SQL "WHERE" clause 403 * 404 * @return string 405 */ 406 function renderWhere() { 407 $cond = $this->render(); 408 return empty($cond) ? '' : "WHERE $cond"; 409 } 410 } 411 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Nov 25 11:44:32 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |