<?php
namespace App\EventSubscriber;
use App\Repository\BookingRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use DateTimeZone;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class CalendarSubscriber implements EventSubscriberInterface
{
private $bookingRepository;
private $router;
public function __construct(
BookingRepository $bookingRepository,
UrlGeneratorInterface $router
) {
$this->bookingRepository = $bookingRepository;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}
public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();
var_dump($filters);
exit();
// Modify the query to fit to your entity and needs
// Change booking.beginAt by your start date property
$bookings = $this->bookingRepository
->createQueryBuilder('booking')
->where('booking.beginAt BETWEEN :start and :end OR booking.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 ($bookings as $booking) {
// this create the events with your data (here booking data) to fill calendar
$s = $booking->getBeginAt()->sub(new \DateInterval("PT1H"));
$e = $booking->getEndAt()->sub(new \DateInterval("PT1H"));
$bookingEvent = new Event(
$booking->getTitle(),
new \DateTime($s->format("Y-m-d H:i:s"),new DateTimeZone('Europe/Paris')),
new \DateTime($e->format("Y-m-d H:i:s"),new DateTimeZone('Europe/Paris'))
);
/*
* 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
*/
$bookingEvent->setOptions([
'backgroundColor' => $booking->getUser()->getCalendarColor()
]);
/*$bookingEvent->addOption(
'url',
$this->router->generate('booking_show', [
'id' => $booking->getId(),
])
);*/
$bookingEvent->addOption(
'id', $booking->getId()
);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($bookingEvent);
}
/* $bookingEvent = new Event(
'null',
new \DateTime(),
new \DateTime()
);
$bookingEvent->setOptions([
'display' => "background"
]);
$calendar->addEvent($bookingEvent);*/
}
}