src/EventSubscriber/CalendarSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\EventController;
  4. use App\Entity\Event as EntityEvent;
  5. use App\Form\EventType;
  6. use App\Repository\EventRepository;
  7. use CalendarBundle\CalendarEvents;
  8. use CalendarBundle\Entity\Event;
  9. use CalendarBundle\Event\CalendarEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Core\Security;
  18. class CalendarSubscriber implements EventSubscriberInterface
  19. {
  20.     
  21.     public function __construct(
  22.         private EventRepository $eventRepository,
  23.         private UrlGeneratorInterface $router,
  24.         private FormFactoryInterface $formFactory,
  25.         private Security $security,
  26.     ) {
  27.         //$this->eventRepository = $eventRepository;
  28.         //$this->router = $router;
  29.     }
  30.    
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  35.         ];
  36.     }
  37.     public function onCalendarSetData(CalendarEvent $calendar)
  38.     {
  39.         $start $calendar->getStart();
  40.         $end $calendar->getEnd();
  41.         $filters $calendar->getFilters();
  42.         $user=$this->security->getUser();
  43.         $roles=$user->getRoles();
  44.         if (in_array('ROLE_ADMIN',$roles)) {
  45.             $events $this->eventRepository
  46.                 ->createQueryBuilder('event')
  47.                 ->where('event.startDateTime BETWEEN :start and :end OR event.endDateTime BETWEEN :start and :end')
  48.                 ->setParameter('start'$start->format('Y-m-d H:i:s'))
  49.                 ->setParameter('end'$end->format('Y-m-d H:i:s'))
  50.                 ->getQuery()
  51.                 ->getResult();
  52.         } else {
  53.             $events $this->eventRepository
  54.                 ->createQueryBuilder('event')
  55.                 ->join('event.users''user')
  56.                 ->where('user = :user')
  57.                 ->andWhere('event.startDateTime BETWEEN :start and :end OR event.endDateTime BETWEEN :start and :end')
  58.                 ->setParameter('user'$user)
  59.                 ->setParameter('start'$start->format('Y-m-d H:i:s'))
  60.                 ->setParameter('end'$end->format('Y-m-d H:i:s'))
  61.                 ->getQuery()
  62.                 ->getResult();
  63.                 ;
  64.         }
  65.       
  66.         
  67.         foreach ($events as $event) {
  68.             // this creates the events with your data (here event data) to fill calendar
  69.             $meetingEvent = new Event(
  70.                 
  71.                 $event->getTitle(),
  72.                 //$event->getDescription(),
  73.                 $event->getStartDateTime(),
  74.                 $event->getEndDateTime(), 
  75.             );
  76.             
  77.             $meetingEvent->setOptions([
  78.                 'id'=>$event->getId(),
  79.                 'description'=> $event->getDescription(),
  80.                 'backgroundColor' => $event->getBackgroundColor(),
  81.                 'borderColor' => $event->getBorderColor(),
  82.                 'textColor'=>$event->getTextColor(),
  83.                 'allDay'=>$event->isAllDay(),
  84.                 
  85.             ]);
  86.             $editURL=$this->router->generate('edit_event', [
  87.                 'ide' => $event->getId(),
  88.             ]);
  89.             $deleteURL=$this->router->generate('delete_event', [
  90.                 'ide' => $event->getId(),
  91.             ]);
  92.             $newURL=$this->router->generate('new_event', [
  93.                 
  94.             ]);
  95.             /*
  96.             $moveURL=$this->router->generate('move_event', [
  97.                 'ide' => $event->getId(),
  98.             ]);
  99.             */
  100.             /*
  101.             $meetingEvent->addOption(
  102.                 'url',
  103.                 $editURL
  104.                 
  105.             );
  106.             */
  107.             
  108.             $meetingEvent->addOption(
  109.                 'extendedProps',
  110.                 [
  111.                     'editURL' => $editURL,
  112.                     'deleteURL' => $deleteURL,
  113.                     'newURL'=>$newURL,
  114.                     //'moveURL'=> $moveURL,
  115.                 ]
  116.             );
  117.             
  118.             /*
  119.             $meetingEvent->addOption(
  120.                 'url',
  121.                 $this->router->generate('new_event', [
  122.                     //'ide' => $event->getId(),
  123.                 ])
  124.             );
  125.             */
  126.             
  127.             // finally, add the event to the CalendarEvent to fill the calendar
  128.             $calendar->addEvent($meetingEvent);
  129.         }
  130.     }
  131.     /*
  132.     public function onKernelController(ControllerEvent $event)
  133.     {
  134.         
  135.             $entityEvent = new EntityEvent();
  136.             $form = $this->formFactory->create(EventType::class, $entityEvent);
  137.             $user = $this->security->getUser();
  138.             $form->handleRequest($event->getRequest());
  139.             if ($form->isSubmitted() && $form->isValid()) {
  140.                 $entityEvent->setCreatedAt(new \DateTime('now'));
  141.                 $entityEvent->addUser($user);
  142.                 $this->eventRepository->save($entityEvent, true);
  143.                  // Get an instance of the controller
  144.                  $controllerInstance = $this->container->get(EventController::class);
  145.                  $dashboardUrl = $controllerInstance->getDashboardUrl();
  146.  
  147.                  return new RedirectResponse($dashboardUrl);
  148.              
  149.             }
  150.             $response = new Response($this->twig->render('events/index.html.twig', [
  151.                 'form' => $form->createView(),
  152.             ]));
  153.             $event->setController(function () use ($response) {
  154.                 return $response;
  155.             });
  156.         
  157.     }
  158.     */
  159.     
  160. }