<?php
namespace App\EventListener;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class CalendarListener
{
public function __construct($em, TokenStorageInterface $tokenStorage)
{
$this->em = $em;
$this->tokenStorage = $tokenStorage;
}
public function load(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();
if (!empty($filters) && $filters['user'] != null) {
$user = $this->em->getRepository('App:User')->find($filters['user']);
} else {
$user = $this->tokenStorage->getToken()->getUser();
}
// Modify the query to fit to your entity and needs
// Change booking.beginAt by your start date property
$events = $this->em->getRepository('App:Calendar')
->createQueryBuilder('cal')
->where('cal.startdate BETWEEN :start and :end')
->andWhere('cal.user = :user_id')
->setParameter('user_id', $user->getId())
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'))
->getQuery()
->getResult();
foreach ($events as $mydata) {
// this create the events with your data (here booking data) to fill calendar
$event = new Event(
$mydata->getTitle(),
$mydata->getStartdate(),
$mydata->getEnddate() // 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
*/
$color = json_decode($mydata->getColor())->primary;
$event->setOptions([
'backgroundColor' => $color,
'borderColor' => $color,
'id' => $mydata->getId()
]);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($event);
}
}
}