src/EventSubscriber/CalendarSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\BaseController;
  4. use App\Repository\EventRepository;
  5. use App\Repository\TaskRepository;
  6. use CalendarBundle\CalendarEvents;
  7. use CalendarBundle\Entity\Event;
  8. use CalendarBundle\Event\CalendarEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. class CalendarSubscriber implements EventSubscriberInterface 
  13. {
  14.     private $eventRepository;
  15.     private $router;
  16.     private $security;
  17.     private $user;
  18.     public function __construct(
  19.         EventRepository $eventRepository,
  20.         TaskRepository $taskRepository,
  21.         UrlGeneratorInterface $router,
  22.         Security $security,
  23.         BaseController $user
  24.     ) {
  25.         $this->eventRepository $eventRepository;
  26.         $this->taskRepository $taskRepository;
  27.         $this->router $router;
  28.         $this->security $security;
  29.         $this->user $user;
  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.         switch($filters['calendar-id']) {
  43.             case 'event-calendar':
  44.                 $this->fillEventsCalendar($calendar$start$end$filters);
  45.                 break;
  46.             case 'task-calendar':
  47.                 $this->fillTasksCalendar($calendar$start$end$filters);
  48.                 break;
  49.         }
  50.     }
  51.     public function fillEventsCalendar(CalendarEvent $calendar\DateTimeInterface $start\DateTimeInterface $end, array $filters) {
  52.         // Modify the query to fit to your entity and needs
  53.         // Change booking.beginAt by your start date property
  54.         if ($this->security->isGranted('ROLE_SUPERUSER') || $this->security->isGranted('ROLE_ADMINISTRATOR') || $this->security->isGranted('ROLE_CSO') || $this->security->isGranted('ROLE_PMM')) {
  55.             $events $this->eventRepository
  56.                 ->createQueryBuilder('event')
  57.                 ->where('event.beginAt BETWEEN :start and :end OR event.endAt BETWEEN :start and :end')
  58.                 ->setParameter('start'$start->format('Y-m-d H:i:s'))
  59.                 ->setParameter('end'$end->format('Y-m-d H:i:s'))
  60.                 ->getQuery()
  61.                 ->getResult()
  62.             ;
  63.         } else {
  64.             $events $this->eventRepository
  65.                 ->createQueryBuilder('event')
  66.                 ->where('event.beginAt BETWEEN :start and :end OR event.endAt BETWEEN :start and :end')
  67.                 ->setParameter('start'$start->format('Y-m-d H:i:s'))
  68.                 ->setParameter('end'$end->format('Y-m-d H:i:s'))
  69.                 ->andwhere('event.user = :userid')
  70.                 ->setParameter('userid'$this->user->getId())
  71.                 ->getQuery()
  72.                 ->getResult()
  73.             ;
  74.         }
  75.         foreach ($events as $event) {
  76.             // this create the events with your data (here booking data) to fill calendar
  77.             $bookingEvent = new Event(
  78.                 $event->getDescription()." - ".$event->getNotes(),
  79.                 $event->getBeginAt(),
  80.                 $event->getEndAt()
  81.                  // If the end date is null or not defined, a all day event is created.
  82.             );
  83.             switch ($event->getType()) {
  84.                 case 'call':
  85.                     $color "#0d6efd";
  86.                     break;
  87.                 case 'interview':
  88.                     $color "#0dcaf0";
  89.                     break;
  90.                 case 'sale':
  91.                     $color "#28a745";
  92.                     break;
  93.                 default:
  94.                     $color "#0d6efd";
  95.                     break;
  96.             }
  97.             
  98.             /*
  99.              * Add custom options to events
  100.              *
  101.              * For more information see: https://fullcalendar.io/docs/event-object
  102.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  103.              */
  104.             $bookingEvent->setOptions([
  105.                 'backgroundColor' => $color,
  106.                 'borderColor' => $color,
  107.                 'id'=>$event->getId(),
  108.                 'startEditable' => true,
  109.                 'useDetailPopup' => true,
  110.                 'overlap'=>$event->getDescription(),
  111.             ]);
  112.             $bookingEvent->addOption(
  113.                 'url',
  114.                 $this->router->generate('app_event_edit', [
  115.                     'id' => $event->getId(),
  116.                 ])
  117.             );
  118.             // finally, add the event to the CalendarEvent to fill the calendar
  119.             $calendar->addEvent($bookingEvent);
  120.         }
  121.     }
  122.     public function fillTasksCalendar(CalendarEvent $calendar\DateTimeInterface $start\DateTimeInterface $end, array $filters) {
  123.         // Modify the query to fit to your entity and needs
  124.         // Change booking.beginAt by your start date property
  125.         if ($this->security->isGranted('ROLE_SUPERUSER') || $this->security->isGranted('ROLE_CSO') || $this->security->isGranted('ROLE_PMM') || $this->security->isGranted('ROLE_ADMINISTRATOR')) {
  126.             $tasks $this->taskRepository
  127.                 ->createQueryBuilder('task')
  128.                 ->where('task.end BETWEEN :start and :endD')
  129.                 ->andWhere("task.status != 'STATUS_SUSPENDED'")
  130.                 ->setParameter('start'$start->format('Y-m-d H:i:s'))
  131.                 ->setParameter('endD'$end->format('Y-m-d H:i:s'))
  132.                 ->getQuery()
  133.                 ->getResult()
  134.             ;
  135.         } else {
  136.             $tasks $this->taskRepository
  137.                 ->createQueryBuilder('task')
  138.                 ->addSelect('user')
  139.                 ->innerJoin('task.strategists''user')
  140.                 ->where('user.id = :userid')
  141.                 ->setParameter('userid'$this->user->getId())
  142.                 ->andWhere('task.end BETWEEN :start and :endD')
  143.                 ->andWhere("task.status != 'STATUS_SUSPENDED'")
  144.                 ->setParameter('start'$start->format('Y-m-d H:i:s'))
  145.                 ->setParameter('endD'$end->format('Y-m-d H:i:s'))
  146.                 ->getQuery()
  147.                 ->getResult()
  148.             ;
  149.         }
  150.         foreach ($tasks as $task) {
  151.             // this create the events with your data (here booking data) to fill calendar
  152.             $strategistsInfo '';
  153.             foreach ($task->getStrategists() as $user) {
  154.                 $strategistsInfo $strategistsInfo." ".$user->getName().' '.$user->getSurname().'; ';
  155.             }
  156.             $bookingEvent = new Event(
  157.                 $task->getName()." - ".$task->getDescription()." - ".$strategistsInfo,
  158.                 $task->getStart(),
  159.                 $task->getEnd()
  160.                  // If the end date is null or not defined, a all day event is created.
  161.             );
  162.             switch ($task->getStatus()) {
  163.                 case 'STATUS_ACTIVE':
  164.                     $color "#0d6efd";
  165.                     break;
  166.                 case 'STATUS_SUSPENDED':
  167.                     $color "#ffc107";
  168.                     break;
  169.                 case 'STATUS_DONE':
  170.                     $color "#198754";
  171.                     break;
  172.                 case 'STATUS_FAILED':
  173.                     $color "#dc3545";
  174.                     break;
  175.                 case 'STATUS_WAITING':
  176.                     $color "#ffc107";
  177.                     break;
  178.                 case 'STATUS_UNDEFINED':
  179.                     $color "#6c757d";
  180.                     break;
  181.                 default:
  182.                     $color "#0d6efd";
  183.                     break;
  184.             }
  185.             
  186.                   
  187.             /*
  188.              * Add custom options to events
  189.              *
  190.              * For more information see: https://fullcalendar.io/docs/event-object
  191.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  192.              */
  193.             $bookingEvent->setOptions([
  194.                 'backgroundColor' => $color,
  195.                 'borderColor' => $color,
  196.                 'id'=>$task->getId(),
  197.                 //'startEditable' => true,
  198.                 'useDetailPopup' => true,
  199.                 'overlap'=>$task->getDescription(),
  200.             ]);
  201.             /*$bookingEvent->addOption(
  202.                 'url',
  203.                 $this->router->generate('app_event_edit', [
  204.                     'id' => $task->getId(),
  205.                 ])
  206.             );*/
  207.             // finally, add the event to the CalendarEvent to fill the calendar
  208.             $calendar->addEvent($bookingEvent);
  209.         }
  210.     }
  211. }