1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\Components\Filters;
13
14 use Grido\Helpers;
15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 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
41 protected $optional;
42
43
44 protected $column = array();
45
46
47 protected $condition = '= ?';
48
49
50 protected $where;
51
52
53 protected $formatValue;
54
55
56 protected $wrapperPrototype;
57
58
59 protected $control;
60
61 62 63 64 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 87 88 89 90 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 111 112 113
114 public function setCondition($condition)
115 {
116 $this->condition = $condition;
117 return $this;
118 }
119
120 121 122 123 124
125 public function setWhere($callback)
126 {
127 $this->where = $callback;
128 return $this;
129 }
130
131 132 133 134 135
136 public function setFormatValue($format)
137 {
138 $this->formatValue = $format;
139 return $this;
140 }
141
142 143 144 145 146
147 public function setDefaultValue($value)
148 {
149 $this->grid->setDefaultFilter(array($this->getName() => $value));
150 return $this;
151 }
152
153
154
155 156 157 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;
165 }
166
167 $this->setColumn($column);
168 }
169
170 return $this->column;
171 }
172
173 174 175 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 188 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 202
203 public function getCondition()
204 {
205 return $this->condition;
206 }
207
208 209 210 211 212 213
214 public function __getCondition($value)
215 {
216 if ($value === '' || $value === NULL) {
217 return FALSE;
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)) {
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 255 256 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 269 270 271 272
273 public function changeValue($value)
274 {
275 return $value;
276 }
277 }
278