1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Traits\Module; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Terranet\Administrator\Collection\Group; |
7
|
|
|
use Terranet\Administrator\Collection\Mutable as MutableCollection; |
8
|
|
|
use Terranet\Administrator\Dashboard\Manager; |
9
|
|
|
use Terranet\Administrator\Decorators\Grid as GridDecorator; |
10
|
|
|
use Terranet\Administrator\Form\Collection\Mutable; |
11
|
|
|
use Terranet\Translatable\Translatable; |
12
|
|
|
|
13
|
|
|
trait HasColumns |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Fetch scaffold columns. |
17
|
|
|
* |
18
|
|
|
* @return MutableCollection |
19
|
|
|
*/ |
20
|
|
|
public function columns() |
21
|
|
|
{ |
22
|
|
|
return $this->scaffoldColumns(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Define the list of attributes visible on View model page. |
27
|
|
|
* |
28
|
|
|
* @param Model $model |
29
|
|
|
* |
30
|
|
|
* @return Mutable |
31
|
|
|
*/ |
32
|
|
|
public function viewColumns() |
33
|
|
|
{ |
34
|
|
|
return $this->scaffoldColumns(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Manager $widgets |
39
|
|
|
* |
40
|
|
|
* @return Manager |
41
|
|
|
*/ |
42
|
|
|
public function widgets(Manager $widgets): Manager |
43
|
|
|
{ |
44
|
|
|
return $widgets; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Manager $cards |
49
|
|
|
* |
50
|
|
|
* @return Manager |
51
|
|
|
*/ |
52
|
|
|
public function cards(Manager $cards): Manager |
53
|
|
|
{ |
54
|
|
|
return $cards; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Scaffold columns. |
59
|
|
|
* |
60
|
|
|
* @return MutableCollection |
61
|
|
|
*/ |
62
|
|
|
protected function scaffoldColumns() |
63
|
|
|
{ |
64
|
|
|
return $this->collectColumns( |
65
|
|
|
$this->model() |
|
|
|
|
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $model |
71
|
|
|
* |
72
|
|
|
* @return MutableCollection |
73
|
|
|
*/ |
74
|
|
|
protected function collectColumns(Model $model = null) |
75
|
|
|
{ |
76
|
|
|
if (!$model) { |
77
|
|
|
return new MutableCollection([]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$pk = $model->getKeyName(); |
81
|
|
|
|
82
|
|
|
$fillable = array_merge( |
83
|
|
|
\is_array($pk) ? $pk : [$pk], |
|
|
|
|
84
|
|
|
$model->getFillable() |
85
|
|
|
); |
86
|
|
|
$hidden = $model->getHidden(); |
87
|
|
|
|
88
|
|
|
if ($model instanceof Translatable && method_exists($model, 'getTranslatedAttributes')) { |
89
|
|
|
$fillable = array_merge($fillable, $model->getTranslatedAttributes()); |
|
|
|
|
90
|
|
|
$hidden = array_merge($hidden, $model->getTranslationModel()->getHidden()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$fillable = array_unique(array_diff($fillable, $hidden)); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create collection. |
97
|
|
|
*/ |
98
|
|
|
$elements = new MutableCollection($fillable); |
99
|
|
|
|
100
|
|
|
if (property_exists($this, 'includeDateColumns') && $this->includeDateColumns && \count($dates = $model->getDates())) { |
101
|
|
|
// allow setting specific timestamp: created_at |
102
|
|
|
if (\is_string($this->includeDateColumns)) { |
103
|
|
|
$dates = array_intersect($dates, [$this->includeDateColumns]); |
104
|
|
|
|
105
|
|
|
$elements = $elements->merge($dates); |
106
|
|
|
} else { |
107
|
|
|
// add timestamps group |
108
|
|
|
$elements->group('dates', function (Group $group) use ($dates) { |
109
|
|
|
$group->merge($dates); |
110
|
|
|
|
111
|
|
|
return $group; |
112
|
|
|
}); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $elements->build( |
117
|
|
|
new GridDecorator($model) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|