From ffa1f7e12cc9b2a588e84d425945a7b11eed2272 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 23 Nov 2022 01:25:01 +0100 Subject: [PATCH] Transform member_freeze_change into POST form --- app/Resources/views/member/show.html.twig | 20 +++--- .../Controller/MembershipController.php | 70 +++++++++++++------ 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/app/Resources/views/member/show.html.twig b/app/Resources/views/member/show.html.twig index b660531a2..72b2782ce 100644 --- a/app/Resources/views/member/show.html.twig +++ b/app/Resources/views/member/show.html.twig @@ -209,27 +209,29 @@
{% if is_granted("freeze_change", member) %} + {{ form_start(freeze_change_form) }} {% if not member.frozen %} {% if member.frozenChange %} - + {% else %} - + {% endif %} {% else %} {% if member.frozenChange %} - + {% else %} - - play_arrowDégeler le compte à la fin du cycle - + {% endif %} {% endif %} + {{ form_end(freeze_change_form) }} {% endif %} {% if is_granted("freeze", member) %} {% if not member.frozen %} diff --git a/src/AppBundle/Controller/MembershipController.php b/src/AppBundle/Controller/MembershipController.php index f050304e8..c4070777d 100644 --- a/src/AppBundle/Controller/MembershipController.php +++ b/src/AppBundle/Controller/MembershipController.php @@ -88,6 +88,7 @@ public function showAction(Membership $member) $freezeForm = $this->createFreezeForm($member); $unfreezeForm = $this->createUnfreezeForm($member); + $freezeChangeForm = $this->createFreezeChangeForm($member); $closeForm = $this->createCloseForm($member); $openForm = $this->createOpenForm($member); $deleteForm = $this->createDeleteForm($member); @@ -171,6 +172,7 @@ public function showAction(Membership $member) 'delete_beneficiary_forms' => $deleteBeneficiaryForms, 'freeze_form' => $freezeForm->createView(), 'unfreeze_form' => $unfreezeForm->createView(), + 'freeze_change_form' => $freezeChangeForm->createView(), 'close_form' => $closeForm->createView(), 'open_form' => $openForm->createView(), 'delete_form' => $deleteForm->createView(), @@ -527,14 +529,13 @@ public function openAction(Request $request, Membership $member) { $this->denyAccessUnlessGranted('open', $member); - $current_user = $this->get('security.token_storage')->getToken()->getUser(); - $session = new Session(); - $form = $this->createOpenForm($member); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $session = new Session(); $em = $this->getDoctrine()->getManager(); + $member->setWithdrawn(false); $em->persist($member); $em->flush(); @@ -558,14 +559,13 @@ public function freezeAction(Request $request, Membership $member) { $this->denyAccessUnlessGranted('freeze', $member); - $current_user = $this->get('security.token_storage')->getToken()->getUser(); - $session = new Session(); - $form = $this->createFreezeForm($member); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $session = new Session(); $em = $this->getDoctrine()->getManager(); + $member->setFrozen(true); $member->setFrozenChange(false); $em->persist($member); @@ -590,14 +590,13 @@ public function unfreezeAction(Request $request, Membership $member) { $this->denyAccessUnlessGranted('freeze', $member); - $current_user = $this->get('security.token_storage')->getToken()->getUser(); - $session = new Session(); - $form = $this->createUnfreezeForm($member); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $session = new Session(); $em = $this->getDoctrine()->getManager(); + $member->setFrozen(false); $member->setFrozenChange(false); $em->persist($member); @@ -613,23 +612,33 @@ public function unfreezeAction(Request $request, Membership $member) * Ask freeze status change for user * * @Route("/{id}/freeze_change", name="member_freeze_change") - * @Method({"GET"}) + * @Method({"POST"}) + * @param Request $request * @param Membership $member * @return \Symfony\Component\HttpFoundation\RedirectResponse */ - public function freezeChangeAction(Membership $member) + public function freezeChangeAction(Request $request, Membership $member) { $this->denyAccessUnlessGranted('freeze_change', $member); - $session = new Session(); - $em = $this->getDoctrine()->getManager(); - $member->setFrozenChange(!$member->getFrozenChange()); - $em->persist($member); - $em->flush(); - if ($member->getFrozenChange()) { - $session->getFlashBag()->add('success', 'Le compte sera gelé à la fin du cycle'); - } else { - $session->getFlashBag()->add('success', 'Le compte sera dégelé à la fin du cycle'); + + $form = $this->createFreezeChangeForm($member); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $session = new Session(); + $em = $this->getDoctrine()->getManager(); + + $member->setFrozenChange(!$member->getFrozenChange()); + $em->persist($member); + $em->flush(); + + if ($member->getFrozenChange()) { + $session->getFlashBag()->add('success', 'Le compte sera gelé à la fin du cycle !'); + } else { + $session->getFlashBag()->add('success', 'Le compte sera dégelé à la fin du cycle !'); + } } + if ($this->getCurrentAppUser()->getBeneficiary() && $member === $this->getCurrentAppUser()->getBeneficiary()->getMembership()) { return $this->redirectToRoute("fos_user_profile_show"); } else { @@ -640,7 +649,7 @@ public function freezeChangeAction(Membership $member) /** * Delete member * - * @Route("/delete/{id}", name="member_delete") + * @Route("/{id}", name="member_delete") * @Method("DELETE") * @Security("has_role('ROLE_SUPER_ADMIN')") * @param Request $request @@ -649,16 +658,17 @@ public function freezeChangeAction(Membership $member) */ public function deleteAction(Request $request, Membership $member) { + $session = new Session(); + $form = $this->createDeleteForm($member); $form->handleRequest($request); - $session = new Session(); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->remove($member); $em->flush(); - $session->getFlashBag()->add('success', "Le membre a bien été supprimé"); + $session->getFlashBag()->add('success', "Le membre a bien été supprimé !"); } return $this->redirectToRoute('user_index'); @@ -1073,6 +1083,20 @@ private function createUnfreezeForm(Membership $member) ->getForm(); } + /** + * Creates a form to edit member frozen_change + * + * @param Membership $member + * @return \Symfony\Component\Form\FormInterface + */ + private function createFreezeChangeForm(Membership $member) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('member_freeze_change', array('id' => $member->getId()))) + ->setMethod('POST') + ->getForm(); + } + /** * Creates a form to close a member entity. *