1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Closure; |
7
|
|
|
use Encore\Admin\Grid; |
8
|
|
|
use Encore\Admin\Grid\Displayers\AbstractDisplayer; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Column. |
17
|
|
|
* |
18
|
|
|
* @method $this editable() |
19
|
|
|
* @method $this switch ($states = []) |
20
|
|
|
* @method $this switchGroup($columns = [], $states = []) |
21
|
|
|
* @method $this select($options = []) |
22
|
|
|
* @method $this image($server = '', $width = 200, $height = 200) |
23
|
|
|
* @method $this label($style = 'success') |
24
|
|
|
* @method $this button($style = null) |
25
|
|
|
* @method $this link($href = '', $target = '_blank') |
26
|
|
|
* @method $this badge($style = 'red') |
27
|
|
|
* @method $this progressBar($style = 'primary', $size = 'sm', $max = 100) |
28
|
|
|
* @method $this radio($options = []) |
29
|
|
|
* @method $this checkbox($options = []) |
30
|
|
|
* @method $this orderable($column, $label = '') |
31
|
|
|
* @method $this table($titles = []) |
32
|
|
|
* @method $this expand($callback = null) |
33
|
|
|
* @method $this modal($callback = null) |
34
|
|
|
* @method $this carousel(int $width = 300, int $height = 200, $server = '') |
35
|
|
|
* @method $this download($server = '') |
36
|
|
|
*/ |
37
|
|
|
class Column |
38
|
|
|
{ |
39
|
|
|
use Column\HasHeader; |
40
|
|
|
|
41
|
|
|
const SELECT_COLUMN_NAME = '__row_selector__'; |
42
|
|
|
|
43
|
|
|
const ACTION_COLUMN_NAME = '__actions__'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Grid |
47
|
|
|
*/ |
48
|
|
|
protected $grid; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Name of column. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $name; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Label of column. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $label; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Original value of column. |
66
|
|
|
* |
67
|
|
|
* @var mixed |
68
|
|
|
*/ |
69
|
|
|
protected $original; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Attributes of column. |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $attributes = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Relation name. |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
protected $relation = false; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Relation column. |
87
|
|
|
* |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
protected $relationColumn; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Original grid data. |
94
|
|
|
* |
95
|
|
|
* @var Collection |
96
|
|
|
*/ |
97
|
|
|
protected static $originalGridModels; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var []Closure |
101
|
|
|
*/ |
102
|
|
|
protected $displayCallbacks = []; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Displayers for grid column. |
106
|
|
|
* |
107
|
|
|
* @var array |
108
|
|
|
*/ |
109
|
|
|
public static $displayers = []; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Defined columns. |
113
|
|
|
* |
114
|
|
|
* @var array |
115
|
|
|
*/ |
116
|
|
|
public static $defined = []; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @var array |
120
|
|
|
*/ |
121
|
|
|
protected static $htmlAttributes = []; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @var Model |
125
|
|
|
*/ |
126
|
|
|
protected static $model; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $name |
130
|
|
|
* @param string $label |
131
|
|
|
*/ |
132
|
|
|
public function __construct($name, $label) |
133
|
|
|
{ |
134
|
|
|
$this->name = $name; |
135
|
|
|
|
136
|
|
|
$this->label = $this->formatLabel($label); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Extend column displayer. |
141
|
|
|
* |
142
|
|
|
* @param $name |
143
|
|
|
* @param $displayer |
144
|
|
|
*/ |
145
|
|
|
public static function extend($name, $displayer) |
146
|
|
|
{ |
147
|
|
|
static::$displayers[$name] = $displayer; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Define a column globally. |
152
|
|
|
* |
153
|
|
|
* @param string $name |
154
|
|
|
* @param mixed $definition |
155
|
|
|
*/ |
156
|
|
|
public static function define($name, $definition) |
157
|
|
|
{ |
158
|
|
|
static::$defined[$name] = $definition; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set grid instance for column. |
163
|
|
|
* |
164
|
|
|
* @param Grid $grid |
165
|
|
|
*/ |
166
|
|
|
public function setGrid(Grid $grid) |
167
|
|
|
{ |
168
|
|
|
$this->grid = $grid; |
169
|
|
|
|
170
|
|
|
$this->setModel($grid->model()->eloquent()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set model for column. |
175
|
|
|
* |
176
|
|
|
* @param $model |
177
|
|
|
*/ |
178
|
|
|
public function setModel($model) |
179
|
|
|
{ |
180
|
|
|
if (is_null(static::$model) && ($model instanceof Model)) { |
181
|
|
|
static::$model = $model->newInstance(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set original data for column. |
187
|
|
|
* |
188
|
|
|
* @param Collection $collection |
189
|
|
|
*/ |
190
|
|
|
public static function setOriginalGridModels(Collection $collection) |
191
|
|
|
{ |
192
|
|
|
static::$originalGridModels = $collection; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set column attributes. |
197
|
|
|
* |
198
|
|
|
* @param array $attributes |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
|
|
public function setAttributes($attributes = []) |
203
|
|
|
{ |
204
|
|
|
static::$htmlAttributes[$this->name] = $attributes; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get column attributes. |
211
|
|
|
* |
212
|
|
|
* @param string $name |
213
|
|
|
* |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
|
|
public static function getAttributes($name) |
217
|
|
|
{ |
218
|
|
|
return Arr::get(static::$htmlAttributes, $name, ''); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set style of this column. |
223
|
|
|
* |
224
|
|
|
* @param string $style |
225
|
|
|
* |
226
|
|
|
* @return $this |
227
|
|
|
*/ |
228
|
|
|
public function style($style) |
229
|
|
|
{ |
230
|
|
|
return $this->setAttributes(compact('style')); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Set the width of column. |
235
|
|
|
* |
236
|
|
|
* @param int $width |
237
|
|
|
* |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
|
|
public function width(int $width) |
241
|
|
|
{ |
242
|
|
|
return $this->style("width: {$width}px;"); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Set the color of column. |
247
|
|
|
* |
248
|
|
|
* @param string $color |
249
|
|
|
* |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
|
|
public function color($color) |
253
|
|
|
{ |
254
|
|
|
return $this->style("color:$color;"); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get original column value. |
259
|
|
|
* |
260
|
|
|
* @return mixed |
261
|
|
|
*/ |
262
|
|
|
public function getOriginal() |
263
|
|
|
{ |
264
|
|
|
return $this->original; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get name of this column. |
269
|
|
|
* |
270
|
|
|
* @return mixed |
271
|
|
|
*/ |
272
|
|
|
public function getName() |
273
|
|
|
{ |
274
|
|
|
return $this->name; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Format label. |
279
|
|
|
* |
280
|
|
|
* @param $label |
281
|
|
|
* |
282
|
|
|
* @return mixed |
283
|
|
|
*/ |
284
|
|
|
protected function formatLabel($label) |
285
|
|
|
{ |
286
|
|
|
if ($label) { |
287
|
|
|
return $label; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$label = ucfirst($this->name); |
291
|
|
|
|
292
|
|
|
return __(str_replace(['.', '_'], ' ', $label)); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get label of the column. |
297
|
|
|
* |
298
|
|
|
* @return mixed |
299
|
|
|
*/ |
300
|
|
|
public function getLabel() |
301
|
|
|
{ |
302
|
|
|
return $this->label; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Set relation. |
307
|
|
|
* |
308
|
|
|
* @param string $relation |
309
|
|
|
* @param string $relationColumn |
310
|
|
|
* |
311
|
|
|
* @return $this |
312
|
|
|
*/ |
313
|
|
|
public function setRelation($relation, $relationColumn = null) |
314
|
|
|
{ |
315
|
|
|
$this->relation = $relation; |
|
|
|
|
316
|
|
|
$this->relationColumn = $relationColumn; |
317
|
|
|
|
318
|
|
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* If this column is relation column. |
323
|
|
|
* |
324
|
|
|
* @return bool |
325
|
|
|
*/ |
326
|
|
|
protected function isRelation() |
327
|
|
|
{ |
328
|
|
|
return (bool)$this->relation; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Mark this column as sortable. |
333
|
|
|
* |
334
|
|
|
* @param null|string $cast |
335
|
|
|
* |
336
|
|
|
* @return Column|string |
337
|
|
|
*/ |
338
|
|
|
public function sortable($cast = null) |
339
|
|
|
{ |
340
|
|
|
return $this->addSorter($cast); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set cast name for sortable. |
345
|
|
|
* |
346
|
|
|
* @return $this |
347
|
|
|
* |
348
|
|
|
* @deprecated Use `$column->sortable($cast)` instead. |
349
|
|
|
*/ |
350
|
|
|
public function cast($cast) |
351
|
|
|
{ |
352
|
|
|
$this->cast = $cast; |
|
|
|
|
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Set help message for column. |
359
|
|
|
* |
360
|
|
|
* @param string $help |
361
|
|
|
* |
362
|
|
|
* @return $this|string |
363
|
|
|
*/ |
364
|
|
|
public function help($help = '') |
365
|
|
|
{ |
366
|
|
|
return $this->addHelp($help); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Set column filter. |
371
|
|
|
* |
372
|
|
|
* @param null $builder |
373
|
|
|
* |
374
|
|
|
* @return $this |
375
|
|
|
*/ |
376
|
|
|
public function filter($builder = null) |
|
|
|
|
377
|
|
|
{ |
378
|
|
|
return $this->addFilter(...func_get_args()); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Add a display callback. |
383
|
|
|
* |
384
|
|
|
* @param Closure $callback |
385
|
|
|
* |
386
|
|
|
* @return $this |
387
|
|
|
*/ |
388
|
|
|
public function display(Closure $callback) |
389
|
|
|
{ |
390
|
|
|
$this->displayCallbacks[] = $callback; |
391
|
|
|
|
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Display using display abstract. |
397
|
|
|
* |
398
|
|
|
* @param string $abstract |
399
|
|
|
* @param array $arguments |
400
|
|
|
* |
401
|
|
|
* @return $this |
402
|
|
|
*/ |
403
|
|
|
public function displayUsing($abstract, $arguments = []) |
404
|
|
|
{ |
405
|
|
|
$grid = $this->grid; |
406
|
|
|
|
407
|
|
|
$column = $this; |
408
|
|
|
|
409
|
|
View Code Duplication |
return $this->display(function ($value) use ($grid, $column, $abstract, $arguments) { |
|
|
|
|
410
|
|
|
/** @var AbstractDisplayer $displayer */ |
411
|
|
|
$displayer = new $abstract($value, $grid, $column, $this); |
412
|
|
|
|
413
|
|
|
return $displayer->display(...$arguments); |
|
|
|
|
414
|
|
|
}); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Display column using array value map. |
419
|
|
|
* |
420
|
|
|
* @param array $values |
421
|
|
|
* @param null $default |
422
|
|
|
* |
423
|
|
|
* @return $this |
424
|
|
|
*/ |
425
|
|
View Code Duplication |
public function using(array $values, $default = null) |
|
|
|
|
426
|
|
|
{ |
427
|
|
|
return $this->display(function ($value) use ($values, $default) { |
428
|
|
|
if (is_null($value)) { |
429
|
|
|
return $default; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
return Arr::get($values, $value, $default); |
433
|
|
|
}); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Replace output value with giving map. |
438
|
|
|
* |
439
|
|
|
* @param array $replacements |
440
|
|
|
* |
441
|
|
|
* @return $this |
442
|
|
|
*/ |
443
|
|
|
public function replace(array $replacements) |
444
|
|
|
{ |
445
|
|
|
return $this->display(function ($value) use ($replacements) { |
446
|
|
|
if (isset($replacements[$value])) { |
447
|
|
|
return $replacements[$value]; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
return $value; |
451
|
|
|
}); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Render this column with the given view. |
456
|
|
|
* |
457
|
|
|
* @param string $view |
458
|
|
|
* |
459
|
|
|
* @return $this |
460
|
|
|
*/ |
461
|
|
|
public function view($view) |
462
|
|
|
{ |
463
|
|
|
return $this->display(function ($value) use ($view) { |
464
|
|
|
$model = $this; |
465
|
|
|
|
466
|
|
|
return view($view, compact('model', 'value'))->render(); |
|
|
|
|
467
|
|
|
}); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Hide this column by default. |
472
|
|
|
* |
473
|
|
|
* @return $this |
474
|
|
|
*/ |
475
|
|
|
public function hide() |
476
|
|
|
{ |
477
|
|
|
$this->grid->hideColumns($this->getName()); |
478
|
|
|
|
479
|
|
|
return $this; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Add column to total-row. |
484
|
|
|
* |
485
|
|
|
* @param null $display |
486
|
|
|
* |
487
|
|
|
* @return $this |
488
|
|
|
*/ |
489
|
|
|
public function totalRow($display = null) |
490
|
|
|
{ |
491
|
|
|
$this->grid->addTotalRow($this->name, $display); |
|
|
|
|
492
|
|
|
|
493
|
|
|
return $this; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Convert file size to a human readable format like `100mb`. |
498
|
|
|
* |
499
|
|
|
* @return $this |
500
|
|
|
*/ |
501
|
|
|
public function filesize() |
502
|
|
|
{ |
503
|
|
|
return $this->display(function ($value) { |
504
|
|
|
return file_size($value); |
505
|
|
|
}); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Display the fields in the email format as gavatar. |
510
|
|
|
* |
511
|
|
|
* @param int $size |
512
|
|
|
* |
513
|
|
|
* @return $this |
514
|
|
|
*/ |
515
|
|
|
public function gravatar($size = 30) |
516
|
|
|
{ |
517
|
|
|
return $this->display(function ($value) use ($size) { |
518
|
|
|
$src = sprintf( |
519
|
|
|
'https://www.gravatar.com/avatar/%s?s=%d', |
520
|
|
|
md5(strtolower($value)), |
521
|
|
|
$size |
522
|
|
|
); |
523
|
|
|
|
524
|
|
|
return "<img src='$src' class='img img-circle'/>"; |
525
|
|
|
}); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Display field as a loading icon. |
530
|
|
|
* |
531
|
|
|
* @param array $values |
532
|
|
|
* @param array $others |
533
|
|
|
* |
534
|
|
|
* @return $this |
535
|
|
|
*/ |
536
|
|
|
public function loading($values = [], $others = []) |
537
|
|
|
{ |
538
|
|
|
return $this->display(function ($value) use ($values, $others) { |
539
|
|
|
$values = (array)$values; |
|
|
|
|
540
|
|
|
|
541
|
|
|
if (in_array($value, $values)) { |
542
|
|
|
return '<i class="fa fa-refresh fa-spin text-primary"></i>'; |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
return Arr::get($others, $value, $value); |
546
|
|
|
}); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Display column as an font-awesome icon based on it's value. |
551
|
|
|
* |
552
|
|
|
* @param array $setting |
553
|
|
|
* @param string $default |
554
|
|
|
* |
555
|
|
|
* @return $this |
556
|
|
|
*/ |
557
|
|
|
public function icon(array $setting, $default = '') |
558
|
|
|
{ |
559
|
|
|
return $this->display(function ($value) use ($setting, $default) { |
560
|
|
|
$fa = ''; |
561
|
|
|
|
562
|
|
|
if (isset($setting[$value])) { |
563
|
|
|
$fa = $setting[$value]; |
564
|
|
|
} elseif ($default) { |
565
|
|
|
$fa = $default; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
return "<i class=\"fa fa-{$fa}\"></i>"; |
569
|
|
|
}); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Return a human readable format time. |
574
|
|
|
* |
575
|
|
|
* @param null $locale |
576
|
|
|
* |
577
|
|
|
* @return $this |
578
|
|
|
*/ |
579
|
|
|
public function diffForHumans($locale = null) |
580
|
|
|
{ |
581
|
|
|
if ($locale) { |
582
|
|
|
Carbon::setLocale($locale); |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
return $this->display(function ($value) { |
586
|
|
|
return Carbon::parse($value)->diffForHumans(); |
|
|
|
|
587
|
|
|
}); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* If has display callbacks. |
592
|
|
|
* |
593
|
|
|
* @return bool |
594
|
|
|
*/ |
595
|
|
|
protected function hasDisplayCallbacks() |
596
|
|
|
{ |
597
|
|
|
return !empty($this->displayCallbacks); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Call all of the "display" callbacks column. |
602
|
|
|
* |
603
|
|
|
* @param mixed $value |
604
|
|
|
* @param int $key |
605
|
|
|
* |
606
|
|
|
* @return mixed |
607
|
|
|
*/ |
608
|
|
|
protected function callDisplayCallbacks($value, $key) |
609
|
|
|
{ |
610
|
|
|
foreach ($this->displayCallbacks as $callback) { |
611
|
|
|
$previous = $value; |
612
|
|
|
|
613
|
|
|
$callback = $this->bindOriginalRowModel($callback, $key); |
614
|
|
|
$value = call_user_func_array($callback, [$value, $this]); |
615
|
|
|
|
616
|
|
|
if (($value instanceof static) && |
617
|
|
|
($last = array_pop($this->displayCallbacks)) |
618
|
|
|
) { |
619
|
|
|
$last = $this->bindOriginalRowModel($last, $key); |
620
|
|
|
$value = call_user_func($last, $previous); |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
return $value; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Set original grid data to column. |
629
|
|
|
* |
630
|
|
|
* @param Closure $callback |
631
|
|
|
* @param int $key |
632
|
|
|
* |
633
|
|
|
* @return Closure |
634
|
|
|
*/ |
635
|
|
|
protected function bindOriginalRowModel(Closure $callback, $key) |
636
|
|
|
{ |
637
|
|
|
$rowModel = static::$originalGridModels[$key]; |
638
|
|
|
|
639
|
|
|
return $callback->bindTo($rowModel); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Fill all data to every column. |
644
|
|
|
* |
645
|
|
|
* @param array $data |
646
|
|
|
* |
647
|
|
|
* @return mixed |
648
|
|
|
*/ |
649
|
|
|
public function fill(array $data) |
650
|
|
|
{ |
651
|
|
|
foreach ($data as $key => &$row) { |
652
|
|
|
$this->original = $value = Arr::get($row, $this->name); |
653
|
|
|
|
654
|
|
|
$value = $this->htmlEntityEncode($value); |
655
|
|
|
|
656
|
|
|
Arr::set($row, $this->name, $value); |
657
|
|
|
|
658
|
|
|
if ($this->isDefinedColumn()) { |
659
|
|
|
$this->useDefinedColumn(); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
if ($this->hasDisplayCallbacks()) { |
663
|
|
|
$value = $this->callDisplayCallbacks($this->original, $key); |
664
|
|
|
Arr::set($row, $this->name, $value); |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
return $data; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* If current column is a defined column. |
673
|
|
|
* |
674
|
|
|
* @return bool |
675
|
|
|
*/ |
676
|
|
|
protected function isDefinedColumn() |
677
|
|
|
{ |
678
|
|
|
return array_key_exists($this->name, static::$defined); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Use a defined column. |
683
|
|
|
* |
684
|
|
|
* @throws \Exception |
685
|
|
|
*/ |
686
|
|
|
protected function useDefinedColumn() |
687
|
|
|
{ |
688
|
|
|
// clear all display callbacks. |
689
|
|
|
$this->displayCallbacks = []; |
690
|
|
|
|
691
|
|
|
$class = static::$defined[$this->name]; |
692
|
|
|
|
693
|
|
|
if ($class instanceof Closure) { |
694
|
|
|
$this->display($class); |
695
|
|
|
|
696
|
|
|
return; |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
if (!class_exists($class) || !is_subclass_of($class, AbstractDisplayer::class)) { |
|
|
|
|
700
|
|
|
throw new \Exception("Invalid column definition [$class]"); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
$grid = $this->grid; |
704
|
|
|
$column = $this; |
705
|
|
|
|
706
|
|
|
$this->display(function ($value) use ($grid, $column, $class) { |
707
|
|
|
/** @var AbstractDisplayer $definition */ |
708
|
|
|
$definition = new $class($value, $grid, $column, $this); |
709
|
|
|
|
710
|
|
|
return $definition->display(); |
711
|
|
|
}); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* Convert characters to HTML entities recursively. |
716
|
|
|
* |
717
|
|
|
* @param array|string $item |
718
|
|
|
* |
719
|
|
|
* @return mixed |
720
|
|
|
*/ |
721
|
|
|
protected function htmlEntityEncode($item) |
722
|
|
|
{ |
723
|
|
|
if (is_array($item)) { |
724
|
|
|
array_walk_recursive($item, function (&$value) { |
725
|
|
|
$value = htmlentities($value); |
726
|
|
|
}); |
727
|
|
|
} else { |
728
|
|
|
$item = htmlentities($item); |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
return $item; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* Find a displayer to display column. |
736
|
|
|
* |
737
|
|
|
* @param string $abstract |
738
|
|
|
* @param array $arguments |
739
|
|
|
* |
740
|
|
|
* @return $this |
741
|
|
|
*/ |
742
|
|
|
protected function resolveDisplayer($abstract, $arguments) |
743
|
|
|
{ |
744
|
|
|
if (array_key_exists($abstract, static::$displayers)) { |
745
|
|
|
return $this->callBuiltinDisplayer(static::$displayers[$abstract], $arguments); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
return $this->callSupportDisplayer($abstract, $arguments); |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Call Illuminate/Support displayer. |
753
|
|
|
* |
754
|
|
|
* @param string $abstract |
755
|
|
|
* @param array $arguments |
756
|
|
|
* |
757
|
|
|
* @return $this |
758
|
|
|
*/ |
759
|
|
|
protected function callSupportDisplayer($abstract, $arguments) |
760
|
|
|
{ |
761
|
|
|
return $this->display(function ($value) use ($abstract, $arguments) { |
762
|
|
|
if (is_array($value) || $value instanceof Arrayable) { |
763
|
|
|
return call_user_func_array([collect($value), $abstract], $arguments); |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
if (is_string($value)) { |
767
|
|
|
return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments)); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
return $value; |
771
|
|
|
}); |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* Call Builtin displayer. |
776
|
|
|
* |
777
|
|
|
* @param string $abstract |
778
|
|
|
* @param array $arguments |
779
|
|
|
* |
780
|
|
|
* @return $this |
781
|
|
|
*/ |
782
|
|
|
protected function callBuiltinDisplayer($abstract, $arguments) |
783
|
|
|
{ |
784
|
|
|
if ($abstract instanceof Closure) { |
785
|
|
|
return $this->display(function ($value) use ($abstract, $arguments) { |
786
|
|
|
return $abstract->call($this, ...array_merge([$value], $arguments)); |
787
|
|
|
}); |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
if (class_exists($abstract) && is_subclass_of($abstract, AbstractDisplayer::class)) { |
|
|
|
|
791
|
|
|
$grid = $this->grid; |
792
|
|
|
$column = $this; |
793
|
|
|
|
794
|
|
View Code Duplication |
return $this->display(function ($value) use ($abstract, $grid, $column, $arguments) { |
|
|
|
|
795
|
|
|
/** @var AbstractDisplayer $displayer */ |
796
|
|
|
$displayer = new $abstract($value, $grid, $column, $this); |
797
|
|
|
|
798
|
|
|
return $displayer->display(...$arguments); |
|
|
|
|
799
|
|
|
}); |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
return $this; |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* Passes through all unknown calls to builtin displayer or supported displayer. |
807
|
|
|
* |
808
|
|
|
* Allow fluent calls on the Column object. |
809
|
|
|
* |
810
|
|
|
* @param string $method |
811
|
|
|
* @param array $arguments |
812
|
|
|
* |
813
|
|
|
* @return $this |
814
|
|
|
*/ |
815
|
|
|
public function __call($method, $arguments) |
816
|
|
|
{ |
817
|
|
|
if ($this->isRelation() && !$this->relationColumn) { |
818
|
|
|
$this->name = "{$this->relation}.$method"; |
819
|
|
|
$this->label = $this->formatLabel($arguments[0] ?? null); |
820
|
|
|
|
821
|
|
|
$this->relationColumn = $method; |
822
|
|
|
|
823
|
|
|
return $this; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
return $this->resolveDisplayer($method, $arguments); |
827
|
|
|
} |
828
|
|
|
} |
829
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: