src/Controller/Seguridades/SeccionesController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Seguridades;
  3. use App\Entity\Seguridades\{PaginaSeccionesPermisosGrupoPermisosUsuario};
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\{Request};
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class SeccionesController extends AbstractController {
  11.     private $em;
  12.     private $msgCreate;
  13.   private $msgUpdate;
  14.   private $msgDelete;
  15.     private $msgNotFound;
  16.   public function __construct(EntityManagerInterface $em) {
  17.     date_default_timezone_set('America/Guayaquil');
  18.     $this->em $em;
  19.         $this->msgCreate "Registro creado exitosamente.";
  20.     $this->msgUpdate "Registro actualizado exitosamente.";
  21.     $this->msgDelete "Registro eliminado exitosamente.";
  22.         $this->msgNotFound "Registro no encontrado.";
  23.   }
  24.     // * READ FUNCTIONS
  25.         /**
  26.          * @Route("/readSecciones", name="secciones_list")
  27.          */
  28.         public function readSecciones(Request $request) {
  29.             $filtro $request->request->get('filtro');
  30.             $idPagina $request->request->get('idPagina');
  31.             $secciones = ($filtro === 'todoSecciones')
  32.                 ? $this->em->getRepository(Secciones::class)->findAll()
  33.                 : $this->em->getRepository(Secciones::class)->findBy(['idPagina' => $idPagina]);
  34.             $list array_map(fn($seccion) => [
  35.                 'id' => $seccion->getId(),
  36.                 'modulo' => $seccion->getIdPagina()->getIdModulo()->getNombre(),
  37.                 'pagina' => $seccion->getIdPagina()->getMenu(),
  38.                 'nombre' => $seccion->getNombre(),
  39.                 'descripcion' => $seccion->getDescripcion()
  40.             ], $secciones);
  41.             return $this->json(['data' => $list], 200);
  42.         }
  43.     // * CREATE OR UPDATE FUNCTIONS
  44.         /**
  45.          * @Route("/createUpdateSeccion", name="seccion_create_update")
  46.          */
  47.         public function add(Request $requestUserInterface $user) {
  48.             try {
  49.                 $data $request->request->get('sections', []);
  50.                 $id $data['id'] ?? null;
  51.                 $seccion $id $this->em->getRepository(Secciones::class)->find($id) : new Secciones;
  52.                 $seccion $this->setData($seccion$data$user);
  53.                 $this->em->persist($seccion);
  54.                 $this->em->flush();
  55.                 return $this->json(['response' => true'message' => $id $this->msgUpdate $this->msgCreate,], 200);
  56.             } catch (Exception $e) {
  57.                 return $this->json([
  58.                     'response' => false,
  59.                     'message' => sprintf('<b>%s</b> error al realizar el registro dentro de la petición <i>seccion_create_update</i>'$e->getMessage())
  60.                 ]);
  61.             }
  62.         }
  63.         private function setData(Secciones $seccion$dataUserInterface $user) {
  64.             $seccion
  65.                 ->setNombre($data['nombre'])
  66.                 ->setDescripcion($data['descripcion']);
  67.             $seccion $this->setGenericData($user$seccion);
  68.             if ($data['id'] == null) :
  69.                 $seccion->setIdPagina($this->em->getRepository(Pagina::class)->find($data['idPagina']));
  70.             endif;
  71.             return $seccion;
  72.         }
  73.         private function setGenericData(?UserInterface $user$entity) {
  74.             $entity
  75.                 ->setIdUsuarioModificacion($user $user->getId() : '1')
  76.                 ->setFechaModificacion(new \DateTime())
  77.                 ->setIpModificacion($_SERVER['REMOTE_ADDR'] ?? '127.0.0.1');
  78.             return $entity;
  79.         }
  80.     // * DELETE FUNCTIONS
  81.         /**
  82.          * @Route("/deleteSeccion", name="seccion_delete")
  83.          */
  84.         public function deleteSeccion(Request $requestUserInterface $user) {
  85.             try {
  86.                 $id $request->request->get('idSec');
  87.                 $repository $this->em->getRepository(Secciones::class);
  88.                 $section $repository->find($id);
  89.                 if (!$section) :
  90.                     return $this->json(['response' => false'message' => $this->msgNotFound'exception' => false], 404);
  91.                 endif;
  92.                 $hasDependencies  $this->em->getRepository(PermisosUsuario::class)->findOneBy(['idSecciones' => $id]) ||
  93.                     $this->em->getRepository(PermisosGrupo::class)->findOneBy(['idSecciones' => $id]);
  94.                 if ($hasDependencies) :
  95.                     return $this->json(['response' => false'exception' => false], 200);
  96.                 endif;
  97.                 $this->em->remove($section);
  98.                 $this->em->flush();
  99.                 return $this->json(['response' => true'message' => $this->msgDelete'exception' => false], 200);
  100.             } catch (Exception $e) {
  101.                 return $this->json([
  102.                     'response' => false,
  103.                     'message' => sprintf('<b>%s</b> error al realizar el registro dentro de la petición <i>seccion_delete</i>'$e->getMessage())
  104.                 ]);
  105.             }
  106.         }
  107. }