src/EventSubscriber/CalendarSubscriber.php line 26

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\MissionRepository;
  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. class CalendarSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private readonly MissionRepository $missionRepository,
  13.         private readonly UrlGeneratorInterface $router
  14.     ) {}
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  19.         ];
  20.     }
  21.     public function onCalendarSetData(CalendarEvent $calendar)
  22.     {
  23.         $start $calendar->getStart();
  24.         $end $calendar->getEnd();
  25.         $missions $this->missionRepository
  26.             ->createQueryBuilder('mission')
  27.             ->where('mission.day BETWEEN :start and :end')
  28.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  29.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  30.             ->getQuery()
  31.             ->getResult()
  32.         ;
  33.         foreach ($missions as $mission) {
  34.             $color $mission->getCustomer()->getColor();
  35.             $eventName $this->getFormattedEventTitle($mission->getCustomer()->getName(), $mission->getPlace());
  36.             $event = new Event(
  37.                 $eventName,
  38.                 $mission->getDay()
  39.             );
  40.             $event->setOptions([
  41.                 'backgroundColor' => $color,
  42.                 'borderColor' => $color
  43.             ]);
  44.             $event->addOption(
  45.                 'url',
  46.                 $this->router->generate('app_mission_show', [
  47.                     'id' => $mission->getId()
  48.                 ])
  49.             );
  50.             $calendar->addEvent($event);
  51.         }
  52.     }
  53.     private function getFormattedEventTitle(string $customerstring $place): string
  54.     {
  55.         return $place  ' (' $customer ')';
  56.     }
  57. }