Source for file jDaoConditions.class.php

Documentation is available at jDaoConditions.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage dao
  5. @author     Gérald Croes, Laurent Jouanneau
  6. @contributor Laurent Jouanneau, Julien Issler, Yannick Le Guédart
  7. @copyright  2001-2005 CopixTeam, 2005-2009 Laurent Jouanneau
  8. @copyright  2008 Thomas
  9. @copyright  2008 Julien Issler, 2009 Yannick Le Guédart
  10. *  This classes was get originally from the Copix project (CopixDAOSearchConditions, Copix 2.3dev20050901, http://www.copix.org)
  11. *  Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence).
  12. *  Initial authors of this Copix classes are Gerald Croes and Laurent Jouanneau,
  13. *  and this classes was adapted for Jelix by Laurent Jouanneau
  14. *
  15. @link     http://jelix.org
  16. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  17. */
  18.  
  19. /**
  20.  * content a sub group of conditions
  21.  * @package  jelix
  22.  * @subpackage dao
  23.  */
  24. class jDaoCondition {
  25.  
  26.     /**
  27.     * the parent group if any
  28.     */
  29.     public $parent = null;
  30.  
  31.     /**
  32.     * the conditions in this group
  33.     */
  34.     public $conditions = array ();
  35.  
  36.     /**
  37.     * the sub groups
  38.     */
  39.     public $group = array ();
  40.  
  41.     /**
  42.     * the kind of group (AND/OR)
  43.     */
  44.     public $glueOp;
  45.  
  46.     function __construct ($glueOp='AND'$parent =null ){
  47.         $this->parent = $parent;
  48.         $this->glueOp = $glueOp;
  49.     }
  50.  
  51.     public function isEmpty(){
  52.         return empty($this->conditions&& empty($this->group);
  53.     }
  54. }
  55.  
  56. /**
  57.  * container for all criteria of a query
  58.  * @package  jelix
  59.  * @subpackage dao
  60. */
  61. class jDaoConditions {
  62.     /**
  63.     * @var jDaoCondition 
  64.     */
  65.     public $condition;
  66.  
  67.     /**
  68.     * the orders we wants the list to be
  69.     */
  70.     public $order = array ();
  71.  
  72.     /**
  73.     * the groups we wants the list to be
  74.     */
  75.     public $group = array ();
  76.  
  77.     /**
  78.     * the condition we actually are browsing
  79.     */
  80.     private $_currentCondition;
  81.  
  82.     /**
  83.      * @param string $glueOp the logical operator which links each conditions : AND or OR
  84.      */
  85.     function __construct ($glueOp 'AND'){
  86.         $this->condition = new jDaoCondition ($glueOp);
  87.         $this->_currentCondition $this->condition;
  88.     }
  89.  
  90.     /**
  91.      * add an order clause
  92.      * @param string $field_id   the property name used to order results
  93.      * @param string $way        the order type : asc or desc
  94.      * @param boolean $allowAnyWay true if the value of $way should be checked. Internal use.
  95.      *                               Not recommended because it may cause security issues
  96.      */
  97.     function addItemOrder ($field_id$way='ASC'$allowAnyWay=false{
  98.         if (!$allowAnyWay && strtoupper($way!='DESC' && strtoupper($way!= 'ASC')
  99.             throw new jException('jelix~dao.error.bad.operator'$way);
  100.  
  101.         $this->order[$field_id$way;
  102.     }
  103.  
  104.     /**
  105.      * add a group clause
  106.      *
  107.      * @param string $field_id    the property name used to group results
  108.      */
  109.     function addItemGroup($field_id{
  110.         $this->group[$field_id;
  111.     }
  112.  
  113.     /**
  114.     * says if there are no conditions nor order
  115.     * @return boolean  false if there isn't condition
  116.     */
  117.     function isEmpty (){
  118.         return (count ($this->condition->group== 0&&
  119.         (count ($this->condition->conditions== 0&&
  120.         (count ($this->order== 0;
  121.     }
  122.  
  123.     /**
  124.     * says if there are no conditions
  125.     * @return boolean  false if there isn't condition
  126.     * @since 1.0
  127.     */
  128.     function hasConditions (){
  129.         return (count ($this->condition->group|| count ($this->condition->conditions));
  130.     }
  131.  
  132.     /**
  133.     * starts a new condition group
  134.     * @param string $glueOp the logical operator which links each conditions in the group : AND or OR
  135.     */
  136.     function startGroup ($glueOp 'AND'){
  137.         $glueOp strtoupper($glueOp);
  138.         if ($glueOp !='AND' && $glueOp != 'OR')
  139.             throw new jException('jelix~dao.error.bad.operator'$glueOp);
  140.         $condnew jDaoCondition ($glueOp$this->_currentCondition);
  141.         $this->_currentCondition $cond;
  142.     }
  143.  
  144.     /**
  145.     * ends a condition group
  146.     */
  147.     function endGroup (){
  148.         if ($this->_currentCondition->parent !== null){
  149.             if(!$this->_currentCondition->isEmpty())
  150.                 $this->_currentCondition->parent->group[$this->_currentCondition;
  151.             $this->_currentCondition $this->_currentCondition->parent;
  152.         }
  153.     }
  154.  
  155.     /**
  156.     * adds a condition
  157.     * @param string $field_id  the property name on which the condition applies
  158.     * @param string $operator  the sql operator
  159.     * @param string $value     the value which is compared to the property
  160.     * @param boolean $foo      parameter for internal use : don't use it or set to false
  161.     */
  162.     function addCondition ($field_id$operator$value$foo false){
  163.         $operator trim(strtoupper($operator));
  164.         if(preg_match ('/^[^\w\d\s;\(\)]+$/'$operator||
  165.            in_array($operatorarray('LIKE''NOT LIKE''ILIKE''IN''NOT IN''IS''IS NOT''IS NULL',
  166.                     'IS NOT NULL''MATCH''REGEXP''NOT REGEXP''~''!~''~*''!~*''RLIKE''SOUNDS LIKE'))) {
  167.  
  168.             $this->_currentCondition->conditions[array (
  169.                'field_id'=>$field_id,
  170.                'value'=>$value,
  171.                'operator'=>$operator'isExpr'=>$foo);
  172.         }
  173.         else
  174.             throw new jException('jelix~dao.error.bad.operator'$operator);
  175.     }
  176. }

Documentation generated on Thu, 19 Sep 2013 00:03:08 +0200 by phpDocumentor 1.4.3