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