svg
This commit is contained in:
@@ -2,179 +2,95 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
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\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Symfony\Component\Form\FormError;
|
|
||||||
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
|
|
||||||
|
|
||||||
use App\Entity\Domaine as Entity;
|
|
||||||
use App\Form\DomaineType as Form;
|
|
||||||
|
|
||||||
class DomaineController extends AbstractController
|
class DomaineController extends AbstractController
|
||||||
{
|
{
|
||||||
private $data = "domaine";
|
private EntityManagerInterface $em;
|
||||||
private $route = "app_domaine";
|
private DomaineRepository $domaineRepository;
|
||||||
private $render = "Domaine/";
|
|
||||||
private $entity = "App:Domaine";
|
|
||||||
|
|
||||||
private $knpSnappy;
|
public function __construct(EntityManagerInterface $em, DomaineRepository $domaineRepository)
|
||||||
public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; }
|
|
||||||
|
|
||||||
public function list(Request $request)
|
|
||||||
{
|
{
|
||||||
$em = $this->getDoctrine()->getManager();
|
$this->em = $em;
|
||||||
$datas = $em->getRepository($this->entity)->findAll();
|
$this->domaineRepository = $domaineRepository;
|
||||||
|
}
|
||||||
|
|
||||||
if($request->query->get('fgprint')) {
|
public function list()
|
||||||
$render = $this->renderView($this->render.'list.html.twig',[
|
{
|
||||||
$this->data."s" => $datas,
|
$domaine = $this->domaineRepository->findAll();
|
||||||
"useheader" => true,
|
|
||||||
"usesidebar" => true,
|
|
||||||
"fgprint" => true,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return new PdfResponse(
|
return $this->render('Domaine/list.html.twig', [
|
||||||
$this->knpSnappy->getOutputFromHtml($render),
|
'domaines' => $domaine,
|
||||||
'domaines.pdf'
|
'useheader' => true,
|
||||||
);
|
'usesidebar' => true,
|
||||||
}
|
]);
|
||||||
else {
|
|
||||||
return $this->render($this->render.'list.html.twig',[
|
|
||||||
$this->data."s" => $datas,
|
|
||||||
"useheader" => true,
|
|
||||||
"usesidebar" => true,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function submit(Request $request)
|
public function submit(Request $request)
|
||||||
{
|
{
|
||||||
// Initialisation de l'enregistrement
|
$domaine = new Domaine();
|
||||||
$em = $this->getDoctrine()->getManager();
|
$form = $this->createForm(DomaineType::class, $domaine, ['mode' => 'submit']);
|
||||||
$data = new Entity();
|
|
||||||
|
|
||||||
// Création du formulaire
|
|
||||||
$form = $this->createForm(Form::class,$data,array("mode"=>"submit"));
|
|
||||||
|
|
||||||
// Récupération des data du formulaire
|
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->em->persist($domaine);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
// Sur erreur
|
return $this->redirectToRoute('app_domaine');
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Affichage du formulaire
|
return $this->render('Domaine/edit.html.twig', [
|
||||||
return $this->render($this->render.'edit.html.twig', [
|
'useheader' => true,
|
||||||
'useheader' => true,
|
'usesidebar' => true,
|
||||||
'usesidebar' => true,
|
'domaine' => $domaine,
|
||||||
$this->data => $data,
|
'mode' => 'submit',
|
||||||
'mode' => 'submit',
|
'form' => $form->createView(),
|
||||||
'form' => $form->createView()
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update($id,Request $request)
|
public function update($id, Request $request)
|
||||||
{
|
{
|
||||||
// Initialisation de l'enregistrement
|
$domaine = $this->domaineRepository->find($id);
|
||||||
$em = $this->getDoctrine()->getManager();
|
if (!$domaine) {
|
||||||
$data=$em->getRepository($this->entity)->find($id);
|
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||||||
|
}
|
||||||
|
|
||||||
// Création du formulaire
|
$form = $this->createForm(DomaineType::class, $domaine, ['mode' => 'update']);
|
||||||
$form = $this->createForm(Form::class,$data,array("mode"=>"update"));
|
|
||||||
|
|
||||||
// Récupération des data du formulaire
|
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
// Sur erreur
|
return $this->redirectToRoute('app_domaine');
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Affichage du formulaire
|
return $this->render('Domaine/edit.html.twig', [
|
||||||
if($request->query->get('fgprint')) {
|
'useheader' => true,
|
||||||
$render = $this->renderView($this->render.'edit.html.twig', [
|
'usesidebar' => true,
|
||||||
'useheader' => true,
|
'domaine' => $domaine,
|
||||||
'usesidebar' => true,
|
'mode' => 'update',
|
||||||
$this->data => $data,
|
'form' => $form->createView(),
|
||||||
'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
|
$domaine = $this->domaineRepository->find($id);
|
||||||
$em = $this->getDoctrine()->getManager();
|
if (!$domaine) {
|
||||||
$data=$em->getRepository($this->entity)->find($id);
|
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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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") {
|
try {
|
||||||
|
$this->em->remove($domaine);
|
||||||
|
$this->em->flush();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->addflash('error', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($form->get('submit')->isClicked() && !$form->isValid()) {
|
return $this->redirectToRoute('app_domaine');
|
||||||
$this->get('session')->getFlashBag()->clear();
|
|
||||||
|
|
||||||
$errors = $form->getErrors();
|
|
||||||
foreach( $errors as $error ) {
|
|
||||||
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user