<?php
namespace App\Controller;
use App\Entity\Event;
use App\Entity\Invitation;
use App\Repository\EventRepository;
use App\Repository\InvitationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class InvitationController extends AbstractController
{
private function getInvitationPeriod(Event $event)
{
$today = new \DateTimeImmutable('today');
if ($this->isTodayInPeriod($today, $event->getInvitationPreselectionStartAt(), $event->getInvitationPreselectionEndAt())) {
return [1, 0];
}
if ($this->isTodayInPeriod($today, $event->getInvitationCeremonyStartAt(), $event->getInvitationCeremonyEndAt())) {
return [0, 1];
}
if ($event->getPreselectionDate() && $today <= $event->getPreselectionDate()) {
return [1, 0];
}
return [0, 1];
}
private function isTodayInPeriod(\DateTimeInterface $today, ?\DateTimeInterface $startAt, ?\DateTimeInterface $endAt): bool
{
if (!$startAt || !$endAt) {
return false;
}
return $today >= $startAt && $today <= $endAt;
}
/**
* @Route("/{type}", name="app_invitation")
*/
public function index($type = 0, InvitationRepository $invitationRepository, EventRepository $eventRepository ): Response
{
$event = $eventRepository->findCurrent();
list($forPreselection, $forCeremony) = $this->getInvitationPeriod($event);
$allInvitations = $invitationRepository->findAllByEvent($event->getId(), $forPreselection, $forCeremony);
$invitations = $invitationRepository->findByEventByType($event->getId(), $type, $forPreselection, $forCeremony);
$validated = $invitationRepository->findByEventByType($event->getId(), 1, $forPreselection, $forCeremony);
$canceled = $invitationRepository->findByEventByType($event->getId(), 3, $forPreselection, $forCeremony);
return $this->render('invitation/index.html.twig', [
'invitations' => $invitations,
'allInvitations' => count($allInvitations),
'validated' => count($validated),
'canceled' => count($canceled),
'expected' => count($allInvitations) - count($validated) - count($canceled),
'type' => $type
]);
}
/**
* @Route("/invitation/show", name="app_invitation_show")
*/
public function show(Request $request, InvitationRepository $invitationRepository )
{
$id = $request->get('id');
$invitation = $invitationRepository->find($id);
if ($invitation->getStatus() == 1) {
if ($invitation->getHasBeenThere() == 1) {
$status = 'ok';
} else {
$status = 'standby';
}
} else {
$status = 'cancel';
}
return $this->render('invitation/show.html.twig', [
'invitation' => $invitation,
'status' => $status
]);
}
/**
* @Route("/invitation/new", name="app_invitation_new")
*/
public function new(Request $request, ManagerRegistry $doctrine, EventRepository $eventRepository )
{
$lastname = $request->get('lastname');
$firstname = $request->get('firstname');
$company = $request->get('company');
$job = $request->get('job');
$comment = $request->get('comment');
if ( (trim($lastname) == '') || (trim($firstname) == '') || (trim($company) == '') ) {
return new Response(false, Response::HTTP_FORBIDDEN);
}
$event = $eventRepository->findCurrent();
list($forPreselection, $forCeremony) = $this->getInvitationPeriod($event);
$entityManager = $doctrine->getManager();
$invitation = new Invitation();
$invitation->setFirstName($firstname);
$invitation->setLastName($lastname);
$invitation->setEmail('-');
$invitation->setJob($job);
$invitation->setComment($comment);
if (isset($company)) {
$invitation->setCompany($company);
}
$invitation->setEventId($event->getId());
$invitation->setForPreselection($forPreselection);
$invitation->setForCeremony($forCeremony);
$invitation->setUserTypeId(1);
$invitation->setHasBeenThere(1);
$invitation->setStatus(1);
$invitation->setCreatedAt(new \DateTimeImmutable());
$entityManager->persist($invitation);
$entityManager->flush();
return $this->redirectToRoute('app_invitation');
}
/**
* @Route("/invitation/edit", name="app_invitation_edit")
*/
public function edit(Request $request, EntityManagerInterface $entityManager, InvitationRepository $invitationRepository )
{
$id = $request->get('id');
$status = $request->get('status');
$comment = $request->get('comment');
$invitation = $invitationRepository->find($id);
$invitation->setComment($comment);
switch ($status) {
case 'ok':
$invitation->setHasBeenThere(1);
$invitation->setStatus(1);
break;
case 'standby':
$invitation->setHasBeenThere(0);
$invitation->setStatus(1);
break;
case 'cancel':
$invitation->setHasBeenThere(0);
$invitation->setStatus(0);
break;
}
$entityManager->flush();
return new Response(false, Response::HTTP_OK);
}
/**
* @Route("/invitation/scan", name="app_invitation_scan")
*/
public function scan()
{
return $this->render('invitation/scan.html.twig', []);
}
/**
* @Route("/invitation/search", name="app_invitation_search")
*/
public function search()
{
return $this->render('invitation/search.html.twig', []);
}
/**
* @Route("/invitation/finder", name="app_invitation_finder")
*/
public function finder(Request $request, EventRepository $eventRepository, InvitationRepository $invitationRepository )
{
$search = $request->get('search');
$event = $eventRepository->findCurrent();
list($forPreselection, $forCeremony) = $this->getInvitationPeriod($event);
$invitations = $invitationRepository->search($event->getId(), $search, $forPreselection, $forCeremony);
return $this->render('invitation/finder.html.twig', [
'invitations' => $invitations,
]);
}
/**
* @Route("/invitation/validate", name="app_invitation_validate")
*/
public function validate(Request $request, InvitationRepository $invitationRepository, EntityManagerInterface $entityManager)
{
$id = $request->get('id');
$invitation = $invitationRepository->find($id);
$invitation->setHasBeenThere(1);
$entityManager->flush();
return $this->render('invitation/validate.html.twig', ['invitation' => $invitation]);
}
}