Total Complexity | 98 |
Total Lines | 1627 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GuiTableConfigurationBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GuiTableConfigurationBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class GuiTableConfigurationBuilder implements GuiTableConfigurationBuilderInterface |
||
42 | { |
||
43 | /** |
||
44 | * @see https://angular.io/api/common/DatePipe for details. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected const DEFAULT_UI_DATE_FORMAT = 'dd.MM.y'; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected const DEFAULT_COLUMN_BUTTON_ACTION_VARIANT = 'outline'; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected const DEFAULT_COLUMN_BUTTON_ACTION_SIZE = 'sm'; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected const DEFAULT_MODAL_OK_BUTTON_VARIANT = 'primary'; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected const DEFAULT_CHIP_MAX_WIDTH = '220px'; |
||
69 | |||
70 | /** |
||
71 | * @var array<\Generated\Shared\Transfer\GuiTableColumnConfigurationTransfer> |
||
72 | */ |
||
73 | protected array $columns = []; |
||
74 | |||
75 | /** |
||
76 | * @var array<\Generated\Shared\Transfer\GuiTableColumnConfigurationTransfer> |
||
77 | */ |
||
78 | protected array $editableColumns = []; |
||
79 | |||
80 | /** |
||
81 | * @var string|null |
||
82 | */ |
||
83 | protected ?string $title = null; |
||
84 | |||
85 | /** |
||
86 | * @var array<\Generated\Shared\Transfer\GuiTableFilterTransfer> |
||
87 | */ |
||
88 | protected array $filters = []; |
||
89 | |||
90 | /** |
||
91 | * @var array<\Generated\Shared\Transfer\GuiTableRowActionTransfer> |
||
92 | */ |
||
93 | protected array $rowActions = []; |
||
94 | |||
95 | /** |
||
96 | * @var string|null |
||
97 | */ |
||
98 | protected ?string $rowOnClickIdAction = null; |
||
99 | |||
100 | /** |
||
101 | * @var string|null |
||
102 | */ |
||
103 | protected ?string $rowActionRowIdPath = null; |
||
104 | |||
105 | /** |
||
106 | * @var string|null |
||
107 | */ |
||
108 | protected ?string $availableRowActionsPath = null; |
||
109 | |||
110 | /** |
||
111 | * @var array<\Generated\Shared\Transfer\GuiTableBatchActionTransfer> |
||
112 | */ |
||
113 | protected array $batchActions = []; |
||
114 | |||
115 | /** |
||
116 | * @var string|null |
||
117 | */ |
||
118 | protected ?string $batchActionRowIdPath = null; |
||
119 | |||
120 | /** |
||
121 | * @var string|null |
||
122 | */ |
||
123 | protected ?string $availableBatchActionsPath = null; |
||
124 | |||
125 | /** |
||
126 | * @var string|null |
||
127 | */ |
||
128 | protected ?string $noBatchActionsMessage = null; |
||
129 | |||
130 | /** |
||
131 | * @var string|null |
||
132 | */ |
||
133 | protected ?string $dataSourceUrl = null; |
||
134 | |||
135 | /** |
||
136 | * @var array<array<string>> |
||
137 | */ |
||
138 | protected array $dataSourceInlineData = []; |
||
139 | |||
140 | /** |
||
141 | * @var int|null |
||
142 | */ |
||
143 | protected ?int $defaultPageSize = null; |
||
144 | |||
145 | /** |
||
146 | * @var bool |
||
147 | */ |
||
148 | protected bool $isSearchEnabled = true; |
||
149 | |||
150 | /** |
||
151 | * @var bool |
||
152 | */ |
||
153 | protected bool $isColumnConfiguratorEnabled = true; |
||
154 | |||
155 | /** |
||
156 | * @var string|null |
||
157 | */ |
||
158 | protected ?string $searchPlaceholder = null; |
||
159 | |||
160 | /** |
||
161 | * @var bool|null |
||
162 | */ |
||
163 | protected ?bool $isItemSelectionEnabled = null; |
||
164 | |||
165 | /** |
||
166 | * @var \Generated\Shared\Transfer\GuiTableEditableConfigurationTransfer|null |
||
167 | */ |
||
168 | protected ?GuiTableEditableConfigurationTransfer $editableConfiguration = null; |
||
169 | |||
170 | /** |
||
171 | * @var bool|null |
||
172 | */ |
||
173 | protected ?bool $isPaginationEnabled = null; |
||
174 | |||
175 | /** |
||
176 | * @var bool |
||
177 | */ |
||
178 | protected bool $isTotalEnabled = true; |
||
179 | |||
180 | /** |
||
181 | * @api |
||
182 | * |
||
183 | * @return \Generated\Shared\Transfer\GuiTableEditableConfigurationTransfer|null |
||
184 | */ |
||
185 | public function getEditableConfiguration(): ?GuiTableEditableConfigurationTransfer |
||
186 | { |
||
187 | return $this->editableConfiguration; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @api |
||
192 | * |
||
193 | * @return array<string, \Generated\Shared\Transfer\GuiTableColumnConfigurationTransfer> |
||
194 | */ |
||
195 | public function getColumns(): array |
||
196 | { |
||
197 | return $this->columns; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @api |
||
202 | * |
||
203 | * @param array<string, \Generated\Shared\Transfer\GuiTableColumnConfigurationTransfer> $columns |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setColumns(array $columns) |
||
208 | { |
||
209 | $this->columns = $columns; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @api |
||
216 | * |
||
217 | * @param string $id |
||
218 | * @param string $title |
||
219 | * @param bool $isSortable |
||
220 | * @param bool $isHideable |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function addColumnText( |
||
225 | string $id, |
||
226 | string $title, |
||
227 | bool $isSortable, |
||
228 | bool $isHideable |
||
229 | ) { |
||
230 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
231 | ->setId($id) |
||
232 | ->setTitle($title) |
||
233 | ->setType(static::COLUMN_TYPE_TEXT) |
||
234 | ->setSortable($isSortable) |
||
235 | ->setHideable($isHideable); |
||
236 | |||
237 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @api |
||
244 | * |
||
245 | * @param string $id |
||
246 | * @param string $title |
||
247 | * @param bool $isSortable |
||
248 | * @param bool $isHideable |
||
249 | * @param string|null $idAltSourceColumn |
||
250 | * |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function addColumnImage( |
||
254 | string $id, |
||
255 | string $title, |
||
256 | bool $isSortable, |
||
257 | bool $isHideable, |
||
258 | ?string $idAltSourceColumn = null |
||
259 | ) { |
||
260 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
261 | ->setId($id) |
||
262 | ->setTitle($title) |
||
263 | ->setType(static::COLUMN_TYPE_IMAGE) |
||
264 | ->setSortable($isSortable) |
||
265 | ->setHideable($isHideable); |
||
266 | |||
267 | if ($idAltSourceColumn !== null) { |
||
268 | $guiTableColumnConfigurationTransfer->addTypeOption('alt', sprintf('${row.%s}', $idAltSourceColumn)); |
||
269 | } |
||
270 | |||
271 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @api |
||
278 | * |
||
279 | * @param string $id |
||
280 | * @param string $title |
||
281 | * @param bool $isSortable |
||
282 | * @param bool $isHideable |
||
283 | * @param string|null $uiDateFormat |
||
284 | * |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function addColumnDate( |
||
288 | string $id, |
||
289 | string $title, |
||
290 | bool $isSortable, |
||
291 | bool $isHideable, |
||
292 | ?string $uiDateFormat = null |
||
293 | ) { |
||
294 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
295 | ->setId($id) |
||
296 | ->setTitle($title) |
||
297 | ->setType(static::COLUMN_TYPE_DATE) |
||
298 | ->setSortable($isSortable) |
||
299 | ->setHideable($isHideable) |
||
300 | ->addTypeOption('format', $uiDateFormat ?? static::DEFAULT_UI_DATE_FORMAT); |
||
301 | |||
302 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
303 | |||
304 | return $this; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @api |
||
309 | * |
||
310 | * @param string $id |
||
311 | * @param string $title |
||
312 | * @param bool $isSortable |
||
313 | * @param bool $isHideable |
||
314 | * @param string|null $color |
||
315 | * @param array<mixed>|null $colorMapping |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function addColumnChip( |
||
320 | string $id, |
||
321 | string $title, |
||
322 | bool $isSortable, |
||
323 | bool $isHideable, |
||
324 | ?string $color, |
||
325 | ?array $colorMapping = [] |
||
326 | ) { |
||
327 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
328 | ->setId($id) |
||
329 | ->setTitle($title) |
||
330 | ->setType(static::COLUMN_TYPE_CHIP) |
||
331 | ->setSortable($isSortable) |
||
332 | ->setHideable($isHideable) |
||
333 | ->addTypeOption('color', $color) |
||
334 | ->addTypeOption('maxWidth', static::DEFAULT_CHIP_MAX_WIDTH) |
||
335 | ->addTypeOptionMapping('color', $colorMapping); |
||
336 | |||
337 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
338 | |||
339 | return $this; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @api |
||
344 | * |
||
345 | * @param string $id |
||
346 | * @param string $title |
||
347 | * @param bool $isSortable |
||
348 | * @param bool $isHideable |
||
349 | * @param int|null $limit |
||
350 | * @param string|null $color |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function addColumnListChip( |
||
355 | string $id, |
||
356 | string $title, |
||
357 | bool $isSortable, |
||
358 | bool $isHideable, |
||
359 | ?int $limit, |
||
360 | ?string $color |
||
361 | ) { |
||
362 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
363 | ->setId($id) |
||
364 | ->setTitle($title) |
||
365 | ->setType(static::COLUMN_TYPE_LIST) |
||
366 | ->setSortable($isSortable) |
||
367 | ->setHideable($isHideable) |
||
368 | ->addTypeOption('type', static::COLUMN_TYPE_CHIP) |
||
369 | ->addTypeOption('limit', $limit); |
||
370 | |||
371 | if ($color) { |
||
372 | $guiTableColumnConfigurationTransfer->addTypeOption('typeOptions', [ |
||
373 | 'color' => $color, |
||
374 | ]); |
||
375 | } |
||
376 | |||
377 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
378 | |||
379 | return $this; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * {@inheritDoc} |
||
384 | * |
||
385 | * @api |
||
386 | * |
||
387 | * @param string $id |
||
388 | * @param string $title |
||
389 | * @param bool $isSortable |
||
390 | * @param bool $isHideable |
||
391 | * @param string $text |
||
392 | * @param string $actionUrl |
||
393 | * @param string|null $modalTitle |
||
394 | * @param string|null $modalDescription |
||
395 | * |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function addColumnButtonAction( |
||
399 | string $id, |
||
400 | string $title, |
||
401 | bool $isSortable, |
||
402 | bool $isHideable, |
||
403 | string $text, |
||
404 | string $actionUrl, |
||
405 | ?string $modalTitle = null, |
||
406 | ?string $modalDescription = null |
||
407 | ) { |
||
408 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
409 | ->setId($id) |
||
410 | ->setTitle($title) |
||
411 | ->setType(static::COLUMN_TYPE_BUTTON_ACTION) |
||
412 | ->setSortable($isSortable) |
||
413 | ->setHideable($isHideable) |
||
414 | ->addTypeOption('text', $text) |
||
415 | ->addTypeOption('variant', static::DEFAULT_COLUMN_BUTTON_ACTION_VARIANT) |
||
416 | ->addTypeOption('size', static::DEFAULT_COLUMN_BUTTON_ACTION_SIZE); |
||
417 | |||
418 | if ($modalTitle !== null && $modalDescription !== null) { |
||
419 | $guiTableColumnConfigurationTransfer->addTypeOption( |
||
420 | 'action', |
||
421 | $this->getTypeOptionConfirmationRedirect($actionUrl, $modalTitle, $modalDescription), |
||
422 | ); |
||
423 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
424 | |||
425 | return $this; |
||
426 | } |
||
427 | |||
428 | $guiTableColumnConfigurationTransfer->addTypeOption('action', $this->getTypeOptionRedirect($actionUrl)); |
||
429 | $this->addColumn($guiTableColumnConfigurationTransfer); |
||
430 | |||
431 | return $this; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param \Generated\Shared\Transfer\GuiTableColumnConfigurationTransfer $guiTableColumnConfigurationTransfer |
||
436 | * |
||
437 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
438 | * |
||
439 | * @return void |
||
440 | */ |
||
441 | protected function addColumn(GuiTableColumnConfigurationTransfer $guiTableColumnConfigurationTransfer): void |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @api |
||
454 | * |
||
455 | * @param string $id |
||
456 | * @param string $title |
||
457 | * @param bool $isMultiselect |
||
458 | * @param array<int|string, string> $values select values in format of ['value1' => 'title1', 'value2' => 'title2'] |
||
459 | * |
||
460 | * @return $this |
||
461 | */ |
||
462 | public function addFilterSelect( |
||
463 | string $id, |
||
464 | string $title, |
||
465 | bool $isMultiselect, |
||
466 | array $values |
||
467 | ) { |
||
468 | $typeOptionTransfers = (new SelectGuiTableFilterTypeOptionsTransfer())->setMultiselect($isMultiselect); |
||
469 | |||
470 | foreach ($values as $value => $optionTitle) { |
||
471 | $optionTransfer = (new OptionSelectGuiTableFilterTypeOptionsTransfer()) |
||
472 | ->setValue((string)$value) |
||
473 | ->setTitle($optionTitle); |
||
474 | $typeOptionTransfers->addValue($optionTransfer); |
||
475 | } |
||
476 | |||
477 | $this->filters[] = (new GuiTableFilterTransfer()) |
||
478 | ->setId($id) |
||
479 | ->setTitle($title) |
||
480 | ->setType(static::FILTER_TYPE_SELECT) |
||
481 | ->setTypeOptions($typeOptionTransfers); |
||
482 | |||
483 | return $this; |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * @api |
||
488 | * |
||
489 | * @param string $id |
||
490 | * @param string $title |
||
491 | * @param bool $isMultiselect |
||
492 | * @param array<\Generated\Shared\Transfer\OptionSelectGuiTableFilterTypeOptionsTransfer> $options |
||
493 | * |
||
494 | * @return $this |
||
495 | */ |
||
496 | public function addFilterTreeSelect(string $id, string $title, bool $isMultiselect, array $options) |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @api |
||
515 | * |
||
516 | * @param string $id |
||
517 | * @param string $title |
||
518 | * @param string|null $placeholderFrom |
||
519 | * @param string|null $placeholderTo |
||
520 | * |
||
521 | * @return $this |
||
522 | */ |
||
523 | public function addFilterDateRange( |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * @api |
||
549 | * |
||
550 | * @param string $id |
||
551 | * @param string $title |
||
552 | * @param string $url |
||
553 | * @param string|null $method |
||
554 | * |
||
555 | * @return $this |
||
556 | */ |
||
557 | public function addRowActionDrawerAjaxForm( |
||
558 | string $id, |
||
559 | string $title, |
||
560 | string $url, |
||
561 | ?string $method = null |
||
562 | ) { |
||
563 | $this->addRowAction( |
||
564 | $id, |
||
565 | $title, |
||
566 | static::ACTION_TYPE_DRAWER, |
||
567 | static::ACTION_DRAWER_COMPONENT_TYPE_AJAX_FORM, |
||
568 | [ |
||
569 | 'action' => $url, |
||
570 | 'method' => $method, |
||
571 | ], |
||
572 | ); |
||
573 | |||
574 | return $this; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * @api |
||
579 | * |
||
580 | * @param string $id |
||
581 | * @param string $title |
||
582 | * @param string $url |
||
583 | * @param string|null $method |
||
584 | * |
||
585 | * @return $this |
||
586 | */ |
||
587 | public function addRowActionDrawerUrlHtmlRenderer( |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @api |
||
609 | * |
||
610 | * @param string $id |
||
611 | * @param string $title |
||
612 | * @param string $url |
||
613 | * @param string|null $method |
||
614 | * |
||
615 | * @return $this |
||
616 | */ |
||
617 | public function addRowActionHttp( |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * @api |
||
639 | * |
||
640 | * @param string $id |
||
641 | * @param string $title |
||
642 | * @param string $url |
||
643 | * |
||
644 | * @return $this |
||
645 | */ |
||
646 | public function addRowActionRedirect(string $id, string $title, string $url) |
||
647 | { |
||
648 | $this->addRowAction( |
||
649 | $id, |
||
650 | $title, |
||
651 | static::ACTION_TYPE_REDIRECT, |
||
652 | null, |
||
653 | [ |
||
654 | 'url' => $url, |
||
655 | ], |
||
656 | ); |
||
657 | |||
658 | return $this; |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * @param string $id |
||
663 | * @param string $title |
||
664 | * @param string $type |
||
665 | * @param string|null $component |
||
666 | * @param array<string, mixed> $options |
||
667 | * |
||
668 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
669 | * |
||
670 | * @return void |
||
671 | */ |
||
672 | protected function addRowAction( |
||
673 | string $id, |
||
674 | string $title, |
||
675 | string $type, |
||
676 | ?string $component = null, |
||
677 | array $options = [] |
||
678 | ): void { |
||
679 | if (isset($this->rowActions[$id])) { |
||
680 | throw new InvalidConfigurationException(sprintf('Row action with id "%s" already exists', $id)); |
||
681 | } |
||
682 | |||
683 | $guiTableRowActionTransfer = (new GuiTableRowActionTransfer()) |
||
684 | ->setId($id) |
||
685 | ->setTitle($title) |
||
686 | ->setType($type); |
||
687 | |||
688 | $this->rowActions[$id] = $guiTableRowActionTransfer; |
||
689 | |||
690 | if ($type === static::ACTION_TYPE_HTTP) { |
||
691 | $guiTableRowActionTransfer |
||
692 | ->setUrl($options['url']) |
||
693 | ->setMethod($options['method']); |
||
694 | |||
695 | return; |
||
696 | } |
||
697 | |||
698 | if ($type === static::ACTION_TYPE_REDIRECT) { |
||
699 | $guiTableRowActionTransfer |
||
700 | ->setUrl($options['url']); |
||
701 | } |
||
702 | |||
703 | $guiTableRowActionTransfer |
||
704 | ->setComponent($component) |
||
705 | ->setOptions( |
||
706 | (new GuiTableRowActionOptionsTransfer()) |
||
707 | ->setInputs($options), |
||
708 | ); |
||
709 | } |
||
710 | |||
711 | /** |
||
712 | * @api |
||
713 | * |
||
714 | * @param string $id |
||
715 | * @param string $title |
||
716 | * @param string $url |
||
717 | * @param string|null $method |
||
718 | * |
||
719 | * @return $this |
||
720 | */ |
||
721 | public function addBatchActionDrawerAjaxForm( |
||
722 | string $id, |
||
723 | string $title, |
||
724 | string $url, |
||
725 | ?string $method = null |
||
726 | ) { |
||
727 | $this->addBatchAction( |
||
728 | $id, |
||
729 | $title, |
||
730 | static::ACTION_TYPE_DRAWER, |
||
731 | static::ACTION_DRAWER_COMPONENT_TYPE_AJAX_FORM, |
||
732 | [ |
||
733 | 'action' => $url, |
||
734 | 'method' => $method, |
||
735 | ], |
||
736 | ); |
||
737 | |||
738 | return $this; |
||
739 | } |
||
740 | |||
741 | /** |
||
742 | * @api |
||
743 | * |
||
744 | * @param string $id |
||
745 | * @param string $title |
||
746 | * @param string $url |
||
747 | * @param string|null $method |
||
748 | * |
||
749 | * @return $this |
||
750 | */ |
||
751 | public function addBatchActionHttp( |
||
752 | string $id, |
||
753 | string $title, |
||
754 | string $url, |
||
755 | ?string $method = null |
||
756 | ) { |
||
757 | $this->addBatchAction( |
||
758 | $id, |
||
759 | $title, |
||
760 | static::ACTION_TYPE_HTTP, |
||
761 | null, |
||
762 | [ |
||
763 | 'url' => $url, |
||
764 | 'method' => $method, |
||
765 | ], |
||
766 | ); |
||
767 | |||
768 | return $this; |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * @api |
||
773 | * |
||
774 | * @param string $id |
||
775 | * @param string $title |
||
776 | * @param string $url |
||
777 | * @param string|null $method |
||
778 | * |
||
779 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
780 | * |
||
781 | * @return $this |
||
782 | */ |
||
783 | public function addBatchActionDrawerUrlHtmlRenderer( |
||
784 | string $id, |
||
785 | string $title, |
||
786 | string $url, |
||
787 | ?string $method = null |
||
788 | ) { |
||
789 | $this->addBatchAction( |
||
790 | $id, |
||
791 | $title, |
||
792 | static::ACTION_TYPE_DRAWER, |
||
793 | static::ACTION_DRAWER_COMPONENT_TYPE_URL_HTML_RENDERER, |
||
794 | [ |
||
795 | 'url' => $url, |
||
796 | 'method' => $method, |
||
797 | ], |
||
798 | ); |
||
799 | |||
800 | return $this; |
||
801 | } |
||
802 | |||
803 | /** |
||
804 | * @param string $id |
||
805 | * @param string $title |
||
806 | * @param string $type |
||
807 | * @param string|null $component |
||
808 | * @param array<string, mixed> $options |
||
809 | * |
||
810 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
811 | * |
||
812 | * @return void |
||
813 | */ |
||
814 | protected function addBatchAction( |
||
815 | string $id, |
||
816 | string $title, |
||
817 | string $type, |
||
818 | ?string $component = null, |
||
819 | array $options = [] |
||
820 | ): void { |
||
821 | if (isset($this->batchActions[$id])) { |
||
822 | throw new InvalidConfigurationException(sprintf('Batch action with id "%s" already exists', $id)); |
||
823 | } |
||
824 | |||
825 | $guiTableBatchActionTransfer = (new GuiTableBatchActionTransfer()) |
||
826 | ->setId($id) |
||
827 | ->setTitle($title) |
||
828 | ->setType($type); |
||
829 | |||
830 | $this->batchActions[$id] = $guiTableBatchActionTransfer; |
||
831 | |||
832 | if ($type === static::ACTION_TYPE_HTTP) { |
||
833 | $guiTableBatchActionTransfer |
||
834 | ->setUrl($options['url']) |
||
835 | ->setMethod($options['method']); |
||
836 | |||
837 | return; |
||
838 | } |
||
839 | |||
840 | $guiTableBatchActionTransfer |
||
841 | ->setComponent($component) |
||
842 | ->setOptions( |
||
843 | (new GuiTableBatchActionOptionsTransfer()) |
||
844 | ->setInputs($options), |
||
845 | ); |
||
846 | } |
||
847 | |||
848 | /** |
||
849 | * @api |
||
850 | * |
||
851 | * @return array<\Generated\Shared\Transfer\GuiTableRowActionTransfer> |
||
852 | */ |
||
853 | public function getRowActions(): array |
||
854 | { |
||
855 | return $this->rowActions; |
||
856 | } |
||
857 | |||
858 | /** |
||
859 | * @api |
||
860 | * |
||
861 | * @param string $id |
||
862 | * |
||
863 | * @throws \Spryker\Shared\GuiTable\Exception\RowActionNotFoundException |
||
864 | * |
||
865 | * @return \Generated\Shared\Transfer\GuiTableRowActionTransfer |
||
866 | */ |
||
867 | public function getRowAction(string $id): GuiTableRowActionTransfer |
||
868 | { |
||
869 | if (!isset($this->rowActions[$id])) { |
||
870 | throw new RowActionNotFoundException($id); |
||
871 | } |
||
872 | |||
873 | return $this->rowActions[$id]; |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * @api |
||
878 | * |
||
879 | * @param string $idAction |
||
880 | * |
||
881 | * @return $this |
||
882 | */ |
||
883 | public function setRowClickAction(string $idAction) |
||
884 | { |
||
885 | $this->rowOnClickIdAction = $idAction; |
||
886 | |||
887 | return $this; |
||
888 | } |
||
889 | |||
890 | /** |
||
891 | * @api |
||
892 | * |
||
893 | * @param string $idPath |
||
894 | * |
||
895 | * @return $this |
||
896 | */ |
||
897 | public function setRowActionRowIdPath(string $idPath) |
||
898 | { |
||
899 | $this->rowActionRowIdPath = $idPath; |
||
900 | |||
901 | return $this; |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * @api |
||
906 | * |
||
907 | * @param string $idPath |
||
908 | * |
||
909 | * @return $this |
||
910 | */ |
||
911 | public function setBatchActionRowIdPath(string $idPath) |
||
912 | { |
||
913 | $this->batchActionRowIdPath = $idPath; |
||
914 | |||
915 | return $this; |
||
916 | } |
||
917 | |||
918 | /** |
||
919 | * @api |
||
920 | * |
||
921 | * @param string $availableRowActionsPath |
||
922 | * |
||
923 | * @return $this |
||
924 | */ |
||
925 | public function setAvailableRowActionsPath(string $availableRowActionsPath) |
||
926 | { |
||
927 | $this->availableRowActionsPath = $availableRowActionsPath; |
||
928 | |||
929 | return $this; |
||
930 | } |
||
931 | |||
932 | /** |
||
933 | * @api |
||
934 | * |
||
935 | * @param string $availableBatchActionsPath |
||
936 | * |
||
937 | * @return $this |
||
938 | */ |
||
939 | public function setAvailableBatchActionsPath(string $availableBatchActionsPath) |
||
940 | { |
||
941 | $this->availableBatchActionsPath = $availableBatchActionsPath; |
||
942 | |||
943 | return $this; |
||
944 | } |
||
945 | |||
946 | /** |
||
947 | * @api |
||
948 | * |
||
949 | * @param string $noBatchActionsMessage |
||
950 | * |
||
951 | * @return $this |
||
952 | */ |
||
953 | public function setNoBatchActionsMessage(string $noBatchActionsMessage) |
||
954 | { |
||
955 | $this->noBatchActionsMessage = $noBatchActionsMessage; |
||
956 | |||
957 | return $this; |
||
958 | } |
||
959 | |||
960 | /** |
||
961 | * @api |
||
962 | * |
||
963 | * @param string $url |
||
964 | * |
||
965 | * @return $this |
||
966 | */ |
||
967 | public function setDataSourceUrl(string $url) |
||
968 | { |
||
969 | $this->dataSourceUrl = $url; |
||
970 | |||
971 | return $this; |
||
972 | } |
||
973 | |||
974 | /** |
||
975 | * @api |
||
976 | * |
||
977 | * @param array<array<string>> $data |
||
978 | * |
||
979 | * @return $this |
||
980 | */ |
||
981 | public function setDataSourceInlineData(array $data) |
||
982 | { |
||
983 | $this->dataSourceInlineData = $data; |
||
984 | |||
985 | return $this; |
||
986 | } |
||
987 | |||
988 | /** |
||
989 | * @api |
||
990 | * |
||
991 | * @param int $defaultPageSize |
||
992 | * |
||
993 | * @return $this |
||
994 | */ |
||
995 | public function setDefaultPageSize(int $defaultPageSize) |
||
996 | { |
||
997 | $this->defaultPageSize = $defaultPageSize; |
||
998 | |||
999 | return $this; |
||
1000 | } |
||
1001 | |||
1002 | /** |
||
1003 | * @api |
||
1004 | * |
||
1005 | * @param bool $isEnabled |
||
1006 | * |
||
1007 | * @return $this |
||
1008 | */ |
||
1009 | public function isSearchEnabled(bool $isEnabled = true) |
||
1010 | { |
||
1011 | $this->isSearchEnabled = false; |
||
1012 | |||
1013 | return $this; |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * @api |
||
1018 | * |
||
1019 | * @param bool $isEnabled |
||
1020 | * |
||
1021 | * @return $this |
||
1022 | */ |
||
1023 | public function isColumnConfiguratorEnabled(bool $isEnabled = true) |
||
1024 | { |
||
1025 | $this->isColumnConfiguratorEnabled = false; |
||
1026 | |||
1027 | return $this; |
||
1028 | } |
||
1029 | |||
1030 | /** |
||
1031 | * @api |
||
1032 | * |
||
1033 | * @param string $searchPlaceholder |
||
1034 | * |
||
1035 | * @return $this |
||
1036 | */ |
||
1037 | public function setSearchPlaceholder(string $searchPlaceholder) |
||
1038 | { |
||
1039 | $this->searchPlaceholder = $searchPlaceholder; |
||
1040 | |||
1041 | return $this; |
||
1042 | } |
||
1043 | |||
1044 | /** |
||
1045 | * @api |
||
1046 | * |
||
1047 | * @param bool $isItemSelectionEnabled |
||
1048 | * |
||
1049 | * @return $this |
||
1050 | */ |
||
1051 | public function setIsItemSelectionEnabled(bool $isItemSelectionEnabled) |
||
1052 | { |
||
1053 | $this->isItemSelectionEnabled = $isItemSelectionEnabled; |
||
1054 | |||
1055 | return $this; |
||
1056 | } |
||
1057 | |||
1058 | /** |
||
1059 | * @api |
||
1060 | * |
||
1061 | * @param string $title |
||
1062 | * |
||
1063 | * @return $this |
||
1064 | */ |
||
1065 | public function setTableTitle(string $title) |
||
1066 | { |
||
1067 | $this->title = $title; |
||
1068 | |||
1069 | return $this; |
||
1070 | } |
||
1071 | |||
1072 | /** |
||
1073 | * @api |
||
1074 | * |
||
1075 | * @param bool $isPaginationEnabled |
||
1076 | * |
||
1077 | * @return $this |
||
1078 | */ |
||
1079 | public function setIsPaginationEnabled(bool $isPaginationEnabled) |
||
1080 | { |
||
1081 | $this->isPaginationEnabled = $isPaginationEnabled; |
||
1082 | |||
1083 | return $this; |
||
1084 | } |
||
1085 | |||
1086 | /** |
||
1087 | * {@inheritDoc} |
||
1088 | * |
||
1089 | * @api |
||
1090 | * |
||
1091 | * @param bool $isTotalEnabled |
||
1092 | * |
||
1093 | * @return $this |
||
1094 | */ |
||
1095 | public function setIsTotalEnabled(bool $isTotalEnabled) |
||
1096 | { |
||
1097 | $this->isTotalEnabled = $isTotalEnabled; |
||
1098 | |||
1099 | return $this; |
||
1100 | } |
||
1101 | |||
1102 | /** |
||
1103 | * @api |
||
1104 | * |
||
1105 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
1106 | * |
||
1107 | * @return \Generated\Shared\Transfer\GuiTableConfigurationTransfer |
||
1108 | */ |
||
1109 | public function createConfiguration(): GuiTableConfigurationTransfer |
||
1110 | { |
||
1111 | if (!$this->columns) { |
||
1112 | throw new InvalidConfigurationException('Table must have at least one column'); |
||
1113 | } |
||
1114 | |||
1115 | $guiTableConfigurationTransfer = new GuiTableConfigurationTransfer(); |
||
1116 | |||
1117 | $guiTableConfigurationTransfer->setColumns(new ArrayObject($this->columns)); |
||
1118 | $guiTableConfigurationTransfer = $this->setFilters($guiTableConfigurationTransfer); |
||
1119 | $guiTableConfigurationTransfer = $this->setRowActions($guiTableConfigurationTransfer); |
||
1120 | $guiTableConfigurationTransfer = $this->setBatchActions($guiTableConfigurationTransfer); |
||
1121 | $guiTableConfigurationTransfer = $this->setDataSource($guiTableConfigurationTransfer); |
||
1122 | |||
1123 | if ($this->title) { |
||
1124 | $guiTableConfigurationTransfer->setTitle( |
||
1125 | (new GuiTableTitleConfigurationTransfer()) |
||
1126 | ->setIsEnabled(true) |
||
1127 | ->setTitle($this->title), |
||
1128 | ); |
||
1129 | } |
||
1130 | |||
1131 | if ($this->defaultPageSize) { |
||
1132 | $guiTableConfigurationTransfer->setPagination( |
||
1133 | (new GuiTablePaginationConfigurationTransfer())->setDefaultSize($this->defaultPageSize), |
||
1134 | ); |
||
1135 | } |
||
1136 | |||
1137 | $guiTableConfigurationTransfer->setColumnConfigurator( |
||
1138 | (new GuiTableColumnConfiguratorConfigurationTransfer())->setEnabled($this->isColumnConfiguratorEnabled), |
||
1139 | ); |
||
1140 | |||
1141 | $guiTableConfigurationTransfer->setSearch( |
||
1142 | (new GuiTableSearchConfigurationTransfer())->setIsEnabled($this->isSearchEnabled), |
||
1143 | ); |
||
1144 | |||
1145 | $guiTableConfigurationTransfer->setTotal( |
||
1146 | (new GuiTableTotalConfigurationTransfer())->setIsEnabled($this->isTotalEnabled), |
||
1147 | ); |
||
1148 | |||
1149 | if ($this->searchPlaceholder) { |
||
1150 | $guiTableConfigurationTransfer->getSearchOrFail() |
||
1151 | ->addSearchOption('placeholder', $this->searchPlaceholder); |
||
1152 | } |
||
1153 | |||
1154 | if ($this->isItemSelectionEnabled !== null) { |
||
1155 | $guiTableConfigurationTransfer->setItemSelection( |
||
1156 | (new GuiTableItemSelectionConfigurationTransfer())->setIsEnabled($this->isItemSelectionEnabled), |
||
1157 | ); |
||
1158 | } |
||
1159 | |||
1160 | if ($this->editableConfiguration) { |
||
1161 | $guiTableConfigurationTransfer->setEditable($this->editableConfiguration); |
||
1162 | } |
||
1163 | |||
1164 | if ($this->isPaginationEnabled !== null) { |
||
1165 | $guiTableConfigurationTransfer->setPagination( |
||
1166 | (new GuiTablePaginationConfigurationTransfer())->setIsEnabled($this->isPaginationEnabled), |
||
1167 | ); |
||
1168 | } |
||
1169 | |||
1170 | return $guiTableConfigurationTransfer; |
||
1171 | } |
||
1172 | |||
1173 | /** |
||
1174 | * @api |
||
1175 | * |
||
1176 | * @param string $formInputName |
||
1177 | * @param array<string, mixed> $initialData |
||
1178 | * @param array<string, mixed>|null $addButton |
||
1179 | * @param array<string, mixed>|null $cancelButton |
||
1180 | * |
||
1181 | * @return $this |
||
1182 | */ |
||
1183 | public function enableAddingNewRows( |
||
1184 | string $formInputName, |
||
1185 | array $initialData = [], |
||
1186 | ?array $addButton = null, |
||
1187 | ?array $cancelButton = null |
||
1188 | ) { |
||
1189 | if (!$this->editableConfiguration) { |
||
1190 | $this->editableConfiguration = (new GuiTableEditableConfigurationTransfer())->setEnabled(true); |
||
1191 | } |
||
1192 | |||
1193 | $guiTableEditableInitialDataTransfer = (bool)$initialData ? $this->mapInitialDataToTransfer($initialData) : null; |
||
1194 | $guiTableEditableCreateConfigurationTransfer = (new GuiTableEditableCreateConfigurationTransfer()) |
||
1195 | ->setFormInputName($formInputName) |
||
1196 | ->setInitialData($guiTableEditableInitialDataTransfer) |
||
1197 | ->setCancelButton($this->createEditableCancelButton($cancelButton)) |
||
1198 | ->setAddButton($this->createEditableAddButton($addButton)); |
||
1199 | |||
1200 | $this->editableConfiguration->setCreate($guiTableEditableCreateConfigurationTransfer); |
||
1201 | |||
1202 | return $this; |
||
1203 | } |
||
1204 | |||
1205 | /** |
||
1206 | * @api |
||
1207 | * |
||
1208 | * @param string $url |
||
1209 | * @param string $method |
||
1210 | * @param array<string, mixed>|null $saveButton |
||
1211 | * @param array<string, mixed>|null $cancelButton |
||
1212 | * |
||
1213 | * @return $this |
||
1214 | */ |
||
1215 | public function enableInlineDataEditing( |
||
1216 | string $url, |
||
1217 | string $method = 'POST', |
||
1218 | ?array $saveButton = null, |
||
1219 | ?array $cancelButton = null |
||
1220 | ) { |
||
1221 | if (!$this->editableConfiguration) { |
||
1222 | $this->editableConfiguration = (new GuiTableEditableConfigurationTransfer())->setEnabled(true); |
||
1223 | } |
||
1224 | |||
1225 | $guiTableEditableUpdateConfigurationTransfer = (new GuiTableEditableUpdateConfigurationTransfer()) |
||
1226 | ->setUrl($this->createEditableUrl($url, $method)) |
||
1227 | ->setSaveButton($this->createEditableSaveButton($saveButton)) |
||
1228 | ->setCancelButton($this->createEditableCancelButton($cancelButton)); |
||
1229 | |||
1230 | $this->editableConfiguration->setUpdate($guiTableEditableUpdateConfigurationTransfer); |
||
1231 | |||
1232 | return $this; |
||
1233 | } |
||
1234 | |||
1235 | /** |
||
1236 | * @api |
||
1237 | * |
||
1238 | * @param string $id |
||
1239 | * @param string $title |
||
1240 | * @param string $inputType |
||
1241 | * @param array<string, mixed> $options |
||
1242 | * |
||
1243 | * @return $this |
||
1244 | */ |
||
1245 | public function addEditableColumnInput(string $id, string $title, string $inputType = 'text', array $options = []) |
||
1246 | { |
||
1247 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
1248 | ->setId($id) |
||
1249 | ->setTitle($title) |
||
1250 | ->setType(static::COLUMN_TYPE_INPUT); |
||
1251 | |||
1252 | $options = array_merge([ |
||
1253 | 'type' => $inputType, |
||
1254 | ], $options); |
||
1255 | |||
1256 | $guiTableColumnConfigurationTransfer->setTypeOptions($options); |
||
1257 | |||
1258 | $this->addEditableColumn($guiTableColumnConfigurationTransfer); |
||
1259 | |||
1260 | return $this; |
||
1261 | } |
||
1262 | |||
1263 | /** |
||
1264 | * @api |
||
1265 | * |
||
1266 | * @param string $id |
||
1267 | * @param string $title |
||
1268 | * @param bool $isMultiselect |
||
1269 | * @param array<int|string, mixed> $options |
||
1270 | * @param string|null $placeholder |
||
1271 | * |
||
1272 | * @return $this |
||
1273 | */ |
||
1274 | public function addEditableColumnSelect( |
||
1275 | string $id, |
||
1276 | string $title, |
||
1277 | bool $isMultiselect, |
||
1278 | array $options, |
||
1279 | ?string $placeholder = null |
||
1280 | ) { |
||
1281 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
1282 | ->setId($id) |
||
1283 | ->setTitle($title) |
||
1284 | ->setType(static::COLUMN_TYPE_SELECT); |
||
1285 | |||
1286 | $typeOptionValues = []; |
||
1287 | |||
1288 | foreach ($options as $value => $optionTitle) { |
||
1289 | $typeOptionValues['options'][] = [ |
||
1290 | 'title' => $optionTitle, |
||
1291 | 'value' => $value, |
||
1292 | ]; |
||
1293 | } |
||
1294 | |||
1295 | if ($placeholder) { |
||
1296 | $typeOptionValues['placeholder'] = $placeholder; |
||
1297 | } |
||
1298 | |||
1299 | $guiTableColumnConfigurationTransfer->setTypeOptions($typeOptionValues); |
||
1300 | |||
1301 | $this->addEditableColumn($guiTableColumnConfigurationTransfer); |
||
1302 | |||
1303 | return $this; |
||
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * @api |
||
1308 | * |
||
1309 | * @param string $id |
||
1310 | * @param string $title |
||
1311 | * @param string $dependableColumn |
||
1312 | * @param array<string|int, mixed> $dataSetTypeOptions |
||
1313 | * @param array<string|int, mixed>|null $defaultTypeOptions |
||
1314 | * |
||
1315 | * @return $this |
||
1316 | */ |
||
1317 | public function addInlineEditableColumnDynamic( |
||
1318 | string $id, |
||
1319 | string $title, |
||
1320 | string $dependableColumn, |
||
1321 | array $dataSetTypeOptions, |
||
1322 | ?array $defaultTypeOptions = null |
||
1323 | ) { |
||
1324 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
1325 | ->setId($id) |
||
1326 | ->setTitle($title) |
||
1327 | ->setType(static::COLUMN_TYPE_DYNAMIC) |
||
1328 | ->setTypeOptions([ |
||
1329 | 'datasource' => [ |
||
1330 | 'type' => static::DATA_SOURCE_TYPE_DEPENDABLE, |
||
1331 | 'dependsOn' => $dependableColumn, |
||
1332 | 'datasource' => [ |
||
1333 | 'type' => static::DATA_SOURCE_TYPE_INLINE, |
||
1334 | 'data' => $dataSetTypeOptions, |
||
1335 | 'dependsOnContext' => [ |
||
1336 | 'contextKey' => $dependableColumn, |
||
1337 | 'default' => $defaultTypeOptions, |
||
1338 | ], |
||
1339 | ], |
||
1340 | ], |
||
1341 | ]); |
||
1342 | |||
1343 | $this->addEditableColumn($guiTableColumnConfigurationTransfer); |
||
1344 | |||
1345 | return $this; |
||
1346 | } |
||
1347 | |||
1348 | /** |
||
1349 | * @param array<string, mixed> $initialData |
||
1350 | * |
||
1351 | * @return \Generated\Shared\Transfer\GuiTableEditableInitialDataTransfer |
||
1352 | */ |
||
1353 | protected function mapInitialDataToTransfer(array $initialData): GuiTableEditableInitialDataTransfer |
||
1354 | { |
||
1355 | $guiTableEditableInitialDataTransfer = (new GuiTableEditableInitialDataTransfer()) |
||
1356 | ->fromArray($initialData, true); |
||
1357 | $errors = $initialData[GuiTableEditableInitialDataTransfer::ERRORS] ?? null; |
||
1358 | |||
1359 | if (!$errors) { |
||
1360 | return $guiTableEditableInitialDataTransfer; |
||
1361 | } |
||
1362 | |||
1363 | $guiTableEditableDataErrorTransfers = []; |
||
1364 | |||
1365 | foreach ($errors as $error) { |
||
1366 | $rowError = $error[GuiTableEditableDataErrorTransfer::ROW_ERROR] ?? null; |
||
1367 | $columnErrors = $error[GuiTableEditableDataErrorTransfer::COLUMN_ERRORS] ?? []; |
||
1368 | |||
1369 | $guiTableEditableDataErrorTransfers[] = (new GuiTableEditableDataErrorTransfer()) |
||
1370 | ->setRowError($rowError) |
||
1371 | ->setColumnErrors($columnErrors); |
||
1372 | } |
||
1373 | |||
1374 | return $guiTableEditableInitialDataTransfer->setErrors(new ArrayObject($guiTableEditableDataErrorTransfers)); |
||
1375 | } |
||
1376 | |||
1377 | /** |
||
1378 | * @param string $url |
||
1379 | * @param string $method |
||
1380 | * |
||
1381 | * @return \Generated\Shared\Transfer\GuiTableEditableUrlTransfer |
||
1382 | */ |
||
1383 | protected function createEditableUrl(string $url, string $method): GuiTableEditableUrlTransfer |
||
1384 | { |
||
1385 | return (new GuiTableEditableUrlTransfer()) |
||
1386 | ->setMethod($method) |
||
1387 | ->setUrl($url); |
||
1388 | } |
||
1389 | |||
1390 | /** |
||
1391 | * @param array<string, mixed>|null $addButton |
||
1392 | * |
||
1393 | * @return \Generated\Shared\Transfer\GuiTableEditableButtonTransfer |
||
1394 | */ |
||
1395 | protected function createEditableAddButton(?array $addButton): GuiTableEditableButtonTransfer |
||
1396 | { |
||
1397 | $editableButtonOptions = $this->resolveEditableButtonOptions($addButton, [ |
||
1398 | GuiTableEditableButtonTransfer::TITLE => 'Create', |
||
1399 | ]); |
||
1400 | |||
1401 | return $this->createEditableButton($editableButtonOptions); |
||
1402 | } |
||
1403 | |||
1404 | /** |
||
1405 | * @param array<string, mixed>|null $saveButton |
||
1406 | * |
||
1407 | * @return \Generated\Shared\Transfer\GuiTableEditableButtonTransfer |
||
1408 | */ |
||
1409 | protected function createEditableSaveButton(?array $saveButton): GuiTableEditableButtonTransfer |
||
1410 | { |
||
1411 | $editableButtonOptions = $this->resolveEditableButtonOptions($saveButton, [ |
||
1412 | GuiTableEditableButtonTransfer::TITLE => 'Save', |
||
1413 | ]); |
||
1414 | |||
1415 | return $this->createEditableButton($editableButtonOptions); |
||
1416 | } |
||
1417 | |||
1418 | /** |
||
1419 | * @param array<string, mixed>|null $cancelButton |
||
1420 | * |
||
1421 | * @return \Generated\Shared\Transfer\GuiTableEditableButtonTransfer |
||
1422 | */ |
||
1423 | protected function createEditableCancelButton(?array $cancelButton): GuiTableEditableButtonTransfer |
||
1424 | { |
||
1425 | $editableButtonOptions = $this->resolveEditableButtonOptions($cancelButton, [ |
||
1426 | GuiTableEditableButtonTransfer::TITLE => 'Cancel', |
||
1427 | ]); |
||
1428 | |||
1429 | return $this->createEditableButton($editableButtonOptions); |
||
1430 | } |
||
1431 | |||
1432 | /** |
||
1433 | * @param array<string, mixed>|null $editableButtonOptions |
||
1434 | * @param array<string, mixed> $defaultOverwriteOptions |
||
1435 | * |
||
1436 | * @return array<string, ?string> |
||
1437 | */ |
||
1438 | protected function resolveEditableButtonOptions(?array $editableButtonOptions, array $defaultOverwriteOptions): array |
||
1439 | { |
||
1440 | $editableButtonOptions = $editableButtonOptions ?? []; |
||
1441 | |||
1442 | $defaultOptions = [ |
||
1443 | GuiTableEditableButtonTransfer::TITLE => null, |
||
1444 | GuiTableEditableButtonTransfer::ICON => null, |
||
1445 | GuiTableEditableButtonTransfer::VARIANT => null, |
||
1446 | GuiTableEditableButtonTransfer::SIZE => null, |
||
1447 | GuiTableEditableButtonTransfer::SHAPE => null, |
||
1448 | ]; |
||
1449 | |||
1450 | return array_merge($defaultOptions, $defaultOverwriteOptions, $editableButtonOptions); |
||
1451 | } |
||
1452 | |||
1453 | /** |
||
1454 | * @param array<string, ?string> $editableButtonOptions |
||
1455 | * |
||
1456 | * @return \Generated\Shared\Transfer\GuiTableEditableButtonTransfer |
||
1457 | */ |
||
1458 | protected function createEditableButton( |
||
1459 | array $editableButtonOptions |
||
1460 | ): GuiTableEditableButtonTransfer { |
||
1461 | return (new GuiTableEditableButtonTransfer()) |
||
1462 | ->setTitle($editableButtonOptions[GuiTableEditableButtonTransfer::TITLE]) |
||
1463 | ->setIcon($editableButtonOptions[GuiTableEditableButtonTransfer::ICON]) |
||
1464 | ->setVariant($editableButtonOptions[GuiTableEditableButtonTransfer::VARIANT]) |
||
1465 | ->setSize($editableButtonOptions[GuiTableEditableButtonTransfer::SIZE]) |
||
1466 | ->setShape($editableButtonOptions[GuiTableEditableButtonTransfer::SHAPE]); |
||
1467 | } |
||
1468 | |||
1469 | /** |
||
1470 | * @param \Generated\Shared\Transfer\GuiTableColumnConfigurationTransfer $guiTableColumnConfigurationTransfer |
||
1471 | * |
||
1472 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
1473 | * |
||
1474 | * @return void |
||
1475 | */ |
||
1476 | protected function addEditableColumn(GuiTableColumnConfigurationTransfer $guiTableColumnConfigurationTransfer): void |
||
1489 | } |
||
1490 | |||
1491 | /** |
||
1492 | * @param \Generated\Shared\Transfer\GuiTableConfigurationTransfer $guiTableConfigurationTransfer |
||
1493 | * |
||
1494 | * @return \Generated\Shared\Transfer\GuiTableConfigurationTransfer |
||
1495 | */ |
||
1496 | protected function setFilters(GuiTableConfigurationTransfer $guiTableConfigurationTransfer): GuiTableConfigurationTransfer |
||
1497 | { |
||
1498 | $guiTableConfigurationTransfer->setFilters( |
||
1499 | (new GuiTableFiltersConfigurationTransfer())->setIsEnabled(false), |
||
1500 | ); |
||
1501 | |||
1502 | if ($this->filters) { |
||
1503 | $guiTableConfigurationTransfer->getFiltersOrFail() |
||
1504 | ->setIsEnabled(true) |
||
1505 | ->setItems(new ArrayObject($this->filters)); |
||
1506 | } |
||
1507 | |||
1508 | return $guiTableConfigurationTransfer; |
||
1509 | } |
||
1510 | |||
1511 | /** |
||
1512 | * @param \Generated\Shared\Transfer\GuiTableConfigurationTransfer $guiTableConfigurationTransfer |
||
1513 | * |
||
1514 | * @return \Generated\Shared\Transfer\GuiTableConfigurationTransfer |
||
1515 | */ |
||
1516 | protected function setRowActions(GuiTableConfigurationTransfer $guiTableConfigurationTransfer): GuiTableConfigurationTransfer |
||
1517 | { |
||
1518 | $guiTableConfigurationTransfer->setRowActions( |
||
1519 | (new GuiTableRowActionsConfigurationTransfer())->setIsEnabled(false), |
||
1520 | ); |
||
1521 | |||
1522 | if ($this->rowActions) { |
||
1523 | $guiTableConfigurationTransfer->getRowActionsOrFail() |
||
1524 | ->setIsEnabled(true) |
||
1525 | ->setActions(new ArrayObject($this->rowActions)) |
||
1526 | ->setClick($this->rowOnClickIdAction) |
||
1527 | ->setRowIdPath($this->rowActionRowIdPath) |
||
1528 | ->setAvailableActionsPath($this->availableRowActionsPath); |
||
1529 | } |
||
1530 | |||
1531 | return $guiTableConfigurationTransfer; |
||
1532 | } |
||
1533 | |||
1534 | /** |
||
1535 | * @param \Generated\Shared\Transfer\GuiTableConfigurationTransfer $guiTableConfigurationTransfer |
||
1536 | * |
||
1537 | * @return \Generated\Shared\Transfer\GuiTableConfigurationTransfer |
||
1538 | */ |
||
1539 | protected function setBatchActions(GuiTableConfigurationTransfer $guiTableConfigurationTransfer): GuiTableConfigurationTransfer |
||
1556 | } |
||
1557 | |||
1558 | /** |
||
1559 | * @param \Generated\Shared\Transfer\GuiTableConfigurationTransfer $guiTableConfigurationTransfer |
||
1560 | * |
||
1561 | * @return \Generated\Shared\Transfer\GuiTableConfigurationTransfer |
||
1562 | */ |
||
1563 | protected function setDataSource(GuiTableConfigurationTransfer $guiTableConfigurationTransfer): GuiTableConfigurationTransfer |
||
1564 | { |
||
1565 | $guiTableDataSourceConfigurationTransfer = new GuiTableDataSourceConfigurationTransfer(); |
||
1566 | |||
1567 | if ($this->dataSourceUrl) { |
||
1568 | $guiTableDataSourceConfigurationTransfer->setUrl($this->dataSourceUrl); |
||
1569 | } |
||
1570 | |||
1571 | if ($this->dataSourceInlineData) { |
||
1572 | $guiTableDataSourceConfigurationTransfer->setData($this->dataSourceInlineData) |
||
1573 | ->setType(GuiTableConfigurationBuilderInterface::DATA_SOURCE_TYPE_INLINE); |
||
1574 | } |
||
1575 | |||
1576 | $guiTableConfigurationTransfer->setDataSource($guiTableDataSourceConfigurationTransfer); |
||
1577 | |||
1578 | return $guiTableConfigurationTransfer; |
||
1579 | } |
||
1580 | |||
1581 | /** |
||
1582 | * @api |
||
1583 | * |
||
1584 | * @param string $id |
||
1585 | * @param string $title |
||
1586 | * @param string $dependableColumn |
||
1587 | * @param string $dependableUrl |
||
1588 | * |
||
1589 | * @throws \Spryker\Shared\GuiTable\Exception\InvalidConfigurationException |
||
1590 | * |
||
1591 | * @return $this |
||
1592 | */ |
||
1593 | public function addEditableColumnDynamic(string $id, string $title, string $dependableColumn, string $dependableUrl) |
||
1594 | { |
||
1595 | $guiTableColumnConfigurationTransfer = (new GuiTableColumnConfigurationTransfer()) |
||
1596 | ->setId($id) |
||
1597 | ->setTitle($title) |
||
1598 | ->setType(static::COLUMN_TYPE_DYNAMIC) |
||
1599 | ->setTypeOptions([ |
||
1600 | 'datasource' => [ |
||
1601 | 'type' => static::DATA_SOURCE_TYPE_DEPENDABLE, |
||
1602 | 'dependsOn' => $dependableColumn, |
||
1603 | 'datasource' => [ |
||
1604 | 'type' => static::DATA_SOURCE_TYPE_HTTP, |
||
1605 | 'url' => $dependableUrl, |
||
1606 | ], |
||
1607 | ], |
||
1608 | ]); |
||
1609 | |||
1610 | $this->addEditableColumn($guiTableColumnConfigurationTransfer); |
||
1611 | |||
1612 | return $this; |
||
1613 | } |
||
1614 | |||
1615 | /** |
||
1616 | * @api |
||
1617 | * |
||
1618 | * @param string $columnId |
||
1619 | * @param string $displayKey |
||
1620 | * |
||
1621 | * @return $this |
||
1622 | */ |
||
1623 | public function setColumnDisplayKey(string $columnId, string $displayKey) |
||
1624 | { |
||
1625 | $guiTableColumnConfigurationTransfer = $this->columns[$columnId]; |
||
1626 | $guiTableColumnConfigurationTransfer->setDisplayKey($displayKey); |
||
1627 | $this->columns[$columnId] = $guiTableColumnConfigurationTransfer; |
||
1628 | |||
1629 | return $this; |
||
1630 | } |
||
1631 | |||
1632 | /** |
||
1633 | * @param string $actionUrl |
||
1634 | * @param string $modalTitle |
||
1635 | * @param string $modalDescription |
||
1636 | * |
||
1637 | * @return array<string, mixed> |
||
1638 | */ |
||
1639 | protected function getTypeOptionConfirmationRedirect( |
||
1654 | ], |
||
1655 | ]; |
||
1656 | } |
||
1657 | |||
1658 | /** |
||
1659 | * @param string $actionUrl |
||
1660 | * |
||
1661 | * @return array<string, string> |
||
1662 | */ |
||
1663 | protected function getTypeOptionRedirect(string $actionUrl): array |
||
1664 | { |
||
1665 | return [ |
||
1666 | 'type' => 'redirect', |
||
1667 | 'url' => $actionUrl, |
||
1668 | ]; |
||
1669 | } |
||
1670 | } |
||
1671 |