src/EventListener/CalendarListener.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  4. use CalendarBundle\Entity\Event;
  5. use CalendarBundle\Event\CalendarEvent;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. class CalendarListener
  8. {
  9.     public function __construct($emTokenStorageInterface $tokenStorage)
  10.     {
  11.         $this->em $em;
  12.         $this->tokenStorage $tokenStorage;
  13.     }
  14.     public function load(CalendarEvent $calendar)
  15.     {
  16.         $start $calendar->getStart();
  17.         $end $calendar->getEnd();
  18.         $filters $calendar->getFilters();
  19.         if (!empty($filters) && $filters['user'] != null) {
  20.             $user $this->em->getRepository('App:User')->find($filters['user']);
  21.         } else {
  22.             $user $this->tokenStorage->getToken()->getUser();
  23.         }
  24.         // Modify the query to fit to your entity and needs
  25.         // Change booking.beginAt by your start date property
  26.         $events $this->em->getRepository('App:Calendar')
  27.             ->createQueryBuilder('cal')
  28.             ->where('cal.startdate BETWEEN :start and :end')
  29.             ->andWhere('cal.user = :user_id')
  30.             ->setParameter('user_id'$user->getId())
  31.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  32.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  33.             ->getQuery()
  34.             ->getResult();
  35.         foreach ($events as $mydata) {
  36.             // this create the events with your data (here booking data) to fill calendar
  37.             $event = new Event(
  38.                 $mydata->getTitle(),
  39.                 $mydata->getStartdate(),
  40.                 $mydata->getEnddate() // If the end date is null or not defined, a all day event is created.
  41.             );
  42.             /*
  43.              * Add custom options to events
  44.              *
  45.              * For more information see: https://fullcalendar.io/docs/event-object
  46.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  47.              */
  48.             $color json_decode($mydata->getColor())->primary;
  49.             $event->setOptions([
  50.                 'backgroundColor' => $color,
  51.                 'borderColor' => $color,
  52.                 'id' => $mydata->getId()
  53.             ]);
  54.             // finally, add the event to the CalendarEvent to fill the calendar
  55.             $calendar->addEvent($event);
  56.         }
  57.     }
  58. }