From a9642b27a61c9f60e1d1360d63ad453028a560a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 21 Sep 2022 13:05:52 +0200 Subject: [PATCH] Show error message if a sub element contains parts while recursive deletion of StructuralDBElement --- .../AdminPages/BaseAdminController.php | 17 +++++++++++++++-- translations/messages.en.xlf | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Controller/AdminPages/BaseAdminController.php b/src/Controller/AdminPages/BaseAdminController.php index dbbf9037..f64a8648 100644 --- a/src/Controller/AdminPages/BaseAdminController.php +++ b/src/Controller/AdminPages/BaseAdminController.php @@ -81,6 +81,8 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Contracts\Translation\TranslatorInterface; +use function Symfony\Component\Translation\t; + abstract class BaseAdminController extends AbstractController { protected $entity_class = ''; @@ -419,7 +421,7 @@ abstract class BaseAdminController extends AbstractController /** @var AbstractPartsContainingRepository $repo */ $repo = $this->entityManager->getRepository($this->entity_class); if ($repo->getPartsCount($entity) > 0) { - $this->addFlash('error', 'entity.delete.must_not_contain_parts'); + $this->addFlash('error', t('entity.delete.must_not_contain_parts', ['%PATH%' => $entity->getFullPath()])); return false; } @@ -442,7 +444,18 @@ abstract class BaseAdminController extends AbstractController //Check if we need to remove recursively if ($entity instanceof AbstractStructuralDBElement && $request->get('delete_recursive', false)) { - $recursionHelper->delete($entity, false); + $can_delete = true; + //Check if any of the children can not be deleted, cause it contains parts + $recursionHelper->execute($entity, function (AbstractStructuralDBElement $element) use (&$can_delete) { + if(!$this->deleteCheck($element)) { + $can_delete = false; + } + }); + if($can_delete) { + $recursionHelper->delete($entity, false); + } else { + return $this->redirectToRoute($this->route_base.'_edit', ['id' => $entity->getID()]); + } } else { if ($entity instanceof AbstractStructuralDBElement) { $parent = $entity->getParent(); diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 1b09308f..c1db526d 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -9078,7 +9078,7 @@ Element 3 entity.delete.must_not_contain_parts - Element still contains parts! You have to move the parts, to be able to delete this element. + Element "%PATH%" still contains parts! You have to move the parts, to be able to delete this element.