svg
This commit is contained in:
@@ -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();
|
||||
|
||||
if($request->query->get('fgprint')) {
|
||||
$render = $this->renderView($this->render.'list.html.twig',[
|
||||
$this->data."s" => $datas,
|
||||
"useheader" => true,
|
||||
"usesidebar" => true,
|
||||
"fgprint" => true,
|
||||
]);
|
||||
|
||||
return new PdfResponse(
|
||||
$this->knpSnappy->getOutputFromHtml($render),
|
||||
'domaines.pdf'
|
||||
);
|
||||
$this->em = $em;
|
||||
$this->domaineRepository = $domaineRepository;
|
||||
}
|
||||
else {
|
||||
return $this->render($this->render.'list.html.twig',[
|
||||
$this->data."s" => $datas,
|
||||
"useheader" => true,
|
||||
"usesidebar" => true,
|
||||
|
||||
public function list()
|
||||
{
|
||||
$domaine = $this->domaineRepository->findAll();
|
||||
|
||||
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);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->em->persist($domaine);
|
||||
$this->em->flush();
|
||||
|
||||
// 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();
|
||||
|
||||
// Retour à la liste
|
||||
return $this->redirectToRoute($this->route);
|
||||
return $this->redirectToRoute('app_domaine');
|
||||
}
|
||||
|
||||
// Affichage du formulaire
|
||||
return $this->render($this->render.'edit.html.twig', [
|
||||
return $this->render('Domaine/edit.html.twig', [
|
||||
'useheader' => true,
|
||||
'usesidebar' => true,
|
||||
$this->data => $data,
|
||||
'domaine' => $domaine,
|
||||
'mode' => 'submit',
|
||||
'form' => $form->createView()
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update($id, Request $request)
|
||||
{
|
||||
// Initialisation de l'enregistrement
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$data=$em->getRepository($this->entity)->find($id);
|
||||
|
||||
// Création du formulaire
|
||||
$form = $this->createForm(Form::class,$data,array("mode"=>"update"));
|
||||
|
||||
// Récupération des data du formulaire
|
||||
$form->handleRequest($request);
|
||||
|
||||
// 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);
|
||||
$domaine = $this->domaineRepository->find($id);
|
||||
if (!$domaine) {
|
||||
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||||
}
|
||||
|
||||
// Affichage du formulaire
|
||||
if($request->query->get('fgprint')) {
|
||||
$render = $this->renderView($this->render.'edit.html.twig', [
|
||||
$form = $this->createForm(DomaineType::class, $domaine, ['mode' => 'update']);
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->em->flush();
|
||||
|
||||
return $this->redirectToRoute('app_domaine');
|
||||
}
|
||||
|
||||
return $this->render('Domaine/edit.html.twig', [
|
||||
'useheader' => true,
|
||||
'usesidebar' => true,
|
||||
$this->data => $data,
|
||||
'domaine' => $domaine,
|
||||
'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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
$domaine = $this->domaineRepository->find($id);
|
||||
if (!$domaine) {
|
||||
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||||
}
|
||||
|
||||
// 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]);
|
||||
$this->em->remove($domaine);
|
||||
$this->em->flush();
|
||||
} catch (\Exception $e) {
|
||||
$this->addflash('error', $e->getMessage());
|
||||
}
|
||||
|
||||
// Retour à la liste
|
||||
return $this->redirectToRoute($this->route);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getErrorForm($id,$form,$request,$data,$mode) {
|
||||
if ($form->get('submit')->isClicked()&&$mode=="delete") {
|
||||
}
|
||||
|
||||
if ($form->get('submit')->isClicked() && $mode=="submit") {
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user