<?php
namespace App\Controller;
use App\Entity\Url;
use App\Form\UrlProduitType;
use App\Repository\ProduitRepository;
use App\Repository\UrlRepository;
use App\Repository\VendeurRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/admin/url')]
class AdminUrlController extends AbstractController
{
#[Route('/', name: 'app_admin_url_index', methods: ['GET'])]
public function index(UrlRepository $urlRepository): Response
{
return $this->render('admin_url/index.html.twig', [
'urls' => $urlRepository->findBy(
[],["erreur" => "DESC", "dateTimeMaj"=>"DESC"], null
),
]);
}
#[Route('/new', name: 'app_admin_url_new', methods: ['GET', 'POST'])]
#[Route('/new/idProduit-{idProduit}/idVendeur-{idVendeur}', name: 'app_admin_url_new_id_produit_id_vendeur', methods: ['GET', 'POST'])]
public function new(Request $request, UrlRepository $urlRepository, ProduitRepository $produitRepository, VendeurRepository $vendeurRepository, ?Url $url, $idProduit = null, $idVendeur = null): Response
{
$url = new Url();
if(isset($idProduit)){
$url->setProduit($produitRepository->find($idProduit));
}
if(isset($idVendeur)){
$url->setVendeur($vendeurRepository->find($idVendeur));
}
$form = $this->createForm(UrlProduitType::class, $url);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$url->setDateTimeMaj(new \DateTime('now'));
$urlRepository->save($url, true);
return $this->redirectToRoute('app_admin_url_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('admin_url/new.html.twig', [
'url' => $url,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_admin_url_show', methods: ['GET'])]
public function show(Url $url): Response
{
return $this->render('admin_url/show.html.twig', [
'url' => $url,
]);
}
#[Route('/{id}/edit', name: 'app_admin_url_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Url $url, UrlRepository $urlRepository): Response
{
$form = $this->createForm(UrlProduitType::class, $url);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$urlRepository->save($url, true);
return $this->redirectToRoute('app_admin_url_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('admin_url/edit.html.twig', [
'url' => $url,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_admin_url_delete', methods: ['POST'])]
public function delete(Request $request, Url $url, UrlRepository $urlRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$url->getId(), $request->request->get('_token'))) {
$urlRepository->remove($url, true);
}
return $this->redirectToRoute('app_admin_url_index', [], Response::HTTP_SEE_OTHER);
}
}