176 lines
5.7 KiB
PHP
176 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Alert;
|
|
use App\Form\AlertType;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class AlertController extends AbstractController
|
|
{
|
|
private $data = 'alert';
|
|
private $entity = "App\Entity\Alert";
|
|
private $twig = 'Alert/';
|
|
private $route = 'app_admin_alert';
|
|
|
|
public function list($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$alerts = $em->getRepository($this->entity)->findBy([], ['rowOrder' => 'asc']);
|
|
$alertcategorys = $em->getRepository('App\Entity\Alertcategory')->findAll();
|
|
$groups = $em->getRepository('App\Entity\Group')->findBy([], ['label' => 'asc']);
|
|
|
|
return $this->render($this->twig.'list.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
$this->data.'s' => $alerts,
|
|
'alertcategorys' => $alertcategorys,
|
|
'groups' => $groups,
|
|
'access' => $access,
|
|
]);
|
|
}
|
|
|
|
public function submit($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = new Alert();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(AlertType::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();
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->persist($data);
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
}
|
|
|
|
// 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(AlertType::class, $data, ['mode' => 'update', 'access' => $access]);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
// Si non masquable on s'assure qu'il n'y a pas de reader
|
|
if (!$data->getFghideable()) {
|
|
$readers = $data->getReaders();
|
|
foreach ($readers as $reader) {
|
|
$data->removeReader($reader);
|
|
}
|
|
}
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'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(str_replace('_admin_', '_'.$access.'_', $this->route).'_update', ['id' => $id]);
|
|
}
|
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
}
|
|
|
|
public function order($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$output = [];
|
|
$id = $request->request->get('id');
|
|
$order = $request->request->get('order');
|
|
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
$data->setRoworder($order);
|
|
$em->getManager()->flush();
|
|
|
|
return new JsonResponse($output);
|
|
}
|
|
|
|
public function read($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$output = [];
|
|
$id = $request->request->get('id');
|
|
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
if (!$data->getReaders()->contains($this->getUser())) {
|
|
$data->addReader($this->getUser());
|
|
$em->getManager()->flush();
|
|
}
|
|
|
|
return new JsonResponse($output);
|
|
}
|
|
}
|