src/Controller/UbicacionController.php line 34

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