From 19c24803d18f8016a28a06817b66403c85300441 Mon Sep 17 00:00:00 2001 From: afornerot Date: Tue, 30 Sep 2025 23:09:03 +0200 Subject: [PATCH] svg --- src/Controller/DomaineController.php | 208 ++++++++------------------- 1 file changed, 62 insertions(+), 146 deletions(-) diff --git a/src/Controller/DomaineController.php b/src/Controller/DomaineController.php index 563beaa..9194f0f 100755 --- a/src/Controller/DomaineController.php +++ b/src/Controller/DomaineController.php @@ -2,179 +2,95 @@ namespace App\Controller; +use App\Entity\Domaine; +use App\Form\DomaineType; +use App\Repository\DomaineRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Form\FormError; -use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; - -use App\Entity\Domaine as Entity; -use App\Form\DomaineType as Form; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class DomaineController extends AbstractController { - private $data = "domaine"; - private $route = "app_domaine"; - private $render = "Domaine/"; - private $entity = "App:Domaine"; + private EntityManagerInterface $em; + private DomaineRepository $domaineRepository; - private $knpSnappy; - public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; } - - public function list(Request $request) + public function __construct(EntityManagerInterface $em, DomaineRepository $domaineRepository) { - $em = $this->getDoctrine()->getManager(); - $datas = $em->getRepository($this->entity)->findAll(); + $this->em = $em; + $this->domaineRepository = $domaineRepository; + } - if($request->query->get('fgprint')) { - $render = $this->renderView($this->render.'list.html.twig',[ - $this->data."s" => $datas, - "useheader" => true, - "usesidebar" => true, - "fgprint" => true, - ]); + public function list() + { + $domaine = $this->domaineRepository->findAll(); - return new PdfResponse( - $this->knpSnappy->getOutputFromHtml($render), - 'domaines.pdf' - ); - } - else { - return $this->render($this->render.'list.html.twig',[ - $this->data."s" => $datas, - "useheader" => true, - "usesidebar" => true, - ]); - } + return $this->render('Domaine/list.html.twig', [ + 'domaines' => $domaine, + 'useheader' => true, + 'usesidebar' => true, + ]); } public function submit(Request $request) { - // Initialisation de l'enregistrement - $em = $this->getDoctrine()->getManager(); - $data = new Entity(); - - // Création du formulaire - $form = $this->createForm(Form::class,$data,array("mode"=>"submit")); - - // Récupération des data du formulaire + $domaine = new Domaine(); + $form = $this->createForm(DomaineType::class, $domaine, ['mode' => 'submit']); $form->handleRequest($request); - - // Sur erreur - $this->getErrorForm(null,$form,$request,$data,"submit"); - - // Sur validation - if ($form->get('submit')->isClicked() && $form->isValid()) { - $data = $form->getData(); - $em->persist($data); - $em->flush(); + if ($form->isSubmitted() && $form->isValid()) { + $this->em->persist($domaine); + $this->em->flush(); - // Retour à la liste - return $this->redirectToRoute($this->route); + return $this->redirectToRoute('app_domaine'); } - - // Affichage du formulaire - return $this->render($this->render.'edit.html.twig', [ - 'useheader' => true, - 'usesidebar' => true, - $this->data => $data, - 'mode' => 'submit', - 'form' => $form->createView() + + return $this->render('Domaine/edit.html.twig', [ + 'useheader' => true, + 'usesidebar' => true, + 'domaine' => $domaine, + 'mode' => 'submit', + 'form' => $form->createView(), ]); - } - - public function update($id,Request $request) + } + + public function update($id, Request $request) { - // Initialisation de l'enregistrement - $em = $this->getDoctrine()->getManager(); - $data=$em->getRepository($this->entity)->find($id); + $domaine = $this->domaineRepository->find($id); + if (!$domaine) { + throw new NotFoundHttpException('La ressource demandée est introuvable.'); + } - // Création du formulaire - $form = $this->createForm(Form::class,$data,array("mode"=>"update")); - - // Récupération des data du formulaire + $form = $this->createForm(DomaineType::class, $domaine, ['mode' => 'update']); $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $this->em->flush(); - // Sur erreur - $this->getErrorForm(null,$form,$request,$data,"update"); - - // Sur validation - if ($form->get('submit')->isClicked() && $form->isValid()) { - $data = $form->getData(); - $em->persist($data); - $em->flush(); - - // Retour à la liste - return $this->redirectToRoute($this->route); + return $this->redirectToRoute('app_domaine'); } - - // Affichage du formulaire - if($request->query->get('fgprint')) { - $render = $this->renderView($this->render.'edit.html.twig', [ - 'useheader' => true, - 'usesidebar' => true, - $this->data => $data, - 'mode' => 'update', - 'form' => $form->createView(), - "fgprint" => true, - ]); - return new PdfResponse( - $this->knpSnappy->getOutputFromHtml($render), - 'domaine.pdf' - ); - } - else { - return $this->render($this->render.'edit.html.twig', [ - 'useheader' => true, - 'usesidebar' => true, - $this->data => $data, - 'mode' => 'update', - 'form' => $form->createView() - ]); - } - } + return $this->render('Domaine/edit.html.twig', [ + 'useheader' => true, + 'usesidebar' => true, + 'domaine' => $domaine, + 'mode' => 'update', + 'form' => $form->createView(), + ]); + } - public function delete($id,Request $request) + public function delete($id) { - // Initialisation de l'enregistrement - $em = $this->getDoctrine()->getManager(); - $data=$em->getRepository($this->entity)->find($id); - - // Controle avant suppression - $error=false; - if($error) - return $this->redirectToRoute($this->route."_update",["id"=>$id]); - else { - try { - $em->remove($data); - $em->flush(); - } - catch(\Doctrine\DBAL\DBALException $e) { - // Création du formulaire - $this->get('session')->getFlashBag()->add('error', 'Impossible de supprimer cet enregistrement'); - return $this->redirectToRoute($this->route."_update",["id"=>$id]); - } - - // Retour à la liste - return $this->redirectToRoute($this->route); - } - } - - protected function getErrorForm($id,$form,$request,$data,$mode) { - if ($form->get('submit')->isClicked()&&$mode=="delete") { + $domaine = $this->domaineRepository->find($id); + if (!$domaine) { + throw new NotFoundHttpException('La ressource demandée est introuvable.'); } - if ($form->get('submit')->isClicked() && $mode=="submit") { + try { + $this->em->remove($domaine); + $this->em->flush(); + } catch (\Exception $e) { + $this->addflash('error', $e->getMessage()); } - if ($form->get('submit')->isClicked() && !$form->isValid()) { - $this->get('session')->getFlashBag()->clear(); - - $errors = $form->getErrors(); - foreach( $errors as $error ) { - $request->getSession()->getFlashBag()->add("error", $error->getMessage()); - } - } - } + return $this->redirectToRoute('app_domaine'); + } }