<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Modulo;
use App\Form\ModuloType;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\Filesystem\Filesystem;
class ModuloController extends AbstractController
{
private $managerRegistry;
public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}
/**
* @Route("/", name="home")
*/
public function redirectToModuloIndex(Request $request): Response
{
$codigo = $request->query->get('codigo');
if (!$codigo) {
$entityManager = $this->managerRegistry->getManager();
$modulos = $entityManager->getRepository(Modulo::class)->findAll();
$randomModulo = $modulos[array_rand($modulos)];
$codigo = $randomModulo->getCodigo();
}
return $this->redirectToRoute('modulo_index', ['codigo' => $codigo]);
}
/**
* @Route("/modulo", name="modulo_index", methods={"GET"})
*/
public function index(Request $request): Response
{
$codigo = $request->query->get('codigo');
$entityManager = $this->managerRegistry->getManager();
$modulo = $entityManager->getRepository(Modulo::class)->findOneBy(
['codigo' => $codigo]
);
if (!$modulo) {
throw $this->createNotFoundException(
'No existe modulo: ' . $codigo
);
}
return $this->render('modulo/index.html.twig', [
'modulo' => $modulo,
'nichos' => $modulo->getNichos(),
]);
}
/**
* @Route("/modulo/new", name="modulo_new", methods={"GET", "POST"})
*/
public function new(Request $request): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$entityManager = $this->managerRegistry->getManager();
$modulo = new Modulo();
$form = $this->createForm(ModuloType::class, $modulo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $imageFile */
$imageFile = $form['imageFile']->getData();
$newFilename = $form['codigo'] . '.' . $imageFile->guessExtension();
try {
$imageFile->move(
$this->getParameter('images_directory'),
$newFilename
);
} catch (FileException $e) {
var_dump($e);
}
$modulo->setImagePath($newFilename);
$entityManager->persist($modulo);
$entityManager->flush();
return $this->redirectToRoute('dashboard');
}
return $this->render('modulo/new.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/modulo/{id}", name="modulo_show", methods={"GET"})
*/
public function show($id): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$modulo = $this->managerRegistry
->getRepository(Modulo::class)
->find($id);
if (!$modulo) {
throw $this->createNotFoundException(
'No existe modulo: ' . $id
);
}
return $this->render('modulo/show.html.twig', [
'modulo' => $modulo,
'nichos' => $modulo->getNichos(),
]);
}
/**
* @Route("/modulo/{id}/edit", name="modulo_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, $id): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$modulo = $this->managerRegistry
->getRepository(Modulo::class)
->find($id);
if (!$modulo) {
throw $this->createNotFoundException(
'No existe modulo: ' . $id
);
}
$entityManager = $this->managerRegistry->getManager();
$form = $this->createForm(ModuloType::class, $modulo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('dashboard');
}
return $this->render('modulo/edit.html.twig', [
'form' => $form->createView(),
'modulo' => $modulo,
]);
}
/**
* @Route("/modulo/{id}/delete", name="modulo_delete", methods={"POST"})
*/
public function delete($id): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$entityManager = $this->managerRegistry->getManager();
$modulo = $this->managerRegistry
->getRepository(Modulo::class)
->find($id);
if (!$modulo) {
throw $this->createNotFoundException(
'No existe modulo: ' . $id
);
}
$entityManager->remove($modulo);
$entityManager->flush();
$imagePath = $modulo->getImagePath();
$filesystem = new Filesystem();
$filesystem->remove($this->getParameter('images_directory') . '/' . $imagePath);
return $this->redirectToRoute('dashboard');
}
}