141 lines
4.6 KiB
PHP
Executable File
141 lines
4.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Cadoles\CoreBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Cadoles\CoreBundle\Entity\Application;
|
|
use Cadoles\CoreBundle\Form\ApplicationType;
|
|
|
|
class ApplicationController extends Controller
|
|
{
|
|
private $labelentity = 'CadolesCoreBundle:Application';
|
|
|
|
public function listAction(Request $request)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$applications = $em->getRepository($this->labelentity)->findAll();
|
|
|
|
return $this->render('CadolesCoreBundle:Application:list.html.twig',[
|
|
"applications"=>$applications,
|
|
"useheader"=>true,
|
|
"usesidebar"=>true,
|
|
"usemenu"=>false
|
|
]);
|
|
}
|
|
|
|
public function submitAction(Request $request)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data = new Application();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(ApplicationType::class,$data,array("mode"=>"submit"));
|
|
|
|
// Récupération des data du formulaire
|
|
$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();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute("cadoles_core_config_application");
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->labelentity.':edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
'usemenu' => false,
|
|
'application' => $data,
|
|
'mode' => 'submit',
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function updateAction($id,Request $request)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data=$em->getRepository($this->labelentity)->find($id);
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(ApplicationType::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("cadoles_core_config_application");
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->labelentity.':edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
'usemenu' => false,
|
|
'application' => $data,
|
|
'mode' => 'update',
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function deleteAction($id,Request $request)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data=$em->getRepository($this->labelentity)->find($id);
|
|
$error=false;
|
|
|
|
if($error)
|
|
return $this->redirectToRoute("cadoles_core_config_application_update",["id"=>$id]);
|
|
else {
|
|
$em->remove($data);
|
|
$em->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute("cadoles_core_config_application");
|
|
}
|
|
}
|
|
|
|
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();
|
|
$validator = $this->get('validator');
|
|
$errors = $validator->validate($data);
|
|
foreach( $errors as $error ) {
|
|
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
|
}
|
|
|
|
$errors = $form->getErrors();
|
|
foreach( $errors as $error ) {
|
|
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|