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