diff --git a/src/Controller/AttachmentTypeController.php b/src/Controller/AttachmentTypeController.php index b3c4ae31..e9fd62e1 100644 --- a/src/Controller/AttachmentTypeController.php +++ b/src/Controller/AttachmentTypeController.php @@ -40,6 +40,7 @@ use App\Form\ExportType; use App\Form\ImportType; use App\Services\EntityExporter; use App\Services\EntityImporter; +use App\Services\StructuralElementRecursionHelper; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -135,24 +136,32 @@ class AttachmentTypeController extends AbstractController /** * @Route("/{id}", name="attachment_type_delete", methods={"DELETE"}) */ - public function delete(Request $request, AttachmentType $entity) + public function delete(Request $request, AttachmentType $entity, StructuralElementRecursionHelper $recursionHelper) { $this->denyAccessUnlessGranted('delete', $entity); if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) { $entityManager = $this->getDoctrine()->getManager(); - $parent = $entity->getParent(); + //Check if we need to remove recursively + if ($request->get('delete_recursive', false)) { + $recursionHelper->delete($entity, false); + } else { + $parent = $entity->getParent(); - //Move all sub entities to the current parent - foreach ($entity->getSubelements() as $subelement) { - $subelement->setParent($parent); - $entityManager->persist($subelement); + //Move all sub entities to the current parent + foreach ($entity->getSubelements() as $subelement) { + $subelement->setParent($parent); + $entityManager->persist($subelement); + } + + //Remove current element + $entityManager->remove($entity); } - //Remove current element - $entityManager->remove($entity); + //Flush changes $entityManager->flush(); + $this->addFlash('success', 'attachment_type.deleted'); } diff --git a/src/Services/StructuralElementRecursionHelper.php b/src/Services/StructuralElementRecursionHelper.php new file mode 100644 index 00000000..33e64913 --- /dev/null +++ b/src/Services/StructuralElementRecursionHelper.php @@ -0,0 +1,102 @@ +em = $em; + } + + /** + * Executes an function (callable) recursivly for $element and every of its children. + * + * @param StructuralDBElement $element The element on which the func should be executed + * @param callable $func The function which should be executed for each element. + * $func has the signature function(StructuralDBElement $element) : void + * @param int $max_depth The maximum depth for which should be recursivly called. So if this is set to 5, after the + * 5th level the execution is stopped. + * @param bool $call_from_bottom If set to true the bottom elements (elements with high level) will be called first. + * Set to false if you want to call the top elements first. + */ + public function execute(StructuralDBElement $element, callable $func, int $max_depth = -1, $call_from_bottom = true) : void + { + //Cancel if we reached our maximal allowed level. Must be zero because -1 is infinity levels + if ($max_depth == 0) { + return; + } + + //Get children of the current class: + $children = $element->getChildren(); + + //If we should call from top we execute the func here. + if (!$call_from_bottom) { + $func($element); + } + + foreach ($children as $child) { + $this->execute($child, $func, $max_depth - 1); + } + + //Otherwise we call it here + if ($call_from_bottom) { + $func($element); + } + } + + /** + * Deletes the $element and all its subelements recursivly. + * @param StructuralDBElement $element The element which should be deleted. + * @param bool $flush When set to true the changes will also be flushed to DB. Set to false if you want to flush + * later. + */ + public function delete(StructuralDBElement $element, bool $flush = true) : void + { + $em = $this->em; + + $this->execute($element, static function(StructuralDBElement $element) use ($em) { + $em->remove($element); + }); + + if($flush) { + $em->flush(); + } + } +} \ No newline at end of file diff --git a/templates/AdminPages/_delete_form.html.twig b/templates/AdminPages/_delete_form.html.twig index 5bac94cd..3ed3456d 100644 --- a/templates/AdminPages/_delete_form.html.twig +++ b/templates/AdminPages/_delete_form.html.twig @@ -7,6 +7,10 @@
+
+ + +