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
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns field name. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getName() |
98
|
|
|
{ |
99
|
|
|
return $this->name; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets field name. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setName($name) |
109
|
|
|
{ |
110
|
|
|
$this->name = $name; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets field name. |
117
|
|
|
* |
118
|
|
|
* @param string|null $name |
119
|
|
|
* @return $this|string |
120
|
|
|
*/ |
121
|
|
|
public function name($name = null) |
122
|
|
|
{ |
123
|
|
|
if (is_null($name)) { |
124
|
|
|
return $this->name; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->name = $name; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns true if column is hidden. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function isHidden() |
138
|
|
|
{ |
139
|
|
|
return $this->is_hidden; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Makes column hidden. |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function hide() |
148
|
|
|
{ |
149
|
|
|
$this->is_hidden = true; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Makes column visible. |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function show() |
160
|
|
|
{ |
161
|
|
|
$this->is_hidden = false; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns text label that will be rendered in table header. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getLabel() |
172
|
|
|
{ |
173
|
|
|
return $this->label ?: ucwords(str_replace(['-', '_', '.'], ' ', $this->name)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Sets text label that will be rendered in table header. |
178
|
|
|
* |
179
|
|
|
* @param string $label |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function setLabel($label) |
183
|
|
|
{ |
184
|
|
|
$this->label = $label; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Sets text label that will be rendered in table header. |
191
|
|
|
* |
192
|
|
|
* @param string|null $label |
193
|
|
|
* @return $this|string |
194
|
|
|
*/ |
195
|
|
|
public function label($label = null) |
196
|
|
|
{ |
197
|
|
|
if (is_null($label)) { |
198
|
|
|
return $this->label; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->label = $label; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns true if column is sortable (sorting controls must be rendered). |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function isSortable() |
212
|
|
|
{ |
213
|
|
|
return $this->is_sortable; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Allows to enable or disable sorting controls for column. |
218
|
|
|
* |
219
|
|
|
* @param boolean $isSortable |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
|
|
public function setSortable($isSortable) |
223
|
|
|
{ |
224
|
|
|
$this->is_sortable = $isSortable; |
225
|
|
|
|
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Allows to enable or disable sorting controls for column. |
231
|
|
|
* |
232
|
|
|
* @param boolean|null $isSortable |
233
|
|
|
* @return $this|boolean |
234
|
|
|
*/ |
235
|
|
|
public function sortable($isSortable) |
236
|
|
|
{ |
237
|
|
|
if (is_null($isSortable)) { |
238
|
|
|
return $this->is_sortable; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->is_sortable = $isSortable; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns current sorting order |
248
|
|
|
* or null if table rows are not sorted using this column. |
249
|
|
|
* |
250
|
|
|
* @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC |
251
|
|
|
*/ |
252
|
|
|
public function getSorting() |
253
|
|
|
{ |
254
|
|
|
return $this->sorting; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Allows to specify sorting by this column for data rows. |
259
|
|
|
* |
260
|
|
|
* @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
|
public function setSorting($sortOrder) |
264
|
|
|
{ |
265
|
|
|
$this->sorting = $sortOrder; |
266
|
|
|
|
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Allows to specify sorting by this column for data rows. |
272
|
|
|
* |
273
|
|
|
* @param null|string|boolean $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC|false |
274
|
|
|
* @return $this |
275
|
|
|
*/ |
276
|
|
|
public function sorting($sortOrder = false) |
277
|
|
|
{ |
278
|
|
|
if ($sortOrder === false) { |
279
|
|
|
return $this->sorting; |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$this->sorting = $sortOrder; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Returns true if data rows are sorted ascending using this column. |
289
|
|
|
* |
290
|
|
|
* @return bool |
291
|
|
|
*/ |
292
|
|
|
public function isSortedAsc() |
293
|
|
|
{ |
294
|
|
|
return $this->sorting === Grid::SORT_ASC; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Returns true if data rows are sorted descending using this column. |
299
|
|
|
* |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
public function isSortedDesc() |
303
|
|
|
{ |
304
|
|
|
return $this->sorting === Grid::SORT_DESC; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Allows to set callback function that will render |
309
|
|
|
* content of table cells for this column. |
310
|
|
|
* |
311
|
|
|
* @param callable $callback |
312
|
|
|
* @return $this |
313
|
|
|
*/ |
314
|
|
|
public function setCallback($callback) |
315
|
|
|
{ |
316
|
|
|
$this->callback = $callback; |
317
|
|
|
|
318
|
|
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Returns function that will render |
323
|
|
|
* content of table cells for this column if specified. |
324
|
|
|
* |
325
|
|
|
* @return callable|null |
326
|
|
|
*/ |
327
|
|
|
public function getCallback() |
328
|
|
|
{ |
329
|
|
|
return $this->callback; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Allows to set callback function that will render |
334
|
|
|
* content of table cells for this column. |
335
|
|
|
* |
336
|
|
|
* @param string|null $callback |
337
|
|
|
* @return $this|callable|null |
338
|
|
|
*/ |
339
|
|
|
public function callback($callback = null) |
340
|
|
|
{ |
341
|
|
|
if (is_null($callback)) { |
342
|
|
|
return $this->callback; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$this->callback = $callback; |
346
|
|
|
|
347
|
|
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Allows to specify filtering controls for column. |
352
|
|
|
* |
353
|
|
|
* @param Collection|FilterConfig[] $filters |
354
|
|
|
* @return $this |
355
|
|
|
*/ |
356
|
|
|
public function setFilters($filters) |
357
|
|
|
{ |
358
|
|
|
$this->filters = Collection::make($filters); |
359
|
|
|
foreach ($this->filters as $filterConfig) { |
360
|
|
|
$filterConfig->attach($this); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Allows to specify filtering controls for column. |
368
|
|
|
* |
369
|
|
|
* @param Collection|FilterConfig[]|null $filters |
370
|
|
|
* @return $this|Collection|FilterConfig[] |
371
|
|
|
*/ |
372
|
|
|
public function filters($filters = null) |
373
|
|
|
{ |
374
|
|
|
if (is_null($filters)) { |
375
|
|
|
return $this->filters; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$this->filters = Collection::make($filters); |
379
|
|
|
foreach ($this->filters as $filterConfig) { |
380
|
|
|
$filterConfig->attach($this); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Allows to add filtering control to column. |
388
|
|
|
* |
389
|
|
|
* @param FilterConfig $filter |
390
|
|
|
* @return $this |
391
|
|
|
*/ |
392
|
|
|
public function addFilter(FilterConfig $filter) |
393
|
|
|
{ |
394
|
|
|
$this->getFilters()->push($filter); |
395
|
|
|
$filter->attach($this); |
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Allows to add filtering control to column. |
402
|
|
|
* |
403
|
|
|
* @param FilterConfig $filter |
404
|
|
|
* @return $this |
405
|
|
|
*/ |
406
|
|
|
public function filter(FilterConfig $filter) |
407
|
|
|
{ |
408
|
|
|
$this->addFilter($filter); |
409
|
|
|
|
410
|
|
|
return $this; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Creates instance of filtering control configuration |
415
|
|
|
* and binds it to the column. |
416
|
|
|
* |
417
|
|
|
* @param string $class |
418
|
|
|
* @return FilterConfig |
419
|
|
|
*/ |
420
|
|
|
public function makeFilter($class = '\Nayjest\Grids\FilterConfig') |
421
|
|
|
{ |
422
|
|
|
$filter = new $class; |
423
|
|
|
$this->addFilter($filter); |
424
|
|
|
|
425
|
|
|
return $filter; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Returns true if any filtering controls specified for the column. |
430
|
|
|
* |
431
|
|
|
* @return bool |
432
|
|
|
*/ |
433
|
|
|
public function hasFilters() |
434
|
|
|
{ |
435
|
|
|
return !$this->getFilters()->isEmpty(); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Returns list of filtering controls specified for the column. |
440
|
|
|
* |
441
|
|
|
* @return Collection|FilterConfig[] |
442
|
|
|
*/ |
443
|
|
|
public function getFilters() |
444
|
|
|
{ |
445
|
|
|
if (null === $this->filters) { |
446
|
|
|
$this->filters = new Collection(); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
return $this->filters; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @todo move to Field instance |
454
|
|
|
* @param DataRowInterface $row |
455
|
|
|
* @return mixed |
456
|
|
|
*/ |
457
|
|
|
public function getValue(DataRowInterface $row) |
458
|
|
|
{ |
459
|
|
|
if ($function = $this->getCallback()) { |
460
|
|
|
return call_user_func($function, $row->getCellValue($this), $row); |
461
|
|
|
} else { |
462
|
|
|
return $row->getCellValue($this); |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|