Advertisement
fernandezekiel

Untitled

Jul 28th, 2013
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.44 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * @author Ezekiel Fernandez <ezekiel_p_fernandez@yahoo.com>
  5.  *
  6.  * allows the user to created nested criterias on the fly
  7.  *
  8.  
  9.  */
  10. class ExtendedDbCriteria extends CDbCriteria {
  11.    
  12.     /**
  13.      * @var ExtendedDbCriteria contains the parent of the subcriteria
  14.      */
  15.     public $parentCriteria;
  16.    
  17.     /**
  18.      * @var string contains the operators used by {@link ExtendedDbCriteria::endSubCriteria}
  19.      */
  20.     public $parentCriteriaOperator;
  21.    
  22.     /**
  23.      * @param string $operator operator
  24.      * @return ExtendedDbCriteria
  25.      */
  26.     public function beginSubCriteria($operator = 'AND'){
  27.         $childCriteria = new self;
  28.         $childCriteria->parentCriteria = $this;
  29.         $childCriteria->parentCriteriaOperator = $operator;
  30.         return $childCriteria;
  31.     }
  32.     /**
  33.      *
  34.      * @return ExtendedDbCriteria
  35.      */
  36.     public function endSubCriteria(){
  37.         $this->parentCriteria->mergeWith($this,$this->parentCriteriaOperator);
  38.         return $this->parentCriteria;
  39.     }
  40.     private $_clauseProperty = array('WHERE' => 'condition', 'HAVING' => 'having');
  41.  
  42.     public function addBetweenCondition($column, $valueStart, $valueEnd, $operator = 'AND', $clause = 'WHERE') {
  43.         if ($valueStart === '' || $valueEnd === '')
  44.             return $this;
  45.  
  46.         $paramStart = self::PARAM_PREFIX . self::$paramCount++;
  47.         $paramEnd = self::PARAM_PREFIX . self::$paramCount++;
  48.         $this->params[$paramStart] = $valueStart;
  49.         $this->params[$paramEnd] = $valueEnd;
  50.         $condition = "$column BETWEEN $paramStart AND $paramEnd";
  51.  
  52.         return $this->addCondition($condition, $operator, $clause);
  53.     }
  54.  
  55.     public function addCondition($condition, $operator = 'AND', $clause = 'WHERE') {
  56.         if (is_array($condition)) {
  57.             if ($condition === array())
  58.                 return $this;
  59.             $condition = '(' . implode(') ' . $operator . ' (', $condition) . ')';
  60.         }
  61.         if ($this->{$this->_clauseProperty[$clause]} === '')
  62.             $this->{$this->_clauseProperty[$clause]} = $condition;
  63.         else
  64.             $this->{$this->_clauseProperty[$clause]} = '(' . $this->{$this->_clauseProperty[$clause]} . ') ' . $operator . ' (' . $condition . ')';
  65.  
  66.         return $this;
  67.     }
  68.  
  69.     public function addInCondition($column, $values, $operator = 'AND', $clause = 'WHERE') {
  70.         if (($n = count($values)) < 1)
  71.             $condition = '0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE
  72.         elseif ($n === 1) {
  73.             $value = reset($values);
  74.             if ($value === null)
  75.                 $condition = $column . ' IS NULL';
  76.             else {
  77.                 $condition = $column . '=' . self::PARAM_PREFIX . self::$paramCount;
  78.                 $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
  79.             }
  80.         } else {
  81.             $params = array();
  82.             foreach ($values as $value) {
  83.                 $params[] = self::PARAM_PREFIX . self::$paramCount;
  84.                 $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
  85.             }
  86.             $condition = $column . ' IN (' . implode(', ', $params) . ')';
  87.         }
  88.         return $this->addCondition($condition, $operator, $clause);
  89.     }
  90.  
  91.     public function addColumnCondition($columns, $columnOperator = 'AND', $operator = 'AND', $clause = 'WHERE') {
  92.         $params = array();
  93.         foreach ($columns as $name => $value) {
  94.             if ($value === null)
  95.                 $params[] = $name . ' IS NULL';
  96.             else {
  97.                 $params[] = $name . '=' . self::PARAM_PREFIX . self::$paramCount;
  98.                 $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
  99.             }
  100.         }
  101.         return $this->addCondition(implode(" $columnOperator ", $params), $operator, $clause);
  102.     }
  103.  
  104.     public function addNotInCondition($column, $values, $operator = 'AND', $clause = 'WHERE') {
  105.         if (($n = count($values)) < 1)
  106.             return $this;
  107.         if ($n === 1) {
  108.             $value = reset($values);
  109.             if ($value === null)
  110.                 $condition = $column . ' IS NOT NULL';
  111.             else {
  112.                 $condition = $column . '!=' . self::PARAM_PREFIX . self::$paramCount;
  113.                 $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
  114.             }
  115.         } else {
  116.             $params = array();
  117.             foreach ($values as $value) {
  118.                 $params[] = self::PARAM_PREFIX . self::$paramCount;
  119.                 $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
  120.             }
  121.             $condition = $column . ' NOT IN (' . implode(', ', $params) . ')';
  122.         }
  123.         return $this->addCondition($condition, $operator, $clause);
  124.     }
  125.  
  126.     public function addSearchCondition($column, $keyword, $escape = true, $operator = 'AND', $like = 'LIKE', $clause = 'WHERE') {
  127.         if ($keyword == '')
  128.             return $this;
  129.         if ($escape)
  130.             $keyword = '%' . strtr($keyword, array('%' => '\%', '_' => '\_', '\\' => '\\\\')) . '%';
  131.         $condition = $column . " $like " . self::PARAM_PREFIX . self::$paramCount;
  132.         $this->params[self::PARAM_PREFIX . self::$paramCount++] = $keyword;
  133.         return $this->addCondition($condition, $operator, $clause);
  134.     }
  135.  
  136.     public function compare($column, $value, $partialMatch = false, $operator = 'AND', $escape = true, $clause = 'WHERE') {
  137.         if (is_array($value)) {
  138.             if ($value === array())
  139.                 return $this;
  140.             return $this->addInCondition($column, $value, $operator, $clause);
  141.         }
  142.         else
  143.             $value = "$value";
  144.  
  145.         if (preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/', $value, $matches)) {
  146.             $value = $matches[2];
  147.             $op = $matches[1];
  148.         }
  149.         else
  150.             $op = '';
  151.  
  152.         if ($value === '')
  153.             return $this;
  154.  
  155.         if ($partialMatch) {
  156.             if ($op === '')
  157.                 return $this->addSearchCondition($column, $value, $escape, $operator, 'LIKE', $clause);
  158.             if ($op === '<>')
  159.                 return $this->addSearchCondition($column, $value, $escape, $operator, 'NOT LIKE', $clause);
  160.         }
  161.         elseif ($op === '')
  162.             $op = '=';
  163.  
  164.         $this->addCondition($column . $op . self::PARAM_PREFIX . self::$paramCount, $operator, $clause);
  165.         $this->params[self::PARAM_PREFIX . self::$paramCount++] = $value;
  166.  
  167.         return $this;
  168.     }
  169.  
  170.     public function compareHaving($column, $value, $partialMatch = false, $operator = 'AND', $escape = true) {
  171.         return $this->compare($column, $value, $partialMatch, $operator, $escape, 'HAVING');
  172.     }
  173.  
  174.     public function addHavingSearchCondition($column, $keyword, $escape = true, $operator = 'AND', $like = 'LIKE') {
  175.         return $this->addSearchCondition($column, $keyword, $escape, $operator, $like, 'HAVING');
  176.     }
  177.  
  178.     public function addHavingNotInCondition($column, $values, $operator = 'AND') {
  179.         return $this->addNotInCondition($column, $values, $operator, 'HAVING');
  180.     }
  181.  
  182.     public function addHavingInCondition($column, $values, $operator = 'AND') {
  183.         return $this->addInCondition($column, $values, 'HAVING');
  184.     }
  185.  
  186.     public function addHavingBetweenCondition($column, $valueStart, $valueEnd, $operator) {
  187.         return $this->addBetweenCondition($column, $valueStart, $valueEnd, $operator, 'HAVING');
  188.     }
  189.  
  190. }
  191.  
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement