|  | @@ -0,0 +1,142 @@
 | 
	
		
			
				|  |  | +<?php
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +namespace App\Controller;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 | 
	
		
			
				|  |  | +use Symfony\Component\HttpFoundation\Request;
 | 
	
		
			
				|  |  | +use Symfony\Component\HttpFoundation\Response;
 | 
	
		
			
				|  |  | +use Symfony\Component\Routing\Requirement\Requirement;
 | 
	
		
			
				|  |  | +use Symfony\Component\Routing\Attribute\Route;
 | 
	
		
			
				|  |  | +use Doctrine\ORM\EntityManagerInterface;
 | 
	
		
			
				|  |  | +use Symfony\Bridge\Twig\Mime\TemplatedEmail;
 | 
	
		
			
				|  |  | +use Symfony\Component\Mime\Address;
 | 
	
		
			
				|  |  | +use Symfony\Component\Mailer\MailerInterface;
 | 
	
		
			
				|  |  | +use Symfony\Component\Mime\Email;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use App\Entity\Slot;
 | 
	
		
			
				|  |  | +use App\Entity\Party;
 | 
	
		
			
				|  |  | +use App\Entity\Event;
 | 
	
		
			
				|  |  | +use App\Entity\Participation;
 | 
	
		
			
				|  |  | +use App\Repository\SlotRepository;
 | 
	
		
			
				|  |  | +use App\Form\ParticipationType;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +final class ParticipationController extends AbstractController
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    #[Route('/cancel/{id}', name: 'app_participation_cancel', requirements: ['id' => Requirement::UUID_V7], methods: ['GET', 'POST'])]
 | 
	
		
			
				|  |  | +    public function cancel(?Participation $participation, Request $request, EntityManagerInterface $manager): Response
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        if (!$participation) {
 | 
	
		
			
				|  |  | +            // TGCM, ça ressemble à un UUID, mais y'a rien derrière
 | 
	
		
			
				|  |  | +            $this->addFlash('danger', 'Participation inexistante !');
 | 
	
		
			
				|  |  | +            return $this->redirectToRoute('app_main');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $form = $this->createFormBuilder(FormType::class)->getForm();
 | 
	
		
			
				|  |  | +        $form->handleRequest($request);
 | 
	
		
			
				|  |  | +        if ($form->isSubmitted() && $form->isValid()) {
 | 
	
		
			
				|  |  | +            $game = $participation->getParty()->getGame()->getName();
 | 
	
		
			
				|  |  | +            $gameDate = $participation->getParty()->getStartOn();
 | 
	
		
			
				|  |  | +            $gameDate->setTimezone(new \DateTimeZone($_ENV['APP_TZ']));
 | 
	
		
			
				|  |  | +            $gameDateStr = $gameDate->format('d/m/y H:i');
 | 
	
		
			
				|  |  | +            $manager->remove($participation);
 | 
	
		
			
				|  |  | +            $manager->flush();
 | 
	
		
			
				|  |  | +            $this->addFlash('info', 'Votre participation à '.$game.' du '.$gameDateStr.' a bien été annulée.');
 | 
	
		
			
				|  |  | +            return $this->redirectToRoute('app_main');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return $this->render('participation/cancel.html.twig' , [
 | 
	
		
			
				|  |  | +            'form' => $form,
 | 
	
		
			
				|  |  | +            'participation' => $participation,
 | 
	
		
			
				|  |  | +        ]);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // afficher les détails d'une partie à partir d'un slot et permettre l'inscription
 | 
	
		
			
				|  |  | +    #[Route('/party/participation/{id}', name: 'app_participation', requirements: ['id' => '\d+'], methods: ['GET', 'POST'])]
 | 
	
		
			
				|  |  | +    public function partiticipation(?Slot $slot, Request $request, SlotRepository $slotRepository, EntityManagerInterface $manager, MailerInterface $mailer): Response
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $party = $slot->getParty();
 | 
	
		
			
				|  |  | +        if (!$party) {
 | 
	
		
			
				|  |  | +            $this->addFlash('danger', 'Aucune partie associée à ce slot !');
 | 
	
		
			
				|  |  | +            //return $this->redirectToRoute('app_main'); // @todo: à modifier !
 | 
	
		
			
				|  |  | +            $referer = $request->headers->get('referer'); 
 | 
	
		
			
				|  |  | +            return $this->redirect($referer);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $user=$this->getUser();
 | 
	
		
			
				|  |  | +        // Si un utilisateur est connecté et que ce n'est ni un admin, ni un gestionnaire, ni le MJ de cette partie
 | 
	
		
			
				|  |  | +        if ($user) {
 | 
	
		
			
				|  |  | +            $roles = $user->getRoles();
 | 
	
		
			
				|  |  | +            // Gestionnaire ou admin -> annuler l'user
 | 
	
		
			
				|  |  | +            if (in_array("ROLE_MANAGER", $roles) || in_array("ROLE_ADMIN", $roles)) {
 | 
	
		
			
				|  |  | +                $user = null;
 | 
	
		
			
				|  |  | +            } elseif (in_array("ROLE_STAFF", $roles) && $party->getGamemaster() == $user->getLinkToGamemaster()) {
 | 
	
		
			
				|  |  | +                $user = null;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Préparation du formulaire de participation
 | 
	
		
			
				|  |  | +        $participation = new Participation();
 | 
	
		
			
				|  |  | +        $participation->setParty($party);
 | 
	
		
			
				|  |  | +        if ($user) {
 | 
	
		
			
				|  |  | +            $participation->setParticipantName($user->getFullName());
 | 
	
		
			
				|  |  | +            $participation->setParticipantEmail($user->getEmail());
 | 
	
		
			
				|  |  | +            // @todo: réger le problème des type téléphone dans user et gamemaster !
 | 
	
		
			
				|  |  | +            // $participation->setParticipantPhone($user->getPhone()); 
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        $form = $this->createForm(ParticipationType::class, $participation);
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        // Traitement des entrées du formulaire
 | 
	
		
			
				|  |  | +        $form->handleRequest($request);
 | 
	
		
			
				|  |  | +        if ($form->isSubmitted() && $form->isValid()) {
 | 
	
		
			
				|  |  | +            // On contrôle qu'il y a encore de la place, sinon -> erreur
 | 
	
		
			
				|  |  | +            if ($party->getSeatsLeft() < 1) {
 | 
	
		
			
				|  |  | +                if ($party->getSeatsLeft() > 0) {
 | 
	
		
			
				|  |  | +                    $this->addFlash('danger', 'Il n\'y a pas assez de places pour vous accueillir sur cette partie.');
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    $this->addFlash('danger', 'Il n\'y a plus de places pour vous accueillir sur cette partie.');
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                $reservationsCounter = 1;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                // @todo: réservation multiples
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                // On enregistre dans la base
 | 
	
		
			
				|  |  | +                $manager->persist($participation);
 | 
	
		
			
				|  |  | +                $manager->flush();
 | 
	
		
			
				|  |  | +                
 | 
	
		
			
				|  |  | +                // Envoyer un mail
 | 
	
		
			
				|  |  | +                // Maintenant uniquement pour avoir l'ID
 | 
	
		
			
				|  |  | +                $email = (new TemplatedEmail())
 | 
	
		
			
				|  |  | +                    ->from(new Address($_ENV['CONTACT_EMAIL'], $_ENV['CONTACT_NAME']))
 | 
	
		
			
				|  |  | +                    ->to((string) $participation->getParticipantEmail())
 | 
	
		
			
				|  |  | +                    ->subject('Votre réservation pour '.$party->getGame()->getName())
 | 
	
		
			
				|  |  | +                    ->htmlTemplate('participation/booking.email.html.twig')
 | 
	
		
			
				|  |  | +                    ->textTemplate('participation/booking.email.txt.twig')
 | 
	
		
			
				|  |  | +                    ->context([
 | 
	
		
			
				|  |  | +                        'participation' => $participation,
 | 
	
		
			
				|  |  | +                    ]);
 | 
	
		
			
				|  |  | +                $mailer->send($email);
 | 
	
		
			
				|  |  | +                // On informe
 | 
	
		
			
				|  |  | +                if ($reservationsCounter > 1) {
 | 
	
		
			
				|  |  | +                    $this->addFlash('success', $reservationsCounter.' réservations enregistrées.');
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    $this->addFlash('success', 'Réservation enregistrée.');
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //$this->redirectToRoute('app_main_booking', ['id' => $party->getEvent()->getId()]);
 | 
	
		
			
				|  |  | +            $referer = $request->headers->get('referer'); 
 | 
	
		
			
				|  |  | +            return $this->redirect($referer);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // Affichage de la modale
 | 
	
		
			
				|  |  | +        return $this->render('participation/_modal.add.html.twig', [
 | 
	
		
			
				|  |  | +            'party' => $party,
 | 
	
		
			
				|  |  | +            'slot' => $slot,
 | 
	
		
			
				|  |  | +            'form' => $form,
 | 
	
		
			
				|  |  | +            'pathController' => 'app_participation'
 | 
	
		
			
				|  |  | +        ]);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |