<?php
namespace App\EventSubscriber;
// ...
use App\Repository\AgendaRepository;
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
{
private $agendaRepository;
private $router;
public function __construct(
AgendaRepository $agendaRepository,
UrlGeneratorInterface $router
) {
$this->AgendaRepository = $agendaRepository;
$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();
$id_sala = $filters['id_sala'];
// Modify the query to fit to your entity and needs
// Change booking.beginAt by your start date property
$agendas = $this->AgendaRepository
->createQueryBuilder('agenda')
->where('agenda.inicio BETWEEN :start and :end OR agenda.fin BETWEEN :start and :end')
->setParameter('start', $start->format('Y-m-d'))
->setParameter('end', $end->format('Y-m-d'))
->andWhere('agenda.sala = :val')
->setParameter('val', $id_sala)
->andWhere('agenda.estado = :val1')
->setParameter('val1', 1)
->getQuery()
->getResult()
;
//dd($agendas);
foreach ($agendas as $agenda) {
// this create the events with your data (here booking data) to fill calendar
$txtestado = '';
if($agenda->getEstadoAgenda()=='0'){
$txtestado = 'LLAMAR A CONFIRMAR';
}elseif ($agenda->getEstadoAgenda()=='1') {
$txtestado = 'CONFIRMADO';
}elseif ($agenda->getEstadoAgenda()=='2') {
$txtestado = 'ADMISIONADO';
}
$agendaEvent = new Event(
$agenda->getProcedimiento()->getNombre().' '.$agenda->getEspecialidad()->getNombre().' : '.$agenda->getPaciente()->getApellido1().' '.$agenda->getPaciente()->getApellido2().' '.$agenda->getPaciente()->getNombre1().' '.$agenda->getPaciente()->getNombre2().' SEGURO:'.$agenda->getseguro()->getNombre().' ESTADO: '.$txtestado,
$agenda->getInicio(),
$agenda->getFin() // If the end date is null or not defined, a all day event is created.
);
/*
* Add custom options to events
*
* For more information see: https://fullcalendar.io/docs/event-object
* and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
*/
if($agenda->getEstadoAgenda()=='0'){
$agendaEvent->setOptions([
'backgroundColor' => 'black',
'borderColor' => 'white',
]);
}elseif ($agenda->getEstadoAgenda()=='1') {
$agendaEvent->setOptions([
'backgroundColor' => 'blue',
'borderColor' => 'white',
]);
}elseif ($agenda->getEstadoAgenda()=='2') {
$agendaEvent->setOptions([
'backgroundColor' => 'green',
'borderColor' => 'white',
]);
}
$agendaEvent->addOption(
'url',
$this->router->generate('agenda_edit', [
'id' => $agenda->getId(),
])
);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($agendaEvent);
}
}
// ...
}