src/EventSubscriber/CalendarSubscriber.php line 31
<?php
namespace App\EventSubscriber;
use App\Repository\DemandeInspectionRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class CalendarSubscriber implements EventSubscriberInterface
{
private $demandeIspectionRepo;
private $router;
public function __construct(
DemandeInspectionRepository $demandeIspectionRepo,
UrlGeneratorInterface $router
) {
$this->demandeIspectionRepo = $demandeIspectionRepo;
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
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
$demande_inspect = $this->demandeIspectionRepo
->createQueryBuilder('di')
->where('di.DateSouhaitee 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 ($demande_inspect as $inspect) {
// this create the events with your data (here booking data) to fill calendar
$inspectEvent = new Event(
$inspect->getDemandeur(),
$inspect->setDateSouhaitee(),
$inspect->setDateSouhaitee() // 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
*/
$inspectEvent->setOptions([
'backgroundColor' => 'red',
'borderColor' => 'red',
]);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($inspectEvent);
}
}
}