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