diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 4a40635..642957e 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Entity\Snip; +use App\Form\ConfirmationType; use App\Form\SnipType; use App\Repository\SnipRepository; use App\Security\Voter\SnipVoter; @@ -96,7 +97,7 @@ class SnipController extends AbstractController $this->repository->save($snip); $this->snipServiceFactory->create($snip)->update($form->get('content')->getData()); - $this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip)); + $this->addFlash('success', sprintf('Snip "%s" saved', $snip)); return $this->redirectToRoute('snip_single', [ 'snip' => $snip->getId(), @@ -118,4 +119,24 @@ class SnipController extends AbstractController return $this->edit($snip, $request); } + + #[Route('/delete/{snip}', name: '_delete')] + public function delete(Snip $snip, Request $request): Response + { + $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); + + $form = $this->createForm(ConfirmationType::class); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $this->snipServiceFactory->create($snip)->deleteRepo(); + $this->repository->remove($snip); + $this->addFlash('success', sprintf('Snip "%s" deleted', $snip)); + return $this->redirectToRoute('snip_index'); + } + + return $this->render('form.html.twig', [ + 'message' => sprintf('Do you really want to delete "%s"?', $snip), + 'form' => $form->createView(), + ]); + } } \ No newline at end of file diff --git a/src/Form/ConfirmationType.php b/src/Form/ConfirmationType.php new file mode 100644 index 0000000..c50b994 --- /dev/null +++ b/src/Form/ConfirmationType.php @@ -0,0 +1,33 @@ +add('Check', CheckboxType::class, [ + 'constraints' => [ + new IsTrue([ + 'message' => $options['message_wrong'], + ]), + ], + ]) + ->add('confirm', SubmitType::class); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'message_wrong' => 'This should be checked.', + ]); + } +} diff --git a/src/Service/SnipService.php b/src/Service/SnipService.php index a50809e..952ff69 100644 --- a/src/Service/SnipService.php +++ b/src/Service/SnipService.php @@ -51,4 +51,9 @@ class SnipService { return $this->repo; } + + public function deleteRepo(): void + { + system("rm -rf ".escapeshellarg($this->repo->getRepositoryPath())); + } } \ No newline at end of file diff --git a/templates/form.html.twig b/templates/form.html.twig new file mode 100644 index 0000000..1b8e7eb --- /dev/null +++ b/templates/form.html.twig @@ -0,0 +1,6 @@ +{% extends 'base/base.html.twig' %} + +{% block body %} +

{{ message }}

+ {{ form(form) }} +{% endblock %} \ No newline at end of file diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index cad79df..f8afa51 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -13,6 +13,9 @@ History + + Delete + {% endif %} Raw