<?php
namespace App\Controller;
use App\Entity\Event;
use App\Entity\User;
use App\Entity\UserEvent;
use App\Entity\UserEventCheckins;
use App\Form\CheckinsByEventType;
use App\Security\CustomAuthenticator;
use App\Service\UserEventCheckinsService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\CheckinsByEvent;
use Doctrine\ORM\EntityRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
class CheckinsByEventController extends AbstractController
{
/**
* @Route("/", name="checkin_list")
* @IsGranted("ROLE_ADMIN")
*/
public function indexAction(EntityManagerInterface $em): Response
{
$events = $em->getRepository(Event::class)->findByCheckins();
return $this->render('checkin/config/index.html.twig',
[
'events' => $events,
'socket_url' => $this->getParameter('socket_url')
]);
}
/**
*
* @Route("checkin/new", name="checkin_by_event_new")
* @IsGranted("ROLE_USER")
*/
public function newAction(Request $request, EntityManagerInterface $em)
{
$checkin = new CheckinsByEvent();
$form = $this->createForm(CheckinsByEventType::class, $checkin);
$form->add('event', EntityType::class,[
'class' => Event::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->select('p');
},
'choice_label' => function ($event) {
return ($event) ? $event->getName() : '';
},
'required' => true,
'attr' => ['class' => 'search-select2']
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$typeCheckinId = $form->getData()->getTypes() ? $form->getData()->getTypes()->getId() : '' ;
if($typeCheckinId == "1" ){
$checkTypeArrive = $em->getRepository(CheckinsByEvent::class)->findBy(['event' => $form->getData()->getEvent(), 'types' =>$typeCheckinId ]);
if($checkTypeArrive) {
return $this->redirectToRoute('checkin_by_event', ['id' => $form->getData()->getEvent()]);
}
}
$em->persist($checkin);
$em->flush();
return $this->redirectToRoute('checkin_by_event', ['id' => $checkin->getEvent()->getId()]);
}
return $this->render('checkin/config/new.html.twig',['form' => $form->createView(), 'checkin' => $checkin]);
}
/**
*
* @Route("checkin/event/{id}/add", name="checkin_by_event_add")
* @IsGranted("ROLE_USER")
*/
public function addAction(Request $request, Event $event, EntityManagerInterface $em)
{
$checkin = new CheckinsByEvent();
$checkin->setEvent($event);
$form = $this->createForm(CheckinsByEventType::class, $checkin, ['event_id'=> $event->getId()]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$typeCheckinId = $form->getData()->getTypes() ? $form->getData()->getTypes()->getId() : '' ;
if($typeCheckinId == "1" ){
$checkTypeArrive = $em->getRepository(CheckinsByEvent::class)->findBy(['event' => $event, 'types' =>$typeCheckinId ]);
if($checkTypeArrive) {
return $this->redirectToRoute('checkin_by_event', ['id' => $event->getId()]);
}
}
$em->persist($checkin);
$em->flush();
return $this->redirectToRoute('checkin_by_event', ['id' => $event->getId()]);
}
return $this->render('checkin/config/form.html.twig',['form' => $form->createView(), 'checkin' => $checkin, 'event' => $event]);
}
/**
* @Route("checkin/event/{eventId}/checkin/{id}/edit", name="checkin_by_event_edit")
* @IsGranted("ROLE_USER")
*/
public function editAction(Request $request,$eventId, $id, EntityManagerInterface $em)
{
$event = $em->find(Event::class, $eventId);
$checkin = $em->find(CheckinsByEvent::class,$id);
$form = $this->createForm(CheckinsByEventType::class, $checkin,['edit_action'=>true, 'event_id'=> $eventId, 'current_checkin'=> $checkin->getId()]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->flush();
return $this->redirectToRoute('checkin_by_event', ['id' => $eventId]);
}
return $this->render('checkin/config/form.html.twig', [
'form' => $form->createView(),
'checkin'=> $checkin,
'event' => $event
]);
}
/**
* @Route("checkin/event/{id}/{key}", name="checkin_by_event")
*/
public function showAction(EntityManagerInterface $em,RequestStack $requestStack, Request $request, UserAuthenticatorInterface $authenticator, CustomAuthenticator $customAuthenticator, $id = null,$key = null): Response
{
if (is_null($key) && !$this->getUser()) {
return $this->redirectToRoute('app_login');
} elseif($key) {
$key = explode('-',$key);
if (count($key)!== 3 || hash_hmac('sha256', $key[0].'-'.$key[1], 'checkin_secret_key') !== $key[2]) {
return $this->redirectToRoute('app_login');
} else{
$admin = $em->getRepository(User::class)->findOneBy(['adminId' => $key[0] ]);
if (is_null($admin)) {
return $this->redirectToRoute('app_login');
}
$event = $em->find(Event::class, $id);
if (is_null($event)) {
$event = new Event();
$event->setId($id);
$event->setName($request->get('title'));
$em->persist($event);
$em->flush();
}
$requestStack->getSession()->set('eventId',$event->getId());
$password = $admin->getAdminId().'-'.$admin->getEmail();
$request->request->set('email', $admin->getEmail());
$request->request->set('password', $password);
$authenticator->authenticateUser($admin, $customAuthenticator, $request);
}
}
$event = $em->find(Event::class, $id);
$checkins = $em->getRepository(CheckinsByEvent::class)->findBy(['event' => $event]);
return $this->render('checkin/config/show.html.twig',
[
'checkins' => $checkins,
'event' => $event,
'socket_url' => $this->getParameter('socket_url')
]);
}
/**
* @Route("checkin/launch/{id}", name="checkin_by_event_launch")
* @IsGranted("ROLE_USER")
*/
public function launchCheckinAction(CheckinsByEvent $checkin, EntityManagerInterface $entityManager, UserEventCheckinsService $userEventCheckinsService)
{
$import = $userEventCheckinsService->importUserEvent($checkin->getEvent(), false);
$oldStartDate = $checkin->getStartDate();
if ($import) {
if(is_null($checkin->getDependsOncheckin())){
$users = $entityManager->getRepository(UserEvent::class)->findBy(['event' => $checkin->getEvent()]);
} else {
$users = $entityManager->getRepository(UserEventCheckins::class)->getUsersEventDependanceApiOnlyPresent(
$checkin->getEvent()->getId(),
[
'checkinByEvent' => $checkin->getdependsOncheckin()->getId(),
]
);
}
if (is_null($oldStartDate)) {
$checkin->setStartDate(new DateTime);
}
$entityManager->persist($checkin);
$userEventCheckinsService->createUserEventCheckins($users, $checkin);
$entityManager->flush();
if (is_null($oldStartDate)) {
$this->addFlash('success','launched_successfully');
} else {
$this->addFlash('success','updated_successfully');
}
}
if (is_null($oldStartDate)) {
return $this->redirectToRoute('checkin_by_event', ['id' => $checkin->getEvent()->getId()]);
} else {
return new JsonResponse(
[
'users' => $userEventCheckinsService->getListUserEventCheckinsWithOutFilter($checkin, 'sansprofile', 0),
'checkin' => $checkin->getId(),
'filters' => $userEventCheckinsService->getProfilesUsersEventStatistic($checkin->getKeyCheckin())
]
);
}
}
/**
* @Route("checkin/stop/{id}", name="checkin_by_event_stop")
* @IsGranted("ROLE_USER")
*/
public function stopCheckinAction(CheckinsByEvent $checkin, EntityManagerInterface $em): RedirectResponse
{
$checkin->setEndDate(new DateTime);
$em->flush();
return $this->redirectToRoute('checkin_by_event', ['id' => $checkin->getEvent()->getId()]);
}
/**
* @Route("checkin/delete/{id}", name="checkin_by_event_delete")
* @IsGranted("ROLE_USER")
*/
public function deleteAction(CheckinsByEvent $checkin, EntityManagerInterface $em): RedirectResponse
{
// function is not used => button is display none
$event = $checkin->getEvent();
$checkin->setEvent(null);
$em->flush();
return $this->redirectToRoute('checkin_by_event', ['id' => $event->getId()]);
}
}