Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  1 <?php
  2 
  3 /**
  4  * This file is part of the Grido (https://github.com/o5/grido)
  5  *
  6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
  7  *
  8  * For the full copyright and license information, please view
  9  * the file LICENSE.md that was distributed with this source code.
 10  */
 11 
 12 namespace Grido\Components\Filters;
 13 
 14 use Grido\Helpers;
 15 
 16 /**
 17  * Data filtering.
 18  *
 19  * @package     Grido
 20  * @subpackage  Components\Filters
 21  * @author      Petr Bugyík
 22  *
 23  * @property-read array $column
 24  * @property-read string $wrapperPrototype
 25  * @property-read \Nette\Forms\Controls\BaseControl $control
 26  * @property-write string $condition
 27  * @property-write callable $where
 28  * @property-write string $formatValue
 29  * @property-write string $defaultValue
 30  */
 31 abstract class Filter extends \Grido\Components\Component
 32 {
 33     const ID = 'filters';
 34 
 35     const VALUE_IDENTIFIER = '%value';
 36 
 37     const RENDER_INNER = 'inner';
 38     const RENDER_OUTER = 'outer';
 39 
 40     /** @var mixed */
 41     protected $optional;
 42 
 43     /** @var array */
 44     protected $column = array();
 45 
 46     /** @var string */
 47     protected $condition = '= ?';
 48 
 49     /** @var callable */
 50     protected $where;
 51 
 52     /** @var string */
 53     protected $formatValue;
 54 
 55     /** @var \Nette\Utils\Html */
 56     protected $wrapperPrototype;
 57 
 58     /** @var \Nette\Forms\Controls\BaseControl */
 59     protected $control;
 60 
 61     /**
 62      * @param \Grido\Grid $grid
 63      * @param string $name
 64      * @param string $label
 65      */
 66     public function __construct($grid, $name, $label)
 67     {
 68         $name = Helpers::formatColumnName($name);
 69         $this->addComponentToGrid($grid, $name);
 70 
 71         $this->label = $label;
 72         $this->type = get_class($this);
 73 
 74         $form = $this->getForm();
 75         $filters = $form->getComponent(self::ID, FALSE);
 76         if ($filters === NULL) {
 77             $filters = $form->addContainer(self::ID);
 78         }
 79 
 80         $filters->addComponent($this->getFormControl(), $name);
 81     }
 82 
 83     /**********************************************************************************************/
 84 
 85     /**
 86      * Map to database column.
 87      * @param string $column
 88      * @param string $operator
 89      * @return Filter
 90      * @throws \InvalidArgumentException
 91      */
 92     public function setColumn($column, $operator = Condition::OPERATOR_OR)
 93     {
 94         $columnAlreadySet = count($this->column) > 0;
 95         if (!Condition::isOperator($operator) && $columnAlreadySet) {
 96             throw new \InvalidArgumentException('Operator must be Condition::OPERATOR_AND or Condition::OPERATOR_OR.');
 97         }
 98 
 99         if ($columnAlreadySet) {
100             $this->column[] = $operator;
101             $this->column[] = $column;
102         } else {
103             $this->column[] = $column;
104         }
105 
106         return $this;
107     }
108 
109     /**
110      * Sets custom condition.
111      * @param $condition
112      * @return Filter
113      */
114     public function setCondition($condition)
115     {
116         $this->condition = $condition;
117         return $this;
118     }
119 
120     /**
121      * Sets custom "sql" where.
122      * @param callable $callback function($value, $source) {}
123      * @return Filter
124      */
125     public function setWhere($callback)
126     {
127         $this->where = $callback;
128         return $this;
129     }
130 
131     /**
132      * Sets custom format value.
133      * @param string $format for example: "%%value%"
134      * @return Filter
135      */
136     public function setFormatValue($format)
137     {
138         $this->formatValue = $format;
139         return $this;
140     }
141 
142     /**
143      * Sets default value.
144      * @param string $value
145      * @return Filter
146      */
147     public function setDefaultValue($value)
148     {
149         $this->grid->setDefaultFilter(array($this->getName() => $value));
150         return $this;
151     }
152 
153     /**********************************************************************************************/
154 
155     /**
156      * @return array
157      * @internal
158      */
159     public function getColumn()
160     {
161         if (!$this->column) {
162             $column = $this->getName();
163             if ($columnComponent = $this->grid->getColumn($column, FALSE)) {
164                 $column = $columnComponent->column; //use db column from column compoment
165             }
166 
167             $this->setColumn($column);
168         }
169 
170         return $this->column;
171     }
172 
173     /**
174      * @return \Nette\Forms\Controls\BaseControl
175      * @internal
176      */
177     public function getControl()
178     {
179         if ($this->control === NULL) {
180             $this->control = $this->getForm()->getComponent(self::ID)->getComponent($this->getName());
181         }
182 
183         return $this->control;
184     }
185 
186     /**
187      * Returns wrapper prototype (<th> html tag).
188      * @return \Nette\Utils\Html
189      */
190     public function getWrapperPrototype()
191     {
192         if (!$this->wrapperPrototype) {
193             $this->wrapperPrototype = \Nette\Utils\Html::el('th')
194                 ->setClass(array('grid-filter-' . $this->getName()));
195         }
196 
197         return $this->wrapperPrototype;
198     }
199 
200     /**
201      * @return string
202      */
203     public function getCondition()
204     {
205         return $this->condition;
206     }
207 
208     /**
209      * @param string $value
210      * @return Condition
211      * @throws \InvalidArgumentException
212      * @internal
213      */
214     public function __getCondition($value)
215     {
216         if ($value === '' || $value === NULL) {
217             return FALSE; //skip
218         }
219 
220         $condition = $this->getCondition();
221 
222         if ($this->where !== NULL) {
223             $condition = Condition::setupFromCallback($this->where, $value);
224 
225         } elseif (is_string($condition)) {
226             $condition = Condition::setup($this->getColumn(), $condition, $this->formatValue($value));
227 
228         } elseif ($condition instanceof Condition) {
229             $condition = $condition;
230 
231         } elseif (is_callable($condition)) {
232             $condition = callback($condition)->invokeArgs(array($value));
233 
234         } elseif (is_array($condition)) {
235             $condition = isset($condition[$value])
236                 ? $condition[$value]
237                 : Condition::setupEmpty();
238         }
239 
240         if (is_array($condition)) { //for user-defined condition by array or callback
241             $condition = Condition::setupFromArray($condition);
242 
243         } elseif ($condition !== NULL && !$condition instanceof Condition) {
244             $type = gettype($condition);
245             throw new \InvalidArgumentException("Condition must be array or Condition object. $type given.");
246         }
247 
248         return $condition;
249     }
250 
251     /**********************************************************************************************/
252 
253     /**
254      * Format value for database.
255      * @param string $value
256      * @return string
257      */
258     protected function formatValue($value)
259     {
260         if ($this->formatValue !== NULL) {
261             return str_replace(static::VALUE_IDENTIFIER, $value, $this->formatValue);
262         } else {
263             return $value;
264         }
265     }
266 
267     /**
268      * Value representation in URI.
269      * @param string $value
270      * @return string
271      * @internal
272      */
273     public function changeValue($value)
274     {
275         return $value;
276     }
277 }
278 
Grido@master API documentation generated by ApiGen