src/Controller/Notificacion/NotificacionController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Notificacion;
  3. use App\Entity\Notificacion\Notificacion;
  4. use App\Form\Notificacion\NotificacionType;
  5. use App\Form\Notificacion\NotificacionEditType;
  6. use App\Repository\Notificacion\NotificacionRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use App\Form\Notificacion\NotificacionFiltroType;
  13. use App\Form\Notificacion\NotificacionReceptoresType;
  14. use App\Repository\SsoPersonaRepository;
  15. use App\Entity\Notificacion\NotificacionUsuario;
  16. use App\Repository\Notificacion\NotificacionAreasRepository;
  17. use App\Repository\Notificacion\NotificacionUsuarioRepository;
  18. use App\Repository\SsoUserRepository;
  19. use App\Security\Voter\NotificacionVoter;
  20. use Symfony\Component\Workflow\Registry;
  21. #[Route('/notificacion/notificacion')]
  22. class NotificacionController extends AbstractController
  23. {
  24.     public const FORM_CONSULTA_NOTIFICACION 'consulta_notificacion_form';
  25.         #[Route('/listado/{reset}'name'notificaciones_listado'methods: ['GET''POST'])]
  26.     public function notificacionesListado(
  27.         Request $request
  28.         NotificacionAreasRepository $notificacionAreasRepository,
  29.         int $reset null,
  30.         ): Response
  31.     {
  32.         // Reinicio el formulario si reset = 1
  33.         if ($reset == 1)
  34.             $request->getSession()->remove(self::FORM_CONSULTA_NOTIFICACION);
  35.         // Inicializo el formulario con lo seteado previamente en la sesión
  36.         $data = ($request->getSession()->has(self::FORM_CONSULTA_NOTIFICACION)) 
  37.             ? $request->getSession()->get(self::FORM_CONSULTA_NOTIFICACION
  38.             : null;
  39.         $data['usuario_admin'] = $notificacionAreasRepository->esUsuarioAdmin($this->getUser()->getUserIdentifier());
  40.         $form $this->createForm(NotificacionFiltroType::class, $data);
  41.         $form->handleRequest($request);
  42.         if ($form->isSubmitted() && $form->isValid()) {
  43.             // Guardo configuración del formulario en la sesión
  44.             $request->getSession()->set(self::FORM_CONSULTA_NOTIFICACION$form->getData());
  45.         }
  46.         return $this->renderForm('notificacion/notificacion/index.html.twig', [
  47.             'form' => $form,
  48.         ]);
  49.     }
  50.     #[Route('/notificacion_datos'name'notificaciones_datos'methods: ['POST'])]
  51.     public function notificacionesDatos(
  52.         Request $request
  53.         NotificacionRepository $notificacionRepository,
  54.         NotificacionAreasRepository $notificacionAreasRepository,
  55.         Registry $workflows
  56.     ): Response
  57.     {
  58.         // Proceso si hay filtro de datos
  59.         if ($request->getSession()->has(self::FORM_CONSULTA_NOTIFICACION)) {
  60.             $params['formulario'] = array_filter($request->getSession()->get(self::FORM_CONSULTA_NOTIFICACION));
  61.         }
  62.         $params['user'] = $this->getUser()->getUserIdentifier();
  63.         $params['draw'] = intval($request->request->get('draw'));
  64.         $params['start'] = $request->request->get('start');
  65.         $params['length'] = $request->request->get('length');
  66.         $params['search'] = $request->request->get('search');
  67.         $params['orders'] = $request->request->get('order');
  68.         $params['columns'] = $request->request->get('columns');
  69.         $params['usuarioAdmin'] = $notificacionAreasRepository->esUsuarioAdmin($params['user']);
  70.         
  71.         $notificaciones $notificacionRepository->loadNotificacionesData($params);
  72.         $params['start'] = 0;
  73.         $params['length'] = $notificacionRepository->count([]);
  74.         $notificacionesSinPaginar $notificacionRepository->loadNotificacionesData($params);
  75.         // Agrego campos de acciones y url de la imagen
  76.         $notificaciones array_reduce($notificaciones, function ($carry$item) use($workflows) {
  77.             $urlSeleccion $this->generateUrl('notificacion_validacion_edit', [
  78.                 'idView' => $item['idView']
  79.             ]);
  80.             $urlShowModal $this->generateUrl('notificacion_show_modal', [
  81.                 'idView' => $item['idView']
  82.             ]);
  83.             $urlDetalle $this->generateUrl('notificaciones_usuario_listado', [
  84.                 'idView' => $item['idView']
  85.             ]);
  86.             $notificacion = new Notificacion();
  87.             $notificacion->setSsoUsuario($item['sso_usuario']);
  88.             $notificacion->setEstado($item['estado']);
  89.             $workflow $workflows->get($notificacion'notificacion');
  90.             if($workflow->can($notificacion'eliminar') && $this->isGranted(NotificacionVoter::ACCESS$notificacion))
  91.             {
  92.                 $urlEliminar $this->generateUrl('notificacion_eliminar', [
  93.                     'idView' => $item['idView']
  94.                 ]);
  95.             }
  96.             // Piso el estado con el span
  97.             $estado_class Notificacion::VIEW_ESTADO[$item['estado']]['show_class'];
  98.             $estado_name Notificacion::VIEW_ESTADO[$item['estado']]['show_name'];
  99.             $item['estado'] = "<span class='badge {$estado_class}'>{$estado_name}</span>";
  100.             $carry[] = $item + [
  101.                 "acciones" => [
  102.                     "seleccionar" => $urlSeleccion,
  103.                     "showModal" => $urlShowModal,
  104.                     "detalle" => $urlDetalle,
  105.                     "eliminar" => isset($urlEliminar) ? $urlEliminar null,
  106.                 ],
  107.                 'estado' => $item['estado'],
  108.             ];
  109.             return $carry;
  110.         }, []);
  111.         $respuesta['draw'] = $params['draw'];
  112.         $respuesta['recordsTotal'] = $params['length'];
  113.         $respuesta['recordsFiltered'] = count($notificacionesSinPaginar);
  114.         $respuesta['data'] = $notificaciones;
  115.         return $this->json($respuesta);
  116.     }
  117.     #[Route('/new'name'notificacion_notificacion_new'methods: ['GET''POST'])]
  118.     public function new(
  119.         Request $request
  120.         EntityManagerInterface $entityManager,
  121.         Registry $workflows
  122.     ): Response
  123.     {
  124.         $notificacion = new Notificacion();
  125.         $form $this->createForm(NotificacionType::class, $notificacion);
  126.         $form->handleRequest($request);
  127.         if ($form->isSubmitted() && $form->isValid()) {
  128.             $workflows->get($notificacion'notificacion')->getMarking($notificacion);
  129.             $entityManager->persist($notificacion);
  130.             $entityManager->flush();
  131.             if($form->getClickedButton() ? $form->getClickedButton()->getName() == "adjuntos" false)
  132.             {
  133.                 return $this->redirectToRoute('notificacion_notificacion_edit', [
  134.                     'idView' => $notificacion->getIdView(),
  135.                     'modalAdjuntoOpen' => true
  136.                 ], Response::HTTP_SEE_OTHER);    
  137.             }
  138.             return $this->redirectToRoute('notificacion_cargar_receptores', [
  139.                 'idView' => $notificacion->getIdView()
  140.             ], Response::HTTP_SEE_OTHER);
  141.         }
  142.         return $this->renderForm('notificacion/notificacion/new.html.twig', [
  143.             'notificacion' => $notificacion,
  144.             'form' => $form,
  145.         ]);
  146.     }
  147.     #[Route('/{idView}/modal'name'notificacion_show_modal'methods: ['GET'])]
  148.     public function showModal(
  149.         Notificacion $notificacion
  150.         NotificacionUsuarioRepository $notificacionUsuarioRepository
  151.     ): Response
  152.     {
  153.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  154.         $leidos $notificacionUsuarioRepository->findLeidosByNotificacion($notificacion);
  155.         $noLeidos $notificacionUsuarioRepository->findNoLeidosByNotificacion($notificacion);
  156.         $mensaje $notificacion->getMensaje();
  157.         //Si contiene el boton (de momento las notificaciones de confirmacion de asistencia)
  158.         if(strpos($mensaje'"btn-'))
  159.         {
  160.             //Se reemplaza el href, por vacio, para que no realice confirmacion
  161.             $mensajeFinal preg_replace('/href="([^\s]+)"/'''$mensaje);
  162.             $notificacion->setMensaje($mensajeFinal);
  163.         }
  164.         return $this->render('notificacion/notificacion/showModal.html.twig', [
  165.             'notificacion' => $notificacion,
  166.             'leidos' => $leidos,
  167.             'noLeidos' => $noLeidos,
  168.         ]);
  169.     }
  170.     #[Route('/{idView}/cargarReceptores'name'notificacion_cargar_receptores'methods: ['GET''POST'])]
  171.     public function cargarReceptores(
  172.         Request $request
  173.         Notificacion $notificacion
  174.         SsoPersonaRepository $ssoPersonaRepository
  175.     ): Response
  176.     {
  177.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  178.         $form $this->createForm(NotificacionReceptoresType::class);
  179.         $form->handleRequest($request);
  180.         $incorrectos = array();
  181.         $correctos = array();
  182.         if ($form->isSubmitted() && $form->isValid()) {
  183.             if($form->get('guardar')->isClicked())
  184.             {
  185.                 $file $form->get('receptores')->getData();
  186.                 $handle fopen($file"r");
  187.                 if ($handle) {
  188.                     while (($line fgets($handle)) !== false) {
  189.                         $dni preg_replace('~[\r\n]+~'''$line);
  190.                         $dniExiste $ssoPersonaRepository->findByDni($dni);
  191.                         if ($dniExiste) {
  192.                         array_push($correctos$dniExiste);
  193.                         }
  194.                         else{
  195.                         array_push($incorrectos$dni);
  196.                         }
  197.                     }
  198.                     fclose($handle);
  199.                 }
  200.             }
  201.             else
  202.             {
  203.                 $dni $form->get('receptor')->getData();
  204.                 $dniExiste $ssoPersonaRepository->findByDni($dni);
  205.                 if ($dniExiste) {
  206.                 array_push($correctos$dniExiste);
  207.                 }
  208.                 else{
  209.                 array_push($incorrectos$dni);
  210.                 }
  211.             }
  212.             $request->getSession()->set('correctos'$correctos);
  213.             $request->getSession()->set('incorrectos'$incorrectos);
  214.             return $this->redirectToRoute('ver_receptores', [
  215.                 'idView' => $notificacion->getIdView()
  216.             ], Response::HTTP_SEE_OTHER);
  217.         }
  218.         return $this->renderForm('notificacion/notificacion/cargarReceptores.html.twig', [
  219.             'notificacion' => $notificacion,
  220.             'form' => $form,
  221.         ]);
  222.     }
  223.     #[Route('/{idView}/verReceptores'name'ver_receptores'methods: ['GET''POST'])]
  224.     public function verReceptores(
  225.         Notificacion $notificacion
  226.         Request $request
  227.     ): Response
  228.     {
  229.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  230.         $correctos $request->getSession()->get('correctos');
  231.         $incorrectos $request->getSession()->get('incorrectos');
  232.         return $this->render('notificacion/notificacion/verReceptores.html.twig', [
  233.             'notificacion' => $notificacion,
  234.             'correctos' => $correctos,
  235.             'incorrectos' => $incorrectos
  236.         ]);
  237.     }
  238.     #[Route('/{idView}/crearNotificacionesUsuario'name'crear_notificaciones_usuario'methods: ['GET''POST'])]
  239.     public function crearNotificacionesUsuario(
  240.         Notificacion $notificacion
  241.         Request $request
  242.         EntityManagerInterface $entityManager
  243.         SsoUserRepository $ssoUserRepository,
  244.         Registry $workflows
  245.     ): Response
  246.     {
  247.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  248.         $correctos $request->getSession()->get('correctos');
  249.         $workflow $workflows->get($notificacion'notificacion');
  250.         foreach ($correctos as $dniCorrecto) {
  251.             $usuario $ssoUserRepository->findUsuarioPorDni($dniCorrecto[0]->getDni());
  252.             $notificacionUsuario = new NotificacionUsuario();
  253.             $notificacionUsuario->setUsuario($usuario);
  254.             
  255.             $workflowUsuario $workflows->get($notificacionUsuario'notificacion_usuario');
  256.             $workflowUsuario->getMarking($notificacionUsuario);
  257.             
  258.             $notificacion->addNotificacionesUsuario($notificacionUsuario);
  259.         }
  260.         $entityManager $this->getDoctrine()->getManager();
  261.         $workflow->apply($notificacion'publicar');
  262.         
  263.         $entityManager->flush();
  264.         $request->getSession()->getFlashBag()->add(
  265.             'notice'// notice / warning / error
  266.             "Estimado/a la notificación, fue publicada correctamente."
  267.         );
  268.         return $this->redirectToRoute('notificaciones_listado', [
  269.             'reset' => 1
  270.         ], Response::HTTP_SEE_OTHER);
  271.     }
  272.     #[Route('/{idView}/edit/{modalAdjuntoOpen}'name'notificacion_notificacion_edit'methods: ['GET''POST'])]
  273.     public function edit(
  274.         Request $request
  275.         Notificacion $notificacion
  276.         EntityManagerInterface $entityManager,
  277.         bool $modalAdjuntoOpen false ,
  278.     ): Response
  279.     {
  280.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  281.         $tieneReceptores count($notificacion->getNotificacionesUsuario()) > 0;
  282.         $form $this->createForm(NotificacionEditType::class, $notificacion);
  283.         $form->handleRequest($request);
  284.         if ($form->isSubmitted() && $form->isValid()) {
  285.             $entityManager->flush();
  286.             if ($form->get('guardar')->isClicked()) {
  287.                 if ($tieneReceptores) {
  288.                     $request->getSession()->getFlashBag()->add(
  289.                         'warning'// notice / warning / error
  290.                         "Estimado/a la notificación fue persistida como BORRADOR y no fue publicada."
  291.                     );
  292.                     return $this->redirectToRoute('notificaciones_listado', [], Response::HTTP_SEE_OTHER);
  293.                 } else {
  294.                     return $this->redirectToRoute('notificacion_cargar_receptores', [
  295.                         'idView' => $notificacion->getIdView()
  296.                     ], Response::HTTP_SEE_OTHER);
  297.                 }
  298.             } elseif(($form->get('guardaryPublicar')->isClicked()) ) {
  299.                 return $this->redirectToRoute('notificacion_editar_publicar', [
  300.                     'idView' => $notificacion->getIdView()
  301.                 ], Response::HTTP_SEE_OTHER);
  302.             }
  303.             else
  304.             {
  305.                 return $this->redirectToRoute('notificacion_cargar_receptores', [
  306.                     'idView' => $notificacion->getIdView()
  307.                 ], Response::HTTP_SEE_OTHER);
  308.             }
  309.         }
  310.         return $this->renderForm('notificacion/notificacion/edit.html.twig', [
  311.             'notificacion' => $notificacion,
  312.             'form' => $form,
  313.             'tieneReceptores' => $tieneReceptores,
  314.             'modalAdjuntoOpen' => $modalAdjuntoOpen,
  315.         ]);
  316.     }
  317.     #[Route('/{idView}/editarypublicar'name'notificacion_editar_publicar'methods: ['GET''POST'])]
  318.     public function editarPublicar(
  319.         Request $request,
  320.         Notificacion $notificacion
  321.         EntityManagerInterface $entityManager,
  322.         Registry $workflows
  323.     ): Response
  324.     {
  325.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  326.         $workflow $workflows->get($notificacion'notificacion');
  327.         $workflow->apply($notificacion'publicar');
  328.         $entityManager->flush();
  329.         $request->getSession()->getFlashBag()->add(
  330.             'notice'// notice / warning / error
  331.             "Estimado/a la notificación, fue publicada correctamente."
  332.         );
  333.         return $this->redirectToRoute('notificaciones_listado'
  334.             [], Response::HTTP_SEE_OTHER);
  335.     }
  336.     #[Route('/{idView}/validacionEdit'name'notificacion_validacion_edit'methods: ['GET''POST'])]
  337.     public function validacionEdit(
  338.         Request $request,
  339.         Notificacion $notificacion
  340.         EntityManagerInterface $entityManager
  341.         NotificacionUsuarioRepository $notificacionUsuarioRepository,
  342.         Registry $workflows): Response
  343.     {
  344.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  345.         $leidos $notificacionUsuarioRepository->findLeidosByNotificacion($notificacion);
  346.         $workflow $workflows->get($notificacion'notificacion');
  347.         if (count($leidos) > 0) {
  348.             //si no cumple condiciones para editar lo dejo en el listado con mensaje flash de error.
  349.             $request->getSession()->getFlashBag()->add(
  350.                 'error'// notice / warning / error
  351.                 "Estimado/a no puede editar esta notificación porque ya fue leída al menos una vez. Ud. podría crear una nueva rectificando ésta."
  352.             );
  353.             return $this->redirectToRoute('notificaciones_listado', [], Response::HTTP_SEE_OTHER);
  354.         } 
  355.         if ($notificacion->getEstado() == Notificacion::ESTADO_PUBLICADO) {
  356.             $workflow->apply($notificacion'despublicar');
  357.             $request->getSession()->getFlashBag()->add(
  358.                 'warning'// notice / warning / error
  359.                 "Estimado/a la notificación fue despublicada y marcada como BORRADOR. Vuelva a publicarla con el botón Guardar y Publicar."
  360.             );
  361.             $entityManager->flush();
  362.         }
  363.         return $this->redirectToRoute('notificacion_notificacion_edit', [
  364.             'idView' => $notificacion->getIdView()
  365.         ], Response::HTTP_SEE_OTHER);
  366.     }
  367.     #[Route('/{idView}/eliminar'name'notificacion_eliminar'methods: ['GET''POST'])]
  368.     public function eliminar(
  369.         Request $request,
  370.         Notificacion $notificacion
  371.         EntityManagerInterface $entityManager,
  372.         Registry $workflows
  373.     ): Response
  374.     {
  375.         $this->denyAccessUnlessGranted(NotificacionVoter::ACCESS$notificacion);
  376.         $workflow $workflows->get($notificacion'notificacion');
  377.         if($workflow->can($notificacion'eliminar'))
  378.         {
  379.             $workflow->apply($notificacion'eliminar');
  380.             $entityManager->flush();
  381.             $request->getSession()->getFlashBag()->add(
  382.                 'notice'// notice / warning / error
  383.                 "Estimado/a la notificación, fue eliminada correctamente."
  384.             );
  385.         }
  386.         else
  387.         {
  388.             $request->getSession()->getFlashBag()->add(
  389.                 'error'// notice / warning / error
  390.                 "Estimado/a la notificación no puede ser eliminada, ya esta publicada."
  391.             );
  392.         }
  393.         return $this->redirectToRoute('notificaciones_listado'
  394.             [], Response::HTTP_SEE_OTHER);
  395.     }
  396. }