<?php
namespace App\EventSubscriber;
use App\Entity\PlanningTemporaire;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CalendarSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}
public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();
if(!empty($filters)){
$agent_id = $filters['agent'];
$planning_name = $filters['planning_temporaire'];
/*$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();*/
$events = $this->em->getRepository(PlanningTemporaire::class)->findBy(array('name' => $planning_name, 'agent' => $agent_id));
foreach($events as $event){
$date_debut = match($event->getJour()){
'lundi' => new \DateTime('Monday this week'),
'mardi' => new \DateTime('Tuesday this week'),
'mercredi' => new \DateTime('Wednesday this week'),
'jeudi' => new \DateTime('Thursday this week'),
'vendredi' => new \DateTime('Friday this week'),
'samedi' => new \DateTime('Saturday this week'),
'dimanche' => new \DateTime('Sunday this week'),
};
$ex_heure_depart = explode(':', $event->getTrainDepartAller());
$ex_heure_arrivee = explode(':', $event->getTrainArriveeAller());
$date_debut->setTime($ex_heure_depart[0], $ex_heure_depart[1]);
$date_fin = clone($date_debut);
$date_fin->setTime($ex_heure_arrivee[0], $ex_heure_arrivee[1]);
if($date_fin->format('H') > 0 && $date_fin->format('H') < 2){
$date_fin->add(new \DateInterval('P1D'));
}
$calendar->addEvent(new Event(
$event->getTrainNumeroAller().' | '.$event->getTrainOrigineAller().'->'.$event->getTrainDestinationAller(),
$date_debut,
$date_fin
));
if($event->getTrainNumeroRetour() != null){
$ex_heure_depart = explode(':', $event->getTrainDepartRetour());
$ex_heure_arrivee = explode(':', $event->getTrainArriveeRetour());
$date_debut2 = clone ($date_debut);
$date_debut2->setTime($ex_heure_depart[0], $ex_heure_depart[1]);
$date_fin2 = clone($date_debut);
$date_fin2->setTime($ex_heure_arrivee[0], $ex_heure_arrivee[1]);
if($date_fin2->format('H') > 0 && $date_fin2->format('H') < 2){
$date_fin2->add(new \DateInterval('P1D'));
}
$calendar->addEvent(new Event(
$event->getTrainNumeroRetour().' | '.$event->getTrainOrigineRetour().'->'.$event->getTrainDestinationRetour(),
$date_debut2,
$date_fin2
));
}
}
}
}
}