|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nayjest\Grids; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class FieldConfig |
|
8
|
|
|
* |
|
9
|
|
|
* This class describes grid column. |
|
10
|
|
|
* |
|
11
|
|
|
* @package Nayjest\Grids |
|
12
|
|
|
*/ |
|
13
|
|
|
class FieldConfig |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Field name. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $name; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Text label that will be rendered in table header. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $label; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $order = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $is_sortable = false; |
|
38
|
|
|
|
|
39
|
|
|
protected $sorting; |
|
40
|
|
|
|
|
41
|
|
|
/** @var Collection|FilterConfig[] */ |
|
42
|
|
|
protected $filters; |
|
43
|
|
|
|
|
44
|
|
|
/** @var callable */ |
|
45
|
|
|
protected $callback; |
|
46
|
|
|
|
|
47
|
|
|
protected $is_hidden = false; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string|null $name column unique name for internal usage |
|
53
|
|
|
* @param string|null $label column label |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($name = null, $label = null) |
|
56
|
|
|
{ |
|
57
|
|
|
if ($name !== null) { |
|
58
|
|
|
$this->setName($name); |
|
59
|
|
|
} |
|
60
|
|
|
if ($label !== null) { |
|
61
|
|
|
$this->setLabel($label); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns column order. |
|
67
|
|
|
* |
|
68
|
|
|
* This property used to to identify column position in grid. |
|
69
|
|
|
* |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getOrder() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->order; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets column order. |
|
79
|
|
|
* |
|
80
|
|
|
* This property used to to identify column position in grid. |
|
81
|
|
|
* |
|
82
|
|
|
* @param $order |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setOrder($order) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->order = $order; |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns field name. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getName() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->name; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets field name. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $name |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setName($name) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->name = $name; |
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Sets field name. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string|null $name |
|
117
|
|
|
* @return $this|string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function name($name = null) |
|
120
|
|
|
{ |
|
121
|
|
|
if (is_null($name)) { |
|
122
|
|
|
return $this->name; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->name = $name; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns true if column is hidden. |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function isHidden() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->is_hidden; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Makes column hidden. |
|
141
|
|
|
* |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
|
|
public function hide() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->is_hidden = true; |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Makes column visible. |
|
152
|
|
|
* |
|
153
|
|
|
* @return $this |
|
154
|
|
|
*/ |
|
155
|
|
|
public function show() |
|
156
|
|
|
{ |
|
157
|
|
|
$this->is_hidden = false; |
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns text label that will be rendered in table header. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getLabel() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->label ? : ucwords(str_replace(array('-', '_', '.'), ' ', $this->name)); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Sets text label that will be rendered in table header. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $label |
|
175
|
|
|
* @return $this |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setLabel($label) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->label = $label; |
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Sets text label that will be rendered in table header. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string|null $label |
|
187
|
|
|
* @return $this|string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function label($label = null) |
|
190
|
|
|
{ |
|
191
|
|
|
if (is_null($label)) { |
|
192
|
|
|
return $this->label; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->label = $label; |
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns true if column is sortable (sorting controls must be rendered). |
|
201
|
|
|
* |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
public function isSortable() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->is_sortable; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Allows to enable or disable sorting controls for column. |
|
211
|
|
|
* |
|
212
|
|
|
* @param boolean $isSortable |
|
213
|
|
|
* @return $this |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setSortable($isSortable) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->is_sortable = $isSortable; |
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Returns current sorting order |
|
223
|
|
|
* or null if table rows are not sorted using this column. |
|
224
|
|
|
* |
|
225
|
|
|
* @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getSorting() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->sorting; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Allows to specify sorting by this column for data rows. |
|
234
|
|
|
* |
|
235
|
|
|
* @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC |
|
236
|
|
|
* @return $this |
|
237
|
|
|
*/ |
|
238
|
|
|
public function setSorting($sortOrder) |
|
239
|
|
|
{ |
|
240
|
|
|
$this->sorting = $sortOrder; |
|
241
|
|
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Allows to enable or disable sorting controls for column. |
|
246
|
|
|
* |
|
247
|
|
|
* @param string|null $sorting |
|
248
|
|
|
* @return $this|string |
|
249
|
|
|
*/ |
|
250
|
|
|
public function sorting($sorting = null) |
|
251
|
|
|
{ |
|
252
|
|
|
if (is_null($sorting)) { |
|
253
|
|
|
return $this->sorting; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$this->sorting = $sorting; |
|
257
|
|
|
return $this; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Returns true if data rows are sorted ascending using this column. |
|
262
|
|
|
* |
|
263
|
|
|
* @return bool |
|
264
|
|
|
*/ |
|
265
|
|
|
public function isSortedAsc() |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->sorting === Grid::SORT_ASC; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Returns true if data rows are sorted descending using this column. |
|
272
|
|
|
* |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
|
public function isSortedDesc() |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->sorting === Grid::SORT_DESC; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Allows to set callback function that will render |
|
282
|
|
|
* content of table cells for this column. |
|
283
|
|
|
* |
|
284
|
|
|
* @param callable $callback |
|
285
|
|
|
* @return $this |
|
286
|
|
|
*/ |
|
287
|
|
|
public function setCallback($callback) |
|
288
|
|
|
{ |
|
289
|
|
|
$this->callback = $callback; |
|
290
|
|
|
return $this; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Returns function that will render |
|
295
|
|
|
* content of table cells for this column if specified. |
|
296
|
|
|
* |
|
297
|
|
|
* @return callable|null |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getCallback() |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->callback; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Allows to set callback function that will render |
|
306
|
|
|
* content of table cells for this column. |
|
307
|
|
|
* |
|
308
|
|
|
* @param string|null $callback |
|
309
|
|
|
* @return $this|callable|null |
|
310
|
|
|
*/ |
|
311
|
|
|
public function callback($callback = null) |
|
312
|
|
|
{ |
|
313
|
|
|
if (is_null($callback)) { |
|
314
|
|
|
return $this->callback; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$this->callback = $callback; |
|
318
|
|
|
return $this; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Allows to specify filtering controls for column. |
|
323
|
|
|
* |
|
324
|
|
|
* @param Collection|FilterConfig[] $filters |
|
325
|
|
|
* @return $this |
|
326
|
|
|
*/ |
|
327
|
|
|
public function setFilters($filters) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->filters = Collection::make($filters); |
|
330
|
|
|
foreach ($this->filters as $filterConfig) { |
|
331
|
|
|
$filterConfig->attach($this); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
return $this; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Allows to specify filtering controls for column. |
|
339
|
|
|
* |
|
340
|
|
|
* @param Collection|FilterConfig[]|null $filters |
|
341
|
|
|
* @return $this|Collection|FilterConfig[] |
|
342
|
|
|
*/ |
|
343
|
|
|
public function filters($filters = null) |
|
344
|
|
|
{ |
|
345
|
|
|
if (is_null($filters)) { |
|
346
|
|
|
return $this->filters; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
$this->filters = Collection::make($filters); |
|
350
|
|
|
foreach ($this->filters as $filterConfig) { |
|
351
|
|
|
$filterConfig->attach($this); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
return $this; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Allows to add filtering control to column. |
|
359
|
|
|
* |
|
360
|
|
|
* @param FilterConfig $filter |
|
361
|
|
|
* @return $this |
|
362
|
|
|
*/ |
|
363
|
|
|
public function addFilter(FilterConfig $filter) |
|
364
|
|
|
{ |
|
365
|
|
|
$this->getFilters()->push($filter); |
|
366
|
|
|
$filter->attach($this); |
|
367
|
|
|
return $this; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Allows to add filtering control to column. |
|
372
|
|
|
* |
|
373
|
|
|
* @param FilterConfig $filter |
|
374
|
|
|
* @return $this |
|
375
|
|
|
*/ |
|
376
|
|
|
public function filter(FilterConfig $filter) |
|
377
|
|
|
{ |
|
378
|
|
|
$this->addFilter($filter); |
|
379
|
|
|
return $this; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Creates instance of filtering control configuration |
|
384
|
|
|
* and binds it to the column. |
|
385
|
|
|
* |
|
386
|
|
|
* @param string $class |
|
387
|
|
|
* @return FilterConfig |
|
388
|
|
|
*/ |
|
389
|
|
|
public function makeFilter($class = '\Nayjest\Grids\FilterConfig') |
|
390
|
|
|
{ |
|
391
|
|
|
$filter = new $class; |
|
392
|
|
|
$this->addFilter($filter); |
|
393
|
|
|
return $filter; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Returns true if any filtering controls specified for the column. |
|
398
|
|
|
* |
|
399
|
|
|
* @return bool |
|
400
|
|
|
*/ |
|
401
|
|
|
public function hasFilters() |
|
402
|
|
|
{ |
|
403
|
|
|
return !$this->getFilters()->isEmpty(); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Returns list of filtering controls specified for the column. |
|
408
|
|
|
* |
|
409
|
|
|
* @return Collection|FilterConfig[] |
|
410
|
|
|
*/ |
|
411
|
|
|
public function getFilters() |
|
412
|
|
|
{ |
|
413
|
|
|
if (null === $this->filters) { |
|
414
|
|
|
$this->filters = new Collection(); |
|
415
|
|
|
} |
|
416
|
|
|
return $this->filters; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* @todo move to Field instance |
|
421
|
|
|
* @param DataRowInterface $row |
|
422
|
|
|
* @return mixed |
|
423
|
|
|
*/ |
|
424
|
|
|
public function getValue(DataRowInterface $row) |
|
425
|
|
|
{ |
|
426
|
|
|
if ($function = $this->getCallback()) { |
|
427
|
|
|
return call_user_func($function, $row->getCellValue($this), $row); |
|
428
|
|
|
} else { |
|
429
|
|
|
return $row->getCellValue($this); |
|
430
|
|
|
} |
|
431
|
|
|
} |
|
432
|
|
|
} |
|
433
|
|
|
|