1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
5
|
|
|
* @author Pavel Janda <[email protected]> |
6
|
|
|
* @package Ublaboo |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\Filter; |
10
|
|
|
|
11
|
|
|
use Nette; |
12
|
|
|
use Ublaboo\DataGrid\DataGrid; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @method void addToFormContainer(Nette\Forms\Container $container) |
16
|
|
|
*/ |
17
|
1 |
|
abstract class Filter extends Nette\Object |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var mixed |
22
|
|
|
*/ |
23
|
|
|
protected $value; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $value_set = FALSE; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var callable |
32
|
|
|
*/ |
33
|
|
|
protected $condition_callback; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $placeholder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $key; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $name; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string|array |
52
|
|
|
*/ |
53
|
|
|
protected $column; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $template; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $type; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var DataGrid |
67
|
|
|
*/ |
68
|
|
|
protected $grid; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $attributes = [ |
74
|
|
|
'class' => ['form-control', 'input-sm'], |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param DataGrid $grid |
80
|
|
|
* @param string $key |
81
|
|
|
* @param string $name |
82
|
|
|
* @param string|array $column |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function __construct($grid, $key, $name, $column) |
|
|
|
|
85
|
|
|
{ |
86
|
1 |
|
$this->grid = $grid; |
87
|
1 |
|
$this->key = $key; |
88
|
1 |
|
$this->name = $name; |
89
|
1 |
|
$this->column = $column; |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get filter key |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function getKey() |
98
|
|
|
{ |
99
|
|
|
return $this->key; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get filter name |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getName() |
108
|
|
|
{ |
109
|
|
|
return $this->name; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get filter column |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getColumn() |
118
|
|
|
{ |
119
|
1 |
|
return $this->column; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Tell whether value has been set in this fitler |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
|
public function isValueSet() |
128
|
|
|
{ |
129
|
1 |
|
return $this->value_set; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set filter value |
135
|
|
|
* @param mixed $value |
136
|
|
|
* @return static |
137
|
|
|
*/ |
138
|
|
|
public function setValue($value) |
139
|
|
|
{ |
140
|
1 |
|
$this->value = $value; |
141
|
1 |
|
$this->value_set = TRUE; |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get filter value |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function getValue() |
152
|
|
|
{ |
153
|
1 |
|
return $this->value; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set html attr placeholder |
159
|
|
|
* @param string $placeholder |
160
|
|
|
* @return static |
161
|
|
|
*/ |
162
|
|
|
public function setPlaceholder($placeholder) |
163
|
|
|
{ |
164
|
|
|
$this->placeholder = $placeholder; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get html attr placeholder |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getPlaceholder() |
175
|
|
|
{ |
176
|
|
|
return $this->placeholder; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set custom condition on filter |
182
|
|
|
* @param callable $condition_callback |
183
|
|
|
* @return static |
184
|
|
|
*/ |
185
|
|
|
public function setCondition($condition_callback) |
186
|
|
|
{ |
187
|
|
|
$this->condition_callback = $condition_callback; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get filter condition |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function getCondition() |
198
|
|
|
{ |
199
|
|
|
return [$this->column => $this->getValue()]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Tell whether custom condition_callback on filter is set |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function hasConditionCallback() |
208
|
|
|
{ |
209
|
1 |
|
return (bool) $this->condition_callback; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get custom filter condition |
215
|
|
|
* @return callable |
216
|
|
|
*/ |
217
|
|
|
public function getConditionCallback() |
218
|
|
|
{ |
219
|
|
|
return $this->condition_callback; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Filter may have its own template |
225
|
|
|
* @param string $template |
226
|
|
|
* @return static |
227
|
|
|
*/ |
228
|
|
|
public function setTemplate($template) |
229
|
|
|
{ |
230
|
|
|
$this->template = (string) $template; |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get filter template path |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getTemplate() |
241
|
|
|
{ |
242
|
|
|
return $this->template; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getType() |
250
|
|
|
{ |
251
|
|
|
return $this->type; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $name |
257
|
|
|
* @param mixed $value |
258
|
|
|
* @return static |
259
|
|
|
*/ |
260
|
|
|
public function addAttribute($name, $value) |
261
|
|
|
{ |
262
|
|
|
$this->attributes[$name][] = $value; |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $name |
270
|
|
|
* @param mixed $value |
271
|
|
|
* @return static |
272
|
|
|
*/ |
273
|
|
|
public function setAttribute($name, $value) |
274
|
|
|
{ |
275
|
|
|
$this->attributes[$name] = (array) $value; |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return array |
283
|
|
|
* @deprecated use getAttributes instead |
284
|
|
|
*/ |
285
|
|
|
public function getAttribtues() |
286
|
|
|
{ |
287
|
|
|
@trigger_error('getAttribtues is deprecated, use getAttributes instead', E_USER_DEPRECATED); |
|
|
|
|
288
|
|
|
return $this->getAttributes(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
|
|
public function getAttributes() |
296
|
|
|
{ |
297
|
|
|
return $this->attributes; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param Nette\Forms\Controls\BaseControl $input |
303
|
|
|
* @return Nette\Forms\Controls\BaseControl |
304
|
|
|
*/ |
305
|
|
|
protected function addAttributes($input) |
306
|
|
|
{ |
307
|
|
|
if ($this->grid->hasAutoSubmit()) { |
308
|
|
|
$input->setAttribute('data-autosubmit', TRUE); |
309
|
|
|
} else { |
310
|
|
|
$input->setAttribute('data-datagrid-manualsubmit', TRUE); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
foreach ($this->attributes as $key => $value) { |
314
|
|
|
if (is_array($value)) { |
315
|
|
|
$value = array_unique($value); |
316
|
|
|
$value = implode(' ', $value); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$input->setAttribute($key, $value); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
if($this->isValueSet()) { |
323
|
|
|
$input->setDefaultValue($this->getValue()); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return $input; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.