diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index fa83c105..823326ad 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -30,12 +30,15 @@ namespace App\Controller; +use App\Entity\Base\NamedDBElement; +use App\Entity\Base\StructuralDBElement; use App\Entity\Parts\Category; use App\Entity\Parts\Part; use App\Form\AttachmentFormType; use App\Form\Part\PartBaseType; use App\Services\AttachmentHelper; use App\Services\PricedetailHelper; +use App\Services\StructuralElementRecursionHelper; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; @@ -43,11 +46,15 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Contracts\Translation\TranslatorInterface; +/** + * @Route("/part") + * @package App\Controller + */ class PartController extends AbstractController { /** - * @Route("/part/{id}/info", name="part_info") - * @Route("/part/{id}", requirements={"id"="\d+"}) + * @Route("/{id}/info", name="part_info") + * @Route("/{id}", requirements={"id"="\d+"}) * @param Part $part * @param AttachmentHelper $attachmentHelper * @return \Symfony\Component\HttpFoundation\Response @@ -66,7 +73,7 @@ class PartController extends AbstractController } /** - * @Route("/part/{id}/edit", name="part_edit") + * @Route("/{id}/edit", name="part_edit") * * @param Part $part * @@ -109,7 +116,32 @@ class PartController extends AbstractController } /** - * @Route("/part/new", name="part_new") + * @Route("/{id}/delete", name="part_delete", methods={"DELETE"}) + * @param Request $request + * @param Part $part + * @return \Symfony\Component\HttpFoundation\RedirectResponse + */ + public function delete(Request $request, Part $part) + { + $this->denyAccessUnlessGranted('delete', $part); + + if ($this->isCsrfTokenValid('delete' . $part->getId(), $request->request->get('_token'))) { + $entityManager = $this->getDoctrine()->getManager(); + + //Remove part + $entityManager->remove($part); + + //Flush changes + $entityManager->flush(); + + $this->addFlash('success', 'part.deleted'); + } + + return $this->redirectToRoute('homepage'); + } + + /** + * @Route("/new", name="part_new") * * @param Request $request * @param EntityManagerInterface $em @@ -158,7 +190,7 @@ class PartController extends AbstractController } /** - * @Route("/part/{id}/clone", name="part_clone") + * @Route("/{id}/clone", name="part_clone") * @param Part $part * @param Request $request * @param EntityManagerInterface $em diff --git a/src/Form/Part/OrderdetailType.php b/src/Form/Part/OrderdetailType.php index 300ba0fe..b9303b5c 100644 --- a/src/Form/Part/OrderdetailType.php +++ b/src/Form/Part/OrderdetailType.php @@ -84,7 +84,7 @@ class OrderdetailType extends AbstractType $orderdetail = $event->getData(); $dummy_pricedetail = new Pricedetail(); - if ($orderdetail->getSupplier() !== null) { + if ($orderdetail !== null && $orderdetail->getSupplier() !== null) { $dummy_pricedetail->setCurrency($orderdetail->getSupplier()->getDefaultCurrency()); } diff --git a/src/Services/EntityURLGenerator.php b/src/Services/EntityURLGenerator.php index 726f3f35..a00a92e9 100644 --- a/src/Services/EntityURLGenerator.php +++ b/src/Services/EntityURLGenerator.php @@ -285,6 +285,10 @@ class EntityURLGenerator public function deleteURL($entity) : string { + if ($entity instanceof Part) { + return $this->urlGenerator->generate('part_delete', ['id' => $entity->getID()]); + } + if ($entity instanceof AttachmentType) { return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); } diff --git a/templates/Parts/info/_tools.html.twig b/templates/Parts/info/_tools.html.twig index 44b3107f..1b7a84aa 100644 --- a/templates/Parts/info/_tools.html.twig +++ b/templates/Parts/info/_tools.html.twig @@ -23,6 +23,17 @@ +{% endif %} - -{% endif %} \ No newline at end of file +
\ No newline at end of file