1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use SleepingOwl\Admin\Form\FormPanel; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use SleepingOwl\Admin\Form\FormDefault; |
8
|
|
|
use SleepingOwl\Admin\Navigation\Badge; |
9
|
|
|
use SleepingOwl\Admin\Form\FormElements; |
10
|
|
|
use KodiComponents\Support\HtmlAttributes; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Validable; |
12
|
|
|
use SleepingOwl\Admin\Form\Columns\Column; |
13
|
|
|
use SleepingOwl\Admin\Form\Element\Hidden; |
14
|
|
|
use SleepingOwl\Admin\Form\Columns\Columns; |
15
|
|
|
use Illuminate\Contracts\Support\Renderable; |
16
|
|
|
use Illuminate\Validation\ValidationException; |
17
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
18
|
|
|
use SleepingOwl\Admin\Traits\VisibleCondition; |
19
|
|
|
use SleepingOwl\Admin\Form\FormElementsCollection; |
20
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormInterface; |
21
|
|
|
use SleepingOwl\Admin\Contracts\WithModelInterface; |
22
|
|
|
use SleepingOwl\Admin\Contracts\Display\TabInterface; |
23
|
|
|
use SleepingOwl\Admin\Contracts\Form\ElementsInterface; |
24
|
|
|
use SleepingOwl\Admin\Contracts\Display\DisplayInterface; |
25
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormElementInterface; |
26
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
27
|
|
|
use SleepingOwl\Admin\Exceptions\Display\DisplayTabException; |
28
|
|
|
|
29
|
|
|
class DisplayTab implements TabInterface, DisplayInterface, FormInterface |
30
|
|
|
{ |
31
|
|
|
use VisibleCondition, \SleepingOwl\Admin\Traits\Renderable, HtmlAttributes; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $label; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $active = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $name; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $icon; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Renderable |
55
|
|
|
*/ |
56
|
|
|
protected $content; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var |
60
|
|
|
*/ |
61
|
|
|
protected $badge; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $view = 'display.tab'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Renderable $content |
70
|
|
|
* @param string|null $label |
71
|
|
|
* @param string|null $icon |
72
|
|
|
* @param Badge|string|\Closure|null $badge |
73
|
|
|
*/ |
74
|
27 |
|
public function __construct(Renderable $content, $label = null, $icon = null, $badge = null) |
75
|
|
|
{ |
76
|
27 |
|
$this->content = $content; |
77
|
|
|
|
78
|
27 |
|
if (! is_null($label)) { |
79
|
1 |
|
$this->setLabel($label); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
27 |
|
if (! is_null($icon)) { |
83
|
1 |
|
$this->setIcon($icon); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
27 |
|
if (! is_null($badge)) { |
87
|
|
|
$this->setBadge($badge); |
88
|
|
|
} |
89
|
|
|
|
90
|
27 |
|
$this->setHtmlAttribute('role', 'presentation'); |
91
|
27 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Badge|string|\Closure|null $badge |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setBadge($badge) |
98
|
|
|
{ |
99
|
|
|
$badgeData = null; |
100
|
|
|
|
101
|
|
|
if (is_string($badge) || is_callable($badge) || is_numeric($badge)) { |
102
|
|
|
$badgeData = new Badge(); |
103
|
|
|
$badgeData->setView('_partials.tabs.badge'); |
104
|
|
|
$badgeData->setValue($badge); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->badge = $badgeData; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
1 |
|
public function getBadge() |
116
|
|
|
{ |
117
|
1 |
|
return $this->badge; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
4 |
|
public function getLabel() |
124
|
|
|
{ |
125
|
4 |
|
return $this->label; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $label |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
3 |
|
public function setLabel($label) |
134
|
|
|
{ |
135
|
3 |
|
$this->label = $label; |
136
|
|
|
|
137
|
3 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
2 |
|
public function isActive() |
144
|
|
|
{ |
145
|
2 |
|
return $this->active; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param bool $active |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
1 |
|
public function setActive($active = true) |
154
|
|
|
{ |
155
|
1 |
|
$this->active = (bool) $active; |
156
|
|
|
|
157
|
1 |
|
if ($active) { |
158
|
1 |
|
$this->setHtmlAttribute('class', 'active'); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
1 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function addTabElement() |
168
|
|
|
{ |
169
|
|
|
if ($this->content instanceof FormInterface) { |
170
|
|
|
$this->content->addElement( |
|
|
|
|
171
|
|
|
new FormElements([ |
172
|
|
|
(new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
173
|
|
|
]) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($this->content instanceof FormElements) { |
178
|
|
|
foreach ($this->content->getElements() as $element) { |
179
|
|
|
if ($element instanceof FormDefault && $element instanceof FormPanel) { |
180
|
|
|
$element->addElement( |
181
|
|
|
new FormElements([ |
182
|
|
|
(new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
183
|
|
|
]) |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
View Code Duplication |
if ($element instanceof FormElements) { |
|
|
|
|
188
|
|
|
foreach ($element->getElements() as $subElement) { |
189
|
|
|
if ($subElement instanceof FormDefault) { |
190
|
|
|
$subElement->addElement( |
191
|
|
|
new FormElements([ |
192
|
|
|
(new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
193
|
|
|
]) |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($element instanceof Columns) { |
200
|
|
|
foreach ($element->getElements() as $column) { |
201
|
|
View Code Duplication |
if ($column instanceof Column) { |
|
|
|
|
202
|
|
|
foreach ($column->getElements() as $columnElement) { |
203
|
|
|
if ($columnElement instanceof FormInterface) { |
204
|
|
|
$columnElement->addElement( |
|
|
|
|
205
|
|
|
new FormElements([ |
206
|
|
|
(new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
207
|
|
|
]) |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return string |
222
|
|
|
* @throws DisplayTabException |
223
|
|
|
*/ |
224
|
3 |
|
public function getName() |
225
|
|
|
{ |
226
|
3 |
|
if (is_null($this->name) && is_null($this->getLabel())) { |
227
|
1 |
|
throw new DisplayTabException('You should set name or label'); |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
return is_null($this->name) |
231
|
2 |
|
? md5($this->getLabel()) |
232
|
2 |
|
: $this->name; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $name |
237
|
|
|
* |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
1 |
|
public function setName($name) |
241
|
|
|
{ |
242
|
1 |
|
$this->name = $name; |
243
|
|
|
|
244
|
1 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
2 |
|
public function getIcon() |
251
|
|
|
{ |
252
|
2 |
|
return $this->icon; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $icon |
257
|
|
|
* |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
1 |
|
public function setIcon($icon) |
261
|
|
|
{ |
262
|
1 |
|
$this->icon = $icon; |
263
|
|
|
|
264
|
1 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return Renderable |
269
|
|
|
*/ |
270
|
19 |
|
public function getContent() |
271
|
|
|
{ |
272
|
19 |
|
return $this->content; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param string $class |
277
|
|
|
* |
278
|
|
|
* @return $this |
279
|
|
|
*/ |
280
|
2 |
|
public function setModelClass($class) |
281
|
|
|
{ |
282
|
2 |
|
if (($content = $this->getContent()) instanceof DisplayInterface) { |
283
|
1 |
|
$content->setModelClass($class); |
284
|
1 |
|
} |
285
|
|
|
|
286
|
2 |
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Initialize tab. |
291
|
|
|
* |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
2 |
|
public function initialize() |
295
|
|
|
{ |
296
|
2 |
|
if (($content = $this->getContent()) instanceof Initializable) { |
297
|
1 |
|
$content->initialize(); |
298
|
1 |
|
} |
299
|
|
|
|
300
|
2 |
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param string $action |
305
|
|
|
* |
306
|
|
|
* @return $this |
307
|
|
|
*/ |
308
|
2 |
|
public function setAction($action) |
309
|
|
|
{ |
310
|
2 |
|
if (($content = $this->getContent()) instanceof FormInterface) { |
311
|
1 |
|
$content->setAction($action); |
312
|
1 |
|
} |
313
|
|
|
|
314
|
2 |
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param int $id |
319
|
|
|
* |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
2 |
|
public function setId($id) |
323
|
|
|
{ |
324
|
2 |
|
if (($content = $this->getContent()) instanceof FormInterface) { |
325
|
1 |
|
$content->setId($id); |
326
|
1 |
|
} |
327
|
|
|
|
328
|
2 |
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param \Illuminate\Http\Request $request |
333
|
|
|
* @param ModelConfigurationInterface $model |
334
|
|
|
* |
335
|
|
|
* @throws ValidationException |
336
|
|
|
*/ |
337
|
2 |
|
public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
338
|
|
|
{ |
339
|
2 |
|
if (($content = $this->getContent()) instanceof FormInterface) { |
340
|
1 |
|
$content->validateForm($request, $model); |
341
|
1 |
|
} |
342
|
2 |
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Save model. |
346
|
|
|
* |
347
|
|
|
* @param \Illuminate\Http\Request $request |
348
|
|
|
* @param ModelConfigurationInterface $model |
349
|
|
|
* |
350
|
|
|
* @return void |
351
|
|
|
*/ |
352
|
2 |
|
public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
353
|
|
|
{ |
354
|
2 |
|
if (($content = $this->getContent()) instanceof FormInterface) { |
355
|
1 |
|
$content->saveForm($request, $model); |
356
|
1 |
|
} |
357
|
2 |
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Set currently rendered instance. |
361
|
|
|
* |
362
|
|
|
* @param Model $model |
363
|
|
|
* |
364
|
|
|
* @return $this |
365
|
|
|
*/ |
366
|
2 |
|
public function setModel(Model $model) |
367
|
|
|
{ |
368
|
2 |
|
if (($content = $this->getContent()) instanceof WithModelInterface) { |
369
|
1 |
|
$content->setModel($model); |
370
|
1 |
|
} |
371
|
|
|
|
372
|
2 |
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return Model $model |
377
|
|
|
*/ |
378
|
2 |
|
public function getModel() |
379
|
|
|
{ |
380
|
2 |
|
if (($content = $this->getContent()) instanceof WithModelInterface) { |
381
|
1 |
|
return $content->getModel(); |
382
|
|
|
} |
383
|
1 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Get form item validation rules. |
387
|
|
|
* @return array |
388
|
|
|
*/ |
389
|
2 |
|
public function getValidationRules() |
390
|
|
|
{ |
391
|
2 |
|
if (($content = $this->getContent()) instanceof Validable) { |
392
|
1 |
|
return $content->getValidationRules(); |
393
|
|
|
} |
394
|
|
|
|
395
|
1 |
|
return []; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return array |
400
|
|
|
*/ |
401
|
2 |
|
public function getValidationMessages() |
402
|
|
|
{ |
403
|
2 |
|
if (($content = $this->getContent()) instanceof Validable) { |
404
|
1 |
|
return $content->getValidationMessages(); |
405
|
|
|
} |
406
|
|
|
|
407
|
1 |
|
return []; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @return array |
412
|
|
|
*/ |
413
|
2 |
|
public function getValidationLabels() |
414
|
|
|
{ |
415
|
2 |
|
if (($content = $this->getContent()) instanceof Validable) { |
416
|
1 |
|
return $content->getValidationLabels(); |
417
|
|
|
} |
418
|
|
|
|
419
|
1 |
|
return []; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param \Illuminate\Http\Request $request |
424
|
|
|
* |
425
|
|
|
* @return void |
426
|
|
|
*/ |
427
|
2 |
|
public function save(\Illuminate\Http\Request $request) |
428
|
|
|
{ |
429
|
2 |
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
430
|
1 |
|
$content->save($request); |
431
|
1 |
|
} |
432
|
2 |
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @param \Illuminate\Http\Request $request |
436
|
|
|
* |
437
|
|
|
* @return void |
438
|
|
|
*/ |
439
|
2 |
|
public function afterSave(\Illuminate\Http\Request $request) |
440
|
|
|
{ |
441
|
2 |
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
442
|
1 |
|
$content->afterSave($request); |
443
|
1 |
|
} |
444
|
2 |
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @return mixed |
448
|
|
|
*/ |
449
|
2 |
|
public function getValue() |
450
|
|
|
{ |
451
|
2 |
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
452
|
1 |
|
return $content->getValue(); |
453
|
|
|
} |
454
|
1 |
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* @return bool |
458
|
|
|
*/ |
459
|
2 |
|
public function isReadonly() |
460
|
|
|
{ |
461
|
2 |
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
462
|
1 |
|
return $content->isReadonly(); |
463
|
|
|
} |
464
|
|
|
|
465
|
1 |
|
return false; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return bool |
470
|
|
|
*/ |
471
|
|
|
public function isValueSkipped() |
472
|
|
|
{ |
473
|
|
|
if (($content = $this->getContent()) instanceof FormElementInterface) { |
474
|
|
|
return $content->isValueSkipped(); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return false; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @param string $path |
482
|
|
|
* |
483
|
|
|
* @return FormElementInterface|null |
484
|
|
|
*/ |
485
|
2 |
|
public function getElement($path) |
486
|
|
|
{ |
487
|
2 |
|
if (($content = $this->getContent()) instanceof ElementsInterface) { |
488
|
1 |
|
return $content->getElement($path); |
489
|
|
|
} |
490
|
1 |
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @return FormElementsCollection |
494
|
|
|
*/ |
495
|
2 |
|
public function getElements() |
496
|
|
|
{ |
497
|
2 |
|
if (($content = $this->getContent()) instanceof ElementsInterface) { |
498
|
1 |
|
return $content->getElements(); |
499
|
|
|
} |
500
|
|
|
|
501
|
1 |
|
return new FormElementsCollection(); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* @param array $elements |
506
|
|
|
* |
507
|
|
|
* @return $this |
508
|
|
|
*/ |
509
|
2 |
|
public function setElements(array $elements) |
510
|
|
|
{ |
511
|
2 |
|
if (($content = $this->getContent()) instanceof ElementsInterface) { |
512
|
1 |
|
$content->setElements($elements); |
513
|
1 |
|
} |
514
|
|
|
|
515
|
2 |
|
return $this; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @return array |
520
|
|
|
*/ |
521
|
1 |
|
public function toArray() |
522
|
|
|
{ |
523
|
|
|
return [ |
524
|
1 |
|
'label' => $this->getLabel(), |
525
|
1 |
|
'active' => $this->isActive(), |
526
|
1 |
|
'name' => $this->getName(), |
527
|
1 |
|
'icon' => $this->getIcon(), |
528
|
1 |
|
'badge' => $this->getBadge(), |
529
|
1 |
|
'arrayAttributes' => $this->getHtmlAttributes(), |
530
|
1 |
|
'attributes' => $this->htmlAttributesToString(), |
531
|
1 |
|
]; |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: