src/EventSubscriber/CalendarSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. // ...
  4. use App\Repository\AgendaRepository;
  5. use CalendarBundle\CalendarEvents;
  6. use CalendarBundle\Entity\Event;
  7. use CalendarBundle\Event\CalendarEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. class CalendarSubscriber implements EventSubscriberInterface
  11. {
  12.     private $agendaRepository;
  13.     private $router;
  14.     public function __construct(
  15.         AgendaRepository $agendaRepository,
  16.         UrlGeneratorInterface $router
  17.     ) {
  18.         $this->AgendaRepository $agendaRepository;
  19.         $this->router $router;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         
  24.         return [
  25.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  26.         ];
  27.     }
  28.     public function onCalendarSetData(CalendarEvent $calendar)
  29.     {
  30.         
  31.         $start $calendar->getStart();
  32.         $end $calendar->getEnd();
  33.         $filters $calendar->getFilters();
  34.         
  35.         $id_sala   $filters['id_sala'];
  36.         $id_unidad $filters['id_unidad'];
  37.         $tipo      $filters['tipo'];
  38.         $q $filters['q'] ?? null;
  39.         //dd($q);
  40.         // Modify the query to fit to your entity and needs
  41.         // Change booking.beginAt by your start date property
  42.         //dd($filters);
  43.         if($id_unidad == 0){
  44.             //dd($q); 
  45.             /*$agendas = $this->AgendaRepository
  46.                 ->createQueryBuilder('agenda')
  47.                 ->where('agenda.inicio BETWEEN :start and :end OR agenda.fin BETWEEN :start and :end')
  48.                 ->setParameter('start', $start->format('Y-m-d'))
  49.                 ->setParameter('end', $end->format('Y-m-d'))
  50.                 ->andWhere('agenda.sala = :val')
  51.                 ->setParameter('val', $id_sala)
  52.                 ->andWhere('agenda.estado = :val1')
  53.                 ->setParameter('val1', 1)
  54.                 ->getQuery()
  55.                 ->getResult()
  56.             ;*/
  57.             $qb $this->AgendaRepository
  58.                 ->createQueryBuilder('a')
  59.                 ->innerJoin('a.sala''s')
  60.                 ->where(
  61.                     '(a.inicio BETWEEN :start AND :end) 
  62.                     OR (a.fin BETWEEN :start AND :end)'
  63.                 )
  64.                 ->andWhere('a.sala = :sala')
  65.                 ->andWhere('a.estado = :estado')
  66.                 ->setParameter('start'$start->format('Y-m-d'))
  67.                 ->setParameter('end'$end->format('Y-m-d'))
  68.                 ->setParameter('sala'$id_sala)
  69.                 ->setParameter('estado'1);
  70.                 
  71.             if (!empty($q)) {
  72.                 $qb
  73.                     ->leftJoin('a.paciente''p')
  74.                     ->leftJoin('a.doctor''d')
  75.                     ->andWhere(
  76.                         'p.apellido1 LIKE :q 
  77.                         OR p.nombre1 LIKE :q
  78.                         OR p.cedula LIKE :q'
  79.                     )
  80.                     ->setParameter('q''%'.$q.'%');
  81.             }  
  82.             
  83.             $agendas $qb->getQuery()->getResult();//dd($agendas);
  84.             
  85.         
  86.             $reuniones $this->AgendaRepository
  87.                 ->createQueryBuilder('agenda')
  88.                 ->where('agenda.inicio BETWEEN :start and :end OR agenda.fin BETWEEN :start and :end')
  89.                 ->setParameter('start'$start->format('Y-m-d'))
  90.                 ->setParameter('end'$end->format('Y-m-d'))
  91.                 ->andWhere('agenda.sala = :val')
  92.                 ->setParameter('val'$id_sala)
  93.                 ->andWhere('agenda.estado = :val1')
  94.                 ->setParameter('val1'2)
  95.                 ->getQuery()
  96.                 ->getResult()
  97.             ;
  98.         }else{
  99.             if($tipo == 'privado'){
  100.                 $procedimientoIds = [3,5,90,91,92,95,193,207,208,215,216];
  101.                 $agendas $this->AgendaRepository
  102.                     ->createQueryBuilder('a')
  103.                     ->innerJoin('a.sala''s')
  104.                     ->innerJoin('a.seguro''seguro')
  105.                     ->innerJoin('a.procedimiento''procedimiento')
  106.                     ->where('a.inicio BETWEEN :start and :end OR a.fin BETWEEN :start and :end')
  107.                     ->setParameter('start'$start->format('Y-m-d'))
  108.                     ->setParameter('end'$end->format('Y-m-d'))
  109.                     ->andWhere('s.unidad = :val')
  110.                     ->setParameter('val'$id_unidad)
  111.                     ->andWhere('a.estado = :val1')
  112.                     ->setParameter('val1'1)
  113.                     ->andWhere('seguro.tipo = :valTipo')
  114.                     ->andWhere('procedimiento.id IN (:procedimientoIds)'// filtro por IDs
  115.                     ->setParameter('procedimientoIds'$procedimientoIds// pasa el array
  116.                     ->setParameter('valTipo'2)
  117.                     ->getQuery()
  118.                     ->getResult()
  119.                 ;   
  120.                 $reuniones = [];
  121.                 
  122.             }else{
  123.                 /*$agendas = $this->AgendaRepository
  124.                     ->createQueryBuilder('a')
  125.                     ->innerJoin('a.sala', 's')
  126.                     ->where('a.inicio BETWEEN :start and :end OR a.fin BETWEEN :start and :end')
  127.                     ->setParameter('start', $start->format('Y-m-d'))
  128.                     ->setParameter('end', $end->format('Y-m-d'))
  129.                     ->andWhere('s.unidad = :val')
  130.                     ->setParameter('val', $id_unidad)
  131.                     ->andWhere('a.estado = :val1')
  132.                     ->setParameter('val1', 1)
  133.                     ->getQuery()
  134.                     ->getResult()
  135.                 ;*/
  136.                 $qb $this->AgendaRepository
  137.                     ->createQueryBuilder('a')
  138.                     ->innerJoin('a.sala''s')
  139.                     ->where(
  140.                         '(a.inicio BETWEEN :start AND :end) 
  141.                         OR (a.fin BETWEEN :start AND :end)'
  142.                     )
  143.                     ->andWhere('s.unidad = :unidad')
  144.                     ->andWhere('a.estado = :estado')
  145.                     ->setParameter('start'$start->format('Y-m-d'))
  146.                     ->setParameter('end'$end->format('Y-m-d'))
  147.                     ->setParameter('unidad'$id_unidad)
  148.                     ->setParameter('estado'1);
  149.                 
  150.                 if (!empty($q)) {
  151.                     $qb
  152.                         ->leftJoin('a.paciente''p')
  153.                         ->leftJoin('a.doctor''d')
  154.                         ->andWhere(
  155.                             'p.apellido1 LIKE :q 
  156.                             OR p.nombre1 LIKE :q
  157.                             OR p.cedula LIKE :q'
  158.                         )
  159.                         ->setParameter('q''%'.$q.'%');
  160.                 }  
  161.                 
  162.                 $agendas $qb->getQuery()->getResult();//dd($agendas);
  163.                 $reuniones $this->AgendaRepository
  164.                     ->createQueryBuilder('a')
  165.                     ->innerJoin('a.sala''s')
  166.                     ->where('a.inicio BETWEEN :start and :end OR a.fin BETWEEN :start and :end')
  167.                     ->setParameter('start'$start->format('Y-m-d'))
  168.                     ->setParameter('end'$end->format('Y-m-d'))
  169.                     ->andWhere('s.unidad = :val')
  170.                     ->setParameter('val'$id_unidad)
  171.                     ->andWhere('a.estado = :val1')
  172.                     ->setParameter('val1'2)
  173.                     ->getQuery()
  174.                     ->getResult()
  175.                 ;
  176.             }    
  177.         
  178.             
  179.         }    
  180.         
  181.         foreach ($agendas as $agenda) {
  182.             $apxs $agenda->getAgendaProcedimientos();
  183.             $txt_ap '';
  184.             foreach($apxs as $ap){
  185.                 $txt_ap .= ' ->'.$ap->getProcedimiento()->getNombre();
  186.             }
  187.             // this create the events with your data (here booking data) to fill calendar
  188.             $txtestado '';
  189.             if($agenda->getEstadoAgenda()=='0'){
  190.                 $txtestado 'LLAMAR A CONFIRMAR';   
  191.             }elseif ($agenda->getEstadoAgenda()=='1') {
  192.                 $txtestado 'CONFIRMADO';        
  193.             }elseif ($agenda->getEstadoAgenda()=='2') {
  194.                 $txtestado 'ADMISIONADO';        
  195.             }
  196.             if($id_unidad == 0){
  197.                 $agendaEvent = new Event(
  198.                     $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,
  199.                     $agenda->getInicio(),
  200.                     $agenda->getFin() // If the end date is null or not defined, a all day event is created.
  201.                 );
  202.                 
  203.                 
  204.                 $htmlDescription '';
  205.                 if($agenda->getConfirma() == '1'){
  206.                     $htmlDescription 'CONFIRMADO';    
  207.                 }
  208.                 
  209.                 
  210.                 $agendaEvent->setOptions([
  211.                     'backgroundColor' => '#581845',
  212.                     'borderColor' => 'white',
  213.                     'description' => $htmlDescription,
  214.                     'confirma' => $agenda->getConfirma(),
  215.                     'id' => $agenda->getId(),
  216.                     'procedimientos' => $agenda->getProcedimiento()->getNombre().$txt_ap
  217.                 ]);
  218.             }else{
  219.                 /*$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;
  220.                 if($agenda->getConfirma()== '1'){
  221.                     $msn = $msn.' -CONFIRMADO';
  222.                 }
  223.                 if($agenda->getDocumentos()){
  224.                     $msn = $msn.' -DOCUMENTOS LISTOS';
  225.                 }*/
  226.                 $msn $agenda->getPaciente()->getApellido1().' '.$agenda->getPaciente()->getApellido2().' '.$agenda->getPaciente()->getNombre1().' '.$agenda->getPaciente()->getNombre2();
  227.                 $agendaEvent = new Event(
  228.                     $msn,
  229.                     $agenda->getInicio(),
  230.                     $agenda->getFin() // If the end date is null or not defined, a all day event is created.
  231.                 );
  232.                 
  233.                 $htmlDescription '';
  234.                 if($agenda->getConfirma() == '1'){
  235.                     $htmlDescription 'CONFIRMADO';    
  236.                 }
  237.                 
  238.                 $confirma $agenda->getConfirma();
  239.                 if($tipo == 'privado' and $agenda->getDocumentos()){
  240.                    
  241.                     $htmlDescription 'DOCUMENTOS OK';
  242.                     $confirma 1;
  243.                 }    
  244.                 
  245.                 $color "#A9A9A9";
  246.                 if($agenda->getDoctor() != null){
  247.                     if($agenda->getDoctor()->getColor() != null){
  248.                         $color $agenda->getDoctor()->getColor();
  249.                     }
  250.                 }
  251.                 $confirmado '';
  252.                 if($agenda->getConfirma()== '1'){
  253.                     $confirmado 'CONFIRMADO';
  254.                 }
  255.                 $documentos '';
  256.                 if($agenda->getDocumentos()){
  257.                     $documentos 'DOCUMENTOS LISTOS';
  258.                 }
  259.                 
  260.                 $agendaEvent->setOptions([
  261.                     'backgroundColor' => $color,
  262.                     'borderColor' => 'black',
  263.                     'description' => $htmlDescription,
  264.                     'confirma' => $confirma,
  265.                     'id' => $agenda->getId(),
  266.                     'procedimientos' => $agenda->getProcedimiento()->getNombre().$txt_ap,
  267.                     'estado' => $txtestado,
  268.                     'confirmado' => $confirmado,
  269.                     'documentos' => $documentos
  270.                     
  271.                 ]);
  272.             }    
  273.             /*
  274.              * Add custom options to events
  275.              *
  276.              * For more information see: https://fullcalendar.io/docs/event-object
  277.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  278.              */
  279.             /*if($agenda->getEstadoAgenda()=='0' and $id_unidad == 0){
  280.                 
  281.                 $agendaEvent->addOption('backgroundColor', 'black');
  282.                 $agendaEvent->addOption('borderColor', 'white');
  283.                    
  284.             }elseif ($agenda->getEstadoAgenda()=='1' and $id_unidad == 0) {
  285.                 
  286.                 $agendaEvent->addOption('backgroundColor', 'blue');
  287.                 $agendaEvent->addOption('borderColor', 'white');
  288.             }elseif ($agenda->getEstadoAgenda()=='2' and $id_unidad == 0) {
  289.                    
  290.                 $agendaEvent->addOption('backgroundColor', 'green');
  291.                 $agendaEvent->addOption('borderColor', 'white');
  292.             }   */
  293.            
  294.             $hoy = new \DateTime('today'); // hoy 00:00:00
  295.             $inicioAgenda $agenda->getInicio(); // DateTime de la agenda  
  296.             //dd($hoy, $inicioAgenda);  
  297.             
  298.             if ($inicioAgenda $hoy) {
  299.                 // ðŸ”´ INACTIVO (antes de hoy)
  300.                 $agendaEvent->addOption('backgroundColor'$agenda->getProcedimiento()->getColorInactivo()); // gris azulado
  301.                 $agendaEvent->addOption('borderColor''#9CA1BB');
  302.             } else {
  303.                 // ðŸŸ¢ ACTIVO (hoy en adelante)
  304.                 $agendaEvent->addOption('backgroundColor'$agenda->getProcedimiento()->getColorActivo()); // dorado
  305.                 $agendaEvent->addOption('borderColor''#C3A73C');
  306.             }
  307.             
  308.             $agendaEvent->addOption(
  309.                 'url',
  310.                 $this->router->generate('agenda_edit', [
  311.                     'id' => $agenda->getId(),
  312.                 ])
  313.             );
  314.             if($agenda->getId()== '99118'){
  315.                         //dd($agendaEvent);
  316.                     }
  317.             // finally, add the event to the CalendarEvent to fill the calendar
  318.             $calendar->addEvent($agendaEvent);
  319.         }
  320.         
  321.         foreach ($reuniones as $agenda) {
  322.             // this create the events with your data (here booking data) to fill calendar
  323.             $txtestado 'REUNION';
  324.             if($id_unidad == 0){
  325.                 $agendaEvent = new Event(  
  326.                     $txtestado.' '.$agenda->getObservacion(),
  327.                     $agenda->getInicio(),
  328.                     $agenda->getFin() // If the end date is null or not defined, a all day event is created.
  329.                 );
  330.                 
  331.                 $htmlDescription '';
  332.                 if($agenda->getConfirma() == '1'){
  333.                     $htmlDescription 'CONFIRMADO';    
  334.                 }
  335.                 
  336.                 $agendaEvent->setOptions([
  337.                     'backgroundColor' => '#581845',
  338.                     'borderColor' => 'white',
  339.                     'description' => $htmlDescription,
  340.                     'confirma' => $agenda->getConfirma(),
  341.                     'id' => $agenda->getId()
  342.                 ]);
  343.             }else{
  344.                 $agendaEvent = new Event(  
  345.                     'DR. '.$agenda->getDoctor()->getApellido1().' '.$agenda->getDoctor()->getNombre1().' - '.$txtestado.' '.$agenda->getObservacion(),
  346.                     $agenda->getInicio(),
  347.                     $agenda->getFin() // If the end date is null or not defined, a all day event is created.
  348.                 );
  349.                 
  350.                 
  351.                 $htmlDescription '';
  352.                 if($agenda->getConfirma() == '1'){
  353.                     $htmlDescription 'CONFIRMADO';    
  354.                 }
  355.                 
  356.                 $agendaEvent->setOptions([
  357.                     'backgroundColor' => $agenda->getDoctor()->getColor(),
  358.                     'borderColor' => 'black',
  359.                     'description' => $htmlDescription,
  360.                     'confirma' => $agenda->getConfirma(),
  361.                     'id' => $agenda->getId()
  362.                 ]);
  363.             }    
  364.             /*
  365.              * Add custom options to events
  366.              *
  367.              * For more information see: https://fullcalendar.io/docs/event-object
  368.              * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
  369.              */
  370.             
  371.             
  372.                
  373.             $agendaEvent->addOption(
  374.                 'url',
  375.                 $this->router->generate('agenda_reunion_edit', [
  376.                     'id' => $agenda->getId(),
  377.                 ])
  378.             );
  379.             // finally, add the event to the CalendarEvent to fill the calendar
  380.             $calendar->addEvent($agendaEvent);
  381.         }
  382.     }
  383.     // ...
  384. }