src/Controller/CheckinsByEventController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Event;
  4. use App\Entity\User;
  5. use App\Entity\UserEvent;
  6. use App\Entity\UserEventCheckins;
  7. use App\Form\CheckinsByEventType;
  8. use App\Security\CustomAuthenticator;
  9. use App\Service\UserEventCheckinsService;
  10. use DateTime;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use App\Entity\CheckinsByEvent;
  13. use Doctrine\ORM\EntityRepository;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  24. class CheckinsByEventController extends AbstractController
  25. {
  26.     /**
  27.      * @Route("/", name="checkin_list")
  28.      * @IsGranted("ROLE_ADMIN")
  29.      */
  30.     public function indexAction(EntityManagerInterface $em): Response
  31.     {
  32.         $events $em->getRepository(Event::class)->findByCheckins();
  33.         return $this->render('checkin/config/index.html.twig',
  34.             [
  35.                 'events' => $events,
  36.                 'socket_url' => $this->getParameter('socket_url')
  37.             ]);
  38.     }
  39.     /**
  40.      *
  41.      * @Route("checkin/new", name="checkin_by_event_new")
  42.      * @IsGranted("ROLE_USER")
  43.      */
  44.     public function newAction(Request $requestEntityManagerInterface $em)
  45.     {
  46.         $checkin = new CheckinsByEvent();
  47.         $form $this->createForm(CheckinsByEventType::class, $checkin);
  48.         $form->add('event'EntityType::class,[
  49.         'class' => Event::class,
  50.                 'query_builder' => function (EntityRepository $er) {
  51.                 return $er->createQueryBuilder('p')
  52.                     ->select('p');
  53.                 },
  54.                 'choice_label' => function ($event) {
  55.                     return ($event) ? $event->getName() : '';
  56.                 },
  57.                 'required' => true,
  58.                 'attr' => ['class' => 'search-select2']
  59.             ]
  60.         );
  61.         $form->handleRequest($request);
  62.         if ($form->isSubmitted() && $form->isValid()) {
  63.             $typeCheckinId $form->getData()->getTypes() ? $form->getData()->getTypes()->getId() : '' ;
  64.             if($typeCheckinId == "1"  ){
  65.                 $checkTypeArrive $em->getRepository(CheckinsByEvent::class)->findBy(['event' => $form->getData()->getEvent(), 'types' =>$typeCheckinId ]);
  66.                 if($checkTypeArrive) {
  67.                     return $this->redirectToRoute('checkin_by_event', ['id' => $form->getData()->getEvent()]);
  68.                 }
  69.             }
  70.             $em->persist($checkin);
  71.             $em->flush();
  72.             return $this->redirectToRoute('checkin_by_event', ['id' => $checkin->getEvent()->getId()]);
  73.         }
  74.         return $this->render('checkin/config/new.html.twig',['form' => $form->createView(), 'checkin' => $checkin]);
  75.     }
  76.     /**
  77.      *
  78.      * @Route("checkin/event/{id}/add", name="checkin_by_event_add")
  79.      * @IsGranted("ROLE_USER")
  80.      */
  81.     public function addAction(Request $requestEvent $eventEntityManagerInterface $em)
  82.     {
  83.         $checkin = new CheckinsByEvent();
  84.         $checkin->setEvent($event);
  85.         $form $this->createForm(CheckinsByEventType::class, $checkin, ['event_id'=> $event->getId()]);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted() && $form->isValid()) {
  88.             $typeCheckinId $form->getData()->getTypes() ? $form->getData()->getTypes()->getId() : '' ;
  89.             if($typeCheckinId == "1"  ){
  90.                 $checkTypeArrive $em->getRepository(CheckinsByEvent::class)->findBy(['event' => $event'types' =>$typeCheckinId ]);
  91.                 if($checkTypeArrive) {
  92.                     return $this->redirectToRoute('checkin_by_event', ['id' =>  $event->getId()]);
  93.                 }
  94.             }
  95.             $em->persist($checkin);
  96.             $em->flush();
  97.             return $this->redirectToRoute('checkin_by_event', ['id' =>  $event->getId()]);
  98.         }
  99.         return $this->render('checkin/config/form.html.twig',['form' => $form->createView(), 'checkin' => $checkin'event' => $event]);
  100.     }
  101.     /**
  102.      * @Route("checkin/event/{eventId}/checkin/{id}/edit", name="checkin_by_event_edit")
  103.      * @IsGranted("ROLE_USER")
  104.      */
  105.     public function editAction(Request $request,$eventId$idEntityManagerInterface $em)
  106.     {
  107.         $event $em->find(Event::class, $eventId);
  108.         $checkin $em->find(CheckinsByEvent::class,$id);
  109.         $form $this->createForm(CheckinsByEventType::class, $checkin,['edit_action'=>true'event_id'=> $eventId'current_checkin'=> $checkin->getId()]);
  110.         $form->handleRequest($request);
  111.         if ($form->isSubmitted() && $form->isValid()) {
  112.             $em->flush();
  113.             return $this->redirectToRoute('checkin_by_event', ['id' => $eventId]);
  114.         }
  115.         return $this->render('checkin/config/form.html.twig', [
  116.             'form' => $form->createView(),
  117.             'checkin'=> $checkin,
  118.             'event' => $event
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("checkin/event/{id}/{key}", name="checkin_by_event")
  123.      */
  124.     public function showAction(EntityManagerInterface $em,RequestStack $requestStackRequest $requestUserAuthenticatorInterface $authenticatorCustomAuthenticator $customAuthenticator$id null,$key null): Response
  125.     {
  126.         if (is_null($key) && !$this->getUser()) {
  127.             return $this->redirectToRoute('app_login');
  128.         } elseif($key) {
  129.             $key explode('-',$key);
  130.             if (count($key)!== || hash_hmac('sha256'$key[0].'-'.$key[1], 'checkin_secret_key') !== $key[2]) {
  131.                 return $this->redirectToRoute('app_login');
  132.             } else{
  133.                 $admin $em->getRepository(User::class)->findOneBy(['adminId' => $key[0] ]);
  134.                 if (is_null($admin)) {
  135.                     return $this->redirectToRoute('app_login');
  136.                 }
  137.                 $event $em->find(Event::class, $id);
  138.                 if (is_null($event)) {
  139.                     $event = new Event();
  140.                     $event->setId($id);
  141.                     $event->setName($request->get('title'));
  142.                     $em->persist($event);
  143.                     $em->flush();
  144.                 }
  145.                 $requestStack->getSession()->set('eventId',$event->getId());
  146.                 $password $admin->getAdminId().'-'.$admin->getEmail();
  147.                 $request->request->set('email'$admin->getEmail());
  148.                 $request->request->set('password'$password);
  149.                 $authenticator->authenticateUser($admin$customAuthenticator$request);
  150.             }
  151.         }
  152.         $event $em->find(Event::class, $id);
  153.         $checkins $em->getRepository(CheckinsByEvent::class)->findBy(['event' => $event]);
  154.         return $this->render('checkin/config/show.html.twig',
  155.             [
  156.             'checkins' => $checkins,
  157.             'event' => $event,
  158.             'socket_url' => $this->getParameter('socket_url')
  159.             ]);
  160.     }
  161.     /**
  162.      * @Route("checkin/launch/{id}", name="checkin_by_event_launch")
  163.      * @IsGranted("ROLE_USER")
  164.      */
  165.     public function launchCheckinAction(CheckinsByEvent $checkinEntityManagerInterface $entityManagerUserEventCheckinsService $userEventCheckinsService)
  166.     {
  167.         $import $userEventCheckinsService->importUserEvent($checkin->getEvent(), false);
  168.         $oldStartDate $checkin->getStartDate();
  169.         if ($import) {
  170.             if(is_null($checkin->getDependsOncheckin())){
  171.                 $users $entityManager->getRepository(UserEvent::class)->findBy(['event' => $checkin->getEvent()]);
  172.             } else {
  173.                 $users $entityManager->getRepository(UserEventCheckins::class)->getUsersEventDependanceApiOnlyPresent(
  174.                     $checkin->getEvent()->getId(),
  175.                     [
  176.                         'checkinByEvent' => $checkin->getdependsOncheckin()->getId(),
  177.                     ]
  178.                 );
  179.             }
  180.             if (is_null($oldStartDate)) {
  181.                 $checkin->setStartDate(new DateTime);
  182.             }
  183.             $entityManager->persist($checkin);
  184.             $userEventCheckinsService->createUserEventCheckins($users$checkin);
  185.             $entityManager->flush();
  186.             if (is_null($oldStartDate)) {
  187.                 $this->addFlash('success','launched_successfully');
  188.             } else {
  189.                 $this->addFlash('success','updated_successfully');
  190.             }
  191.         }
  192.         if (is_null($oldStartDate)) {
  193.             return $this->redirectToRoute('checkin_by_event', ['id' => $checkin->getEvent()->getId()]);
  194.         } else {
  195.             return new JsonResponse(
  196.                 [
  197.                     'users' => $userEventCheckinsService->getListUserEventCheckinsWithOutFilter($checkin'sansprofile'0),
  198.                     'checkin' => $checkin->getId(),
  199.                     'filters' => $userEventCheckinsService->getProfilesUsersEventStatistic($checkin->getKeyCheckin())
  200.                 ]
  201.             );
  202.         }
  203.     }
  204.     /**
  205.      * @Route("checkin/stop/{id}", name="checkin_by_event_stop")
  206.      * @IsGranted("ROLE_USER")
  207.      */
  208.     public function stopCheckinAction(CheckinsByEvent $checkinEntityManagerInterface $em): RedirectResponse
  209.     {
  210.         $checkin->setEndDate(new DateTime);
  211.         $em->flush();
  212.         return $this->redirectToRoute('checkin_by_event', ['id' => $checkin->getEvent()->getId()]);
  213.     }
  214.     /**
  215.      * @Route("checkin/delete/{id}", name="checkin_by_event_delete")
  216.      * @IsGranted("ROLE_USER")
  217.      */
  218.     public function deleteAction(CheckinsByEvent $checkinEntityManagerInterface $em): RedirectResponse
  219.     {
  220.         // function is not used => button is display none
  221.         $event $checkin->getEvent();
  222.         $checkin->setEvent(null);
  223.         $em->flush();
  224.         return $this->redirectToRoute('checkin_by_event', ['id' => $event->getId()]);
  225.     }
  226. }