src/Controller/InvitationController.php line 180

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Event;
  4. use App\Entity\Invitation;
  5. use App\Repository\EventRepository;
  6. use App\Repository\InvitationRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class InvitationController extends AbstractController
  14. {
  15.   private function getInvitationPeriod(Event $event)
  16.   {
  17.     $today = new \DateTimeImmutable('today');
  18.     if ($this->isTodayInPeriod($today$event->getInvitationPreselectionStartAt(), $event->getInvitationPreselectionEndAt())) {
  19.       return [10];
  20.     }
  21.     if ($this->isTodayInPeriod($today$event->getInvitationCeremonyStartAt(), $event->getInvitationCeremonyEndAt())) {
  22.       return [01];
  23.     }
  24.     if ($event->getPreselectionDate() && $today <= $event->getPreselectionDate()) {
  25.       return [10];
  26.     }
  27.     return [01];
  28.   }
  29.   private function isTodayInPeriod(\DateTimeInterface $today, ?\DateTimeInterface $startAt, ?\DateTimeInterface $endAt): bool
  30.   {
  31.     if (!$startAt || !$endAt) {
  32.       return false;
  33.     }
  34.     return $today >= $startAt && $today <= $endAt;
  35.   }
  36.   /**
  37.    * @Route("/{type}", name="app_invitation")
  38.    */
  39.   public function index($type 0InvitationRepository $invitationRepositoryEventRepository $eventRepository ): Response
  40.   {
  41.     $event $eventRepository->findCurrent();
  42.     list($forPreselection$forCeremony) = $this->getInvitationPeriod($event);
  43.     $allInvitations $invitationRepository->findAllByEvent($event->getId(), $forPreselection$forCeremony);
  44.     $invitations $invitationRepository->findByEventByType($event->getId(), $type$forPreselection$forCeremony);
  45.     $validated $invitationRepository->findByEventByType($event->getId(), 1$forPreselection$forCeremony);
  46.     $canceled $invitationRepository->findByEventByType($event->getId(), 3$forPreselection$forCeremony);
  47.     return $this->render('invitation/index.html.twig', [
  48.       'invitations' => $invitations,
  49.       'allInvitations' => count($allInvitations),
  50.       'validated' => count($validated),
  51.       'canceled' => count($canceled),
  52.       'expected' => count($allInvitations) - count($validated) - count($canceled),
  53.       'type' => $type
  54.     ]);
  55.   }
  56.   /**
  57.    * @Route("/invitation/show", name="app_invitation_show")
  58.    */
  59.   public function show(Request $requestInvitationRepository $invitationRepository 
  60.   {
  61.     $id $request->get('id');
  62.     $invitation $invitationRepository->find($id);
  63.     if ($invitation->getStatus() == 1) {
  64.       if ($invitation->getHasBeenThere() == 1) {
  65.         $status 'ok';
  66.       } else {
  67.         $status 'standby';
  68.       }
  69.     } else {
  70.       $status 'cancel';
  71.     }
  72.     return $this->render('invitation/show.html.twig', [
  73.       'invitation' => $invitation,
  74.       'status' => $status
  75.     ]);
  76.   }
  77.   /**
  78.    * @Route("/invitation/new", name="app_invitation_new")
  79.    */
  80.   public function new(Request $requestManagerRegistry $doctrineEventRepository $eventRepository 
  81.   {
  82.     $lastname $request->get('lastname');
  83.     $firstname $request->get('firstname');
  84.     $company $request->get('company');
  85.     $job $request->get('job');
  86.     $comment $request->get('comment');
  87.     if ( (trim($lastname) == '') || (trim($firstname) == '') || (trim($company) == '') ) {
  88.       return new Response(falseResponse::HTTP_FORBIDDEN);
  89.     }
  90.     $event $eventRepository->findCurrent();
  91.     list($forPreselection$forCeremony) = $this->getInvitationPeriod($event);
  92.     $entityManager $doctrine->getManager();
  93.     $invitation = new Invitation();
  94.     $invitation->setFirstName($firstname);
  95.     $invitation->setLastName($lastname);
  96.     $invitation->setEmail('-');
  97.     $invitation->setJob($job);
  98.     $invitation->setComment($comment);
  99.     if (isset($company)) {
  100.       $invitation->setCompany($company);
  101.     }
  102.     $invitation->setEventId($event->getId());
  103.     $invitation->setForPreselection($forPreselection);
  104.     $invitation->setForCeremony($forCeremony);
  105.     $invitation->setUserTypeId(1);
  106.     $invitation->setHasBeenThere(1);
  107.     $invitation->setStatus(1);
  108.     $invitation->setCreatedAt(new \DateTimeImmutable());
  109.     $entityManager->persist($invitation);
  110.     $entityManager->flush();
  111.     return $this->redirectToRoute('app_invitation');
  112.   }
  113.   /**
  114.    * @Route("/invitation/edit", name="app_invitation_edit")
  115.    */
  116.   public function edit(Request $requestEntityManagerInterface $entityManagerInvitationRepository $invitationRepository 
  117.   {
  118.     $id $request->get('id');
  119.     $status $request->get('status');
  120.     $comment $request->get('comment');
  121.     $invitation $invitationRepository->find($id);
  122.     $invitation->setComment($comment);
  123.     switch ($status) {
  124.       case 'ok':
  125.         $invitation->setHasBeenThere(1);
  126.         $invitation->setStatus(1);
  127.         break;
  128.       case 'standby':
  129.         $invitation->setHasBeenThere(0);
  130.         $invitation->setStatus(1);
  131.         break;
  132.       case 'cancel':
  133.         $invitation->setHasBeenThere(0);
  134.         $invitation->setStatus(0);
  135.         break;
  136.     }
  137.     $entityManager->flush();
  138.     return new Response(falseResponse::HTTP_OK);
  139.   }
  140.   /**
  141.    * @Route("/invitation/scan", name="app_invitation_scan")
  142.    */
  143.   public function scan() 
  144.   {
  145.     return $this->render('invitation/scan.html.twig', []);
  146.   }
  147.   /**
  148.    * @Route("/invitation/search", name="app_invitation_search")
  149.    */
  150.   public function search() 
  151.   {
  152.     return $this->render('invitation/search.html.twig', []);
  153.   }
  154.   /**
  155.    * @Route("/invitation/finder", name="app_invitation_finder")
  156.    */
  157.   public function finder(Request $requestEventRepository $eventRepositoryInvitationRepository $invitationRepository 
  158.   {
  159.     $search $request->get('search');
  160.     $event $eventRepository->findCurrent();
  161.     list($forPreselection$forCeremony) = $this->getInvitationPeriod($event);
  162.     $invitations $invitationRepository->search($event->getId(), $search$forPreselection$forCeremony);
  163.     return $this->render('invitation/finder.html.twig', [
  164.       'invitations' => $invitations,
  165.     ]);
  166.   }
  167.   /**
  168.    * @Route("/invitation/validate", name="app_invitation_validate")
  169.    */
  170.   public function validate(Request $requestInvitationRepository $invitationRepositoryEntityManagerInterface $entityManager
  171.   {
  172.     $id $request->get('id');
  173.     $invitation $invitationRepository->find($id);
  174.     $invitation->setHasBeenThere(1);
  175.     $entityManager->flush();
  176.     return $this->render('invitation/validate.html.twig', ['invitation' => $invitation]);
  177.   }
  178. }