125 lines
4.2 KiB
PHP
125 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Alertcategory;
|
|
use App\Form\AlertcategoryType;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class AlertcategoryController extends AbstractController
|
|
{
|
|
private $data = 'alertcategory';
|
|
private $entity = "App\Entity\Alertcategory";
|
|
private $twig = 'Alertcategory/';
|
|
private $route = 'app_admin_alertcategory';
|
|
|
|
public function submit($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = new Alertcategory();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(AlertcategoryType::class, $data, ['mode' => 'submit', 'access' => $access]);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
$idicon = $form->get('idicon')->getData();
|
|
$icon = $em->getRepository('App\Entity\Icon')->findoneby(['id' => $idicon]);
|
|
$data->setIcon($icon);
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->persist($data);
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', 'app_admin_alert'));
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
$this->data => $data,
|
|
'mode' => 'submit',
|
|
'icons' => $em->getRepository('App\Entity\Icon')->findAll(),
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function update($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(AlertcategoryType::class, $data, [
|
|
'mode' => 'update',
|
|
'idicon' => ($data->getIcon() ? $data->getIcon()->getId() : null),
|
|
]);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
$idicon = $form->get('idicon')->getData();
|
|
$icon = $em->getRepository('App\Entity\Icon')->findoneby(['id' => $idicon]);
|
|
$data->setIcon($icon);
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', 'app_admin_alert'));
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'icons' => $em->getRepository('App\Entity\Icon')->findAll(),
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function delete($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Tentative de suppression
|
|
try {
|
|
$em->getManager()->remove($data);
|
|
$em->getManager()->flush();
|
|
} catch (\Exception $e) {
|
|
$request->getSession()->getFlashBag()->add('error', $e->getMessage());
|
|
|
|
return $this->redirectToRoute($this->route.'_update', ['id' => $id]);
|
|
}
|
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', 'app_admin_alert'));
|
|
}
|
|
}
|