EthikTag/src/Controller/TubeController.php

146 lines
4.5 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Tube;
use App\Form\TubeType;
use App\Form\Handler\TubeHandler;
use App\Repository\TubeRepository;
use App\Services\Tools;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
/**
* Class TubeController
* @package App\Controller
* @Route("/admin/tube", name="admin_app_tube_")
*/
class TubeController extends AbstractController
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/", name="index", methods={"GET"})
* @param TubeRepository $tubeRepository
* PaginatorInterface $paginator
* Request $request
*/
public function index(TubeRepository $tubeRepository, PaginatorInterface $paginator, Request $request): Response
{
$els = $paginator->paginate(
$tubeRepository->createQueryBuilder('a')->getQuery(),
$request->query->getInt('page', 1),
10
);
return $this->render('admin/crud/index.html.twig', [
'els'=>$els,
'paginator'=>false,
'search'=>false,
'class'=> Tube::class,
'route'=> 'admin_app_tube',
'breadcrumb'=>[
[
'text'=>'tous les éléments'
]
],
'fields' => [
'Id' => 'Id',
'Reference' => 'Reference',
'Couleur' => 'ColorName',
'ColorCss' => 'ColorCss',
'Analyse' => 'Analyse',
],
'title' => 'Tous les élements',
'add_button_label'=>'Ajouter un élément'
]);
}
/**
* @Route("/new", name="new", methods={"GET","POST"})
* @param Request $request
* @param TubeHandler $tubeHandler
* @param Tools $tools
* @return Response
*/
public function new(Request $request, Tools $tools, TubeHandler $tubeHandler): Response
{
$tube = new Tube();
$form = $this->createForm(TubeType::class, $tube);
if ($tubeHandler->new($form, $request)) {
return $this->redirectToRoute('admin_app_tube_index');
}
return $this->render('admin/crud/_form.html.twig', [
'form'=>$form->createView(),
'el'=>$tube,
'button_label'=>'Créer',
'route'=>'admin_app_tube',
'title'=>'Ajouter un élément',
'breadcrumb'=>[
[
'route'=>'admin_app_tube_index',
'text'=>'tous les éléments'
],
[
'text'=>'ajouter un élément'
]
],
]);
}
/**
* @Route("/edit/{id}", name="edit")
* @param Request $request
* @param Tube $tube
* @param Tools $tools
* @return Response
*/
public function edit(Request $request, Tube $tube, TubeHandler $tubeHandler): Response
{
$form = $this->createForm(TubeType::class, $tube);
if ($tubeHandler->edit($form, $request)) {
return $this->redirectToRoute('admin_app_tube_edit', ['id'=>$tube->getId()]);
}
return $this->render('admin/crud/_form.html.twig', [
'el' => $tube,
'route'=> 'admin_app_tube',
'form' => $form->createView(),
'button_label' => 'Mettre à jour',
'title' => 'Edition',
'breadcrumb'=>[
[
'route'=>'admin_app_tube_index',
'text'=>'tubes'
],
[
'text'=>'édition '
]
],
]);
}
/**
* @Route("/{id}", name="delete", methods={"DELETE"})
* @param Request $request
* @return Response
*/
public function delete(Request $request,Tube $tube): Response
{
if ($this->isCsrfTokenValid('delete'.$tube->getId(), $request->request->get('_token'))) {
$this->entityManager->remove($tube);
$this->entityManager->flush();
}
return $this->redirectToRoute('admin_app_tube_index');
}
}