v0 ninegate
Some checks failed
Cadoles/nineskeletor/pipeline/head There was a failure building this commit
Some checks failed
Cadoles/nineskeletor/pipeline/head There was a failure building this commit
This commit is contained in:
196
src/Controller/AlertController.php
Normal file
196
src/Controller/AlertController.php
Normal file
@ -0,0 +1,196 @@
|
||||
<?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 selectlist($access, Request $request, ManagerRegistry $em): Response
|
||||
{
|
||||
$output = [];
|
||||
$page_limit = $request->query->get('page_limit');
|
||||
$q = $request->query->get('q');
|
||||
|
||||
$qb = $em->getManager()->createQueryBuilder();
|
||||
$qb->select('table')->from($this->entity, 'table')
|
||||
->where('table.title LIKE :value')
|
||||
->setParameter('value', '%'.$q.'%')
|
||||
->orderBy('table.title');
|
||||
|
||||
$datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
||||
foreach ($datas as $data) {
|
||||
array_push($output, ['id' => $data->getId(), 'text' => $data->getLabel()]);
|
||||
}
|
||||
$ret_string['results'] = $output;
|
||||
|
||||
return new JsonResponse($ret_string);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user