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