<?php
namespace App\EventSubscriber;
use App\Repository\CalendarRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CalendarSubscriber implements EventSubscriberInterface
{
private $calendarRepository;
private $router;
public function __construct(
CalendarRepository $calendarRepository,
UrlGeneratorInterface $router,
TranslatorInterface $translator
) {
$this->calendarRepository = $calendarRepository;
$this->router = $router;
$this->translator = $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
// Change booking.beginAt by your start date property
$dbcalendarevents = $this->calendarRepository
->createQueryBuilder('calendar')
->where('calendar.approved = 1 AND (calendar.beginAt BETWEEN :start and :end OR calendar.endAt BETWEEN :start and :end)')
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'))
->getQuery()
->getResult()
;
foreach ($dbcalendarevents as $event) {
// this create the events with your data (here booking data) to fill calendar
/*if($translator->getLocale() == 'nl') {
$calendarEvent = new Event(
$event->getTitleNl(),
$event->getBeginAt(),
$event->getEndAt() // If the end date is null or not defined, a all day event is created.
);
} else {
$calendarEvent = new Event(
$event->getTitleNl(),
$event->getBeginAt(),
$event->getEndAt() // If the end date is null or not defined, a all day event is created.
);
}*/
$calendarEvent = new Event(
$event->getTitleNl(),
$event->getBeginAt(),
$event->getEndAt() // If the end date is null or not defined, a all day event is created.
);
/*
* Add custom options to events
*
* For more information see: https://fullcalendar.io/docs/event-object
* and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
*/
$calendarEvent->setOptions([
'backgroundColor' => '#90B116',
'borderColor' => '#90B116',
'textColor' => '#000000',
]);
$calendarEvent->addOption(
'url',
$this->router->generate('calendar_event', [
'id' => $event->getId(),
])
);
$calendarEvent->addOption(
'title_en', $event->getTitleEn()
);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($calendarEvent);
}
}
}