145 lines
4.4 KiB
PHP
145 lines
4.4 KiB
PHP
|
<?php
|
||
|
namespace App\Controller;
|
||
|
|
||
|
use App\Entity\Don;
|
||
|
use App\Form\DonType;
|
||
|
use App\Form\Handler\DonHandler;
|
||
|
use App\Repository\DonRepository;
|
||
|
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 DonController
|
||
|
* @package App\Controller\AdminController
|
||
|
* @Route("/admin/don", name="admin_app_don_")
|
||
|
*/
|
||
|
class DonController extends AbstractController
|
||
|
{
|
||
|
private EntityManagerInterface $entityManager;
|
||
|
public function __construct(EntityManagerInterface $entityManager)
|
||
|
{
|
||
|
$this->entityManager = $entityManager;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @Route("/", name="index", methods={"GET"})
|
||
|
* @param DonRepository $donRepository
|
||
|
* PaginatorInterface $paginator
|
||
|
* Request $request
|
||
|
*/
|
||
|
public function index(DonRepository $donRepository, PaginatorInterface $paginator, Request $request): Response
|
||
|
{
|
||
|
$els = $paginator->paginate(
|
||
|
$donRepository->createQueryBuilder('a')->getQuery(),
|
||
|
$request->query->getInt('page', 1),
|
||
|
10
|
||
|
);
|
||
|
return $this->render('admin/crud/index.html.twig', [
|
||
|
'els'=>$els,
|
||
|
'paginator'=>true,
|
||
|
'search'=>true,
|
||
|
'class'=> Don::class,
|
||
|
'route'=> 'admin_app_don',
|
||
|
'breadcrumb'=>[
|
||
|
[
|
||
|
'text'=>'tous les éléments'
|
||
|
]
|
||
|
],
|
||
|
'fields' => [
|
||
|
'Id' => 'Id',
|
||
|
'GiftNumber' => 'GiftNumber',
|
||
|
'Categorie' => 'DonCat',
|
||
|
'Donneur' => 'Donneur',
|
||
|
|
||
|
],
|
||
|
'title' => 'Tous les élements',
|
||
|
'add_button_label'=>'Ajouter un élément'
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @Route("/new", name="new", methods={"GET","POST"})
|
||
|
* @param Request $request
|
||
|
* @param DonHandler $donHandler
|
||
|
* @param Tools $tools
|
||
|
* @return Response
|
||
|
*/
|
||
|
public function new(Request $request, Tools $tools, DonHandler $donHandler): Response
|
||
|
{
|
||
|
$don = new Don();
|
||
|
$form = $this->createForm(DonType::class, $don);
|
||
|
if ($donHandler->new($form, $request)) {
|
||
|
return $this->redirectToRoute('admin_app_don_index');
|
||
|
}
|
||
|
|
||
|
return $this->render('admin/crud/_form.html.twig', [
|
||
|
'form'=>$form->createView(),
|
||
|
'el'=>$don,
|
||
|
'button_label'=>'Créer',
|
||
|
'route'=>'admin_app_don',
|
||
|
'title'=>'Ajouter un élément',
|
||
|
'breadcrumb'=>[
|
||
|
[
|
||
|
'route'=>'admin_app_don_index',
|
||
|
'text'=>'tous les éléments'
|
||
|
],
|
||
|
[
|
||
|
'text'=>'ajouter un élément'
|
||
|
]
|
||
|
],
|
||
|
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @Route("/edit/{id}", name="edit")
|
||
|
* @param Request $request
|
||
|
* @param Don $don
|
||
|
* @param Tools $tools
|
||
|
* @return Response
|
||
|
*/
|
||
|
public function edit(Request $request, Don $don, Tools $tools, DonHandler $donHandler): Response
|
||
|
{
|
||
|
$form = $this->createForm(DonType::class, $don);
|
||
|
if ($donHandler->edit($form, $request)) {
|
||
|
return $this->redirectToRoute('admin_app_don_edit', ['id'=>$don->getId()]);
|
||
|
}
|
||
|
return $this->render('admin/crud/_form.html.twig', [
|
||
|
'el' => $don,
|
||
|
'route'=> 'admin_app_don',
|
||
|
'form' => $form->createView(),
|
||
|
'button_label' => 'Mettre à jour',
|
||
|
'title' => 'Edition',
|
||
|
'breadcrumb'=>[
|
||
|
[
|
||
|
'route'=>'admin_app_don_index',
|
||
|
'text'=>'dons'
|
||
|
],
|
||
|
[
|
||
|
'text'=>'édition '
|
||
|
]
|
||
|
],
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @Route("/{id}", name="delete", methods={"DELETE"})
|
||
|
* @param Request $request
|
||
|
* @return Response
|
||
|
*/
|
||
|
public function delete(Request $request,Don $don): Response
|
||
|
{
|
||
|
if ($this->isCsrfTokenValid('delete'.$don->getId(), $request->request->get('_token'))) {
|
||
|
$this->entityManager->remove($don);
|
||
|
$this->entityManager->flush();
|
||
|
}
|
||
|
return $this->redirectToRoute('admin_app_don_index');
|
||
|
}
|
||
|
}
|