src/Controller/ComSopDocumentosPController.php line 38

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