From 30e0c663dc7c5a4866c95550efed899d49e752cd Mon Sep 17 00:00:00 2001 From: Tchama Date: Thu, 28 Nov 2019 17:37:24 +0100 Subject: [PATCH] adding remove event feature --- Controller/EventController.php | 69 ++++++++++++++++++- Controller/ParticipationController.php | 46 ++++++++----- Resources/config/routing/event.yml | 6 ++ Resources/translations/messages.fr.yml | 4 +- .../views/Event/confirm_delete.html.twig | 6 +- Resources/views/Event/show.html.twig | 10 +-- .../Participation/confirm_delete.html.twig | 20 ++++++ 7 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 Resources/views/Participation/confirm_delete.html.twig diff --git a/Controller/EventController.php b/Controller/EventController.php index d4b336b04..e9e0b10d6 100644 --- a/Controller/EventController.php +++ b/Controller/EventController.php @@ -30,7 +30,6 @@ use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\HttpFoundation\StreamedResponse; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Chill\EventBundle\Security\Authorization\EventVoter; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\Person; @@ -49,6 +48,7 @@ use Chill\MainBundle\Entity\Center; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Translation\TranslatorInterface; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; /** @@ -210,7 +210,7 @@ class EventController extends Controller /** * Finds and displays a Event entity. * - * @paramConverter("event", options={"id" = "event_id"}) + * @ParamConverter("event", options={"id" = "event_id"}) * @param Event $event * @param Request $request * @return \Symfony\Component\HttpFoundation\Response @@ -604,4 +604,69 @@ class EventController extends Controller return $spreadsheet; } + /** + * @param $event_id + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response + */ + public function deleteAction($event_id, Request $request) + { + $em = $this->getDoctrine()->getManager(); + $event = $em->getRepository('ChillEventBundle:Event')->findOneBy([ + 'id' => $event_id + ]); + + if (! $event) { + throw $this->createNotFoundException('Unable to find this event.'); + } + + /** @var array $participations */ + $participations = $event->getParticipations(); + + $form = $this->createDeleteForm($event_id); + + if ($request->getMethod() === Request::METHOD_DELETE) { + $form->handleRequest($request); + + if ($form->isValid()) { + + foreach ($participations as $participation) { + $em->remove($participation); + } + + $em->remove($event); + $em->flush(); + + $this->addFlash('success', $this->get('translator') + ->trans("The event has been sucessfully removed") + ); + + return $this->redirectToRoute('chill_main_search', [ + 'name' => 'event_regular', + 'q' => '@event' + ]); + } + } + return $this->render('ChillEventBundle:Event:confirm_delete.html.twig', [ + 'event_id' => $event->getId(), + 'delete_form' => $form->createView() + ]); + } + + /** + * @param $event_id + * @return \Symfony\Component\Form\FormInterface + */ + private function createDeleteForm($event_id) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('chill_event__event_delete', [ + 'event_id' => $event_id + ])) + ->setMethod('DELETE') + ->add('submit', SubmitType::class, ['label' => 'Delete']) + ->getForm() + ; + } + } diff --git a/Controller/ParticipationController.php b/Controller/ParticipationController.php index dd99e6fed..bb5c03213 100644 --- a/Controller/ParticipationController.php +++ b/Controller/ParticipationController.php @@ -19,8 +19,8 @@ namespace Chill\EventBundle\Controller; +use ArrayIterator; use Chill\EventBundle\Entity\Event; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -31,8 +31,9 @@ use Chill\EventBundle\Security\Authorization\ParticipationVoter; use Symfony\Component\Form\Extension\Core\Type\CollectionType; /** - * + * Class ParticipationController * + * @package Chill\EventBundle\Controller * @author Julien Fastré */ class ParticipationController extends Controller @@ -44,9 +45,9 @@ class ParticipationController extends Controller * This function parse the person_id / persons_ids query argument * and decide if it should process a single or multiple participation. Depending * on this, the appropriate layout and form. - * + * * @param Request $request - * @return Response + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function newAction(Request $request) { @@ -158,9 +159,9 @@ class ParticipationController extends Controller * * If all participations must be ignored, an error is shown and the method redirects * to the event 'show' view with an appropriate flash message. - * + * * @param Request $request - * @return Response + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ protected function newMultiple(Request $request) { @@ -224,6 +225,10 @@ class ParticipationController extends Controller } } + /** + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + */ public function createAction(Request $request) { // test the request is correct @@ -257,6 +262,10 @@ class ParticipationController extends Controller . "'persons_ids' argument in query"); } + /** + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + */ public function createSingle(Request $request) { $participation = $this->handleRequest($request, new Participation(), false); @@ -295,6 +304,10 @@ class ParticipationController extends Controller )); } + /** + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + */ public function createMultiple(Request $request) { $participations = $this->handleRequest($request, new Participation(), true); @@ -431,9 +444,8 @@ class ParticipationController extends Controller } /** - * * @param array $participations - * @return type + * @return \Symfony\Component\Form\FormInterface */ public function createCreateFormMultiple(array $participations) { @@ -491,6 +503,11 @@ class ParticipationController extends Controller )); } + /** + * @param $participation_id + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + */ public function updateAction($participation_id, Request $request) { /* @var $participation Participation */ @@ -640,15 +657,12 @@ class ParticipationController extends Controller } /** - * - * @param \Doctrine\Common\Collections\Collection $participations contains object of Participation type - * @param \Chill\EventBundle\Entity\Event $event + * @param ArrayIterator $participations + * @param Event $event * @return \Symfony\Component\Form\FormInterface */ - protected function createEditFormMultiple( - \Doctrine\Common\Collections\Collection $participations, - \Chill\EventBundle\Entity\Event $event - ) { + protected function createEditFormMultiple(ArrayIterator $participations, Event $event) + { $form = $this->createForm(\Symfony\Component\Form\Extension\Core\Type\FormType::class, array('participations' => $participations), array( 'method' => 'POST', @@ -710,7 +724,7 @@ class ParticipationController extends Controller ]); } } - return $this->render('ChillEventBundle:Event:confirm_delete.html.twig', [ + return $this->render('ChillEventBundle:Participation:confirm_delete.html.twig', [ 'event_id' => $event->getId(), 'delete_form' => $form->createView() ]); diff --git a/Resources/config/routing/event.yml b/Resources/config/routing/event.yml index 840a7cda8..83bedcd9e 100644 --- a/Resources/config/routing/event.yml +++ b/Resources/config/routing/event.yml @@ -41,3 +41,9 @@ chill_event__list_by_person: defaults: { _controller: "ChillEventBundle:Event:listByPerson" } methods: [ GET ] +chill_event__event_delete: + path: /{event_id}/delete + requirements: + event_id: \d+ + defaults: { _controller: "ChillEventBundle:Event:delete" } + methods: [ GET, DELETE ] \ No newline at end of file diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index b2ab63f01..27c77d456 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -44,11 +44,13 @@ The participation was updated: La participation a été mise à jour There are no participation to edit for this event: Il n'y a pas de participation pour cet événement The participations have been successfully updated.: Les participations ont été mises à jour. The participation has been sucessfully removed: La participation a été correctement supprimée. +The event has been sucessfully removed: L'événement et toutes les participations associées ont été correctement supprimées. The participations were created: Les participations ont été créées Events participation: Participation aux événements - Remove participation: Supprimer la participation +Delete event: Supprimer l'événement Are you sure you want to remove that participation ?: Êtes-vous certain de vouloir supprimer cette participation ? +Are you sure you want to remove that event ?: Êtes-vous certain de vouloir supprimer cet événement, ainsi que toutes les participations associées ? #search Event search: Recherche d'événements diff --git a/Resources/views/Event/confirm_delete.html.twig b/Resources/views/Event/confirm_delete.html.twig index f235ebb00..86b38101f 100644 --- a/Resources/views/Event/confirm_delete.html.twig +++ b/Resources/views/Event/confirm_delete.html.twig @@ -2,14 +2,14 @@ {% set activeRouteKey = 'chill_event__event_show' %} -{% block title 'Remove participation'|trans %} +{% block title 'Delete event'|trans %} {% block event_content %} {{ include('ChillMainBundle:Util:confirmation_template.html.twig', { - 'title' : 'Remove participation'|trans, - 'confirm_question' : 'Are you sure you want to remove that participation ?'|trans, + 'title' : 'Delete event'|trans, + 'confirm_question' : 'Are you sure you want to remove that event ?'|trans, 'cancel_route' : activeRouteKey, 'cancel_parameters' : { 'event_id' : event_id }, 'form' : delete_form diff --git a/Resources/views/Event/show.html.twig b/Resources/views/Event/show.html.twig index e818cab46..7ca6917a7 100644 --- a/Resources/views/Event/show.html.twig +++ b/Resources/views/Event/show.html.twig @@ -57,6 +57,10 @@ {% endif %} +
  • + {{ 'Delete event'|trans }} +
  • @@ -87,13 +91,11 @@ {% if is_granted('CHILL_EVENT_PARTICIPATION_UPDATE', participation) %}
  • - {{ 'Edit'|trans }} - + class="sc-button bt-edit" title="{{ 'Edit'|trans }}">
  • + class="sc-button bt-delete" title="{{ 'Delete'|trans }}">
  • {% endif %} diff --git a/Resources/views/Participation/confirm_delete.html.twig b/Resources/views/Participation/confirm_delete.html.twig new file mode 100644 index 000000000..f235ebb00 --- /dev/null +++ b/Resources/views/Participation/confirm_delete.html.twig @@ -0,0 +1,20 @@ +{% extends 'ChillEventBundle::layout.html.twig' %} + +{% set activeRouteKey = 'chill_event__event_show' %} + +{% block title 'Remove participation'|trans %} + +{% block event_content %} + + {{ include('ChillMainBundle:Util:confirmation_template.html.twig', + { + 'title' : 'Remove participation'|trans, + 'confirm_question' : 'Are you sure you want to remove that participation ?'|trans, + 'cancel_route' : activeRouteKey, + 'cancel_parameters' : { 'event_id' : event_id }, + 'form' : delete_form + } + ) }} + +{% endblock %} +