src/EventSubscriber/CalendarSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\PlanningTemporaire;
  4. use CalendarBundle\CalendarEvents;
  5. use CalendarBundle\Entity\Event;
  6. use CalendarBundle\Event\CalendarEvent;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CalendarSubscriber implements EventSubscriberInterface
  10. {
  11.     private $em;
  12.     public function __construct(EntityManagerInterface $em)
  13.     {
  14.         $this->em $em;        
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  20.         ];
  21.     }
  22.     public function onCalendarSetData(CalendarEvent $calendar)
  23.     {
  24.         $start $calendar->getStart();
  25.         $end $calendar->getEnd();
  26.         $filters $calendar->getFilters();
  27.         if(!empty($filters)){
  28.             $agent_id $filters['agent'];
  29.             $planning_name $filters['planning_temporaire'];
  30.             /*$events = $this->em->getRepository('App:Calendar')
  31.             ->createQueryBuilder('cal')
  32.             ->where('cal.startdate BETWEEN :start and :end')
  33.             ->andWhere('cal.user = :user_id')
  34.             ->setParameter('user_id', $user->getId())
  35.             ->setParameter('start', $start->format('Y-m-d H:i:s'))
  36.             ->setParameter('end', $end->format('Y-m-d H:i:s'))
  37.             ->getQuery()
  38.             ->getResult();*/
  39.             $events $this->em->getRepository(PlanningTemporaire::class)->findBy(array('name' => $planning_name'agent' => $agent_id));
  40.             foreach($events as $event){
  41.                 $date_debut = match($event->getJour()){
  42.                     'lundi' => new \DateTime('Monday this week'),
  43.                     'mardi' => new \DateTime('Tuesday this week'),
  44.                     'mercredi' => new \DateTime('Wednesday this week'),
  45.                     'jeudi' => new \DateTime('Thursday this week'),
  46.                     'vendredi' => new \DateTime('Friday this week'),
  47.                     'samedi' => new \DateTime('Saturday this week'),
  48.                     'dimanche' => new \DateTime('Sunday this week'),
  49.                 };
  50.                 $ex_heure_depart explode(':'$event->getTrainDepartAller());
  51.                 $ex_heure_arrivee explode(':'$event->getTrainArriveeAller());
  52.                 $date_debut->setTime($ex_heure_depart[0], $ex_heure_depart[1]);
  53.                 $date_fin = clone($date_debut);
  54.                 $date_fin->setTime($ex_heure_arrivee[0], $ex_heure_arrivee[1]);
  55.                 if($date_fin->format('H') > && $date_fin->format('H') < 2){
  56.                     $date_fin->add(new \DateInterval('P1D'));
  57.                 }
  58.                 $calendar->addEvent(new Event(
  59.                     $event->getTrainNumeroAller().' | '.$event->getTrainOrigineAller().'->'.$event->getTrainDestinationAller(),
  60.                     $date_debut,
  61.                     $date_fin
  62.                 ));
  63.                 if($event->getTrainNumeroRetour() != null){
  64.                     $ex_heure_depart explode(':'$event->getTrainDepartRetour());
  65.                     $ex_heure_arrivee explode(':'$event->getTrainArriveeRetour());
  66.                     $date_debut2 = clone ($date_debut);
  67.                     $date_debut2->setTime($ex_heure_depart[0], $ex_heure_depart[1]);
  68.                     $date_fin2 = clone($date_debut);
  69.                     $date_fin2->setTime($ex_heure_arrivee[0], $ex_heure_arrivee[1]);
  70.                     if($date_fin2->format('H') > && $date_fin2->format('H') < 2){
  71.                         $date_fin2->add(new \DateInterval('P1D'));
  72.                     }
  73.                     $calendar->addEvent(new Event(
  74.                         $event->getTrainNumeroRetour().' | '.$event->getTrainOrigineRetour().'->'.$event->getTrainDestinationRetour(),
  75.                         $date_debut2,
  76.                         $date_fin2
  77.                     ));
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }