src/Controller/ModuloController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use App\Entity\Modulo;
  8. use App\Form\ModuloType;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\HttpFoundation\File\UploadedFile;
  11. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. class ModuloController extends AbstractController
  14. {
  15.     private $managerRegistry;
  16.     public function __construct(ManagerRegistry $managerRegistry)
  17.     {
  18.         $this->managerRegistry $managerRegistry;
  19.     }
  20.     /**
  21.      * @Route("/", name="home")
  22.      */
  23.     public function redirectToModuloIndex(Request $request): Response
  24.     {
  25.         $codigo $request->query->get('codigo');
  26.         if (!$codigo) {
  27.             $entityManager $this->managerRegistry->getManager();
  28.             $modulos $entityManager->getRepository(Modulo::class)->findAll();
  29.             $randomModulo $modulos[array_rand($modulos)];
  30.             $codigo $randomModulo->getCodigo();
  31.         }
  32.         return $this->redirectToRoute('modulo_index', ['codigo' => $codigo]);
  33.     }
  34.     /**
  35.      * @Route("/modulo", name="modulo_index", methods={"GET"})
  36.      */
  37.     public function index(Request $request): Response
  38.     {
  39.         $codigo $request->query->get('codigo');
  40.         $entityManager $this->managerRegistry->getManager();
  41.         $modulo $entityManager->getRepository(Modulo::class)->findOneBy(
  42.             ['codigo' => $codigo]
  43.         );
  44.         if (!$modulo) {
  45.             throw $this->createNotFoundException(
  46.                 'No existe modulo: ' $codigo
  47.             );
  48.         }
  49.         return $this->render('modulo/index.html.twig', [
  50.             'modulo' => $modulo,
  51.             'nichos' => $modulo->getNichos(),
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/modulo/new", name="modulo_new", methods={"GET", "POST"})
  56.      */
  57.     public function new(Request $request): Response
  58.     {
  59.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  60.         $entityManager $this->managerRegistry->getManager();
  61.         $modulo = new Modulo();
  62.         $form $this->createForm(ModuloType::class, $modulo);
  63.         $form->handleRequest($request);
  64.         if ($form->isSubmitted() && $form->isValid()) {
  65.             /** @var UploadedFile $imageFile */
  66.             $imageFile $form['imageFile']->getData();
  67.             $newFilename $form['codigo'] . '.' $imageFile->guessExtension();
  68.             try {
  69.                 $imageFile->move(
  70.                     $this->getParameter('images_directory'),
  71.                     $newFilename
  72.                 );
  73.             } catch (FileException $e) {
  74.                 var_dump($e);
  75.             }
  76.             $modulo->setImagePath($newFilename);
  77.             $entityManager->persist($modulo);
  78.             $entityManager->flush();
  79.             return $this->redirectToRoute('dashboard');
  80.         }
  81.         return $this->render('modulo/new.html.twig', [
  82.             'form' => $form->createView(),
  83.         ]);
  84.     }
  85.     /**
  86.      * @Route("/modulo/{id}", name="modulo_show", methods={"GET"})
  87.      */
  88.     public function show($id): Response
  89.     {
  90.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  91.         $modulo $this->managerRegistry
  92.             ->getRepository(Modulo::class)
  93.             ->find($id);
  94.         if (!$modulo) {
  95.             throw $this->createNotFoundException(
  96.                 'No existe modulo: ' $id
  97.             );
  98.         }
  99.         return $this->render('modulo/show.html.twig', [
  100.             'modulo' => $modulo,
  101.             'nichos' => $modulo->getNichos(),
  102.         ]);
  103.     }
  104.     /**
  105.      * @Route("/modulo/{id}/edit", name="modulo_edit", methods={"GET", "POST"})
  106.      */
  107.     public function edit(Request $request$id): Response
  108.     {
  109.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  110.         $modulo $this->managerRegistry
  111.             ->getRepository(Modulo::class)
  112.             ->find($id);
  113.         if (!$modulo) {
  114.             throw $this->createNotFoundException(
  115.                 'No existe modulo: ' $id
  116.             );
  117.         }
  118.         $entityManager $this->managerRegistry->getManager();
  119.         $form $this->createForm(ModuloType::class, $modulo);
  120.         $form->handleRequest($request);
  121.         if ($form->isSubmitted() && $form->isValid()) {
  122.             $entityManager->flush();
  123.             return $this->redirectToRoute('dashboard');
  124.         }
  125.         return $this->render('modulo/edit.html.twig', [
  126.             'form' => $form->createView(),
  127.             'modulo' => $modulo,
  128.         ]);
  129.     }
  130.     /**
  131.      * @Route("/modulo/{id}/delete", name="modulo_delete", methods={"POST"})
  132.      */
  133.     public function delete($id): Response
  134.     {
  135.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  136.         $entityManager $this->managerRegistry->getManager();
  137.         $modulo $this->managerRegistry
  138.             ->getRepository(Modulo::class)
  139.             ->find($id);
  140.         if (!$modulo) {
  141.             throw $this->createNotFoundException(
  142.                 'No existe modulo: ' $id
  143.             );
  144.         }
  145.         $entityManager->remove($modulo);
  146.         $entityManager->flush();
  147.         $imagePath $modulo->getImagePath();
  148.         $filesystem = new Filesystem();
  149.         $filesystem->remove($this->getParameter('images_directory') . '/' $imagePath);
  150.         return $this->redirectToRoute('dashboard');
  151.     }
  152. }