1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use SleepingOwl\Admin\Display\ControlLink; |
8
|
|
|
use SleepingOwl\Admin\Display\TableColumn; |
9
|
|
|
use SleepingOwl\Admin\Display\ControlButton; |
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\ControlButtonInterface; |
11
|
|
|
|
12
|
|
|
class Control extends TableColumn |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $view = 'column.control'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $width = '90px'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Collection |
26
|
|
|
*/ |
27
|
|
|
protected $buttons; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $editable = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $deletable = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $destroyable = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $restorable = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Control constructor. |
51
|
|
|
* |
52
|
|
|
* @param string|null $label |
53
|
|
|
*/ |
54
|
|
|
public function __construct($label = null) |
55
|
|
|
{ |
56
|
|
|
parent::__construct($label); |
57
|
|
|
|
58
|
|
|
$this->buttons = new Collection(); |
59
|
|
|
|
60
|
|
|
$this->setHtmlAttribute('class', 'text-right'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function initialize() |
64
|
|
|
{ |
65
|
|
|
parent::initialize(); |
66
|
|
|
|
67
|
|
|
$this->buttons->put('edit', $button = new ControlLink(function (Model $model) { |
68
|
|
|
return $this->getModelConfiguration()->getEditUrl($model->getKey()); |
69
|
|
|
}, trans('sleeping_owl::lang.table.edit'), 100)); |
|
|
|
|
70
|
|
|
$button->hideText(); |
71
|
|
|
$button->setCondition(function () { |
72
|
|
|
return $this->isEditable(); |
73
|
|
|
}); |
74
|
|
|
$button->setIcon('fa fa-pencil'); |
75
|
|
|
$button->setHtmlAttribute('class', 'btn-primary'); |
76
|
|
|
|
77
|
|
|
$this->buttons->put('delete', $button = new ControlButton(function (Model $model) { |
78
|
|
|
return $this->getModelConfiguration()->getDeleteUrl($model->getKey()); |
79
|
|
|
}, trans('sleeping_owl::lang.table.delete'), 200)); |
|
|
|
|
80
|
|
|
$button->setCondition(function () { |
81
|
|
|
return $this->isDeletable(); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$button->setMethod('delete'); |
85
|
|
|
$button->hideText(); |
86
|
|
|
$button->setIcon('fa fa-trash'); |
87
|
|
|
$button->setHtmlAttribute('class', 'btn-danger btn-delete'); |
88
|
|
|
|
89
|
|
|
$this->buttons->put('destroy', $button = new ControlButton(function (Model $model) { |
90
|
|
|
return $this->getModelConfiguration()->getDestroyUrl($model->getKey()); |
91
|
|
|
}, trans('sleeping_owl::lang.table.destroy'), 300)); |
|
|
|
|
92
|
|
|
$button->setCondition(function () { |
93
|
|
|
return $this->isDestroyable(); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$button->setMethod('delete'); |
97
|
|
|
$button->hideText(); |
98
|
|
|
$button->setIcon('fa fa-trash'); |
99
|
|
|
$button->setHtmlAttribute('class', 'btn-danger btn-destroy'); |
100
|
|
|
|
101
|
|
|
$this->buttons->put('restore', $button = new ControlButton(function (Model $model) { |
102
|
|
|
return $this->getModelConfiguration()->getRestoreUrl($model->getKey()); |
103
|
|
|
}, trans('sleeping_owl::lang.table.restore'), 400)); |
|
|
|
|
104
|
|
|
$button->setCondition(function () { |
105
|
|
|
return $this->isRestorable(); |
106
|
|
|
}); |
107
|
|
|
$button->hideText(); |
108
|
|
|
$button->setIcon('fa fa-reply'); |
109
|
|
|
$button->setHtmlAttribute('class', 'btn-warning'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param ControlButtonInterface $button |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function addButton(ControlButtonInterface $button) |
118
|
|
|
{ |
119
|
|
|
$this->buttons->push($button); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ControlButtonInterface $button |
|
|
|
|
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function addButtons(array $buttons) |
130
|
|
|
{ |
131
|
|
|
foreach ($buttons as $button) { |
132
|
|
|
if ($button instanceof ControlButtonInterface) { |
133
|
|
|
$this->buttons->push($button); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param bool $editable |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setEditable($editable) |
145
|
|
|
{ |
146
|
|
|
$this->editable = (bool) $editable; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param bool $deletable |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setDeletable($deletable) |
156
|
|
|
{ |
157
|
|
|
$this->deletable = (bool) $deletable; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param bool $destroyable |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function setDestroyable($destroyable) |
167
|
|
|
{ |
168
|
|
|
$this->destroyable = (bool) $destroyable; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param bool $restorable |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
|
public function setRestorable($restorable) |
178
|
|
|
{ |
179
|
|
|
$this->restorable = (bool) $restorable; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Check if instance supports soft-deletes and trashed. |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
protected function isTrashed() |
190
|
|
|
{ |
191
|
|
|
if (method_exists($this->getModel(), 'trashed')) { |
192
|
|
|
return $this->getModel()->trashed(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if instance editable. |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
protected function isEditable() |
204
|
|
|
{ |
205
|
|
|
return |
206
|
|
|
$this->editable |
207
|
|
|
&& |
208
|
|
|
! $this->isTrashed() |
209
|
|
|
&& |
210
|
|
|
$this->getModelConfiguration()->isEditable( |
211
|
|
|
$this->getModel() |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Check if instance is deletable. |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
protected function isDeletable() |
221
|
|
|
{ |
222
|
|
|
return |
223
|
|
|
$this->deletable |
224
|
|
|
&& |
225
|
|
|
! $this->isTrashed() |
226
|
|
|
&& |
227
|
|
|
$this->getModelConfiguration()->isDeletable( |
228
|
|
|
$this->getModel() |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Check if instance is force deletable. |
234
|
|
|
* |
235
|
|
|
* @return bool |
236
|
|
|
*/ |
237
|
|
|
protected function isDestroyable() |
238
|
|
|
{ |
239
|
|
|
return |
240
|
|
|
$this->destroyable |
241
|
|
|
&& |
242
|
|
|
$this->isTrashed() |
243
|
|
|
&& |
244
|
|
|
$this->getModelConfiguration()->isDestroyable( |
245
|
|
|
$this->getModel() |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Check if instance is restorable. |
251
|
|
|
* |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
protected function isRestorable() |
255
|
|
|
{ |
256
|
|
|
return |
257
|
|
|
$this->restorable |
258
|
|
|
&& |
259
|
|
|
$this->isTrashed() |
260
|
|
|
&& |
261
|
|
|
$this->getModelConfiguration()->isRestorable( |
262
|
|
|
$this->getModel() |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param Model $model |
268
|
|
|
* |
269
|
|
|
* @return $this |
270
|
|
|
*/ |
271
|
|
|
public function setModel(Model $model) |
272
|
|
|
{ |
273
|
|
|
parent::setModel($model); |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
public function toArray() |
282
|
|
|
{ |
283
|
|
|
return parent::toArray() + [ |
284
|
|
|
'buttons' => $this->buttons |
285
|
|
|
->each(function (ControlButtonInterface $button) { |
286
|
|
|
$button->setModel($this->getModel()); |
287
|
|
|
}) |
288
|
|
|
->filter(function (ControlButtonInterface $button) { |
289
|
|
|
return $button->isActive(); |
290
|
|
|
}) |
291
|
|
|
->sortBy(function (ControlButtonInterface $button) { |
292
|
|
|
return $button->getPosition(); |
293
|
|
|
}), |
294
|
|
|
]; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.