Ejemplo n.º 1
0
 /**
  * Does the value get filtered?
  *
  * @param  array      $row
  * @throws \Exception
  * @return boolean
  */
 public function applyFilter(array $row)
 {
     $wasTrueOneTime = false;
     $isApply = false;
     foreach ($this->getFilter()->getValues() as $filterValue) {
         $filter = $this->getFilter();
         $col = $filter->getColumn();
         $value = $row[$col->getUniqueId()];
         $value = $col->getType()->getFilterValue($value);
         if ($filter->getOperator() == DatagridFilter::BETWEEN) {
             //BETWEEN have to be tested in one call
             $isApply = DatagridFilter::isApply($value, $this->getFilter()->getValues(), $filter->getOperator());
             return $isApply;
         } else {
             $isApply = DatagridFilter::isApply($value, $filterValue, $filter->getOperator());
         }
         if ($isApply === true) {
             $wasTrueOneTime = true;
         }
         switch ($filter->getOperator()) {
             case DatagridFilter::NOT_LIKE:
             case DatagridFilter::NOT_LIKE_LEFT:
             case DatagridFilter::NOT_LIKE_RIGHT:
             case DatagridFilter::NOT_EQUAL:
             case DatagridFilter::NOT_IN:
                 if ($isApply === false) {
                     // normally one "match" is okay -> so it's applied
                     // but e.g. NOT_LIKE is not allowed to match so even if the othere rules are true
                     // it has to fail!
                     return false;
                 }
                 break;
         }
     }
     if ($isApply === false && $wasTrueOneTime === true) {
         return true;
     }
     return $isApply;
 }
 /**
  * Display this action on this row?
  *
  * @param  array   $row
  * @return boolean
  */
 public function isDisplayed(array $row)
 {
     if ($this->hasShowOnValues() === false) {
         return true;
     }
     $isDisplayed = false;
     foreach ($this->getShowOnValues() as $rule) {
         $value = '';
         if (isset($row[$rule['column']->getUniqueId()])) {
             $value = $row[$rule['column']->getUniqueId()];
         }
         $isDisplayedMatch = Filter::isApply($value, $rule['value'], $rule['comparison']);
         if ($this->getShowOnValueOperator() == 'OR' && $isDisplayedMatch === true) {
             // For OR one match is enough
             return true;
         } elseif ($this->getShowOnValueOperator() == 'AND' && $isDisplayedMatch === false) {
             return false;
         } else {
             $isDisplayed = $isDisplayedMatch;
         }
     }
     return $isDisplayed;
 }
Ejemplo n.º 3
0
 /**
  * @param  DatagridFilter            $filter
  * @throws \InvalidArgumentException
  */
 public function applyFilter(DatagridFilter $filter)
 {
     $qb = $this->getQueryBuilder();
     $expr = new Expr();
     $column = $filter->getColumn();
     $colString = $column->getSelectPart1();
     if ($column->getSelectPart2() != '') {
         $colString .= '.' . $column->getSelectPart2();
     }
     if ($column instanceof Column\Select && $column->hasFilterSelectExpression()) {
         $colString = sprintf($column->getFilterSelectExpression(), $colString);
     }
     $values = $filter->getValues();
     $wheres = array();
     foreach ($values as $key => $value) {
         $valueParameterName = ':' . str_replace('.', '', $column->getUniqueId() . $key);
         switch ($filter->getOperator()) {
             case DatagridFilter::LIKE:
                 $wheres[] = $expr->like($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, '%' . $value . '%');
                 break;
             case DatagridFilter::LIKE_LEFT:
                 $wheres[] = $expr->like($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, '%' . $value);
                 break;
             case DatagridFilter::LIKE_RIGHT:
                 $wheres[] = $expr->like($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value . '%');
                 break;
             case DatagridFilter::NOT_LIKE:
                 $wheres[] = $expr->notLike($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, '%' . $value . '%');
                 break;
             case DatagridFilter::NOT_LIKE_LEFT:
                 $wheres[] = $expr->notLike($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, '%' . $value);
                 break;
             case DatagridFilter::NOT_LIKE_RIGHT:
                 $wheres[] = $expr->notLike($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value . '%');
                 break;
             case DatagridFilter::EQUAL:
                 $wheres[] = $expr->eq($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value);
                 break;
             case DatagridFilter::NOT_EQUAL:
                 $wheres[] = $expr->neq($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value);
                 break;
             case DatagridFilter::GREATER_EQUAL:
                 $wheres[] = $expr->gte($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value);
                 break;
             case DatagridFilter::GREATER:
                 $wheres[] = $expr->gt($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value);
                 break;
             case DatagridFilter::LESS_EQUAL:
                 $wheres[] = $expr->lte($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value);
                 break;
             case DatagridFilter::LESS:
                 $wheres[] = $expr->lt($colString, $valueParameterName);
                 $qb->setParameter($valueParameterName, $value);
                 break;
             case DatagridFilter::BETWEEN:
                 $minParameterName = ':' . str_replace('.', '', $colString . '0');
                 $maxParameterName = ':' . str_replace('.', '', $colString . '1');
                 $wheres[] = $expr->between($colString, $minParameterName, $maxParameterName);
                 $qb->setParameter($minParameterName, $values[0]);
                 $qb->setParameter($maxParameterName, $values[1]);
                 break 2;
             default:
                 throw new \InvalidArgumentException('This operator is currently not supported: ' . $filter->getOperator());
                 break;
         }
     }
     if (count($wheres) > 0) {
         $orWhere = $qb->expr()->orX();
         $orWhere->addMultiple($wheres);
         $qb->andWhere($orWhere);
     }
 }
 /**
  * Get the default filter conditions defined for the columns
  *
  * @return array
  */
 public function getFiltersDefault()
 {
     $filters = array();
     // @todo skip this, if $grid->isUserFilterEnabled() ?
     if ($this->getRequest() instanceof ConsoleRequest || $this->getRequest() instanceof HttpRequest && !$this->getRequest()->isPost()) {
         foreach ($this->getColumns() as $column) {
             /* @var $column \ZfcDatagrid\Column\AbstractColumn */
             if ($column->hasFilterDefaultValue() === true) {
                 $filter = new Filter();
                 $filter->setFromColumn($column, $column->getFilterDefaultValue());
                 $filters[] = $filter;
                 $column->setFilterActive($filter->getDisplayColumnValue());
             }
         }
     }
     return $filters;
 }
Ejemplo n.º 5
0
 /**
  * Load the data detail
  */
 public function loadDataCrud($identity, $render = 'getFormRenderer', $action = 'editCrud')
 {
     if ($this->hasDataSource() === false) {
         throw new \Exception('No datasource defined! Please call "setDataSource()" first"');
     }
     // var_dump($this->getDataSource ()->executeDetail ( $filter ));
     $renderer = $this->{$render}();
     /**
      * Step 1.1) Filtering Column
      */
     foreach ($renderer->getFilters() as $filter) {
         $this->getDataSource()->addFilter($filter);
     }
     /**
      * Step 1.2) Filtering from Crud
      */
     $filterCrudColumn = $this->getConditionCrudColumn()[$action];
     if (is_array($filterCrudColumn)) {
         $oFilter = new Filter();
         $oFilter->setFromColumn($filterCrudColumn['field'], $filterCrudColumn['filter_operator'] . $filterCrudColumn['filter_crud']);
         $this->getDataSource()->addFilter($oFilter);
     }
     $entityObject = $this->getDataSource()->findByIdentity($identity);
     $entityObject = is_object($entityObject) || is_array($entityObject) ? $entityObject : $this->getDataSource()->getDefaultBindObject();
     $this->setCrudDetail($entityObject);
     return $this;
 }
 public function testFilter()
 {
     $source = clone $this->source;
     /*
      * LIKE
      */
     $filter = new Filter();
     $filter->setFromColumn($this->colVolumne, '~7');
     $source->addFilter($filter);
     $source->execute();
     // $this->assertEquals(2, $source->getPaginatorAdapter()
     // ->count());
 }
Ejemplo n.º 7
0
 public function testIsApplyInvalidArgumentException()
 {
     $filter = new Filter();
     $this->setExpectedException('\\InvalidArgumentException');
     $filter->isApply(123, 100, 'UndefinedFilter');
 }
Ejemplo n.º 8
0
 public static function applyStaticFilter(DatagridFilter $filter, $colString, $adapter)
 {
     if (!isset(self::$where)) {
         self::$where = new Where();
     }
     $qi = function ($name) use($adapter) {
         return $adapter->getPlatform()->quoteIdentifier($name);
     };
     $values = $filter->getValues();
     $wheres = array();
     foreach ($values as $value) {
         switch ($filter->getOperator()) {
             case DatagridFilter::LIKE:
                 $wheres[] = self::$where->like($colString, '%' . $value . '%');
                 break;
             case DatagridFilter::LIKE_LEFT:
                 $wheres[] = self::$where->like($colString, '%' . $value);
                 break;
             case DatagridFilter::LIKE_RIGHT:
                 $wheres[] = self::$where->like($colString, $value . '%');
                 break;
             case DatagridFilter::NOT_LIKE:
                 $wheres[] = self::$where->literal($qi($colString) . 'NOT LIKE ?', array('%' . $value . '%'));
                 break;
             case DatagridFilter::NOT_LIKE_LEFT:
                 $wheres[] = self::$where->literal($qi($colString) . 'NOT LIKE ?', array('%' . $value));
                 break;
             case DatagridFilter::NOT_LIKE_RIGHT:
                 $wheres[] = self::$where->literal($qi($colString) . 'NOT LIKE ?', array($value . '%'));
                 break;
             case DatagridFilter::EQUAL:
                 $wheres[] = self::$where->equalTo($colString, $value);
                 break;
             case DatagridFilter::NOT_EQUAL:
                 $wheres[] = self::$where->notEqualTo($colString, $value);
                 break;
             case DatagridFilter::GREATER_EQUAL:
                 $wheres[] = self::$where->greaterThanOrEqualTo($colString, $value);
                 break;
             case DatagridFilter::GREATER:
                 $wheres[] = self::$where->greaterThan($colString, $value);
                 break;
             case DatagridFilter::LESS_EQUAL:
                 $wheres[] = self::$where->lessThanOrEqualTo($colString, $value);
                 break;
             case DatagridFilter::LESS:
                 $wheres[] = self::$where->lessThan($colString, $value);
                 break;
             case DatagridFilter::BETWEEN:
                 $wheres[] = self::$where->between($colString, $values[0], $values[1]);
                 break;
             case DatagridFilter::IN:
                 $wheres[] = self::$where->in($colString, (array) $value);
                 break;
             case DatagridFilter::NOT_IN:
                 $wheres[] = self::$where->notin($colString, (array) $value);
                 break;
             default:
                 throw new \InvalidArgumentException('This operator is currently not supported: ' . $filter->getOperator());
                 break;
         }
     }
     return self::$where;
 }