326 lines
12 KiB
PHP
326 lines
12 KiB
PHP
<?php
|
|
namespace App\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use App\Entity\Niveau02 as Entity;
|
|
use App\Form\Niveau02Type as Form;
|
|
|
|
class Niveau02Controller extends AbstractController
|
|
{
|
|
private $data="niveau02";
|
|
private $entity="App\Entity\Niveau02";
|
|
private $twig="Niveau02/";
|
|
private $route="app_admin_niveau02";
|
|
|
|
public function list($access): Response
|
|
{
|
|
return $this->render($this->twig.'list.html.twig',[
|
|
"useheader"=>true,
|
|
"usemenu"=>false,
|
|
"usesidebar"=>true,
|
|
"access"=>$access,
|
|
]);
|
|
}
|
|
|
|
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'];
|
|
|
|
// Nombre total d'enregistrement
|
|
switch($access) {
|
|
case "admin":
|
|
$total = $em->getManager()->createQueryBuilder()->select('COUNT(entity)')->from($this->entity,'entity')->getQuery()->getSingleScalarResult();
|
|
break;
|
|
|
|
case "modo":
|
|
$total = $em->getManager()->createQueryBuilder()
|
|
->select('COUNT(entity)')
|
|
->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;
|
|
}
|
|
|
|
|
|
|
|
// Nombre d'enregistrement filtré
|
|
if(!$search||$search["value"]=="")
|
|
$totalf = $total;
|
|
else {
|
|
switch($access) {
|
|
case "admin":
|
|
$totalf= $em->getManager()->createQueryBuilder()
|
|
->select('COUNT(entity)')
|
|
->from($this->entity,'entity')
|
|
->from("App\Entity\Niveau01",'niveau01')
|
|
->where('entity.niveau01=niveau01.id')
|
|
->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value')
|
|
->setParameter("value", "%".$search["value"]."%")
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
break;
|
|
|
|
case "modo":
|
|
$totalf= $em->getManager()->createQueryBuilder()
|
|
->select('COUNT(entity)')
|
|
->from($this->entity,'entity')
|
|
->from("App\Entity\Niveau01",'niveau01')
|
|
->from("App\Entity\UserModo",'usermodo')
|
|
->where('entity.niveau01=niveau01.id')
|
|
->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value')
|
|
->andWhere("usermodo.niveau01 = entity.niveau01")
|
|
->andWhere("usermodo.user = :user")
|
|
->setParameter("value", "%".$search["value"]."%")
|
|
->setParameter("user", $this->getUser())
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Construction du tableau de retour
|
|
$output = array(
|
|
'draw' => $draw,
|
|
'recordsFiltered' => $totalf,
|
|
'recordsTotal' => $total,
|
|
'data' => array(),
|
|
);
|
|
|
|
// Parcours des Enregistrement
|
|
$qb = $em->getManager()->createQueryBuilder();
|
|
switch($access) {
|
|
case "admin":
|
|
$qb->select('entity')
|
|
->from($this->entity,'entity')
|
|
->from("App:Niveau01",'niveau01')
|
|
->where('entity.niveau01=niveau01.id');
|
|
break;
|
|
|
|
case "modo":
|
|
$qb->select('entity')
|
|
->from($this->entity,'entity')
|
|
->from("App:Niveau01",'niveau01')
|
|
->from("App\Entity\UserModo",'usermodo')
|
|
->where('entity.niveau01=niveau01.id')
|
|
->andWhere("usermodo.niveau01 = entity.niveau01")
|
|
->andWhere("usermodo.user = :user")
|
|
->setParameter("user", $this->getUser());
|
|
break;
|
|
}
|
|
|
|
if($search&&$search["value"]!="") {
|
|
$qb ->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value')
|
|
->setParameter("value", "%".$search["value"]."%");
|
|
}
|
|
|
|
if($ordercolumn) {
|
|
switch($ordercolumn) {
|
|
case 1 :
|
|
$qb->orderBy('niveau01.label',$orderdir);
|
|
break;
|
|
|
|
case 2 :
|
|
$qb->orderBy('entity.label',$orderdir);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$datas=$qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult();
|
|
|
|
foreach($datas as $data) {
|
|
// Action
|
|
$action = "";
|
|
switch($access) {
|
|
case "admin":
|
|
$action.="<a href='".$this->generateUrl($this->route.'_update', array('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', array('id'=>$data->getId()))."'><i class='fa fa-file fa-fw fa-2x'></i></a>";
|
|
break;
|
|
}
|
|
|
|
$tmp=array();
|
|
array_push($tmp,$action);
|
|
array_push($tmp,$data->getNiveau01()->getLabel());
|
|
array_push($tmp,$data->getLabel());
|
|
|
|
array_push($output["data"],$tmp);
|
|
}
|
|
|
|
// Retour
|
|
return new JsonResponse($output);
|
|
}
|
|
|
|
public function selectlist(Request $request,ManagerRegistry $em): Response
|
|
{
|
|
$output=array();
|
|
$page_limit=$request->query->get('page_limit');
|
|
$q=$request->query->get('q');
|
|
$niveau01id=$request->get('niveau01');
|
|
|
|
$qb = $em->getManager()->createQueryBuilder();
|
|
$qb->select('entity')
|
|
->from($this->entity,'entity')
|
|
->where('entity.label LIKE :value')
|
|
->andwhere('entity.niveau01=:niveau01')
|
|
->setParameter("value", "%".$q."%")
|
|
->setParameter("niveau01", $niveau01id)
|
|
->orderBy('entity.label');
|
|
|
|
$datas=$qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach($datas as $data) {
|
|
array_push($output,array("id"=>$data->getId(),"text"=>$data->getLabel()));
|
|
}
|
|
|
|
$ret_string["results"]=$output;
|
|
$response = new Response(json_encode($ret_string));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
return $response;
|
|
}
|
|
|
|
public function submit($access,Request $request,ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = new Entity();
|
|
$data->setApikey(Uuid::uuid4());
|
|
|
|
// Controler les permissions
|
|
$this->cansubmit($access,$em);
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class,$data,array(
|
|
"mode"=>"submit",
|
|
"access"=>$access,
|
|
"userid"=>$this->getUser()->getId(),
|
|
"appMasteridentity"=>$this->GetParameter("appMasteridentity"),
|
|
"appNiveau01label"=>$this->GetParameter("appNiveau01label"),
|
|
"appNiveau02label"=>$this->GetParameter("appNiveau02label"),
|
|
));
|
|
|
|
// 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,
|
|
"mode"=>"submit",
|
|
"access"=>$access,
|
|
"form"=>$form->createView(),
|
|
$this->data=>$data,
|
|
]);
|
|
}
|
|
|
|
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.');
|
|
|
|
// Controler les permissions
|
|
$this->canupdate($access,$data,$em);
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class,$data,array(
|
|
"mode"=>"update",
|
|
"appMasteridentity"=>$this->GetParameter("appMasteridentity"),
|
|
"appNiveau01label"=>$this->GetParameter("appNiveau01label"),
|
|
"appNiveau02label"=>$this->GetParameter("appNiveau02label"),
|
|
));
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
$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,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'access' => $access,
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function delete($id,$access,Request $request,ManagerRegistry $em): Response
|
|
{
|
|
// Récupération de l'enregistrement courant
|
|
$data=$em->getRepository($this->entity)->find($id);
|
|
if (!$data) throw $this->createNotFoundException('Unable to find entity.');
|
|
|
|
// Controler les permissions
|
|
$this->canupdate($access,$data,$em);
|
|
|
|
// 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));
|
|
}
|
|
|
|
private function cansubmit($access,$em) {
|
|
switch($access) {
|
|
case "admin" : return true; break;
|
|
case "modo" : return true; break;
|
|
}
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
}
|
|
|
|
|
|
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');
|
|
return true;
|
|
break;
|
|
}
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
}
|
|
} |