From 894034121ee6d3d5cfbc89b2166e9d89faeb634e Mon Sep 17 00:00:00 2001 From: Kinhelm Date: Fri, 19 Jul 2024 11:11:11 +0200 Subject: [PATCH] feature(BatchSelection): add choice to display or not the modal confirmation --- assets/js/app.js | 10 +++++++++- doc/actions.rst | 14 ++++++++++++++ src/Config/Action.php | 7 +++++++ src/Dto/ActionDto.php | 11 +++++++++++ src/Factory/ActionFactory.php | 8 +++++++- 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index 521123a0be..e74c196b1a 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -338,16 +338,24 @@ class App { .replace('%action_name%', actionName) .replace('%num_items%', selectedItems.length.toString()); + if (actionElement.getAttribute('data-display-batch-modal-confirmation') === 'false') { + batchFormSubmit(actionElement, selectedItems); + } + document.querySelector('#modal-batch-action-button').addEventListener('click', () => { // prevent double submission of the batch action form actionElement.setAttribute('disabled', 'disabled'); + batchFormSubmit(actionElement, selectedItems); + }); + function batchFormSubmit(actionElement, selectedItems) { const batchFormFields = { 'batchActionName': actionElement.getAttribute('data-action-name'), 'entityFqcn': actionElement.getAttribute('data-entity-fqcn'), 'batchActionUrl': actionElement.getAttribute('data-action-url'), 'batchActionCsrfToken': actionElement.getAttribute('data-action-csrf-token'), }; + selectedItems.forEach((item, i) => { batchFormFields[`batchActionEntityIds[${i}]`] = item.value; }); @@ -365,7 +373,7 @@ class App { document.body.appendChild(batchForm); batchForm.submit(); - }); + } }); }); } diff --git a/doc/actions.rst b/doc/actions.rst index 419f69de6e..07ff29a449 100644 --- a/doc/actions.rst +++ b/doc/actions.rst @@ -441,6 +441,20 @@ they can link to a CRUD controller method, to a Symfony route or to some URL. If there's at least one batch action, the backend interface is updated to add some "checkboxes" that allow selecting more than one row of the index listing. +By default, batch processes open a confirmation modal before proceeding with the action. +This is useful for batch deletions. +However, if you do not want the confirmation modal, use the ``displayBatchConfirmationModal()`` method:: + + // ... + + return $actions + ->addBatchAction(Action::new('approve', 'Approve Users') + ->linkToCrudAction('approveUsers') + ->addCssClass('btn btn-primary') + ->setIcon('fa fa-user-check') + ->displayBatchConfirmationModal(false) // <- here + ; + When the user clicks on the batch action link/button, a form is submitted using the ``POST`` method to the action or route configured in the action. The easiest way to get the submitted data is to type-hint some argument of your batch action diff --git a/src/Config/Action.php b/src/Config/Action.php index 6488f319ab..cef37ab7df 100644 --- a/src/Config/Action.php +++ b/src/Config/Action.php @@ -88,6 +88,13 @@ public function createAsBatchAction(): self return $this; } + public function displayBatchConfirmationModal(bool $value = true): self + { + $this->dto->setBatchConfirmationModal($value); + + return $this; + } + /** * @param TranslatableInterface|string|false|null $label Use FALSE to hide the label; use NULL to autogenerate it */ diff --git a/src/Dto/ActionDto.php b/src/Dto/ActionDto.php index 45fcccbc69..230622bb31 100644 --- a/src/Dto/ActionDto.php +++ b/src/Dto/ActionDto.php @@ -27,6 +27,7 @@ final class ActionDto private $url; private array $translationParameters = []; private $displayCallable; + private ?bool $batchConfirmationModal = true; public function getType(): string { @@ -261,6 +262,16 @@ public function setDisplayCallable(callable $displayCallable): void $this->displayCallable = $displayCallable; } + public function getBatchConfirmationModal(): ?bool + { + return $this->batchConfirmationModal; + } + + public function setBatchConfirmationModal(?bool $batchConfirmationModal): void + { + $this->batchConfirmationModal = $batchConfirmationModal; + } + /** * @internal */ diff --git a/src/Factory/ActionFactory.php b/src/Factory/ActionFactory.php index e1e0c604a9..0764240f7f 100644 --- a/src/Factory/ActionFactory.php +++ b/src/Factory/ActionFactory.php @@ -159,13 +159,19 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt if ($actionDto->isBatchAction()) { $actionDto->addHtmlAttributes([ - 'data-bs-toggle' => 'modal', 'data-bs-target' => '#modal-batch-action', 'data-action-csrf-token' => $this->csrfTokenManager?->getToken('ea-batch-action-'.$actionDto->getName()), 'data-action-batch' => 'true', 'data-entity-fqcn' => $adminContext->getCrud()->getEntityFqcn(), + 'data-display-batch-modal-confirmation' => $actionDto->getBatchConfirmationModal() ? 'true' : 'false', 'data-action-url' => $actionDto->getLinkUrl(), ]); + + if ($actionDto->getBatchConfirmationModal()) { + $actionDto->addHtmlAttributes([ + 'data-bs-toggle' => 'modal', + ]); + } } return $actionDto;