<?php
namespace App\EventSubscriber;
use App\Repository\NotaRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class CalendarSubscriber implements EventSubscriberInterface
{
public function __construct(NotaRepository $notaRepository, UrlGeneratorInterface $router)
{
$this->notaRepository = $notaRepository;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}
public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();
// You may want to make a custom query from your database to fill the calendar
$notas = $this->notaRepository->filter($filters);
for ($i = 0; $i < count($notas); $i++) {
$event = $this->createEvent($notas[$i]);
$calendar->addEvent($event);
}
}
private function createEvent($nota)
{
$event = new Event(
$nota->getTitulo(),
$nota->getFecha()
);
$event
->addOption(
'url',
$this->router->generate(
'agenda_edit',
[
'id' => $nota->getId()
]
)
);
return $event;
}
}