<?php
namespace App\Event\Listener;
use App\Entity\User;
use App\Repository\Calendar\CalendarEventRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class CalendarSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly CalendarEventRepository $calendarEventRepository,
private readonly UrlGeneratorInterface $router,
private readonly Security $security,
private readonly TranslatorInterface $translator,
) {
}
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}
public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();
// Modify the query to fit to your entity and needs
$calendarEvents = $this->calendarEventRepository->getCalendarEventsBetweenDatesForUser($start, $end, $this->security);
/** @var \App\Entity\Calendar\CalendarEvent $calendarEvent */
foreach ($calendarEvents as $calendarEvent) {
// this creates the events with your data to fill calendar
$event = new Event(
$calendarEvent->getTitle(),
$calendarEvent->getBeginAt(),
$calendarEvent->getEndAt(), // If the end date is null or not defined, an all day event is created.
);
/*
* Add custom options to events
*
* For more information see: https://fullcalendar.io/docs/event-object
*/
$event->setOptions([
'id' => $calendarEvent->getId(),
'allDay' => (bool) $calendarEvent->isAllDayEvent(),
'display' => 'auto', // default is 'auto' but Vulco wants all events in a block with background
'backgroundColor' => '#'.$calendarEvent->getCalendarEventCategory()->getColor(),
'borderColor' => '#'.$calendarEvent->getCalendarEventCategory()->getColor(),
'textColor' => '#'.$calendarEvent->getCalendarEventCategory()->getTextColor(),
'classNames' => ['fc-event-id-'.$calendarEvent->getId()],
]);
// aquesta url pot servir per anar al show (o al edit):
/*$event->addOption(
'url',
$this->router->generate('admin_calendar_event_show', [
'id' => $calendarEvent->getId(),
])
);*/
$description = $calendarEvent->getDescription() ? '<div class="mb-2">'.$calendarEvent->getDescription().'</div>' : '';
/** @var User $user */
if ($user = $calendarEvent->getUser()) {
// created by
// $createdBy = $this->translator->trans('calendar.calendar_event.list.user');
$description = $description.'<div><i>' /* .$createdBy .': ' */.$user->getName().'</i></div>';
}
$event->addOption(
'extendedProps',
[
'description' => $description,
'link' => $calendarEvent->getLink() ?? '',
'link_label' => $calendarEvent->getLinkLabel() ?? '', // OJO!, que no maneja bien variables de nombre camelcased como linkLabel
'background_color' => $calendarEvent->getCalendarEventCategory()->getColor(),
'text_color' => $calendarEvent->getCalendarEventCategory()->getTextColor(),
]
);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($event);
}
}
}