From 15e18653c8ff5ad0879209dd517aba975b4a3451 Mon Sep 17 00:00:00 2001 From: tuxes3 Date: Thu, 3 Aug 2023 16:33:32 +0200 Subject: [PATCH] feat(batch-actions): defiend in table-bundle --- .../views/tailwind_2/_table.html.twig | 2 +- src/Table/Table.php | 59 +++++++++++++++---- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/Resources/views/tailwind_2/_table.html.twig b/src/Resources/views/tailwind_2/_table.html.twig index bfb974d..2e27e79 100644 --- a/src/Resources/views/tailwind_2/_table.html.twig +++ b/src/Resources/views/tailwind_2/_table.html.twig @@ -84,7 +84,7 @@
- {% for batchAction in table.getOption('definition').batchActions | filter(action => not action.getOption('voter_attribute') or is_granted(action.getOption('voter_attribute'), action)) %} + {% for batchAction in table.batchActions | filter(action => not action.getOption('voter_attribute') or is_granted(action.getOption('voter_attribute'), action)) %} {% if batchAction.hasConfirmation() %} {{ _self.confirmationLink(batchAction, {}, stimulus_action('@araise/table-bundle/table_select', 'doAction') ~ ' href="' ~ path(batchAction.route, batchAction.routeParameters) ~ '"') }} {% else %} diff --git a/src/Table/Table.php b/src/Table/Table.php index 9fde6b3..e7c269c 100644 --- a/src/Table/Table.php +++ b/src/Table/Table.php @@ -58,6 +58,8 @@ class Table protected array $actions = []; + protected array $batchActions = []; + protected \Traversable $rows; protected bool $loaded = false; @@ -135,11 +137,11 @@ public function getSubTables(object|array $row): array } $subTables = ($this->getOption(self::OPT_SUB_TABLE_LOADER))($row); - if (! $subTables) { + if (!$subTables) { return []; } - if (! is_array($subTables)) { + if (!is_array($subTables)) { $subTables = [$subTables]; } @@ -197,13 +199,13 @@ public function addColumn(string $acronym, $type = null, array $options = [], ?i } if ($this->options[self::OPT_DEFINITION]) { - if (! isset($options[Column::OPT_LABEL])) { + if (!isset($options[Column::OPT_LABEL])) { $options[Column::OPT_LABEL] = sprintf('wwd.%s.property.%s', $this->options[self::OPT_DEFINITION]->getEntityAlias(), $acronym); } } // set link_the_column_content on first column if not set - if (! isset($options[Column::OPT_LINK_THE_COLUMN_CONTENT]) && count($this->columns) === 0) { + if (!isset($options[Column::OPT_LINK_THE_COLUMN_CONTENT]) && count($this->columns) === 0) { $options[Column::OPT_LINK_THE_COLUMN_CONTENT] = true; } @@ -234,12 +236,15 @@ public function removeColumn($acronym): static */ public function getActions(): array { - uasort( - $this->actions, - static fn (Action $a, Action $b) => $a->getOption(Action::OPT_PRIORITY) <=> $b->getOption(Action::OPT_PRIORITY) - ); + return $this->getInternalActions($this->actions); + } - return $this->actions; + /** + * @return Action[] + */ + public function getBatchActions(): array + { + return $this->getInternalActions($this->batchActions); } public function addAction(string $acronym, array $options = [], $type = Action::class): static @@ -249,6 +254,25 @@ public function addAction(string $acronym, array $options = [], $type = Action:: return $this; } + public function addBatchAction(string $acronym, array $options = [], string $type = Action::class): static + { + if (! isset($options['voter_attribute'])) { + $options['voter_attribute'] = 'batch_action'; + } + $this->batchActions[$acronym] = new $type($acronym, $options); + + return $this; + } + + public function removeBatchAction(string $acronym): static + { + if (isset($this->batchActions[$acronym])) { + unset($this->batchActions[$acronym]); + } + + return $this; + } + public function getRows(): \Traversable { $this->loadData(); @@ -333,9 +357,7 @@ public function setParent(?self $parent): void public function hasBatchActions(): bool { return $this->parent === null - && $this->getOption(self::OPT_DEFINITION) - && (enum_exists('araise\CrudBundle\Enums\Page') - && $this->getOption(self::OPT_DEFINITION)::hasCapability(\araise\CrudBundle\Enums\Page::BATCH)) + && count($this->getBatchActions()) > 0 ; } @@ -377,4 +399,17 @@ protected function insertColumnAtPosition($key, $value, $position) } $this->columns = $newArray; } + + /** + * @return Action[] + */ + private function getInternalActions(array $actions): array + { + uasort( + $actions, + static fn (Action $a, Action $b) => $a->getOption(Action::OPT_PRIORITY) <=> $b->getOption(Action::OPT_PRIORITY) + ); + + return $actions; + } }