src/Controller/ComSopDocumentosController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ComSop;
  4. use App\Entity\ComSopDocumentos;
  5. use App\Form\ComSopDocumentosType;
  6. use App\Repository\ComSopDocumentosRepository;
  7. use App\Services\DocumentHandler;
  8. use App\Services\MailerCore;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class ComSopDocumentosController extends AbstractController
  16. {
  17.     private $entityManager;
  18.     private $repository;
  19.     private $code 0;
  20.     private $msj "";
  21.     private $url "";
  22.     private $html "";
  23.     private $mailerCore;
  24.     public function __construct(EntityManagerInterface $entityManagerMailerCore $mailerCoreComSopDocumentosRepository $repository)
  25.     {
  26.         $this->entityManager $entityManager;
  27.         $this->mailerCore $mailerCore;
  28.         $this->repository $repository;
  29.     }
  30.     #[Route('/com/sop_documentos/new/{id}'name'app_com_sop_documentos_new')]
  31.     public function new(Request $requestEntityManagerInterface $entityManager$id nullDocumentHandler $documentHandler): Response
  32.     {
  33.         $comSop $this->entityManager->getRepository(ComSop::class)->find($id);
  34.         if (!$comSop) {
  35.             throw $this->createNotFoundException('No se encontro el registro');
  36.         }
  37.         $comSopDocumento = new ComSopDocumentos();
  38.         $form $this->createForm(ComSopDocumentosType::class, $comSopDocumento, [
  39.             'method' => 'POST',
  40.             'action' => $this->generateUrl('app_com_sop_documentos_new', [
  41.                 'id' => $comSop->getId(),
  42.             ])
  43.         ]);
  44.         $form->handleRequest($request);
  45.         $files $form->get('file')->getData();
  46.         if ($form->isSubmitted() && $form->isValid()) {
  47.             $files $form->get('file')->getData();
  48.             $nombreDocumento $form->get('nombre')->getData();
  49.             if ($files) {
  50.                 foreach ($files as $file) {
  51.                     if ($file->getClientMimeType() !== 'application/pdf') {
  52.                         $this->code 'warning';
  53.                         $this->msj "El archivo {$file->getClientOriginalName()} no es un PDF válido";
  54.                         return new Response(json_encode(['code' => $this->code'msj' => $this->msj'url' => $this->url'html' => $this->html]));
  55.                     }
  56.                     $rutaArchivo $documentHandler->cargarArchivo($fileComSop::class);
  57.                     if ($rutaArchivo['code'] === 'success') {
  58.                         $documento = new ComSopDocumentos();
  59.                         $documento->setArchivo($rutaArchivo['msj']);
  60.                         $documento->setNombre($nombreDocumento);
  61.                         $documento->setComSop($comSop);
  62.                         $documento->setCreateAt(new \DateTimeImmutable());
  63.                         $documento->setCreateUser($this->getUser()->getUserIdentifier());
  64.                         $documento->setUpdateAt(new \DateTimeImmutable());
  65.                         $documento->setUpdateUser($this->getUser()->getUserIdentifier());
  66.                         $entityManager->persist($documento);
  67.                     } else {
  68.                         return $this->json([
  69.                             'code' => 'warning',
  70.                             'msj'  => "Imposible cargar el archivo, {$rutaArchivo['msj']}"
  71.                         ]);
  72.                     }
  73.                 }
  74.                 $entityManager->flush();
  75.             }
  76.             return $this->json([
  77.                 'code' => 'success',
  78.                 'msj'  => "Registro creado exitosamente",
  79.                 'url'  => $this->generateUrl('app_com_sop_new', [
  80.                     'id'  => $comSop->getTerEmpresaCliente()->getId(),
  81.                     'idS' => $comSop->getId(),
  82.                 ]),
  83.             ]);
  84.         }
  85.         return $this->renderForm('com_sop_documentos/new.html.twig', [
  86.             'com_sop_documento' => $comSopDocumento,
  87.             'form' => $form,
  88.         ]);
  89.     }
  90.     #[Route('/com/sop_documentos/edit/{id}'name'app_com_sop_documentos_edit')]
  91.     public function edit(Request $requestComSopDocumentos $comSopDocumentoEntityManagerInterface $entityManagerDocumentHandler $documentHandler): Response
  92.     {
  93.         $comSop $comSopDocumento->getComSop();
  94.         if (!$comSop) {
  95.             throw $this->createNotFoundException('No se encontro el registro');
  96.         }
  97.         if (!$comSopDocumento) {
  98.             throw $this->createNotFoundException('Registro no encontrado');
  99.         }
  100.         $esEditar true;
  101.         $form $this->createForm(ComSopDocumentosType::class, $comSopDocumento, [
  102.             'esEditar' => $esEditar,
  103.             'method' => 'POST',
  104.             'action' => $this->generateUrl('app_com_sop_documentos_edit', [
  105.                 'id' => $comSopDocumento->getId(),
  106.             ])
  107.         ]);
  108.         $form->handleRequest($request);
  109.         $file $form->get('file')->getData();
  110.         if ($form->isSubmitted() && $form->isValid()) {
  111.             $comSopDocumento->setUpdateAt(new \DateTime('now'));
  112.             $comSopDocumento->setUpdateUser($this->getUser()->getUsername());
  113.             if($file){
  114.                 if ($file->getClientMimeType() !== 'application/pdf') {
  115.                     $this->code 'warning';
  116.                     $this->msj "El archivo {$file->getClientOriginalName()} no es un PDF válido";
  117.                     return new Response(json_encode(['code' => $this->code'msj' => $this->msj'url' => $this->url'html' => $this->html]));
  118.                 }
  119.                 $rutaArchivo $documentHandler->cargarArchivo($fileComSop::class);
  120.                 if($rutaArchivo['code'] === 'success'){
  121.                     $comSopDocumento->setArchivo($rutaArchivo['msj']);
  122.                     $entityManager->persist($comSopDocumento);
  123.                 }else {
  124.                     $this->code 'warning';
  125.                     $this->msj "Imposible cargar el archivo, {$rutaArchivo['msj']}";
  126.                     return new Response(json_encode(['code' => $this->code'msj'  => $this->msj'url'  => $this->url'html' => $this->html]));
  127.                 }
  128.                 $entityManager->flush();
  129.             }
  130.             $this->code 'success';
  131.             $this->msj "Registro creado exitosamente";
  132.             $this->url $this->generateUrl('app_com_sop_new', [
  133.                 'id' => $comSop->getTerEmpresaCliente()->getId(),
  134.                 'idS' => $comSop->getId(),
  135.             ]);
  136.             $this->html '';
  137.             return new Response(json_encode(['code' => $this->code'msj' => $this->msj'url' => $this->url'html' => $this->html]));
  138.         }
  139.         return $this->renderForm('com_sop_documentos/edit.html.twig', [
  140.             'com_sop_documento' => $comSopDocumento,
  141.             'form' => $form,
  142.         ]);
  143.     }
  144.     #[Route('/com/sop_documentos/delete/{id}'name'app_com_sop_documentos_delete'methods: ['POST'])]
  145.     public function delete(Request $requestComSopDocumentos $comSopDocumentoEntityManagerInterface $entityManager): Response
  146.     {
  147.         $comSop $comSopDocumento->getComSop();
  148.         if (!$comSop) {
  149.             throw $this->createNotFoundException('No se encontro el registro');
  150.         }
  151.         if (!$comSopDocumento) {
  152.             throw $this->createNotFoundException('Registro no encontrado');
  153.         }
  154.         $entityManager->remove($comSopDocumento);
  155.         $entityManager->flush();
  156.         $url $this->generateUrl('app_com_sop_new', [
  157.             'id'  => $comSop->getTerEmpresaCliente()->getId(),
  158.             'idS' => $comSop->getId(),
  159.         ]);
  160.         return new JsonResponse([
  161.             'code' => 'success',
  162.             'msj' => 'Registro eliminado exitosamente',
  163.             'url' => $url,
  164.             'html' => ''
  165.         ]);
  166.     }
  167. }