XOOPS Docs - kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file criteria.php

Documentation is available at criteria.php

  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. {
  61.     /**
  62.      * Sort order
  63.      * @var    string 
  64.      */
  65.     var $order = 'ASC';
  66.  
  67.     /**
  68.      * @var    string 
  69.      */
  70.     var $sort = '';
  71.  
  72.     /**
  73.      * Number of records to retrieve
  74.      * @var    int 
  75.      */
  76.     var $limit = 0;
  77.  
  78.     /**
  79.      * Offset of first record
  80.      * @var    int 
  81.      */
  82.     var $start = 0;
  83.  
  84.     /**
  85.      * @var    string 
  86.      */
  87.     var $groupby = '';
  88.  
  89.     /**
  90.      * Constructor
  91.      **/
  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. {
  201.  
  202.     /**
  203.      * The elements of the collection
  204.      * @var    array   Array of {@link CriteriaElement} objects
  205.      */
  206.     var $criteriaElements = array();
  207.  
  208.     /**
  209.      * Conditions
  210.      * @var    array 
  211.      */
  212.     var $conditions = array();
  213.  
  214.     /**
  215.      * Constructor
  216.      * 
  217.      * @param   object  $ele 
  218.      * @param   string  $condition 
  219.      ***/
  220.     function CriteriaCompo($ele=null$condition='AND')
  221.     {
  222.         if (isset($ele&& is_object($ele)) {
  223.             $this->add($ele$condition);
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Add an element
  229.      * 
  230.      * @param   object  &$criteriaElement 
  231.      * @param   string  $condition 
  232.      * 
  233.      * @return  object  reference to this collection
  234.      ***/
  235.     function &add(&$criteriaElement$condition='AND')
  236.     {
  237.         $this->criteriaElements[=$criteriaElement;
  238.         $this->conditions[$condition;
  239.         return $this;
  240.     }
  241.  
  242.     /**
  243.      * Make the criteria into a query string
  244.      * 
  245.      * @return    string 
  246.      */
  247.     function render()
  248.     {
  249.         $ret '';
  250.         $count count($this->criteriaElements);
  251.         if ($count 0{
  252.             $ret '('$this->criteriaElements[0]->render();
  253.             for ($i 1$i $count$i++{
  254.                 $ret .= ' '.$this->conditions[$i].' '.$this->criteriaElements[$i]->render();
  255.             }
  256.             $ret .= ')';
  257.         }
  258.         return $ret;
  259.     }
  260.  
  261.     /**
  262.      * Make the criteria into a SQL "WHERE" clause
  263.      * 
  264.      * @return    string 
  265.      */
  266.     function renderWhere()
  267.     {
  268.         $ret $this->render();
  269.         $ret ($ret != '''WHERE ' $ret $ret;
  270.         return $ret;
  271.     }
  272.  
  273.     /**
  274.      * Generate an LDAP filter from criteria
  275.      *
  276.      * @return string 
  277.      * @author Nathan Dial ndial@trillion21.com
  278.      */
  279.     function renderLdap(){
  280.         $retval '';
  281.         $count count($this->criteriaElements);
  282.         if ($count 0{
  283.             $retval $this->criteriaElements[0]->renderLdap();
  284.             for ($i 1$i $count$i++{
  285.                 $cond $this->conditions[$i];
  286.                 if(strtoupper($cond== 'AND'){
  287.                     $op '&';
  288.                 elseif (strtoupper($cond)=='OR'){
  289.                     $op '|';
  290.                 }
  291.                 $retval "($op$retval$this->criteriaElements[$i]->renderLdap().")";
  292.             }
  293.         }
  294.         return $retval;
  295.     }
  296. }
  297.  
  298.  
  299. /**
  300.  * A single criteria
  301.  * 
  302.  * @package     kernel
  303.  * @subpackage  database
  304.  * 
  305.  * @author        Kazumi Ono    <onokazu@xoops.org>
  306.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  307.  */
  308. class Criteria extends CriteriaElement
  309. {
  310.  
  311.     /**
  312.      * @var    string 
  313.      */
  314.     var $prefix;
  315.     var $function;
  316.     var $column;
  317.     var $operator;
  318.     var $value;
  319.  
  320.     /**
  321.      * Constructor
  322.      * 
  323.      * @param   string  $column 
  324.      * @param   string  $value 
  325.      * @param   string  $operator 
  326.      ***/
  327.     function Criteria($column$value=''$operator='='$prefix ''$function ''{
  328.         $this->prefix = $prefix;
  329.         $this->function = $function;
  330.         $this->column = $column;
  331.         $this->value = $value;
  332.         $this->operator = $operator;
  333.     }
  334.  
  335.     /**
  336.      * Make a sql condition string
  337.      * 
  338.      * @return  string 
  339.      ***/
  340.     function render({
  341.         $clause (!empty($this->prefix"{$this->prefix}." : "") . $this->column;
  342.         if !empty($this->function) ) {
  343.             $clause = sprintf($this->function$clause);
  344.         }
  345.         if ( in_array( strtoupper( $this->operator )array'IS NULL''IS NOT NULL' ) ) ) {
  346.             $clause .= ' ' . $this->operator;
  347.         } else {
  348.             if ( '' === ($value = trim($this->value) ) ) {
  349.                 return '';
  350.             }
  351.             if ( !in_array( strtoupper($this->operator)array('IN''NOT IN') ) ) {
  352.                 if ( ( substr( $value, 0, 1 ) != '`' ) && ( substr( $value, -1 ) != '`' ) ) {
  353.                     $value = "'$value'";
  354.                 } elseif ( !preg_match( '/^[a-zA-Z0-9_\.\-`]*$/', $value ) ) {
  355.                     $value = '``';
  356.                 }
  357.             }
  358.             $clause .= " {$this->operator$value";
  359.         }
  360.         return $clause;
  361.     }
  362.  
  363.     /**
  364.      * Generate an LDAP filter from criteria
  365.      *
  366.      * @return string
  367.      * @author Nathan Dial ndial@trillion21.com, improved by Pierre-Eric MENUET pemen@sourceforge.net
  368.      */
  369.     function renderLdap(){
  370.         if ($this->operator == '>'{
  371.             $this->operator = '>=';
  372.         }
  373.         if ($this->operator == '<'{
  374.             $this->operator = '<=';
  375.         }
  376.  
  377.         if ($this->operator == '!=' || $this->operator == '<>'{
  378.             $operator = '=';
  379.             $clause = "(!(" . $this->column . $operator $this->value . "))";
  380.         }
  381.         else {
  382.             if ($this->operator == 'IN'{
  383.                 $newvalue = str_replace(array('(',')'),'',
  384.                 $this->value);
  385.                 $tab explode(',',$newvalue);
  386.                 foreach ($tab as $uid)
  387.                 {
  388.                     $clause .= '(' . $this->column . '=' $uid
  389.                     .')';
  390.                 }
  391.                 $clause = '(|' . $clause . ')';
  392.             }
  393.             else {
  394.                 $clause = "(" . $this->column . $this->operator . $this->value . ")";
  395.             }
  396.         }
  397.         return $clause;
  398.     }
  399.  
  400.     /**
  401.      * Make a SQL "WHERE" clause
  402.      * 
  403.      * @return    string
  404.      */
  405.     function renderWhere() {
  406.         $cond = $this->render();
  407.         return empty($cond'' "WHERE $cond";
  408.     }
  409. }

XOOPS Docs generated by phpDocumentor