2022-07-21 16:15:47 +02:00
|
|
|
<?php
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
use App\Entity\Niveau02 as Entity;
|
|
|
|
use App\Form\Niveau02Type as Form;
|
|
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2022-07-21 16:15:47 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2022-09-23 16:14:15 +02:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2022-07-21 16:15:47 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
class Niveau02Controller extends AbstractController
|
|
|
|
{
|
2022-09-23 16:14:15 +02:00
|
|
|
private $data = 'niveau02';
|
|
|
|
private $entity = "App\Entity\Niveau02";
|
|
|
|
private $twig = 'Niveau02/';
|
|
|
|
private $route = 'app_admin_niveau02';
|
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
public function list($access): Response
|
2022-09-23 16:14:15 +02:00
|
|
|
{
|
|
|
|
return $this->render($this->twig.'list.html.twig', [
|
|
|
|
'useheader' => true,
|
|
|
|
'usemenu' => false,
|
|
|
|
'usesidebar' => true,
|
|
|
|
'access' => $access,
|
2022-07-21 16:15:47 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function tablelist($access, Request $request, ManagerRegistry $em): Response
|
|
|
|
{
|
|
|
|
$query = $request->query->all();
|
|
|
|
$start = $query['start'];
|
|
|
|
$length = $query['length'];
|
|
|
|
$search = $query['search'];
|
|
|
|
$draw = $query['draw'];
|
|
|
|
$ordercolumn = $query['order'][0]['column'];
|
|
|
|
$orderdir = $query['order'][0]['dir'];
|
|
|
|
$total = null;
|
|
|
|
$totalf = null;
|
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Nombre total d'enregistrement
|
2022-09-23 16:14:15 +02:00
|
|
|
switch ($access) {
|
|
|
|
case 'admin':
|
|
|
|
$total = $em->getManager()->createQueryBuilder()->select('COUNT(entity)')->from($this->entity, 'entity')->getQuery()->getSingleScalarResult();
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'modo':
|
2022-07-21 16:15:47 +02:00
|
|
|
$total = $em->getManager()->createQueryBuilder()
|
|
|
|
->select('COUNT(entity)')
|
2022-09-23 16:14:15 +02:00
|
|
|
->from($this->entity, 'entity')
|
|
|
|
->from("App\Entity\UserModo", 'usermodo')
|
|
|
|
->where('usermodo.niveau01 = entity.niveau01')
|
|
|
|
->andWhere('usermodo.user = :user')
|
|
|
|
->setParameter('user', $this->getUser())
|
|
|
|
->getQuery()->getSingleScalarResult();
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nombre d'enregistrement filtré
|
2022-09-23 16:14:15 +02:00
|
|
|
if (!$search || '' == $search['value']) {
|
2022-07-21 16:15:47 +02:00
|
|
|
$totalf = $total;
|
2022-09-23 16:14:15 +02:00
|
|
|
} else {
|
|
|
|
switch ($access) {
|
|
|
|
case 'admin':
|
|
|
|
$totalf = $em->getManager()->createQueryBuilder()
|
2022-07-21 16:15:47 +02:00
|
|
|
->select('COUNT(entity)')
|
2022-09-23 16:14:15 +02:00
|
|
|
->from($this->entity, 'entity')
|
|
|
|
->from("App\Entity\Niveau01", 'niveau01')
|
2022-07-21 16:15:47 +02:00
|
|
|
->where('entity.niveau01=niveau01.id')
|
|
|
|
->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value')
|
2022-09-23 16:14:15 +02:00
|
|
|
->setParameter('value', '%'.$search['value'].'%')
|
2022-07-21 16:15:47 +02:00
|
|
|
->getQuery()
|
|
|
|
->getSingleScalarResult();
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'modo':
|
|
|
|
$totalf = $em->getManager()->createQueryBuilder()
|
2022-07-21 16:15:47 +02:00
|
|
|
->select('COUNT(entity)')
|
2022-09-23 16:14:15 +02:00
|
|
|
->from($this->entity, 'entity')
|
|
|
|
->from("App\Entity\Niveau01", 'niveau01')
|
|
|
|
->from("App\Entity\UserModo", 'usermodo')
|
2022-07-21 16:15:47 +02:00
|
|
|
->where('entity.niveau01=niveau01.id')
|
|
|
|
->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value')
|
2022-09-23 16:14:15 +02:00
|
|
|
->andWhere('usermodo.niveau01 = entity.niveau01')
|
|
|
|
->andWhere('usermodo.user = :user')
|
|
|
|
->setParameter('value', '%'.$search['value'].'%')
|
|
|
|
->setParameter('user', $this->getUser())
|
2022-07-21 16:15:47 +02:00
|
|
|
->getQuery()
|
|
|
|
->getSingleScalarResult();
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construction du tableau de retour
|
2022-09-23 16:14:15 +02:00
|
|
|
$output = [
|
2022-07-21 16:15:47 +02:00
|
|
|
'draw' => $draw,
|
|
|
|
'recordsFiltered' => $totalf,
|
|
|
|
'recordsTotal' => $total,
|
2022-09-23 16:14:15 +02:00
|
|
|
'data' => [],
|
|
|
|
];
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Parcours des Enregistrement
|
|
|
|
$qb = $em->getManager()->createQueryBuilder();
|
2022-09-23 16:14:15 +02:00
|
|
|
switch ($access) {
|
|
|
|
case 'admin':
|
2022-07-21 16:15:47 +02:00
|
|
|
$qb->select('entity')
|
2022-09-23 16:14:15 +02:00
|
|
|
->from($this->entity, 'entity')
|
|
|
|
->from('App:Niveau01', 'niveau01')
|
2022-07-21 16:15:47 +02:00
|
|
|
->where('entity.niveau01=niveau01.id');
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'modo':
|
2022-07-21 16:15:47 +02:00
|
|
|
$qb->select('entity')
|
2022-09-23 16:14:15 +02:00
|
|
|
->from($this->entity, 'entity')
|
|
|
|
->from('App:Niveau01', 'niveau01')
|
|
|
|
->from("App\Entity\UserModo", 'usermodo')
|
2022-07-21 16:15:47 +02:00
|
|
|
->where('entity.niveau01=niveau01.id')
|
2022-09-23 16:14:15 +02:00
|
|
|
->andWhere('usermodo.niveau01 = entity.niveau01')
|
|
|
|
->andWhere('usermodo.user = :user')
|
|
|
|
->setParameter('user', $this->getUser());
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
if ($search && '' != $search['value']) {
|
|
|
|
$qb->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value')
|
|
|
|
->setParameter('value', '%'.$search['value'].'%');
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
if ($ordercolumn) {
|
|
|
|
switch ($ordercolumn) {
|
|
|
|
case 1:
|
|
|
|
$qb->orderBy('niveau01.label', $orderdir);
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 2:
|
|
|
|
$qb->orderBy('entity.label', $orderdir);
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
$datas = $qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult();
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
foreach ($datas as $data) {
|
2022-07-21 16:15:47 +02:00
|
|
|
// Action
|
2022-09-23 16:14:15 +02:00
|
|
|
$action = '';
|
|
|
|
switch ($access) {
|
|
|
|
case 'admin':
|
|
|
|
$action .= "<a href='".$this->generateUrl($this->route.'_update', ['id' => $data->getId()])."'><i class='fa fa-file fa-fw fa-2x'></i></a>";
|
|
|
|
break;
|
|
|
|
case 'modo':
|
|
|
|
$action .= "<a href='".$this->generateUrl(str_replace('_admin_', '_modo_', $this->route).'_update', ['id' => $data->getId()])."'><i class='fa fa-file fa-fw fa-2x'></i></a>";
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
$tmp = [];
|
|
|
|
array_push($tmp, $action);
|
|
|
|
array_push($tmp, $data->getNiveau01()->getLabel());
|
|
|
|
array_push($tmp, $data->getLabel());
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
array_push($output['data'], $tmp);
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retour
|
|
|
|
return new JsonResponse($output);
|
2022-09-23 16:14:15 +02:00
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function selectlist(Request $request, ManagerRegistry $em): Response
|
2022-07-21 16:15:47 +02:00
|
|
|
{
|
2022-09-23 16:14:15 +02:00
|
|
|
$output = [];
|
|
|
|
$page_limit = $request->query->get('page_limit');
|
|
|
|
$q = $request->query->get('q');
|
|
|
|
$niveau01id = $request->get('niveau01');
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
$qb = $em->getManager()->createQueryBuilder();
|
|
|
|
$qb->select('entity')
|
2022-09-23 16:14:15 +02:00
|
|
|
->from($this->entity, 'entity')
|
2022-07-21 16:15:47 +02:00
|
|
|
->where('entity.label LIKE :value')
|
|
|
|
->andwhere('entity.niveau01=:niveau01')
|
2022-09-23 16:14:15 +02:00
|
|
|
->setParameter('value', '%'.$q.'%')
|
|
|
|
->setParameter('niveau01', $niveau01id)
|
2022-07-21 16:15:47 +02:00
|
|
|
->orderBy('entity.label');
|
2022-09-23 16:14:15 +02:00
|
|
|
|
|
|
|
$datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
|
|
foreach ($datas as $data) {
|
|
|
|
array_push($output, ['id' => $data->getId(), 'text' => $data->getLabel()]);
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
$ret_string['results'] = $output;
|
|
|
|
$response = new Response(json_encode($ret_string));
|
|
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
return $response;
|
2022-09-23 16:14:15 +02:00
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function submit($access, Request $request, ManagerRegistry $em): Response
|
2022-07-21 16:15:47 +02:00
|
|
|
{
|
|
|
|
// Initialisation de l'enregistrement
|
|
|
|
$data = new Entity();
|
|
|
|
$data->setApikey(Uuid::uuid4());
|
|
|
|
|
|
|
|
// Controler les permissions
|
2022-09-23 16:14:15 +02:00
|
|
|
$this->cansubmit($access, $em);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Création du formulaire
|
2022-09-23 16:14:15 +02:00
|
|
|
$form = $this->createForm(Form::class, $data, [
|
|
|
|
'mode' => 'submit',
|
|
|
|
'access' => $access,
|
|
|
|
'userid' => $this->getUser()->getId(),
|
|
|
|
'appMasteridentity' => $this->GetParameter('appMasteridentity'),
|
|
|
|
'appNiveau01label' => $this->GetParameter('appNiveau01label'),
|
|
|
|
'appNiveau02label' => $this->GetParameter('appNiveau02label'),
|
|
|
|
]);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Récupération des data du formulaire
|
|
|
|
$form->handleRequest($request);
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Sur validation
|
2022-09-23 16:14:15 +02:00
|
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
|
|
$data = $form->getData();
|
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Sauvegarde
|
|
|
|
$em->getManager()->persist($data);
|
|
|
|
$em->getManager()->flush();
|
|
|
|
|
|
|
|
// Retour à la liste
|
2022-09-23 16:14:15 +02:00
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Affichage du formulaire
|
|
|
|
return $this->render($this->twig.'edit.html.twig', [
|
2022-09-23 16:14:15 +02:00
|
|
|
'useheader' => true,
|
|
|
|
'usemenu' => false,
|
|
|
|
'usesidebar' => true,
|
|
|
|
'mode' => 'submit',
|
|
|
|
'access' => $access,
|
|
|
|
'form' => $form->createView(),
|
|
|
|
$this->data => $data,
|
2022-07-21 16:15:47 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function update($id, $access, Request $request, ManagerRegistry $em): Response
|
2022-07-21 16:15:47 +02:00
|
|
|
{
|
|
|
|
// Initialisation de l'enregistrement
|
2022-09-23 16:14:15 +02:00
|
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
|
|
if (!$data) {
|
|
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Controler les permissions
|
2022-09-23 16:14:15 +02:00
|
|
|
$this->canupdate($access, $data, $em);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Création du formulaire
|
2022-09-23 16:14:15 +02:00
|
|
|
$form = $this->createForm(Form::class, $data, [
|
|
|
|
'mode' => 'update',
|
|
|
|
'appMasteridentity' => $this->GetParameter('appMasteridentity'),
|
|
|
|
'appNiveau01label' => $this->GetParameter('appNiveau01label'),
|
|
|
|
'appNiveau02label' => $this->GetParameter('appNiveau02label'),
|
|
|
|
]);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Récupération des data du formulaire
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
// Sur validation
|
2022-09-23 16:14:15 +02:00
|
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
|
|
$data = $form->getData();
|
2022-07-21 16:15:47 +02:00
|
|
|
$em->getManager()->flush();
|
|
|
|
|
|
|
|
// Retour à la liste
|
2022-09-23 16:14:15 +02:00
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Affichage du formulaire
|
|
|
|
return $this->render($this->twig.'edit.html.twig', [
|
2022-09-23 16:14:15 +02:00
|
|
|
'useheader' => true,
|
|
|
|
'usemenu' => false,
|
|
|
|
'usesidebar' => true,
|
|
|
|
$this->data => $data,
|
|
|
|
'mode' => 'update',
|
|
|
|
'access' => $access,
|
|
|
|
'form' => $form->createView(),
|
2022-07-21 16:15:47 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function delete($id, $access, Request $request, ManagerRegistry $em): Response
|
2022-07-21 16:15:47 +02:00
|
|
|
{
|
|
|
|
// Récupération de l'enregistrement courant
|
2022-09-23 16:14:15 +02:00
|
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
|
|
if (!$data) {
|
|
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Controler les permissions
|
2022-09-23 16:14:15 +02:00
|
|
|
$this->canupdate($access, $data, $em);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Tentative de suppression
|
2022-09-23 16:14:15 +02:00
|
|
|
try {
|
2022-07-21 16:15:47 +02:00
|
|
|
$em->getManager()->remove($data);
|
|
|
|
$em->getManager()->flush();
|
2022-09-23 16:14:15 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$request->getSession()->getFlashBag()->add('error', $e->getMessage());
|
|
|
|
|
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route).'_update', ['id' => $id]);
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cansubmit($access, $em)
|
|
|
|
{
|
|
|
|
switch ($access) {
|
|
|
|
case 'admin': return true;
|
|
|
|
break;
|
|
|
|
case 'modo': return true;
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
private function canupdate($access, $entity, $em)
|
|
|
|
{
|
|
|
|
switch ($access) {
|
|
|
|
case 'admin': return true;
|
|
|
|
break;
|
|
|
|
case 'modo':
|
|
|
|
$usermodo = $em->getRepository("App\Entity\UserModo")->findOneBy(['user' => $this->getUser(), 'niveau01' => $entity->getNiveau01()]);
|
|
|
|
if (!$usermodo) {
|
|
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
return true;
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
}
|