<?php
namespace App\Controller;
use App\Entity\ComSop;
use App\Entity\ComSopDocumentos;
use App\Form\ComSopDocumentosType;
use App\Repository\ComSopDocumentosRepository;
use App\Services\DocumentHandler;
use App\Services\MailerCore;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ComSopDocumentosController extends AbstractController
{
private $entityManager;
private $repository;
private $code = 0;
private $msj = "";
private $url = "";
private $html = "";
private $mailerCore;
public function __construct(EntityManagerInterface $entityManager, MailerCore $mailerCore, ComSopDocumentosRepository $repository)
{
$this->entityManager = $entityManager;
$this->mailerCore = $mailerCore;
$this->repository = $repository;
}
#[Route('/com/sop_documentos/new/{id}', name: 'app_com_sop_documentos_new')]
public function new(Request $request, EntityManagerInterface $entityManager, $id = null, DocumentHandler $documentHandler): Response
{
$comSop = $this->entityManager->getRepository(ComSop::class)->find($id);
if (!$comSop) {
throw $this->createNotFoundException('No se encontro el registro');
}
$comSopDocumento = new ComSopDocumentos();
$form = $this->createForm(ComSopDocumentosType::class, $comSopDocumento, [
'method' => 'POST',
'action' => $this->generateUrl('app_com_sop_documentos_new', [
'id' => $comSop->getId(),
])
]);
$form->handleRequest($request);
$files = $form->get('file')->getData();
if ($form->isSubmitted() && $form->isValid()) {
$files = $form->get('file')->getData();
$nombreDocumento = $form->get('nombre')->getData();
if ($files) {
foreach ($files as $file) {
if ($file->getClientMimeType() !== 'application/pdf') {
$this->code = 'warning';
$this->msj = "El archivo {$file->getClientOriginalName()} no es un PDF válido";
return new Response(json_encode(['code' => $this->code, 'msj' => $this->msj, 'url' => $this->url, 'html' => $this->html]));
}
$rutaArchivo = $documentHandler->cargarArchivo($file, ComSop::class);
if ($rutaArchivo['code'] === 'success') {
$documento = new ComSopDocumentos();
$documento->setArchivo($rutaArchivo['msj']);
$documento->setNombre($nombreDocumento);
$documento->setComSop($comSop);
$documento->setCreateAt(new \DateTimeImmutable());
$documento->setCreateUser($this->getUser()->getUserIdentifier());
$documento->setUpdateAt(new \DateTimeImmutable());
$documento->setUpdateUser($this->getUser()->getUserIdentifier());
$entityManager->persist($documento);
} else {
return $this->json([
'code' => 'warning',
'msj' => "Imposible cargar el archivo, {$rutaArchivo['msj']}"
]);
}
}
$entityManager->flush();
}
return $this->json([
'code' => 'success',
'msj' => "Registro creado exitosamente",
'url' => $this->generateUrl('app_com_sop_new', [
'id' => $comSop->getTerEmpresaCliente()->getId(),
'idS' => $comSop->getId(),
]),
]);
}
return $this->renderForm('com_sop_documentos/new.html.twig', [
'com_sop_documento' => $comSopDocumento,
'form' => $form,
]);
}
#[Route('/com/sop_documentos/edit/{id}', name: 'app_com_sop_documentos_edit')]
public function edit(Request $request, ComSopDocumentos $comSopDocumento, EntityManagerInterface $entityManager, DocumentHandler $documentHandler): Response
{
$comSop = $comSopDocumento->getComSop();
if (!$comSop) {
throw $this->createNotFoundException('No se encontro el registro');
}
if (!$comSopDocumento) {
throw $this->createNotFoundException('Registro no encontrado');
}
$esEditar = true;
$form = $this->createForm(ComSopDocumentosType::class, $comSopDocumento, [
'esEditar' => $esEditar,
'method' => 'POST',
'action' => $this->generateUrl('app_com_sop_documentos_edit', [
'id' => $comSopDocumento->getId(),
])
]);
$form->handleRequest($request);
$file = $form->get('file')->getData();
if ($form->isSubmitted() && $form->isValid()) {
$comSopDocumento->setUpdateAt(new \DateTime('now'));
$comSopDocumento->setUpdateUser($this->getUser()->getUsername());
if($file){
if ($file->getClientMimeType() !== 'application/pdf') {
$this->code = 'warning';
$this->msj = "El archivo {$file->getClientOriginalName()} no es un PDF válido";
return new Response(json_encode(['code' => $this->code, 'msj' => $this->msj, 'url' => $this->url, 'html' => $this->html]));
}
$rutaArchivo = $documentHandler->cargarArchivo($file, ComSop::class);
if($rutaArchivo['code'] === 'success'){
$comSopDocumento->setArchivo($rutaArchivo['msj']);
$entityManager->persist($comSopDocumento);
}else {
$this->code = 'warning';
$this->msj = "Imposible cargar el archivo, {$rutaArchivo['msj']}";
return new Response(json_encode(['code' => $this->code, 'msj' => $this->msj, 'url' => $this->url, 'html' => $this->html]));
}
$entityManager->flush();
}
$this->code = 'success';
$this->msj = "Registro creado exitosamente";
$this->url = $this->generateUrl('app_com_sop_new', [
'id' => $comSop->getTerEmpresaCliente()->getId(),
'idS' => $comSop->getId(),
]);
$this->html = '';
return new Response(json_encode(['code' => $this->code, 'msj' => $this->msj, 'url' => $this->url, 'html' => $this->html]));
}
return $this->renderForm('com_sop_documentos/edit.html.twig', [
'com_sop_documento' => $comSopDocumento,
'form' => $form,
]);
}
#[Route('/com/sop_documentos/delete/{id}', name: 'app_com_sop_documentos_delete', methods: ['POST'])]
public function delete(Request $request, ComSopDocumentos $comSopDocumento, EntityManagerInterface $entityManager): Response
{
$comSop = $comSopDocumento->getComSop();
if (!$comSop) {
throw $this->createNotFoundException('No se encontro el registro');
}
if (!$comSopDocumento) {
throw $this->createNotFoundException('Registro no encontrado');
}
$entityManager->remove($comSopDocumento);
$entityManager->flush();
$url = $this->generateUrl('app_com_sop_new', [
'id' => $comSop->getTerEmpresaCliente()->getId(),
'idS' => $comSop->getId(),
]);
return new JsonResponse([
'code' => 'success',
'msj' => 'Registro eliminado exitosamente',
'url' => $url,
'html' => ''
]);
}
}