src/Controller/UbicacionController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Periodo;
  4. use App\Entity\Ubicacion;
  5. use App\Entity\Usuario;
  6. use App\Form\UbicacionType;
  7. use App\Form\UnidadFiltroType;
  8. use http\Client\Curl\User;
  9. use App\Repository\UbicacionRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. #[Route('/ubicacion')]
  18. class UbicacionController extends AbstractController
  19. {
  20.     public EntityManagerInterface $emi;
  21.     public function __construct(EntityManagerInterface $entityManager)
  22.     {
  23.         $this->emi $entityManager;
  24.     }
  25.     #[Route('/'name'app_ubicacion_index'methods: ['GET''POST'])]
  26.     public function index(Request $requestUbicacionRepository $ubicacionRepository): Response #EntityManagerInterface $entityManager
  27.     {
  28.         # Se obtiene el nivel de acceso
  29.         $perfil $request->getSession()->get('perfil');
  30.         $nivel $perfil[3]["nivel"];
  31.         //$ubicacions = [];
  32.         # Si el nivel de acceso es 0 = "Sin acceso" entonces se retorna a página de inicio
  33.         if ($nivel == 0) {
  34.             $this->addFlash('danger''El acceso a agregar "Otros equipos" está restringido.');
  35.             return $this->redirectToRoute('homepage');
  36.         }
  37.         # Usuario que está autenticado
  38.         /** @var Usuario $user */
  39.         $user $this->getUser();
  40.         # Formulario de filtro por unidad
  41.         (in_array($nivel, [23])) ? $uni true $uni false;
  42.         $form $this->createForm(UnidadFiltroType::class, null, ['unidad' => $uni]);
  43.         $form->handleRequest($request);
  44.         if ($request->isMethod('POST')) {
  45.             if (empty($form['unidad']->getData())) {
  46.                 $ubicacions $ubicacionRepository->findAll();
  47.             } else {
  48.                 $ubicacions $ubicacionRepository->findBy(['unidad' => $form['unidad']->getData()]);
  49.             }
  50.             $data = [];
  51.             foreach ($ubicacions as $ubicacion) {
  52.                 /*$activo = match ($ubicacion->getActivo()) {
  53.                     "1" => '<span class="badge bg-gradient-yellow d-block">Activo</span>',
  54.                     "0" => '<span class="badge bg-gradient-primary d-block">Inactivo</span>',
  55.                     default => ""
  56.                 };*/
  57.                 if ($ubicacion->getActivo() == 1) {
  58.                     $activo "Si";
  59.                 } else {
  60.                     $activo "No";
  61.                 }
  62.                 $ver $this->generateUrl('app_ubicacion_show', ['idUbi' => $ubicacion->getIdUbi()]);
  63.                 $editar $this->generateUrl('app_ubicacion_edit', ['idUbi' => $ubicacion->getIdUbi()]);
  64.                 $data[] = [
  65.                     'idUbi' => $ubicacion->getIdUbi(),
  66.                     'area' => $ubicacion->getArea(),
  67.                     'unidad' => $ubicacion->getUnidad(),
  68.                     'activo' => $activo,
  69.                     /*'estatus' => $estatus,'usuario' => $ubicacion->getUsuario()->getNombre() . ' ' . $ubicacion->getUsuario()->getPapellido(),
  70.                     'ubicacion' => substr($ubicacion->getUnidad()->getIdUni(), 3, 3) . ' - ' . $ubicacion->getUbicacion()->getArea(),*/
  71.                     'ver' => $ver,
  72.                     'editar' => $editar
  73.                 ];
  74.             }
  75.             return new JsonResponse($dataResponse::HTTP_OK);
  76.         }
  77.         if ($nivel == 1) {
  78.             $ubicacions $ubicacionRepository->findBy(['unidad' => $user->getUnidad()]);
  79.         }
  80.         if (in_array($nivel, [23])) {
  81.             $ubicacions $ubicacionRepository->findAll();
  82.         }
  83.         return $this->render('ubicacion/index.html.twig', [
  84.             //'url' => $this->generateUrl('app_ubicacion_index'),
  85.             'ubicacions' => $ubicacions,
  86.             'form' => $form->createView(),
  87.             'nivel' => $nivel,
  88.         ]);
  89.     }
  90.     #[Route('/new'name'app_ubicacion_new'methods: ['GET''POST'])]
  91.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  92.     {
  93.         $perfil $request->getSession()->get('perfil');
  94.         $nivel $perfil[3]["nivel"];
  95.         if ($nivel == 3) { //regresar a 3
  96.             $this->addFlash('danger''El acceso a agregar "Ubicaciones" está restringido.');
  97.             return $this->redirectToRoute('homepage');
  98.         }
  99.         if ($nivel ==4) { //regresar a 3
  100.             $this->addFlash('danger''El acceso a agregar "Ubicaciones" está restringido.');
  101.             return $this->redirectToRoute('homepage');
  102.         }
  103.         /** @var Usuario $user */
  104.         $user $this->getUser();
  105.         $ubicacion = new Ubicacion();
  106.         $form $this->createForm(UbicacionType::class, $ubicacion);
  107.         $form->handleRequest($request);
  108.         if ($form->isSubmitted() && $form->isValid()) {
  109.             if ($nivel==2){
  110.                 $ubicacion->setUnidad($ubicacion->getUnidad());
  111.             }else{
  112.                 $ubicacion->setUnidad($user->getUnidad());
  113.             }
  114.             $entityManager->persist($ubicacion);
  115.             $entityManager->flush();
  116.             return $this->redirectToRoute('app_ubicacion_index', [], Response::HTTP_SEE_OTHER);
  117.         }
  118.         return $this->renderForm('ubicacion/new.html.twig', [
  119.             'ubicacion' => $ubicacion,
  120.             'form' => $form,
  121.             'rol' => $user->getRol()->getIdRol()
  122.         ]);
  123.     }
  124.     #[Route('/{idUbi}'name'app_ubicacion_show'methods: ['GET'])]
  125.     public function show(Ubicacion $ubicacion): Response
  126.     {
  127.         return $this->render('ubicacion/modalV.html.twig', [
  128.             'ubicacion' => $ubicacion,
  129.         ]);
  130.     }
  131.     #[Route('/{idUbi}/edit'name'app_ubicacion_edit'methods: ['GET''POST'])]
  132.     public function edit(Request $requestUbicacion $ubicacionEntityManagerInterface $entityManager): Response
  133.     {
  134.         $form $this->createForm(UbicacionType::class, $ubicacion);
  135.         //if ($periodo ->getActual() == 1){
  136.         $form->remove('unidad');
  137.         //}
  138.         $form->handleRequest($request);
  139.         /** @var Usuario $user */
  140.         $user $this->getUser();
  141.         if ($form->isSubmitted() && $form->isValid()) {
  142.             $entityManager->flush();
  143.             if ($ubicacion->getActivo() == 1) {
  144.                 $activo "Activo";
  145.             } else {
  146.                 $activo "Inactivo";
  147.             }
  148.             $data = array(
  149.                 'idUbi' => $ubicacion->getIdUbi(),
  150.                 'area' => $ubicacion->getArea(),
  151.                 'unidad' => $ubicacion->getUnidad($ubicacion->setUnidad($user->getUnidad())),
  152.                 'activo' => $activo,
  153.                 'mensaje' => 'Se actualizo correctamente',
  154.             );
  155.             return new JsonResponse($data);
  156.             //return $this->redirectToRoute('app_ubicacion_index', [], Response::HTTP_SEE_OTHER);
  157.         }
  158.         if($form->isSubmitted() && !$form->isValid()){
  159.             $view $this->renderForm('ubicacion/modal.html.twig', [
  160.                 'ubicacion' => $ubicacion,
  161.                 'form' => $form,
  162.             ]);
  163.             $response = new JsonResponse(['form' => $view]);
  164.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  165.             return $response;
  166.         }
  167.         return $this->renderForm('ubicacion/modal.html.twig', [
  168.             'ubicacion' => $ubicacion,
  169.             'form' => $form,
  170.         ]);
  171.     }
  172.     #[Route('/{idUbi}'name'app_ubicacion_delete'methods: ['POST'])]
  173.     public function delete(Request $requestUbicacion $ubicacionEntityManagerInterface $entityManagerSessionInterface $session): Response
  174.     {
  175.         if ($this->isCsrfTokenValid('delete' $ubicacion->getIdUbi(), $request->request->get('_token'))) {
  176.             try {
  177.                 $entityManager->remove($ubicacion);
  178.                 $entityManager->flush();
  179.                 $session->getFlashBag()->add('success''La ubicación se eliminó correctamente.');
  180.             } catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) {
  181.                 $session->getFlashBag()->add('error''No se puede eliminar esta ubicación porque tiene registros asociados.');
  182.             }
  183.         }
  184.         return $this->redirectToRoute('app_ubicacion_index', [], Response::HTTP_SEE_OTHER);
  185.     }
  186. }