示例#1
0
 /**
  * Apply filter data on array.
  *
  * @param Builder $source Data.
  *
  * @return array
  */
 protected function _applyFilter(Builder $source)
 {
     $data = $this->_getParam('filter');
     foreach ($this->_grid->getColumns() as $name => $column) {
         // Can't use empty(), coz value can be '0'.
         if (!isset($data[$name]) || $data[$name] == '') {
             continue;
         }
         $conditionLike = !isset($column[AbstractGrid::COLUMN_PARAM_USE_LIKE]) || $column[AbstractGrid::COLUMN_PARAM_USE_LIKE];
         if (!empty($column[AbstractGrid::COLUMN_PARAM_USE_HAVING])) {
             if ($conditionLike) {
                 $value = '%' . $data[$name] . '%';
             } else {
                 $value = $data[$name];
             }
             if (isset($column[AbstractGrid::COLUMN_PARAM_TYPE])) {
                 $value = $this->_grid->getDI()->getDb()->getInternalHandler()->quote($value, $column[AbstractGrid::COLUMN_PARAM_TYPE]);
             }
             if ($conditionLike) {
                 $source->having($name . ' LIKE ' . $value);
             } else {
                 $source->having($name . ' = ' . $value);
             }
         } else {
             $bindType = null;
             $alias = str_replace('.', '_', $name);
             if (isset($column[AbstractGrid::COLUMN_PARAM_TYPE])) {
                 $bindType = [$alias => $column[AbstractGrid::COLUMN_PARAM_TYPE]];
             }
             if ($conditionLike) {
                 $source->where($name . ' LIKE :' . $alias . ':', [$alias => '%' . $data[$name] . '%'], $bindType);
             } else {
                 $source->where($name . ' = :' . $alias . ':', [$alias => $data[$name]], $bindType);
             }
         }
     }
 }