src/EventSubscriber/CalendarSubscriber.php line 37

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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class CalendarSubscriber implements EventSubscriberInterface
  11. {
  12.     private $bookingRepository;
  13.     private $router;
  14.     private $token;
  15.     private $user;
  16.     private $role;
  17.     public function __construct(TokenStorageInterface $tokenBookingRepository $bookingRepositoryUrlGeneratorInterface $router)
  18.     {
  19.         $this->token $token;
  20.         $this->user $this->token->getToken()->getUser();
  21.         $this->role $this->user->getRoles();
  22.         $this->bookingRepository $bookingRepository;
  23.         $this->router $router;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  29.         ];
  30.     }
  31.     public function onCalendarSetData(CalendarEvent $calendar)
  32.     {
  33.         $start $calendar->getStart();
  34.         $end $calendar->getEnd();
  35.         $filters $calendar->getFilters();
  36.         $strRole '%' $this->role[0] . '%';
  37.         // Modify the query to fit to your entity and needs
  38.         // Change booking.beginAt by your start date property
  39.         $bookings $this->bookingRepository
  40.             ->createQueryBuilder('booking')
  41.             ->innerJoin('booking.user''u')
  42.             ->where('booking.beginAt BETWEEN :start and :end OR booking.endAt BETWEEN :start and :end')
  43.             ->andWhere('u.roles LIKE :role')
  44.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  45.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  46.             ->setParameter('role'$strRole)
  47.             ->getQuery()
  48.             ->getResult();
  49.         foreach ($bookings as $booking) {
  50.             // this create the events with your data (here booking data) to fill calendar
  51.             $bookingEvent = new Event(
  52.                 $booking->getTitle(),
  53.                 $booking->getBeginAt(),
  54.                 $booking->getEndAt() // If the end date is null or not defined, a all day event is created.
  55.             );
  56.             /*
  57.              * Add custom options to events
  58.              *
  59.              * For more information see: https://fullcalendar.io/docs/event-object
  60.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  61.              */
  62.             $bookingEvent->setOptions([
  63.                 'backgroundColor' => 'blue',
  64.                 'borderColor' => 'blue',
  65.             ]);
  66.             $bookingEvent->addOption(
  67.                 'url',
  68.                 $this->router->generate('app_booking_show', [
  69.                     'id' => $booking->getId(),
  70.                 ])
  71.             );
  72.             // finally, add the event to the CalendarEvent to fill the calendar
  73.             $calendar->addEvent($bookingEvent);
  74.         }
  75.     }
  76. }