developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Ticket #724: 724-jDao-createConditions-emptiness-recursive-check.diff

File 724-jDao-createConditions-emptiness-recursive-check.diff, 3.7 KB (added by Julien, 13 years ago)

patch + unit tests

  • testapp/modules/jelix_tests/tests/jdao.conditions.html_cli.php

     
    33* @package     testapp
    44* @subpackage  jelix_tests module
    55* @author      Jouanneau Laurent
    6 * @contributor
     6* @contributor Julien Issler
    77* @copyright   2006-2007 Jouanneau laurent
     8* @copyright   2008 Julien Issler
    89* @link        http://www.jelix.org
    910* @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
    1011*/
     
    106107    }
    107108
    108109
     110    function testEmptyRecursive(){
     111        $cond = new jDaoConditions();
     112        $cond->startGroup();
     113        $cond->startGroup('OR');
     114        $cond->endGroup();
     115        $cond->endGroup();
     116        $this->assertFalse($cond->hasConditions());
     117        $this->assertTrue($cond->isEmpty());
     118    }
    109119
     120    function testNonEmptyRecursive(){
     121        $cond = new jDaoConditions();
     122        $cond->startGroup();
     123        $cond->startGroup('OR');
     124        $cond->addCondition('test','=',1);
     125        $cond->endGroup();
     126        $cond->endGroup();
     127        $this->assertTrue($cond->hasConditions());
     128        $this->assertFalse($cond->isEmpty());
     129    }
     130
    110131}
    111132
    112133
  • lib/jelix/dao/jDaoConditions.class.php

     
    33* @package    jelix
    44* @subpackage dao
    55* @author     Croes Gérald, Laurent Jouanneau
    6 * @contributor Laurent Jouanneau
     6* @contributor Laurent Jouanneau, Julien Issler
    77* @copyright  2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau
    88* @copyright  2008 Thomas
     9* @copyright  2008 Julien Issler
    910* This classes was get originally from the Copix project (CopixDAOSearchConditions, Copix 2.3dev20050901, http://www.copix.org)
    1011* Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence).
    1112* Initial authors of this Copix classes are Gerald Croes and Laurent Jouanneau,
     
    4647        $this->parent = $parent;
    4748        $this->glueOp = $glueOp;
    4849    }
     50
     51    function isEmpty(){
     52        if(count($this->conditions))
     53            return false;
     54        if(!count($this->group))
     55            return true;
     56        foreach($this->group as $group){
     57            if(!$group->isEmpty())
     58                return false;
     59        }
     60        return true;
     61    }
    4962}
    5063
    5164/**
     
    6881    * the groups we wants the list to be
    6982    */
    7083    public $group = array ();
    71    
     84
    7285    /**
    7386    * the condition we actually are browsing
    7487    */
     
    99112    function addItemGroup($field_id) {
    100113        $this->group[] = $field_id;
    101114    }
    102    
     115
    103116    /**
    104117    * says if there are no conditions nor order
    105118    * @return boolean  false if there isn't condition
    106119    */
    107120    function isEmpty (){
    108         return (count ($this->condition->group) == 0) &&
    109         (count ($this->condition->conditions) == 0) &&
    110         (count ($this->order) == 0) ;
     121        if(!empty($this->order))
     122            return false;
     123        return !$this->hasConditions();
    111124    }
    112125
    113126    /**
     
    116129    * @since 1.0
    117130    */
    118131    function hasConditions (){
    119         return (count ($this->condition->group) || count ($this->condition->conditions));
     132        if(!empty($this->condition->conditions))
     133            return true;
     134        if(empty($this->condition->group))
     135            return false;
     136        foreach($this->condition->group as $group){
     137            if(!$group->isEmpty())
     138                return true;
     139        }
     140        return false;
    120141    }
    121142
    122143    /**