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
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $attributes = []; |
51
|
|
|
|
52
|
|
|
protected $is_hidden = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* |
57
|
|
|
* @param string|null $name column unique name for internal usage |
58
|
|
|
* @param string|null $label column label |
59
|
|
|
*/ |
60
|
|
|
public function __construct($name = null, $label = null) |
61
|
|
|
{ |
62
|
|
|
if ($name !== null) { |
63
|
|
|
$this->setName($name); |
64
|
|
|
} |
65
|
|
|
if ($label !== null) { |
66
|
|
|
$this->setLabel($label); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns column order. |
72
|
|
|
* |
73
|
|
|
* This property used to to identify column position in grid. |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
|
|
public function getOrder() |
78
|
|
|
{ |
79
|
|
|
return $this->order; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets column order. |
84
|
|
|
* |
85
|
|
|
* This property used to to identify column position in grid. |
86
|
|
|
* |
87
|
|
|
* @param $order |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setOrder($order) |
91
|
|
|
{ |
92
|
|
|
$this->order = $order; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns field name. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getName() |
102
|
|
|
{ |
103
|
|
|
return $this->name; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets field name. |
108
|
|
|
* |
109
|
|
|
* @param string $name |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function setName($name) |
113
|
|
|
{ |
114
|
|
|
$this->name = $name; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns true if column is hidden. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function isHidden() |
124
|
|
|
{ |
125
|
|
|
return $this->is_hidden; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Makes column hidden. |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function hide() |
134
|
|
|
{ |
135
|
|
|
$this->is_hidden = true; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Makes column visible. |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function show() |
145
|
|
|
{ |
146
|
|
|
$this->is_hidden = false; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns text label that will be rendered in table header. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getLabel() |
156
|
|
|
{ |
157
|
|
|
return $this->label ? : ucwords(str_replace(array('-', '_', '.'), ' ', $this->name)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sets text label that will be rendered in table header. |
162
|
|
|
* |
163
|
|
|
* @param string $label |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function setLabel($label) |
167
|
|
|
{ |
168
|
|
|
$this->label = $label; |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns true if column is sortable (sorting controls must be rendered). |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function isSortable() |
178
|
|
|
{ |
179
|
|
|
return $this->is_sortable; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Allows to enable or disable sorting controls for column. |
184
|
|
|
* |
185
|
|
|
* @param boolean $isSortable |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setSortable($isSortable) |
189
|
|
|
{ |
190
|
|
|
$this->is_sortable = $isSortable; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns current sorting order |
196
|
|
|
* or null if table rows are not sorted using this column. |
197
|
|
|
* |
198
|
|
|
* @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC |
199
|
|
|
*/ |
200
|
|
|
public function getSorting() |
201
|
|
|
{ |
202
|
|
|
return $this->sorting; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Allows to specify sorting by this column for data rows. |
207
|
|
|
* |
208
|
|
|
* @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function setSorting($sortOrder) |
212
|
|
|
{ |
213
|
|
|
$this->sorting = $sortOrder; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns true if data rows are sorted ascending using this column. |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function isSortedAsc() |
223
|
|
|
{ |
224
|
|
|
return $this->sorting === Grid::SORT_ASC; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns true if data rows are sorted descending using this column. |
229
|
|
|
* |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function isSortedDesc() |
233
|
|
|
{ |
234
|
|
|
return $this->sorting === Grid::SORT_DESC; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Allows to set callback function that will render |
239
|
|
|
* content of table cells for this column. |
240
|
|
|
* |
241
|
|
|
* @param callable $callback |
242
|
|
|
* @return $this |
243
|
|
|
*/ |
244
|
|
|
public function setCallback($callback) |
245
|
|
|
{ |
246
|
|
|
$this->callback = $callback; |
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Returns function that will render |
252
|
|
|
* content of table cells for this column if specified. |
253
|
|
|
* |
254
|
|
|
* @return callable|null |
255
|
|
|
*/ |
256
|
|
|
public function getCallback() |
257
|
|
|
{ |
258
|
|
|
return $this->callback; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Allows to specify filtering controls for column. |
263
|
|
|
* |
264
|
|
|
* @param Collection|FilterConfig[] $filters |
265
|
|
|
* @return $this |
266
|
|
|
*/ |
267
|
|
|
public function setFilters($filters) |
268
|
|
|
{ |
269
|
|
|
$this->filters = Collection::make($filters); |
270
|
|
|
foreach ($this->filters as $filterConfig) { |
271
|
|
|
$filterConfig->attach($this); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Allows to add filtering control to column. |
279
|
|
|
* |
280
|
|
|
* @param FilterConfig $filter |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
|
|
public function addFilter(FilterConfig $filter) |
284
|
|
|
{ |
285
|
|
|
$this->getFilters()->push($filter); |
286
|
|
|
$filter->attach($this); |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Creates instance of filtering control configuration |
292
|
|
|
* and binds it to the column. |
293
|
|
|
* |
294
|
|
|
* @param string $class |
295
|
|
|
* @return FilterConfig |
296
|
|
|
*/ |
297
|
|
|
public function makeFilter($class = '\Nayjest\Grids\FilterConfig') |
298
|
|
|
{ |
299
|
|
|
$filter = new $class; |
300
|
|
|
$this->addFilter($filter); |
301
|
|
|
return $filter; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Returns true if any filtering controls specified for the column. |
306
|
|
|
* |
307
|
|
|
* @return bool |
308
|
|
|
*/ |
309
|
|
|
public function hasFilters() |
310
|
|
|
{ |
311
|
|
|
return !$this->getFilters()->isEmpty(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Returns list of filtering controls specified for the column. |
316
|
|
|
* |
317
|
|
|
* @return Collection|FilterConfig[] |
318
|
|
|
*/ |
319
|
|
|
public function getFilters() |
320
|
|
|
{ |
321
|
|
|
if (null === $this->filters) { |
322
|
|
|
$this->filters = new Collection(); |
323
|
|
|
} |
324
|
|
|
return $this->filters; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @todo move to Field instance |
329
|
|
|
* @param DataRowInterface $row |
330
|
|
|
* @return mixed |
331
|
|
|
*/ |
332
|
|
|
public function getValue(DataRowInterface $row) |
333
|
|
|
{ |
334
|
|
|
if ($function = $this->getCallback()) { |
335
|
|
|
return call_user_func($function, $row->getCellValue($this), $row); |
336
|
|
|
} else { |
337
|
|
|
return $row->getCellValue($this); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param array $attributes |
343
|
|
|
* @return $this |
344
|
|
|
*/ |
345
|
|
|
public function setAttributes($attributes) |
346
|
|
|
{ |
347
|
|
|
$this->attributes = $attributes; |
348
|
|
|
return $this; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return array |
353
|
|
|
*/ |
354
|
|
|
public function getAttributes() |
355
|
|
|
{ |
356
|
|
|
return $this->attributes; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|