<?php
namespace App\Controller;
use App\Entity\Vehicule;
use App\Form\VehiculeType;
use App\Repository\VehiculeRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/vehicule")
*/
class VehiculeController extends AbstractController
{
/**
* @Route("/", name="app_vehicule_index", methods={"GET"})
*/
public function index(VehiculeRepository $vehiculeRepository): Response
{
return $this->render('vehicule/index.html.twig', [
'vehicules' => $vehiculeRepository->findAll(),
]);
}
/**
* @Route("/{idadr}/new", name="app_vehicule_new", methods={"GET", "POST"})
* @Route("/{id}/Admin/edit", name="app_vehicule_edit", methods={"GET", "POST"})
*/
public function new(Request $request, Vehicule $vehicule=null, VehiculeRepository $vehiculeRepository,$idadr=null, \App\Services\AppSevices $services): Response
{
if(!$vehicule){
$vehicule = new Vehicule();
}
if(!$vehicule->getAdherent()){
$adr = $services->getAdherents(['id' => $idadr])->getQuery()->getOneOrNullResult();
$vehicule->setAdherent($adr);
}
$form = $this->createForm(VehiculeType::class, $vehicule);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$vehiculeRepository->add($vehicule, true);
$id=$vehicule->getAdherent()->getId();
return $this->redirectToRoute('app_adherent_show', ['id'=>$id], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('vehicule/new.html.twig', [
'vehicule' => $vehicule,
'form' => $form,
]);
}
}