From 2f884df60219ae94ffe16e0ee56bec6cb02cc822 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 12 Oct 2023 15:25:52 +0200 Subject: [PATCH 1/3] add CrudConfiguration::setGlobalBatchAction method --- src/core/Crud/CrudConfiguration.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/Crud/CrudConfiguration.php b/src/core/Crud/CrudConfiguration.php index 664c0a6..aec5d0f 100644 --- a/src/core/Crud/CrudConfiguration.php +++ b/src/core/Crud/CrudConfiguration.php @@ -135,7 +135,25 @@ class CrudConfiguration ); } - public function setBatchAction(string $page, string $action, string $label, callable $callback): self + public function setGlobalBatchAction( + string $page, + string $action, + string $label, + callable $callback + ): self + { + $this->setBatchAction($page, $action, $name); + $this->batchActions[$page][$action]['isGlobal'] = true; + + return $this; + } + + public function setBatchAction( + string $page, + string $action, + string $label, + callable $callback + ): self { if (!isset($this->batchActions[$page])) { $this->batchActions[$page] = []; @@ -144,6 +162,7 @@ class CrudConfiguration $this->batchActions[$page][$action] = [ 'label' => $label, 'callback' => $callback, + 'isGlobal' => false ]; return $this; From c65cc26be8f130cc0b191558cff4e4f6091933c6 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 12 Oct 2023 15:27:36 +0200 Subject: [PATCH 2/3] batch form is not submitted with XHR when it's a global action --- src/core/Resources/assets/js/modules/batch.js | 28 +++++++++++++------ .../views/admin/crud/index.html.twig | 9 ++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/core/Resources/assets/js/modules/batch.js b/src/core/Resources/assets/js/modules/batch.js index 2d5c1f2..9e11c9f 100644 --- a/src/core/Resources/assets/js/modules/batch.js +++ b/src/core/Resources/assets/js/modules/batch.js @@ -8,16 +8,28 @@ module.exports = () => { const form = $('#form-batch') form.submit((e) => { - e.preventDefault() + const select = document.querySelector('#form-batch-action') + const options = select.querySelectorAll('#form-batch-action option') + let doPrevent = true - const route = form.attr('action') - const datas = form.serialize() + options.forEach((option) => { + if (option.value === select.value && option.getAttribute('data-isglobal') === 'true') { + doPrevent = false + } + }) - form.addClass('is-loading') + if (doPrevent) { + e.preventDefault() - $.post(route, datas) - .always(() => { - document.location.reload() - }) + const route = form.attr('action') + const datas = form.serialize() + + form.addClass('is-loading') + + $.post(route, datas) + .always(() => { + document.location.reload() + }) + } }) } diff --git a/src/core/Resources/views/admin/crud/index.html.twig b/src/core/Resources/views/admin/crud/index.html.twig index cdf4cc0..d8e17f9 100644 --- a/src/core/Resources/views/admin/crud/index.html.twig +++ b/src/core/Resources/views/admin/crud/index.html.twig @@ -283,15 +283,18 @@ {% if configuration.hasBatchAction(context) %}
-
+ - {% for action, conf in configuration.batchActions(context) %} - {% endfor %} From dda43ef3cce3f4abc775fa8980f6e5cd9121e311 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 12 Oct 2023 15:28:14 +0200 Subject: [PATCH 3/3] change CrudController::doBatch to manage a global batch action --- .../Controller/Admin/Crud/CrudController.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/core/Controller/Admin/Crud/CrudController.php b/src/core/Controller/Admin/Crud/CrudController.php index fcd5fab..f86c634 100644 --- a/src/core/Controller/Admin/Crud/CrudController.php +++ b/src/core/Controller/Admin/Crud/CrudController.php @@ -162,7 +162,7 @@ abstract class CrudController extends AdminController $lastRequest = $session->get($lastRequestId); - if ($lastRequest !== null && !$request->isMethod('POST')) { + if (null !== $lastRequest && !$request->isMethod('POST')) { $fakeRequest = Request::create( uri: $request->getUri(), method: 'POST', @@ -284,16 +284,25 @@ abstract class CrudController extends AdminController $query->useFilters($this->filters); + $useSelection = 'selection' === $target; + + if ($batchAction['isGlobal']) { + $result = $callback($query, $useSelection, $entityManager); + + if ($result instanceof Response) { + return $result; + } + + return $this->redirect($request->query->get('redirectTo')); + } if ('selection' === $target) { - $isSelection = true; $pager = $query->paginate($page, $configuration->getMaxPerPage($context)); } else { - $isSelection = false; $pager = $query->find(); } foreach ($pager as $key => $entity) { - if (($isSelection && isset($items[$key + 1])) || !$isSelection) { + if (($useSelection && isset($items[$key + 1])) || !$useSelection) { $callback($entity, $entityManager); } }