src/EventSubscriber/CalendarSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\NotaRepository;
  4. use CalendarBundle\CalendarEvents;
  5. use CalendarBundle\Entity\Event;
  6. use CalendarBundle\Event\CalendarEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. class CalendarSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(NotaRepository $notaRepositoryUrlGeneratorInterface $router)
  12.     {
  13.         $this->notaRepository $notaRepository;
  14.         $this->router $router;
  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.         // You may want to make a custom query from your database to fill the calendar
  28.         $notas $this->notaRepository->filter($filters);
  29.         for ($i 0$i count($notas); $i++) {
  30.             $event $this->createEvent($notas[$i]);
  31.             $calendar->addEvent($event);
  32.         }
  33.     }
  34.     private function createEvent($nota)
  35.     {
  36.         $event = new Event(
  37.             $nota->getTitulo(),
  38.             $nota->getFecha()
  39.         );
  40.         $event
  41.             ->addOption(
  42.                 'url',
  43.                 $this->router->generate(
  44.                     'agenda_edit',
  45.                     [
  46.                         'id' => $nota->getId()
  47.                     ]
  48.                 )
  49.             );
  50.         return $event;
  51.     }
  52. }