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