From 4dfbae6d169a3bf8c30cc72cc187ffc5006ac59e Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 5 Sep 2023 13:14:13 +0200 Subject: [PATCH] =?UTF-8?q?Membre=20&=20Profil=20:=20am=C3=A9liorations=20?= =?UTF-8?q?de=20la=20gestion=20des=20badges=20(#978)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Profile: move template to beneficiary partials * Create member partial * Refactor add_modal template * Common image template * Cleanup enable, disable and delete views --- .../views/Profile/show_content.html.twig | 2 +- .../_partial/swipe_cards.html.twig | 30 +++ .../member/_partial/swipe_card.html.twig | 95 --------- .../member/_partial/swipe_cards.html.twig | 33 ++++ app/Resources/views/member/show.html.twig | 49 +---- .../swipeCard/_partial/add_modal.html.twig | 51 +++++ .../_partial/disable_modal.html.twig | 18 ++ .../views/swipeCard/_partial/image.html.twig | 5 + .../views/swipeCard/_partial/list.html.twig | 99 +++++----- .../Controller/SwipeCardController.php | 181 +++++++++--------- 10 files changed, 277 insertions(+), 286 deletions(-) create mode 100644 app/Resources/views/beneficiary/_partial/swipe_cards.html.twig delete mode 100644 app/Resources/views/member/_partial/swipe_card.html.twig create mode 100644 app/Resources/views/member/_partial/swipe_cards.html.twig create mode 100644 app/Resources/views/swipeCard/_partial/add_modal.html.twig create mode 100644 app/Resources/views/swipeCard/_partial/disable_modal.html.twig create mode 100644 app/Resources/views/swipeCard/_partial/image.html.twig diff --git a/app/Resources/views/Profile/show_content.html.twig b/app/Resources/views/Profile/show_content.html.twig index 20bb59129..ff120897e 100644 --- a/app/Resources/views/Profile/show_content.html.twig +++ b/app/Resources/views/Profile/show_content.html.twig @@ -72,7 +72,7 @@ credit_cardBadge{% if beneficiary.swipeCards | length > 1 %}s{% endif %}
- {% include "member/_partial/swipe_card.html.twig" with { member: member, show_badge_image: true } %} + {% include "beneficiary/_partial/swipe_cards.html.twig" with { beneficiary: beneficiary, show_badge_image: true } %}
{% endif %} diff --git a/app/Resources/views/beneficiary/_partial/swipe_cards.html.twig b/app/Resources/views/beneficiary/_partial/swipe_cards.html.twig new file mode 100644 index 000000000..ad35b586d --- /dev/null +++ b/app/Resources/views/beneficiary/_partial/swipe_cards.html.twig @@ -0,0 +1,30 @@ +{% if beneficiary.getEnabledSwipeCards | length %} + {% if (show_badge_image ?? false) %} + {% include "swipeCard/_partial/image.html.twig" with { card: beneficiary.getEnabledSwipeCards.first, with_badge_card: true } %} + {% endif %} + Badge perdu ? + {% include "swipeCard/_partial/disable_modal.html.twig" with { card: beneficiary.getEnabledSwipeCards.first } %} +{% else %} +

Aucun badge actif

+ + addAssocier un badge à ton compte + + {% include "swipeCard/_partial/add_modal.html.twig" with { beneficiary: beneficiary } %} +{% endif %} + +{% if beneficiary.getSwipeCards | length %} +
+
+ + credit_card{% if beneficiary.getSwipeCards | length > 1 %}Vos badges{% else %}Ton badge{% endif %} + + +{% endif %} diff --git a/app/Resources/views/member/_partial/swipe_card.html.twig b/app/Resources/views/member/_partial/swipe_card.html.twig deleted file mode 100644 index cccc54413..000000000 --- a/app/Resources/views/member/_partial/swipe_card.html.twig +++ /dev/null @@ -1,95 +0,0 @@ -{% if app.user.beneficiary.getSwipeCards | length %} - - credit_card{% if app.user.beneficiary.getSwipeCards | length > 1 %}Vos badges{% else %}Ton badge{% endif %} - -
- -{% endif %} -{% if app.user.beneficiary.getEnabledSwipeCards | length %} - {% set card = app.user.beneficiary.getEnabledSwipeCards.first %} - {% if (show_badge_image ?? false) %} -
- barecode -
-
- {% endif %} - Badge perdu ? -
-
- -{% else %} - - addAssocier un badge à ton compte - - -{% endif %} diff --git a/app/Resources/views/member/_partial/swipe_cards.html.twig b/app/Resources/views/member/_partial/swipe_cards.html.twig new file mode 100644 index 000000000..f6586f69d --- /dev/null +++ b/app/Resources/views/member/_partial/swipe_cards.html.twig @@ -0,0 +1,33 @@ +
+ {% for beneficiary in member.beneficiaries %} +
+

{{ beneficiary.firstname }}

+ {% if beneficiary.getEnabledSwipeCards | length %} + {% include "swipeCard/_partial/image.html.twig" with { card: beneficiary.getEnabledSwipeCards.first, with_badge_card: true } %} + {% else %} +

Aucun badge actif

+ + addAssocier un badge à ce compte + + {% include "swipeCard/_partial/add_modal.html.twig" with { beneficiary: beneficiary, from_admin: true } %} + {% endif %} + + {% if beneficiary.getSwipeCards | length %} +
+
+ + credit_cardBadges de {{ beneficiary.firstname }} + + + {% endif %} +
+ {% endfor %} +
diff --git a/app/Resources/views/member/show.html.twig b/app/Resources/views/member/show.html.twig index a31f3894b..34de07389 100644 --- a/app/Resources/views/member/show.html.twig +++ b/app/Resources/views/member/show.html.twig @@ -113,54 +113,7 @@ credit_cardBadges
-
- {% for beneficiary in member.beneficiaries %} -
-

{{ beneficiary.firstname }}

- {% if (beneficiary.getEnabledSwipeCards | length == 0) %} -

Aucun badge actif

- addAssocier un badge à ce compte - - {% else %} -
- barecode -
-
- {% endif %} - {% if beneficiary.getSwipeCards | length %} - credit_cardBadges de {{ beneficiary.firstname }} - - {% endif %} -
- {% endfor %} -
+ {% include "member/_partial/swipe_cards.html.twig" with { member: member } %}
{% endif %} diff --git a/app/Resources/views/swipeCard/_partial/add_modal.html.twig b/app/Resources/views/swipeCard/_partial/add_modal.html.twig new file mode 100644 index 000000000..14968fb8a --- /dev/null +++ b/app/Resources/views/swipeCard/_partial/add_modal.html.twig @@ -0,0 +1,51 @@ +{% set from_admin = from_admin ?? false %} + + + + diff --git a/app/Resources/views/swipeCard/_partial/disable_modal.html.twig b/app/Resources/views/swipeCard/_partial/disable_modal.html.twig new file mode 100644 index 000000000..b373cc36e --- /dev/null +++ b/app/Resources/views/swipeCard/_partial/disable_modal.html.twig @@ -0,0 +1,18 @@ + diff --git a/app/Resources/views/swipeCard/_partial/image.html.twig b/app/Resources/views/swipeCard/_partial/image.html.twig new file mode 100644 index 000000000..7fab5a111 --- /dev/null +++ b/app/Resources/views/swipeCard/_partial/image.html.twig @@ -0,0 +1,5 @@ +{% set with_badge_card = with_badge_card ?? false %} + +
+ barcode +
diff --git a/app/Resources/views/swipeCard/_partial/list.html.twig b/app/Resources/views/swipeCard/_partial/list.html.twig index 5f6b1aa58..d85bb2c37 100644 --- a/app/Resources/views/swipeCard/_partial/list.html.twig +++ b/app/Resources/views/swipeCard/_partial/list.html.twig @@ -1,27 +1,31 @@ +{% set from_admin = from_admin ?? false %} +{% set swipeCards = beneficiary.swipeCards | reverse %} + {% if swipeCards | length %}
- {% for swipeCard in swipeCards %} -
-
-
Badge n°{{ swipeCard.number }}
- barecode -

- associé : {{ swipeCard.createdAt | date_time }}
- {% if swipeCard.disabledAt %}désactivé : {{ swipeCard.disabledAt | date_time }}{% endif %} -

-
- -
- -
-
-
- {% endfor %} + {% for swipeCard in swipeCards %} +
+
+
Badge n°{{ swipeCard.number }}
+ {% include "swipeCard/_partial/image.html.twig" with { card: swipeCard } %} +

+ associé : {{ swipeCard.createdAt | date_time }}
+ {% if swipeCard.disabledAt %}désactivé : {{ swipeCard.disabledAt | date_time }}{% endif %} +

+
+ + +
+ +
+
+
+ {% endfor %}
@@ -38,40 +42,31 @@ {% for swipeCard in swipeCards %} - + diff --git a/src/AppBundle/Controller/SwipeCardController.php b/src/AppBundle/Controller/SwipeCardController.php index 601c2b91c..f8ebd07e6 100644 --- a/src/AppBundle/Controller/SwipeCardController.php +++ b/src/AppBundle/Controller/SwipeCardController.php @@ -37,217 +37,218 @@ class SwipeCardController extends Controller * @return Response * @Route("/in/{code}", name="swipe_in", methods={"GET"}) */ - public function swipeInAction(Request $request, $code){ + public function swipeInAction(Request $request, $code) + { $session = new Session(); - $code = $this->get('AppBundle\Helper\SwipeCard')->vigenereDecode($code); $em = $this->getDoctrine()->getManager(); + + $code = $this->get('AppBundle\Helper\SwipeCard')->vigenereDecode($code); $card = $em->getRepository('AppBundle:SwipeCard')->findLastEnable($code); - if (!$card){ + + if (!$card) { $session->getFlashBag()->add("error","Oups, ce badge n'est pas actif ou n'est pas associé à un compte"); $card = $em->getRepository('AppBundle:SwipeCard')->findOneBy(array("code"=>$code)); if ($card && !$card->getEnable() && !$card->getDisabledAt()) $session->getFlashBag()->add("warning","Si c'est le tiens, generateUrl('fos_user_security_login')."\">connecte toi sur ton espace membre pour l'activer"); - }else{ + } else { $user = $card->getBeneficiary()->getUser(); $token = new UsernamePasswordToken($user, $user->getPassword(), "main", $user->getRoles()); $this->get("security.token_storage")->setToken($token); $event = new InteractiveLoginEvent($request, $token); $this->get("event_dispatcher")->dispatch("security.interactive_login", $event); } + return $this->redirectToRoute('homepage'); } - public function homepageAction(){ + public function homepageAction() + { return $this->render('user/swipe_card/homepage.html.twig'); } /** - * activate / pair Swipe Card + * activate (pair) Swipe Card * - * @param Request $request - * @param Beneficiary $beneficiary - * @return Response - * @Route("/active/", name="active_swipe", methods={"GET","POST"}) - * @Route("/active/{id}", name="active_swipe_for_beneficiary", methods={"POST"}) + * @Route("/activate", name="activate_swipe", methods={"POST"}) * @Security("has_role('ROLE_USER')") */ - public function activeSwipeCardAction(Request $request,Beneficiary $beneficiary = null) + public function activateSwipeCardAction(Request $request) { $session = new Session(); + $em = $this->getDoctrine()->getManager(); $this->denyAccessUnlessGranted(SwipeCardVoter::PAIR, new SwipeCard()); - $referer = $request->headers->get('referer'); + $current_user = $this->get('security.token_storage')->getToken()->getUser(); + $referer = $request->headers->get('referer'); $code = $request->get("code"); - //verify code : + $beneficiaryId = $request->get("beneficiary"); + + // verify code if (!SwipeCard::checkEAN13($code)) { $session->getFlashBag()->add('error', 'Hum, ces chiffres ne correspondent pas à un code badge valide... 🤔'); return new RedirectResponse($referer); } - $code = substr($code, 0, -1); //remove controle - if ($code === '421234567890'){ - $session->getFlashBag()->add('warning', 'Hihi, ceci est le numéro d’exemple 😁 Utilise un badge physique 🍌'); + // remove controle + $code = substr($code, 0, -1); + if ($code === '421234567890') { + $session->getFlashBag()->add('warning', 'Hihi, ceci est le numéro d\'exemple 😁 Utilise un badge physique 🍌'); return new RedirectResponse($referer); } - $em = $this->getDoctrine()->getManager(); - if (!$beneficiary){ - $beneficiary = $this->getUser()->getBeneficiary(); - } - $cards = $beneficiary->getEnabledSwipeCards(); - if ($cards->count()) { - if ($beneficiary->getUser() === $this->getUser()) + // get beneficiary + $beneficiary = $em->getRepository('AppBundle:Beneficiary')->find($beneficiaryId); + + // beneficiary should have 0 enabled cards + $beneficiaryCards = $beneficiary->getEnabledSwipeCards(); + if ($beneficiaryCards->count()) { + if ($current_user === $beneficiary->getUser()) { $session->getFlashBag()->add('error', 'Ton compte possède déjà un badge actif'); - else + } else { $session->getFlashBag()->add('error', 'Il existe déjà un badge actif associé à ce compte'); + } return new RedirectResponse($referer); } + // card should not be already in use $card = $em->getRepository('AppBundle:SwipeCard')->findOneBy(array('code' => $code)); - if ($card) { - if ($card->getBeneficiary() != $this->getUser()->getBeneficiary()) { + if ($beneficiary != $card->getBeneficiary()) { $session->getFlashBag()->add('error', 'Ce badge est déjà associé à un autre utilisateur 👮'); } else { - $session->getFlashBag()->add('error', 'Oups ! Ce badge est déjà associé mais il est inactif. Reactive le !'); + $session->getFlashBag()->add('error', 'Oups ! Ce badge est déjà associé mais il est inactif. Réactive-le !'); } return new RedirectResponse($referer); - } else { - $lastCard = $em->getRepository('AppBundle:SwipeCard')->findLast($this->getUser()->getBeneficiary()); - $card = new SwipeCard(); - $card->setBeneficiary($beneficiary); - $card->setCode($code); - $card->setNumber($lastCard ? max($lastCard->getNumber(),$beneficiary->getSwipeCards()->count()) + 1 : 1); - $card->setEnable(1); - $em->persist($card); - $em->flush(); - $session->getFlashBag()->add('success', 'Le badge ' . $card->getcode() . ' a bien été associé à ton compte.'); - return new RedirectResponse($referer); } + + $lastCard = $em->getRepository('AppBundle:SwipeCard')->findLast($beneficiary); + $card = new SwipeCard(); + $card->setBeneficiary($beneficiary); + $card->setCode($code); + $card->setNumber($lastCard ? max($lastCard->getNumber(),$beneficiary->getSwipeCards()->count()) + 1 : 1); + $card->setEnable(1); + $em->persist($card); + $em->flush(); + + $session->getFlashBag()->add('success', 'Le badge ' . $card->getcode() . ' a bien été associé à ton compte.'); + return new RedirectResponse($referer); } /** * enable existing Swipe Card * - * @param Request $request - * @param Beneficiary $beneficiary - * @return Response - * @Route("/enable/", name="enable_swipe") - * @Route("/enable/{id}", name="enable_swipe_for_beneficiary", methods={"POST"}) + * @Route("/enable", name="enable_swipe", methods={"POST"}) * @Security("has_role('ROLE_USER')") */ - public function enableSwipeCardAction(Request $request,Beneficiary $beneficiary = null){ + public function enableSwipeCardAction(Request $request) + { $session = new Session(); - $referer = $request->headers->get('referer'); + $em = $this->getDoctrine()->getManager(); + $current_user = $this->get('security.token_storage')->getToken()->getUser(); + $referer = $request->headers->get('referer'); $code = $request->get("code"); $code = $this->get('AppBundle\Helper\SwipeCard')->vigenereDecode($code); + $beneficiaryId = $request->get("beneficiary"); - $em = $this->getDoctrine()->getManager(); - if (!$beneficiary){ - $beneficiary = $this->getUser()->getBeneficiary(); - } - $cards = $beneficiary->getEnabledSwipeCards(); - if ($cards->count()) { + // get beneficiary + $beneficiary = $em->getRepository('AppBundle:Beneficiary')->find($beneficiaryId); + + // beneficiary should have 0 enabled cards + $beneficiaryCards = $beneficiary->getEnabledSwipeCards(); + if ($beneficiaryCards->count()) { $session->getFlashBag()->add('error', 'Tu as déjà un badge actif'); return new RedirectResponse($referer); } - /** @var SwipeCard $card */ $card = $em->getRepository('AppBundle:SwipeCard')->findOneBy(array('code' => $code)); - if ($card) { $this->denyAccessUnlessGranted(SwipeCardVoter::ENABLE, $card); - if ($card->getBeneficiary() != $beneficiary) { - if ($beneficiary === $this->getUser()->getBeneficiary()) + if ($beneficiary != $card->getBeneficiary()) { + if ($current_user === $beneficiary->getUser()) $session->getFlashBag()->add('error', 'Ce badge ne t\'appartient pas'); else - $session->getFlashBag()->add('error', 'Ce badge n\'appartient pas au beneficiaire'); + $session->getFlashBag()->add('error', 'Ce badge n\'appartient pas au bénéficiaire'); } else { $card->setEnable(true); $card->setDisabledAt(null); $em->persist($card); $em->flush(); - $session->getFlashBag()->add('success', 'Le badge #' . $card->getNumber() . ' a bien été ré-activé'); + $session->getFlashBag()->add('success', 'Le badge #' . $card->getNumber() . ' a bien été réactivé'); } } else { $session->getFlashBag()->add('error', 'Aucun badge ne correspond à ce code'); } + return new RedirectResponse($referer); } /** * disable Swipe Card * - * @param Request $request - * @param Beneficiary $beneficiary - * @return Response - * @Route("/disable/", name="disable_swipe") - * @Route("/disable/{id}", name="disable_swipe_for_beneficiary", methods={"POST"}) + * @Route("/disable", name="disable_swipe", methods={"POST"}) * @Security("has_role('ROLE_USER')") */ - public function disableSwipeCardAction(Request $request,Beneficiary $beneficiary = null){ + public function disableSwipeCardAction(Request $request) + { $session = new Session(); - $referer = $request->headers->get('referer'); + $em = $this->getDoctrine()->getManager(); + $current_user = $this->get('security.token_storage')->getToken()->getUser(); + $referer = $request->headers->get('referer'); $code = $request->get("code"); $code = $this->get('AppBundle\Helper\SwipeCard')->vigenereDecode($code); + $beneficiaryId = $request->get("beneficiary"); - $em = $this->getDoctrine()->getManager(); - /** @var SwipeCard $card */ - $card = $em->getRepository('AppBundle:SwipeCard')->findOneBy(array('code'=>$code)); - if (!$beneficiary){ - $beneficiary = $this->getUser()->getBeneficiary(); - } + // get beneficiary + $beneficiary = $em->getRepository('AppBundle:Beneficiary')->find($beneficiaryId); - if ($card){ + $card = $em->getRepository('AppBundle:SwipeCard')->findOneBy(array('code'=>$code)); + if ($card) { $this->denyAccessUnlessGranted(SwipeCardVoter::DISABLE, $card); - if ($card->getBeneficiary() != $beneficiary) { - if ($beneficiary === $this->getUser()->getBeneficiary()) + if ($beneficiary != $card->getBeneficiary()) { + if ($current_user === $beneficiary->getUser()) $session->getFlashBag()->add('error', 'Ce badge ne t\'appartient pas'); else - $session->getFlashBag()->add('error', 'Ce badge n\'appartient pas au beneficiaire'); + $session->getFlashBag()->add('error', 'Ce badge n\'appartient pas au bénéficiaire'); } else { $card->setEnable(false); $em->persist($card); $em->flush(); $session->getFlashBag()->add('success','Ce badge est maintenant désactivé'); } - }else{ + } else { $session->getFlashBag()->add('error','Aucune badge trouvé'); } + return new RedirectResponse($referer); } /** * remove Swipe Card * - * @param Request $request - * @return Response - * @Route("/delete/", name="delete_swipe", methods={"POST"}) + * @Route("/delete", name="delete_swipe", methods={"POST"}) * @Security("has_role('ROLE_ADMIN')") */ - public function deleteAction(Request $request){ + public function deleteAction(Request $request) + { $session = new Session(); - $referer = $request->headers->get('referer'); + $em = $this->getDoctrine()->getManager(); + $referer = $request->headers->get('referer'); $code = $request->get("code"); $code = $this->get('AppBundle\Helper\SwipeCard')->vigenereDecode($code); - $em = $this->getDoctrine()->getManager(); - /** @var SwipeCard $card */ $card = $em->getRepository('AppBundle:SwipeCard')->findOneBy(array('code'=>$code)); - if ($card){ - if (!$this->get('security.authorization_checker')->isGranted(SwipeCardVoter::DELETE, $card)) { - $session->getFlashBag()->add('error','Tu ne peux pas supprimer ce badge'); - return new RedirectResponse($referer); - } + if ($card) { + $this->denyAccessUnlessGranted(SwipeCardVoter::DELETE, $card); $em->remove($card); $em->flush(); - $session->getFlashBag()->add('success','Le badge '.$code.' a bien été supprimé'); - }else{ + $session->getFlashBag()->add('success','Le badge a bien été supprimé'); + } else { $session->getFlashBag()->add('error','Aucune badge trouvé'); } + return new RedirectResponse($referer); }
{{ swipeCard.number }}barecode + {% include "swipeCard/_partial/image.html.twig" with { card: swipeCard } %} + {{ swipeCard.createdAt | date_fr_full_with_time }} {% if swipeCard.disabledAt %}{{ swipeCard.disabledAt | date_fr_full_with_time }}{% endif %} - {% if beneficiary_id > 0 %} -
- -
- -
-
- {% else %} -
- -
- -
-
- {% endif %} - {% if is_granted("delete", swipeCard) %} -
-
+ + + +
+ +
+
+ {% if from_admin and is_granted("delete", swipeCard) %} +
+
- +
{% endif %}