-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathColumn.php
More file actions
392 lines (337 loc) · 8.82 KB
/
Column.php
File metadata and controls
392 lines (337 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
namespace Sebastienheyd\Boilerplate\Datatables;
use Closure;
class Column
{
protected array $actions = [];
protected array $attributes = [];
protected ?Closure $filter = null;
protected array $filterOptions = [];
protected string $filterType = 'input';
protected ?Closure $order = null;
protected ?Closure $raw = null;
protected string $title = '';
protected string $tooltip = '';
protected bool $sum = false;
/**
* Instanciate a new column.
*
* @param string $title
*/
public function __construct(string $title)
{
$this->title = $title;
}
/**
* Add a new column to the DataTable.
*
* @param string $title
* @return Column
*/
public static function add(string $title = ''): Column
{
return new static($title);
}
/**
* Get attributes used by the DataTable initialization script.
*
* @return string
*/
public function get(): string
{
$attributes = [];
foreach ($this->attributes as $k => $v) {
if (is_string($v)) {
$attributes[] = "$k:\"$v\"";
} elseif ($v instanceof Closure) {
$attributes[] = "$k:".$v();
} elseif (is_bool($v)) {
$attributes[] = "$k:".($v ? 'true' : 'false');
} elseif (is_array($v)) {
$attributes[] = "$k:[".implode(',', $v).']';
} else {
$attributes[] = "$k:".intval($v);
}
}
return '{'.implode(',', $attributes).'}';
}
/**
* Set the property to use as data source. Property can be formatted by using a Closure.
*
* @param string $name
* @param Closure|null $format
* @return $this
*/
public function data(string $name, ?Closure $format = null): Column
{
$this->attributes['data'] = $name;
if ($format !== null) {
$this->raw = $format;
}
return $this;
}
/**
* Define an array of options to be used by the filter select.
*
* @param $filterOptions
* @param string $filterType
* @return $this
*/
public function filterOptions($filterOptions, bool $multiple = false): Column
{
if (is_array($filterOptions)) {
$this->filterOptions = $filterOptions;
}
if ($filterOptions instanceof Closure) {
$this->filterOptions = $filterOptions->call($this);
}
$this->filterType($multiple ? 'select-multiple' : 'select');
return $this;
}
/**
* For dates, convert the date to a "From Now" format.
*
* @return $this
*/
public function fromNow(): Column
{
$this->attributes['render'] = fn () => '$.fn.dataTable.render.fromNow()';
return $this;
}
/**
* For dates, convert the date to the given format. Default format is "YYYY-MM-DD HH:mm:ss".
*
* @param string|Closure|null $format
* @return $this
*/
public function dateFormat(string|Closure|null $format = null): Column
{
if ($format instanceof Closure) {
$this->raw = $format;
} else {
if ($format === null) {
$format = __('boilerplate::date.YmdHis');
}
$this->attributes['render'] = fn () => "$.fn.dataTable.render.moment('".$format."')";
}
if (isset($query) && method_exists($query, 'whereBetween')) {
$this->filter(function ($query, $q) {
if (preg_match('#^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}|[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}$#', $q)) {
[$start, $end] = explode('|', $q);
$query->whereBetween($this->name ?? $this->data, [$start, $end]);
}
});
$this->filterType('daterangepicker');
}
return $this;
}
/**
* Define the filter type.
*
* @param string $type
* @return $this
*/
public function filterType(string $type = 'text'): Column
{
if (in_array($type, ['text', 'daterangepicker', 'select', 'select-multiple'])) {
$this->filterType = $type;
}
return $this;
}
/**
* Filter to use for custom searches.
*
* @param Closure $filter
* @return $this
*/
public function filter(Closure $filter): Column
{
$filter = Closure::bind($filter, $this);
$this->filter = $filter;
return $this;
}
/**
* Order to use for custom searches.
*
* @param Closure $order
* @return $this
*/
public function order(Closure $order): Column
{
$order = Closure::bind($order, $this);
$this->order = $order;
return $this;
}
/**
* Alias for actions column.
*
* @param Closure $actions
* @return $this
*/
public function actions(Closure $actions): Column
{
$this->data('dt-actions', $actions)->class('visible-on-hover text-nowrap')->notSearchable()->notOrderable();
return $this;
}
/**
* Column class.
*
* @param string $class
* @return $this
*/
public function class(string $class): Column
{
$this->attributes['class'] = $class;
return $this;
}
/**
* For eager loaded relationships, specify the relation.column_name to use.
*
* @param string $name
* @return $this
*/
public function name(string $name): Column
{
$this->attributes['name'] = $name;
return $this;
}
/**
* Specify the column width.
*
* @param string $width
* @return $this
*/
public function width(string $width): Column
{
$this->attributes['width'] = $width;
return $this;
}
/**
* Set a tooltip on the column title.
*
* @param string $tooltip
* @return $this
*/
public function tooltip(string $tooltip): Column
{
$this->tooltip = $tooltip;
return $this;
}
/**
* Set the column visibility.
*
* @param bool $visible
* @return Column
*/
public function visible(bool $visible = true): Column
{
if ($visible) {
unset($this->attributes['visible']);
} else {
$this->attributes['visible'] = false;
}
return $this;
}
/**
* Column must be hidden.
*
* @return Column
*/
public function hidden(): Column
{
return $this->visible(false);
}
/**
* Setting a boolean attribute.
*
* @param $name
* @param $value
* @return Column
*/
private function booleanAttribute($name, $value): Column
{
$this->attributes[$name] = false;
if ($value === true) {
unset($this->attributes[$name]);
}
return $this;
}
/**
* Column content must not be searchable.
*
* @return Column
*/
public function notSearchable(): Column
{
return $this->booleanAttribute('searchable', false);
}
/**
* Column content must not be orderable. Alias of notOrderable.
*
* @return Column
*/
public function notSortable(): Column
{
return $this->notOrderable();
}
/**
* Column content must not be orderable.
*
* @return Column
*/
public function notOrderable(): Column
{
return $this->booleanAttribute('orderable', false);
}
/**
* Specify the column index(es) or data names to use for ordering this column.
* Useful when you want to order by a hidden column.
*
* @param int|string|array $columnIndexOrData
* @return Column
*/
public function orderData(int|string|array $columnIndexOrData): Column
{
$this->attributes['orderData'] = $columnIndexOrData;
return $this;
}
/**
* Magic method to get properties or attributes.
*
* @param $name
* @return mixed|null
*/
public function __get($name)
{
if (property_exists($this, $name)) {
return $this->$name;
}
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
/**
* Enable calculation of the sum for this column in the footer.
*
* @return Column
*/
public function sum(): Column
{
$this->sum = true;
return $this;
}
/**
* Magic method to know if magic property or attribute is set.
*
* @param $name
* @return bool
*/
public function __isset($name)
{
if (property_exists($this, $name)) {
return ! empty($this->$name);
}
return isset($this->attributes[$name]);
}
}