<?php
namespace App\EventListener;
use App\Entity\Inscription;
use App\Entity\Planification;
use App\Entity\User;
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
{
private $em;
private $tokenStorage;
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(User::class)->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(Planification::class)
->createQueryBuilder('cal')
->where('cal.date_debut 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) {
$titles = null;
foreach($mydata->getPrestations() as $prestation){
if($titles != null){
$titles .= " / ";
}
$titles .= $prestation->getTitle();
}
// this create the events with your data (here booking data) to fill calendar
$event = new Event(
$titles,
$mydata->getDateDebut(),
$mydata->getDateFin() // If the end date is null or not defined, a all day event is created.
);
$event->setOptions([
//'backgroundColor' => $color,
//'borderColor' => $color,
'id' => $mydata->getId()
]);
/*
* 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);
}
$inscrits = $this->em->getRepository(Inscription::class)
->createQueryBuilder('cal')
->join('cal.prestation', 'prestation')
->where('cal.date_debut BETWEEN :start and :end')
->andWhere('prestation.user = :user_id')
->andWhere('cal.statut = 1')
->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 ($inscrits as $mydata) {
$title = $mydata->getPrestation()->getTitle();
// this create the events with your data (here booking data) to fill calendar
$event = new Event(
$title,
$mydata->getDateDebut(),
$mydata->getDateFin() // If the end date is null or not defined, a all day event is created.
);
$event->setOptions([
'backgroundColor' => "red",
//'borderColor' => $color,
'id' => $mydata->getId()
]);
/*
* 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);
}
}
}