src/Service/AlertService.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\GpReminder;
  4. use App\MDS\GreenPatioBundle\Entity\Reservation;
  5. use App\MDS\GreenPatioBundle\Entity\ReservationDeposit;
  6. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeSimple;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\Routing\RouterInterface;
  10. final class AlertService
  11. {
  12.     private $em;
  13.     private $session;
  14.     private $router;
  15.     public function __construct(EntityManagerInterface $emSessionInterface $sessionRouterInterface $router)
  16.     {
  17.         $this->em $em;
  18.         $this->session $session;
  19.         $this->router $router;
  20.     }
  21.     /**
  22.      * Para cargar las alertas de los usuarios de GP
  23.      * @param $user User
  24.      */
  25.     public function loadGpAlerts($user)
  26.     {
  27.         $gpReminders $this->em->getRepository(GpReminder::class)->findBy(['user' => $user]);
  28.         
  29.         $data = [];
  30.         foreach ($gpReminders as $gpReminder) {
  31.             // Solo se muestran las alertas a las que les quedan dos días o menos y no mostrar las que ya pasaron
  32.             if ($gpReminder->getReminderAt() < new \DateTime('now') || $gpReminder->getReminderAt()->diff(new \DateTime())->days 2) {
  33.                 continue;
  34.             }
  35.             $data[] = [
  36.                 'type' => 'reminder',
  37.                 'title' => $gpReminder->getReminder(),
  38.             ];
  39.         }
  40.         // Comprobar si los expedientes del usuario tienen depositos, si no tienen se le avisa
  41.         $reservation $this->em->getRepository(Reservation::class)->findBy(['createdBy' => $user->getId(), 'status' => ['Bloqueo''Confirmed''Cotizado']]);
  42.         foreach ($reservation as $res) {
  43.             $lounge $this->em->getRepository(ReservationLoungeSimple::class)->findOneBy(['idReservation' => $res->getId()]);
  44.             if (!$lounge || $lounge->getDateEnd() < new \DateTime('now')) {
  45.                 continue;
  46.             }
  47.             $deposit $this->em->getRepository(ReservationDeposit::class)->findOneBy(['reservationId' => $res->getId()]);
  48.             if (!$deposit) {
  49.                 $data[] = [
  50.                     'type' => 'link',
  51.                     'title' => 'No se ha realizado el depósito de la reserva: ' $res->getId() . ' - ' $res->getTitle(),
  52.                     'url' => $this->router->generate('reservations_greenpatio_edit_simple', ['id' => $res->getId()]),
  53.                 ];
  54.             }
  55.         }
  56.         // Cogemos los reservations que estan en estado Bloqueo y la fecha del campo days le quedan 3 días o menos
  57.         $reservations $this->em->getRepository(Reservation::class)->createQueryBuilder('r')
  58.             ->where('r.status = :status')
  59.             ->andWhere('r.days > :hoy')
  60.             ->setParameter('status''Bloqueo')
  61.             ->setParameter('hoy', new \DateTime('now'))
  62.             ->getQuery()
  63.             ->getResult();
  64.         foreach ($reservations as $reserva) {
  65.             if ($reserva->getDays()->diff(new \DateTime())->days 3) {
  66.                 continue;
  67.             }
  68.             $data[] = [
  69.                 'type' => 'link',
  70.                 'title' => 'La reserva ' $reserva->getId() . ' - ' $reserva->getTitle() . ' está a punto de desbloquearse',
  71.                 'url' => $this->router->generate('reservations_greenpatio_edit_simple', ['id' => $reserva->getId()]),
  72.             ];
  73.         }
  74.         $this->loadAlerts($data);
  75.     }
  76.     /**
  77.      * Carga las alertas en la vista de los calendarios de lo que tiene que hacer el usuario
  78.      * @param $data array
  79.      */
  80.     private function loadAlerts($data)
  81.     {
  82.         // Carga las alertas        
  83.         foreach ($data as $link) {
  84.             $message '';
  85.             if ($link['type'] == 'link') {
  86.                 $message '<a href="' $link['url'] . '" target="_blank">' $link['title'] . '</a>';
  87.             } else if ($link['type'] == 'reminder') {
  88.                 $message $link['title'];
  89.             }
  90.             $this->session->getFlashBag()->add('notification'$message);
  91.         }
  92.     }
  93. }