1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Extension; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Contracts\Support\Renderable; |
7
|
|
|
use SleepingOwl\Admin\Display\Column\Control; |
8
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
9
|
|
|
use SleepingOwl\Admin\Contracts\ColumnInterface; |
10
|
|
|
|
11
|
|
|
class Columns extends Extension implements Initializable, Renderable |
12
|
|
|
{ |
13
|
|
|
use \SleepingOwl\Admin\Traits\Renderable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ColumnInterface[]|Collection |
17
|
|
|
*/ |
18
|
|
|
protected $columns; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $controlActive = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $view = 'display.extensions.columns'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $initialize = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Control |
37
|
|
|
*/ |
38
|
|
|
protected $controlColumn; |
39
|
|
|
|
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->columns = new Collection(); |
43
|
|
|
|
44
|
|
|
$this->setControlColumn(app('sleeping_owl.table.column')->control()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ColumnInterface $controlColumn |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setControlColumn(ColumnInterface $controlColumn) |
53
|
|
|
{ |
54
|
|
|
$this->controlColumn = $controlColumn; |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Control |
61
|
|
|
*/ |
62
|
|
|
public function getControlColumn() |
63
|
|
|
{ |
64
|
|
|
return $this->controlColumn; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function isControlActive() |
71
|
|
|
{ |
72
|
|
|
return $this->controlActive; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function enableControls() |
79
|
|
|
{ |
80
|
|
|
$this->controlActive = true; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function disableControls() |
89
|
|
|
{ |
90
|
|
|
$this->controlActive = false; |
91
|
|
|
|
92
|
|
|
if ($this->isInitialize()) { |
93
|
|
|
$this->columns = $this->columns->filter(function ($column) { |
94
|
|
|
$class = get_class($this->getControlColumn()); |
95
|
|
|
|
96
|
|
|
return ! ($column instanceof $class); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Collection|\SleepingOwl\Admin\Contracts\ColumnInterface[] |
105
|
|
|
*/ |
106
|
|
|
public function all() |
107
|
|
|
{ |
108
|
|
|
return $this->columns; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $columns |
113
|
|
|
* |
114
|
|
|
* @return \SleepingOwl\Admin\Contracts\DisplayInterface |
115
|
|
|
*/ |
116
|
|
|
public function set($columns) |
117
|
|
|
{ |
118
|
|
|
if (! is_array($columns)) { |
119
|
|
|
$columns = func_get_args(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($columns as $column) { |
123
|
|
|
$this->push($column); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->getDisplay(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param ColumnInterface $column |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function push(ColumnInterface $column) |
135
|
|
|
{ |
136
|
|
|
$this->columns->push($column); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isInitialize() |
145
|
|
|
{ |
146
|
|
|
return $this->initialize; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the instance as an array. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function toArray() |
155
|
|
|
{ |
156
|
|
|
return [ |
157
|
|
|
'columns' => $this->all(), |
158
|
|
|
'attributes' => $this->getDisplay()->htmlAttributesToString(), |
|
|
|
|
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function initialize() |
163
|
|
|
{ |
164
|
|
|
$this->all()->each(function (ColumnInterface $column) { |
165
|
|
|
$column->initialize(); |
166
|
|
|
}); |
167
|
|
|
|
168
|
|
|
if ($this->isControlActive()) { |
169
|
|
|
$this->push($this->getControlColumn()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->initialize = true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the evaluated contents of the object. |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function render() |
181
|
|
|
{ |
182
|
|
|
$params = $this->toArray(); |
183
|
|
|
$params['collection'] = $this->getDisplay()->getCollection(); |
|
|
|
|
184
|
|
|
$params['pagination'] = null; |
185
|
|
|
|
186
|
|
|
if ($params['collection'] instanceof \Illuminate\Contracts\Pagination\Paginator) { |
187
|
|
|
if (class_exists('Illuminate\Pagination\BootstrapThreePresenter')) { |
188
|
|
|
$params['pagination'] = (new \Illuminate\Pagination\BootstrapThreePresenter($params['collection']))->render(); |
189
|
|
|
} else { |
190
|
|
|
$params['pagination'] = $params['collection']->render(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return app('sleeping_owl.template')->view($this->getView(), $params)->render(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.