src/EventSubscriber/CalendarSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use CalendarBundle\Entity\Event;
  4. use CalendarBundle\CalendarEvents;
  5. use App\Repository\AgendaRepository;
  6. use App\Repository\TaskRepository;
  7. use CalendarBundle\Event\CalendarEvent;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CalendarSubscriber implements EventSubscriberInterface
  12. {
  13.     private $taskRepository;
  14.     private $security;
  15.     private $router;
  16.     public const TASK_VISIBILITY 'Public';
  17.     public function __construct(TaskRepository $taskRepositorySecurity $securityUrlGeneratorInterface $router)
  18.     {
  19.         $this->taskRepository $taskRepository;
  20.         $this->security $security;
  21.         $this->router $router;
  22.     }
  23.     
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  28.         ];
  29.     }
  30.     public function onCalendarSetData(CalendarEvent $calendar)
  31.     {
  32.         $start $calendar->getStart();
  33.         $end $calendar->getEnd();
  34.         $filters $calendar->getFilters();
  35.         // You may want to make a custom query from your database to fill the calendar
  36.         $tasks $this->taskRepository
  37.             ->createQueryBuilder('t')
  38.             ->leftJoin('t.participants''p')
  39.             ->addSelect('p')
  40.             //->where('(t.begin_at BETWEEN :start and :end OR t.end_at BETWEEN :start and :end) and t.created_by = :created_by')
  41.             ->where('t.begin_at BETWEEN :start and :end OR t.end_at BETWEEN :start and :end')
  42.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  43.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  44.             //->setParameter('created_by', $this->security->getUser())
  45.             ->getQuery()
  46.             ->getResult()
  47.         ;
  48.         foreach ($tasks as $task) {
  49.             if (($task->getVisibiity() == self::TASK_VISIBILITY) or ($task->getCreatedBy()->getUsername() == $this->security->getUser()->getUsername() or $task->isUserParticipated($this->security->getUser()))) {
  50.                 
  51.                 $taskEvent = new Event(
  52.                     $task->getTitle(),
  53.                     $task->getBeginAt(),
  54.                     $task->getEndAt()
  55.                 );
  56.                 $taskEvent->setOptions([
  57.                     'backgroundColor' => $task->getColor(),
  58.                     'borderColor' => '#d3dde2',
  59.                 ]);
  60.                 $taskEvent->addOption(
  61.                     'url',
  62.                     $this->router->generate('task_edit', [
  63.                         'id' => $task->getId(),
  64.                     ])
  65.                 );
  66.             
  67.                 // finally, add the event to the CalendarEvent to fill the calendar
  68.                 $calendar->addEvent($taskEvent);
  69.             }
  70.         }
  71.     }
  72. }