<?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'];
$id_unidad = $filters['id_unidad'];
$tipo = $filters['tipo'];
// Modify the query to fit to your entity and needs
// Change booking.beginAt by your start date property
//dd($filters);
if($id_unidad == 0){
$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()
;
$reuniones = $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', 2)
->getQuery()
->getResult()
;
}else{
if($tipo == 'privado'){
$procedimientoIds = [3,5,90,91,92,95,193,207,208,215,216];
$agendas = $this->AgendaRepository
->createQueryBuilder('a')
->innerJoin('a.sala', 's')
->innerJoin('a.seguro', 'seguro')
->innerJoin('a.procedimiento', 'procedimiento')
->where('a.inicio BETWEEN :start and :end OR a.fin BETWEEN :start and :end')
->setParameter('start', $start->format('Y-m-d'))
->setParameter('end', $end->format('Y-m-d'))
->andWhere('s.unidad = :val')
->setParameter('val', $id_unidad)
->andWhere('a.estado = :val1')
->setParameter('val1', 1)
->andWhere('seguro.tipo = :valTipo')
->andWhere('procedimiento.id IN (:procedimientoIds)') // filtro por IDs
->setParameter('procedimientoIds', $procedimientoIds) // pasa el array
->setParameter('valTipo', 2)
->getQuery()
->getResult()
;
$reuniones = [];
}else{
$agendas = $this->AgendaRepository
->createQueryBuilder('a')
->innerJoin('a.sala', 's')
->where('a.inicio BETWEEN :start and :end OR a.fin BETWEEN :start and :end')
->setParameter('start', $start->format('Y-m-d'))
->setParameter('end', $end->format('Y-m-d'))
->andWhere('s.unidad = :val')
->setParameter('val', $id_unidad)
->andWhere('a.estado = :val1')
->setParameter('val1', 1)
->getQuery()
->getResult()
;
$reuniones = $this->AgendaRepository
->createQueryBuilder('a')
->innerJoin('a.sala', 's')
->where('a.inicio BETWEEN :start and :end OR a.fin BETWEEN :start and :end')
->setParameter('start', $start->format('Y-m-d'))
->setParameter('end', $end->format('Y-m-d'))
->andWhere('s.unidad = :val')
->setParameter('val', $id_unidad)
->andWhere('a.estado = :val1')
->setParameter('val1', 2)
->getQuery()
->getResult()
;
}
}
foreach ($agendas as $agenda) {
$apxs = $agenda->getAgendaProcedimientos();
$txt_ap = '';
foreach($apxs as $ap){
$txt_ap .= ' ->'.$ap->getProcedimiento()->getNombre();
}
// 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';
}
if($id_unidad == 0){
$agendaEvent = new Event(
$agenda->getPaciente()->getApellido1().' '.$agenda->getPaciente()->getApellido2().' '.$agenda->getPaciente()->getNombre1().' '.$agenda->getPaciente()->getNombre2().' - '.$agenda->getProcedimiento()->getNombre().$txt_ap.' - '.$agenda->getEspecialidad()->getNombre().' : 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.
);
$htmlDescription = '';
if($agenda->getConfirma() == '1'){
$htmlDescription = 'CONFIRMADO';
}
$agendaEvent->setOptions([
'backgroundColor' => '#581845',
'borderColor' => 'white',
'description' => $htmlDescription,
'confirma' => $agenda->getConfirma(),
'id' => $agenda->getId(),
'procedimientos' => $agenda->getProcedimiento()->getNombre().$txt_ap
]);
}else{
$msn = $agenda->getPaciente()->getApellido1().' '.$agenda->getPaciente()->getApellido2().' '.$agenda->getPaciente()->getNombre1().' '.$agenda->getPaciente()->getNombre2().' - '.$agenda->getProcedimiento()->getNombre().$txt_ap.' - '.$agenda->getEspecialidad()->getNombre().' : SEGURO:'.$agenda->getseguro()->getNombre().' ESTADO: '.$txtestado;
if($agenda->getConfirma()== '1'){
$msn = $msn.' -CONFIRMADO';
}
if($agenda->getDocumentos()){
$msn = $msn.' -DOCUMENTOS LISTOS';
}
$agendaEvent = new Event(
$msn,
$agenda->getInicio(),
$agenda->getFin() // If the end date is null or not defined, a all day event is created.
);
$htmlDescription = '';
if($agenda->getConfirma() == '1'){
$htmlDescription = 'CONFIRMADO';
}
$confirma = $agenda->getConfirma();
if($tipo == 'privado' and $agenda->getDocumentos()){
$htmlDescription = 'DOCUMENTOS OK';
$confirma = 1;
}
$color = "#A9A9A9";
if($agenda->getDoctor() != null){
if($agenda->getDoctor()->getColor() != null){
$color = $agenda->getDoctor()->getColor();
}
}
$agendaEvent->setOptions([
'backgroundColor' => $color,
'borderColor' => 'black',
'description' => $htmlDescription,
'confirma' => $confirma,
'id' => $agenda->getId(),
'procedimientos' => $agenda->getProcedimiento()->getNombre().$txt_ap
]);
}
/*
* 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' and $id_unidad == 0){
$agendaEvent->addOption('backgroundColor', 'black');
$agendaEvent->addOption('borderColor', 'white');
}elseif ($agenda->getEstadoAgenda()=='1' and $id_unidad == 0) {
$agendaEvent->addOption('backgroundColor', 'blue');
$agendaEvent->addOption('borderColor', 'white');
}elseif ($agenda->getEstadoAgenda()=='2' and $id_unidad == 0) {
$agendaEvent->addOption('backgroundColor', 'green');
$agendaEvent->addOption('borderColor', 'white');
}
$agendaEvent->addOption(
'url',
$this->router->generate('agenda_edit', [
'id' => $agenda->getId(),
])
);
if($agenda->getId()== '99118'){
//dd($agendaEvent);
}
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($agendaEvent);
}
foreach ($reuniones as $agenda) {
// this create the events with your data (here booking data) to fill calendar
$txtestado = 'REUNION';
if($id_unidad == 0){
$agendaEvent = new Event(
$txtestado.' '.$agenda->getObservacion(),
$agenda->getInicio(),
$agenda->getFin() // If the end date is null or not defined, a all day event is created.
);
$htmlDescription = '';
if($agenda->getConfirma() == '1'){
$htmlDescription = 'CONFIRMADO';
}
$agendaEvent->setOptions([
'backgroundColor' => '#581845',
'borderColor' => 'white',
'description' => $htmlDescription,
'confirma' => $agenda->getConfirma(),
'id' => $agenda->getId()
]);
}else{
$agendaEvent = new Event(
'DR. '.$agenda->getDoctor()->getApellido1().' '.$agenda->getDoctor()->getNombre1().' - '.$txtestado.' '.$agenda->getObservacion(),
$agenda->getInicio(),
$agenda->getFin() // If the end date is null or not defined, a all day event is created.
);
$htmlDescription = '';
if($agenda->getConfirma() == '1'){
$htmlDescription = 'CONFIRMADO';
}
$agendaEvent->setOptions([
'backgroundColor' => $agenda->getDoctor()->getColor(),
'borderColor' => 'black',
'description' => $htmlDescription,
'confirma' => $agenda->getConfirma(),
'id' => $agenda->getId()
]);
}
/*
* 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
*/
$agendaEvent->addOption(
'url',
$this->router->generate('agenda_reunion_edit', [
'id' => $agenda->getId(),
])
);
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($agendaEvent);
}
}
// ...
}