src/Event/Listener/CalendarSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Event\Listener;
  3. use App\Entity\User;
  4. use App\Repository\Calendar\CalendarEventRepository;
  5. use CalendarBundle\CalendarEvents;
  6. use CalendarBundle\Entity\Event;
  7. use CalendarBundle\Event\CalendarEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class CalendarSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private readonly CalendarEventRepository $calendarEventRepository,
  16.         private readonly UrlGeneratorInterface $router,
  17.         private readonly Security $security,
  18.         private readonly TranslatorInterface $translator,
  19.     ) {
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  25.         ];
  26.     }
  27.     public function onCalendarSetData(CalendarEvent $calendar)
  28.     {
  29.         $start $calendar->getStart();
  30.         $end $calendar->getEnd();
  31.         $filters $calendar->getFilters();
  32.         // Modify the query to fit to your entity and needs
  33.         $calendarEvents $this->calendarEventRepository->getCalendarEventsBetweenDatesForUser($start$end$this->security);
  34.         /** @var \App\Entity\Calendar\CalendarEvent $calendarEvent */
  35.         foreach ($calendarEvents as $calendarEvent) {
  36.             // this creates the events with your data to fill calendar
  37.             $event = new Event(
  38.                 $calendarEvent->getTitle(),
  39.                 $calendarEvent->getBeginAt(),
  40.                 $calendarEvent->getEndAt(), // If the end date is null or not defined, an 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.              */
  47.             $event->setOptions([
  48.                 'id' => $calendarEvent->getId(),
  49.                 'allDay' => (bool) $calendarEvent->isAllDayEvent(),
  50.                 'display' => 'auto'// default is 'auto' but Vulco wants all events in a block with background
  51.                 'backgroundColor' => '#'.$calendarEvent->getCalendarEventCategory()->getColor(),
  52.                 'borderColor' => '#'.$calendarEvent->getCalendarEventCategory()->getColor(),
  53.                 'textColor' => '#'.$calendarEvent->getCalendarEventCategory()->getTextColor(),
  54.                 'classNames' => ['fc-event-id-'.$calendarEvent->getId()],
  55.             ]);
  56.             // aquesta url pot servir per anar al show (o al edit):
  57.             /*$event->addOption(
  58.                 'url',
  59.                 $this->router->generate('admin_calendar_event_show', [
  60.                     'id' => $calendarEvent->getId(),
  61.                 ])
  62.             );*/
  63.             $description $calendarEvent->getDescription() ? '<div class="mb-2">'.$calendarEvent->getDescription().'</div>' '';
  64.             /** @var User $user */
  65.             if ($user $calendarEvent->getUser()) {
  66.                 // created by
  67.                 // $createdBy = $this->translator->trans('calendar.calendar_event.list.user');
  68.                 $description $description.'<div><i>' /* .$createdBy .': ' */.$user->getName().'</i></div>';
  69.             }
  70.             $event->addOption(
  71.                 'extendedProps',
  72.                 [
  73.                     'description' => $description,
  74.                     'link' => $calendarEvent->getLink() ?? '',
  75.                     'link_label' => $calendarEvent->getLinkLabel() ?? '',  // OJO!, que no maneja bien variables de nombre camelcased como linkLabel
  76.                     'background_color' => $calendarEvent->getCalendarEventCategory()->getColor(),
  77.                     'text_color' => $calendarEvent->getCalendarEventCategory()->getTextColor(),
  78.                 ]
  79.             );
  80.             // finally, add the event to the CalendarEvent to fill the calendar
  81.             $calendar->addEvent($event);
  82.         }
  83.     }
  84. }