src/EventListener/CalendarListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Inscription;
  4. use App\Entity\Planification;
  5. use App\Entity\User;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use CalendarBundle\Entity\Event;
  8. use CalendarBundle\Event\CalendarEvent;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class CalendarListener
  11. {
  12.     private $em;
  13.     private $tokenStorage;
  14.     public function __construct($emTokenStorageInterface $tokenStorage)
  15.     {
  16.         $this->em $em;
  17.         $this->tokenStorage $tokenStorage;
  18.     }
  19.     public function load(CalendarEvent $calendar)
  20.     {
  21.         $start $calendar->getStart();
  22.         $end $calendar->getEnd();
  23.         $filters $calendar->getFilters();
  24.         if (!empty($filters) && $filters['user'] != null) {
  25.             $user $this->em->getRepository(User::class)->find($filters['user']);
  26.         } else {
  27.             $user $this->tokenStorage->getToken()->getUser();
  28.         }
  29.         // Modify the query to fit to your entity and needs
  30.         // Change booking.beginAt by your start date property
  31.         $events $this->em->getRepository(Planification::class)
  32.             ->createQueryBuilder('cal')
  33.             ->where('cal.date_debut BETWEEN :start and :end')
  34.             ->andWhere('cal.user = :user_id')
  35.             ->setParameter('user_id'$user->getId())
  36.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  37.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  38.             ->getQuery()
  39.             ->getResult()
  40.         ;
  41.         foreach ($events as $mydata) {
  42.             $titles null;
  43.             foreach($mydata->getPrestations() as $prestation){
  44.                 if($titles != null){
  45.                     $titles .= " / ";
  46.                 }
  47.                 $titles .= $prestation->getTitle();
  48.             }
  49.             // this create the events with your data (here booking data) to fill calendar
  50.             $event = new Event(
  51.                 $titles,
  52.                 $mydata->getDateDebut(),
  53.                 $mydata->getDateFin() // If the end date is null or not defined, a all day event is created.
  54.             );
  55.             $event->setOptions([
  56.                 //'backgroundColor' => $color,
  57.                 //'borderColor' => $color,
  58.                 'id' => $mydata->getId()
  59.             ]);
  60.             /*
  61.              * Add custom options to events
  62.              *
  63.              * For more information see: https://fullcalendar.io/docs/event-object
  64.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  65.              */
  66.             /*$color = json_decode($mydata->getColor())->primary;
  67.             $event->setOptions([
  68.                 'backgroundColor' => $color,
  69.                 'borderColor' => $color,
  70.                 'id' => $mydata->getId()
  71.             ]);*/
  72.             // finally, add the event to the CalendarEvent to fill the calendar
  73.             $calendar->addEvent($event);
  74.         }
  75.         $inscrits $this->em->getRepository(Inscription::class)
  76.             ->createQueryBuilder('cal')
  77.             ->join('cal.prestation''prestation')
  78.             ->where('cal.date_debut BETWEEN :start and :end')
  79.             ->andWhere('prestation.user = :user_id')
  80.             ->andWhere('cal.statut = 1')
  81.             ->setParameter('user_id'$user->getId())
  82.             ->setParameter('start'$start->format('Y-m-d H:i:s'))
  83.             ->setParameter('end'$end->format('Y-m-d H:i:s'))
  84.             ->getQuery()
  85.             ->getResult()
  86.         ;
  87.         foreach ($inscrits as $mydata) {
  88.             $title $mydata->getPrestation()->getTitle();            
  89.             // this create the events with your data (here booking data) to fill calendar
  90.             $event = new Event(
  91.                 $title,
  92.                 $mydata->getDateDebut(),
  93.                 $mydata->getDateFin() // If the end date is null or not defined, a all day event is created.
  94.             );
  95.             $event->setOptions([
  96.                 'backgroundColor' => "red",
  97.                 //'borderColor' => $color,
  98.                 'id' => $mydata->getId()
  99.             ]);
  100.             /*
  101.              * Add custom options to events
  102.              *
  103.              * For more information see: https://fullcalendar.io/docs/event-object
  104.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  105.              */
  106.             /*$color = json_decode($mydata->getColor())->primary;
  107.             $event->setOptions([
  108.                 'backgroundColor' => $color,
  109.                 'borderColor' => $color,
  110.                 'id' => $mydata->getId()
  111.             ]);*/
  112.             // finally, add the event to the CalendarEvent to fill the calendar
  113.             $calendar->addEvent($event);
  114.         }
  115.     }
  116. }