From 10f39b7f45d805c35fa3ef462dcd879d1e5720f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 19 Mar 2019 19:53:23 +0100 Subject: [PATCH] Added a 'clone part' function. --- src/Controller/PartController.php | 33 +++++++++++++++++- src/Entity/DBElement.php | 7 ++++ src/Services/EntityURLGenerator.php | 10 ++++++ src/Twig/AppExtension.php | 2 ++ templates/{ => Parts}/new_part.html.twig | 2 +- templates/Parts/show_part_info.html.twig | 43 ++++++++++++++++++------ templates/base.html.twig | 6 ++-- 7 files changed, 87 insertions(+), 16 deletions(-) rename templates/{ => Parts}/new_part.html.twig (84%) diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index a624d3c9..417ce10a 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -121,7 +121,38 @@ class PartController extends AbstractController } - return $this->render('new_part.html.twig', + return $this->render('Parts/new_part.html.twig', + [ + "part" => $new_part, + "form" => $form->createView() + ]); + } + + /** + * @Route("/part/{id}/clone", name="part_clone") + * + */ + public function clone(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator) + { + + /** @var Part $new_part */ + $new_part = clone($part); + + $this->denyAccessUnlessGranted('create', $new_part); + + $form = $this->createForm(PartType::class, $new_part); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em->persist($new_part); + $em->flush(); + $this->addFlash('success', $translator->trans('part.created_flash')); + return $this->redirectToRoute('part_edit',['id' => $new_part->getID()]); + } + + + return $this->render('Parts/new_part.html.twig', [ "part" => $new_part, "form" => $form->createView() diff --git a/src/Entity/DBElement.php b/src/Entity/DBElement.php index 905584e9..cece1561 100644 --- a/src/Entity/DBElement.php +++ b/src/Entity/DBElement.php @@ -56,6 +56,7 @@ abstract class DBElement return (int) $this->id; } + /** * Returns the ID as an string, defined by the element class. * This should have a form like P000014, for a part with ID 14. @@ -63,4 +64,10 @@ abstract class DBElement */ abstract public function getIDString() : string; + public function __clone() + { + //Set ID to null, so that an new entry is created + $this->id = null; + } + } \ No newline at end of file diff --git a/src/Services/EntityURLGenerator.php b/src/Services/EntityURLGenerator.php index 3c653c49..2087d0b1 100644 --- a/src/Services/EntityURLGenerator.php +++ b/src/Services/EntityURLGenerator.php @@ -90,6 +90,16 @@ class EntityURLGenerator throw new EntityNotSupported('The given entity is not supported yet!'); } + public function cloneURL($entity) : string + { + if($entity instanceof Part) + { + return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); + } + + throw new EntityNotSupported('The given entity is not supported yet!'); + } + /** * Generates an HTML link to the info page about the given entity. * @param $entity mixed The entity for which the info link should be generated. diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 3cd0e5af..2dd72c74 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -68,6 +68,8 @@ class AppExtension extends AbstractExtension return $this->entityURLGenerator->editURL($entity); case 'create': return $this->entityURLGenerator->createURL($entity); + case 'clone': + return $this->entityURLGenerator->cloneURL($entity); } throw new \InvalidArgumentException('method is not supported!'); diff --git a/templates/new_part.html.twig b/templates/Parts/new_part.html.twig similarity index 84% rename from templates/new_part.html.twig rename to templates/Parts/new_part.html.twig index 07fdf8c4..2d67795f 100644 --- a/templates/new_part.html.twig +++ b/templates/Parts/new_part.html.twig @@ -1,4 +1,4 @@ -{% extends "edit_part_info.html.twig" %} +{% extends "Parts/edit_part_info.html.twig" %} {% block card_border %}border-success{% endblock %} {% block card_type %}bg-success text-white{% endblock %} diff --git a/templates/Parts/show_part_info.html.twig b/templates/Parts/show_part_info.html.twig index 464caaa2..a4729f97 100644 --- a/templates/Parts/show_part_info.html.twig +++ b/templates/Parts/show_part_info.html.twig @@ -68,8 +68,8 @@