src/EventSubscriber/CalendarSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\BookingRepository;
  4. use CalendarBundle\CalendarEvents;
  5. use CalendarBundle\Entity\Event;
  6. use CalendarBundle\Event\CalendarEvent;
  7. use DateTimeZone;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. class CalendarSubscriber implements EventSubscriberInterface
  11. {
  12.     private $bookingRepository;
  13.     private $router;
  14.     public function __construct(
  15.         BookingRepository $bookingRepository,
  16.         UrlGeneratorInterface $router
  17.     ) {
  18.         $this->bookingRepository $bookingRepository;
  19.         $this->router $router;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  25.         ];
  26.     }
  27.     public function onCalendarSetData(CalendarEvent $calendar)
  28.     {
  29.         $start $calendar->getStart();
  30.         $end $calendar->getEnd();
  31.         $filters $calendar->getFilters();
  32.         var_dump($filters);
  33.         exit();
  34.         // Modify the query to fit to your entity and needs
  35.         // Change booking.beginAt by your start date property
  36.         $bookings $this->bookingRepository
  37.             ->createQueryBuilder('booking')
  38.             ->where('booking.beginAt BETWEEN :start and :end OR booking.endAt BETWEEN :start and :end')
  39.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  40.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  41.             ->getQuery()
  42.             ->getResult()
  43.         ;
  44.         foreach ($bookings as $booking) {
  45.             // this create the events with your data (here booking data) to fill calendar
  46.             $s $booking->getBeginAt()->sub(new \DateInterval("PT1H"));
  47.             $e $booking->getEndAt()->sub(new \DateInterval("PT1H"));
  48.             $bookingEvent = new Event(
  49.                 $booking->getTitle(),
  50.                 new \DateTime($s->format("Y-m-d H:i:s"),new DateTimeZone('Europe/Paris')),
  51.                 new \DateTime($e->format("Y-m-d H:i:s"),new DateTimeZone('Europe/Paris'))
  52.             );
  53.             /*
  54.              * Add custom options to events
  55.              *
  56.              * For more information see: https://fullcalendar.io/docs/event-object
  57.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  58.              */
  59.             $bookingEvent->setOptions([
  60.                 'backgroundColor' => $booking->getUser()->getCalendarColor()
  61.             ]);
  62.             /*$bookingEvent->addOption(
  63.                 'url',
  64.                 $this->router->generate('booking_show', [
  65.                     'id' => $booking->getId(),
  66.                 ])
  67.             );*/
  68.             $bookingEvent->addOption(
  69.                 'id'$booking->getId()
  70.             );
  71.             // finally, add the event to the CalendarEvent to fill the calendar
  72.             $calendar->addEvent($bookingEvent);
  73.         }
  74.        /* $bookingEvent = new Event(
  75.             'null',
  76.             new \DateTime(),
  77.             new \DateTime()
  78.         );
  79.         $bookingEvent->setOptions([
  80.             'display' => "background"
  81.         ]);
  82.         $calendar->addEvent($bookingEvent);*/
  83.     }
  84. }