1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
8
|
|
|
use Illuminate\Contracts\Validation\Validator; |
9
|
|
|
use SleepingOwl\Admin\Contracts\FormInterface; |
10
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
11
|
|
|
use SleepingOwl\Admin\Traits\VisibleCondition; |
12
|
|
|
use SleepingOwl\Admin\Contracts\DisplayInterface; |
13
|
|
|
use SleepingOwl\Admin\Contracts\Display\TabInterface; |
14
|
|
|
use SleepingOwl\Admin\Contracts\FormElementInterface; |
15
|
|
|
use SleepingOwl\Admin\Contracts\Form\ElementsInterface; |
16
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
17
|
|
|
|
18
|
|
|
class DisplayTab implements TabInterface, DisplayInterface, FormInterface |
19
|
|
|
{ |
20
|
|
|
use VisibleCondition; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $label = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $active = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $icon; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Renderable |
44
|
|
|
*/ |
45
|
|
|
protected $content; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $view = 'display.tab'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Renderable $content |
54
|
|
|
* @param string|null $label |
55
|
|
|
* @param string|null $icon |
56
|
|
|
*/ |
57
|
|
|
public function __construct(Renderable $content, $label = null, $icon = null) |
58
|
|
|
{ |
59
|
|
|
$this->content = $content; |
60
|
|
|
|
61
|
|
|
if (! is_null($label)) { |
62
|
|
|
$this->setLabel($label); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (! is_null($icon)) { |
66
|
|
|
$this->setIcon($icon); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getLabel() |
74
|
|
|
{ |
75
|
|
|
return $this->label; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $label |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setLabel($label) |
84
|
|
|
{ |
85
|
|
|
$this->label = $label; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isActive() |
94
|
|
|
{ |
95
|
|
|
return $this->active; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param bool $active |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function setActive($active = true) |
104
|
|
|
{ |
105
|
|
|
$this->active = (bool) $active; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getName() |
114
|
|
|
{ |
115
|
|
|
return is_null($this->name) |
116
|
|
|
? md5($this->getLabel()) |
117
|
|
|
: $this->name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setName($name) |
126
|
|
|
{ |
127
|
|
|
$this->name = $name; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getIcon() |
136
|
|
|
{ |
137
|
|
|
return $this->icon; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $icon |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setIcon($icon) |
146
|
|
|
{ |
147
|
|
|
$this->icon = $icon; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return Renderable |
154
|
|
|
*/ |
155
|
|
|
public function getContent() |
156
|
|
|
{ |
157
|
|
|
return $this->content; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $class |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function setModelClass($class) |
166
|
|
|
{ |
167
|
|
|
if ($this->getContent() instanceof DisplayInterface) { |
168
|
|
|
$this->getContent()->setModelClass($class); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Initialize tab. |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function initialize() |
180
|
|
|
{ |
181
|
|
|
if ($this->getContent() instanceof Initializable) { |
182
|
|
|
$this->getContent()->initialize(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $action |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function setAction($action) |
194
|
|
|
{ |
195
|
|
|
if ($this->getContent() instanceof FormInterface) { |
196
|
|
|
$this->getContent()->setAction($action); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param int $id |
204
|
|
|
* |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function setId($id) |
208
|
|
|
{ |
209
|
|
|
if ($this->getContent() instanceof FormInterface) { |
210
|
|
|
$this->getContent()->setId($id); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param ModelConfigurationInterface $model |
218
|
|
|
* |
219
|
|
|
* @return Validator|null |
220
|
|
|
*/ |
221
|
|
|
public function validateForm(ModelConfigurationInterface $model) |
222
|
|
|
{ |
223
|
|
|
if ($this->getContent() instanceof FormInterface) { |
224
|
|
|
return $this->getContent()->validateForm($model); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Save model. |
230
|
|
|
* |
231
|
|
|
* @param ModelConfigurationInterface $model |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function saveForm(ModelConfigurationInterface $model) |
236
|
|
|
{ |
237
|
|
|
if ($this->getContent() instanceof FormInterface) { |
238
|
|
|
$this->getContent()->saveForm($model); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function getView() |
248
|
|
|
{ |
249
|
|
|
return $this->view; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set currently rendered instance. |
254
|
|
|
* |
255
|
|
|
* @param Model $model |
256
|
|
|
*/ |
257
|
|
|
public function setModel(Model $model) |
258
|
|
|
{ |
259
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
260
|
|
|
$content->setModel($model); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return Model $model |
266
|
|
|
*/ |
267
|
|
|
public function getModel() |
268
|
|
|
{ |
269
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
270
|
|
|
return $content->getModel(); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get form item validation rules. |
276
|
|
|
* @return array |
277
|
|
|
*/ |
278
|
|
|
public function getValidationRules() |
279
|
|
|
{ |
280
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
281
|
|
|
return $content->getValidationRules(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return []; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
|
|
public function getValidationMessages() |
291
|
|
|
{ |
292
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
293
|
|
|
return $content->getValidationMessages(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return []; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
|
|
public function getValidationLabels() |
303
|
|
|
{ |
304
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
305
|
|
|
return $content->getValidationLabels(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return []; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Save form item. |
313
|
|
|
*/ |
314
|
|
|
public function save() |
315
|
|
|
{ |
316
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
317
|
|
|
$content->save(); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Save form item. |
323
|
|
|
*/ |
324
|
|
|
public function afterSave() |
325
|
|
|
{ |
326
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
327
|
|
|
$content->afterSave(); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param string $path |
333
|
|
|
* |
334
|
|
|
* @return FormElementInterface|null |
335
|
|
|
*/ |
336
|
|
|
public function getElement($path) |
337
|
|
|
{ |
338
|
|
|
if ($content = $this->getContent() instanceof ElementsInterface) { |
339
|
|
|
return $content->getElement($path); |
|
|
|
|
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @return Collection |
345
|
|
|
*/ |
346
|
|
|
public function getElements() |
347
|
|
|
{ |
348
|
|
|
if ($content = $this->getContent() instanceof ElementsInterface) { |
349
|
|
|
return $content->getElements(); |
|
|
|
|
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param array $elements |
355
|
|
|
* |
356
|
|
|
* @return $this |
357
|
|
|
*/ |
358
|
|
|
public function setElements(array $elements) |
359
|
|
|
{ |
360
|
|
|
if ($content = $this->getContent() instanceof ElementsInterface) { |
361
|
|
|
return $content->setElements($elements); |
|
|
|
|
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @return mixed |
367
|
|
|
*/ |
368
|
|
|
public function getValue() |
369
|
|
|
{ |
370
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
371
|
|
|
return $content->getValue(); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return bool |
377
|
|
|
*/ |
378
|
|
|
public function isReadonly() |
379
|
|
|
{ |
380
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
381
|
|
|
return $content->isReadonly(); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return false; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return array |
389
|
|
|
*/ |
390
|
|
|
public function toArray() |
391
|
|
|
{ |
392
|
|
|
return [ |
393
|
|
|
'label' => $this->getLabel(), |
394
|
|
|
'active' => $this->isActive(), |
395
|
|
|
'name' => $this->getName(), |
396
|
|
|
'icon' => $this->getIcon(), |
397
|
|
|
]; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
402
|
|
|
*/ |
403
|
|
|
public function render() |
404
|
|
|
{ |
405
|
|
|
return app('sleeping_owl.template')->view($this->getView(), $this->toArray()); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @return string |
410
|
|
|
*/ |
411
|
|
|
public function __toString() |
412
|
|
|
{ |
413
|
|
|
return (string) $this->render(); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.