<?php
namespace App\Controller;
use App\Entity\Rutas;
use App\Form\RutasType;
use App\Repository\RutasRepository;
use App\Repository\RutasRestaurantesRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/rutas')]
class RutasController extends AbstractController
{
#[Route('/', name: 'app_rutas_index', methods: ['GET'])]
public function index(RutasRepository $rutasRepository): Response
{
return $this->render('rutas/index.html.twig', [
'rutas' => $rutasRepository->findAll(),
]);
}
#[Route('/new', name: 'app_rutas_new', methods: ['GET', 'POST'])]
public function new(Request $request, RutasRepository $rutasRepository): Response
{
$ruta = new Rutas();
$form = $this->createForm(RutasType::class, $ruta);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$rutasRepository->save($ruta, true);
return $this->redirectToRoute('app_rutas_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('rutas/new.html.twig', [
'ruta' => $ruta,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_rutas_show', methods: ['GET'])]
public function show($id, RutasRestaurantesRepository $rutasRestaurantesRepository, RutasRepository $rutasRepository): Response
{
$ruta = $rutasRepository->find($id);
if ( empty($ruta) ){
return $this->redirectToRoute('app_home_index');
}
$restaurantes = $rutasRestaurantesRepository->findBy(['id_ruta' => $id, 'estado' => 'Activo']);
return $this->render('rutas/show_ruta.html.twig', [
'ruta' => $ruta,
'restaurantes' => $restaurantes,
]);
}
#[Route('/{id}/edit', name: 'app_rutas_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, $id, RutasRepository $rutasRepository): Response
{
$ruta = $rutasRepository->find($id);
$form = $this->createForm(RutasType::class, $ruta);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$rutasRepository->save($ruta, true);
return $this->redirectToRoute('app_rutas_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('rutas/edit.html.twig', [
'ruta' => $ruta,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_rutas_delete', methods: ['POST'])]
public function delete(Request $request, $id, RutasRepository $rutasRepository): Response
{
$ruta = $rutasRepository->find($id);
if ($this->isCsrfTokenValid('delete'.$ruta->getId(), $request->request->get('_token'))) {
$rutasRepository->remove($ruta, true);
}
return $this->redirectToRoute('app_rutas_index', [], Response::HTTP_SEE_OTHER);
}
}