<?php
namespace App\Controller;
use App\Entity\Codinsee;
use App\Entity\DepDone;
use App\Repository\ResultResto44Repository;
use App\Entity\ResultResto44;
use App\Repository\DepartementRepository;
use App\Repository\DepDoneRepository;
use App\Repository\RestaurantRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Service\AgentService;
use App\Service\admin\AdminService;
use Symfony\Component\HttpFoundation\RequestStack;
class ValidationFicheController extends AbstractController {
private $twig;
private $restoRep;
private $agentService;
private $depRepo;
private $requestStack;
public function __construct(Environment $twig,ResultResto44Repository $rep, AgentService $agentService,AdminService $adminService,
DepartementRepository $depRepo,RequestStack $requestStack){
$this->twig=$twig;
$this->restoRep=$rep;
$this->agentService=$agentService;
$this->depRepo=$depRepo;
$this->requestStack = $requestStack;
}
/**
*@Route("/depChoice", name="choice_page", methods={"GET"})
*
*/
public function choicePage(){
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$user = $this->getUser();
$items=$this->depRepo->getDepByUseridRes($user->getId());
return new Response($this->twig->render('agent/choiceList.html.twig',["items"=>$items]));
}
/**
* @Route("/view/choice", name="view.choice")
*
*/
public function viewChoice(){
return new Response($this->twig->render('agent/viewChoice.html.twig'));
}
/**
* @Route("/show/carte/{topo}", name="carte.or.list")
*/
public function carteOrList($topo){
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$session = $this->requestStack->getSession();
$session->set('topo',$topo);
return $this->redirectToRoute('fiche_valid_rest',["topo"=>$topo]);
// if($topo == "carte"){
// $session = $this->requestStack->getSession();
// $dep=$session->get('dep');
// return new Response( $this->twig->render('agent/carte.html.twig',["dep"=>$dep]));
// }else{
// return $this->redirectToRoute('fiche_valid_rest',["topo"=>$topo]);
// }
}
/**
* @Route("/validation-carte/{codinsee}/{dep}", name="validation-carte-codinsee", methods={"GET"})
*/
public function serveCarteByCodinsee($codinsee,$dep){
return new Response(
$this->twig->render('agent/carte.html.twig',["codinsee"=>$codinsee,"dep"=>$dep])
);
}
/**
* @Route("/fiche/id/{id}",name="fiche_valid_id_dep", methods={"GET"})
*/
public function getViewById($id,RestaurantRepository $rep){
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$session = $this->requestStack->getSession();
$dep=$session->get('dep');
$codinsee=$session->get('codinsee');
$items=$rep->getFicheToValidateByIdAndDep($dep,$id);
// if($dep == "75" || $dep=="06"){
// $items=$this->agentService->getCodinseeByDep($dep);
// //dd($items);
// return new Response($this->twig->render("agent/codinsseChoose.html.twig",["items"=>$items]));
// }else{
if(count($items)>0){
$user = $this->getUser();
$deps=$this->depRepo->getDepByUseridRes($user->getId());
return new Response($this->twig->render('agent/interfaceDeValidation.html.twig',
['items'=>$items,"deps"=>$deps, "depChoiced"=>$dep,"codinsee"=>$codinsee ]));
}else{
return new Response($this->twig->render('agent/validationEnd.html.twig'));
}
}
/**
* @Route ("/pResto", name="fiche_valid_rest", methods={"GET"})
*
*/
public function firstView(){
// INSERT INTO codinsee (dep, codinsee)
// SELECT DISTINCT dep, codinsee
// FROM your_table_name
// WHERE dep = '75';
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$session = $this->requestStack->getSession();
$dep=$session->get('dep');
$topo=$session->get('topo');
$items=$this->agentService->getFicheToValidate($dep);
$deps=array("75","06","13","33","59","69","67","31","34","38","74","83");
// if(in_array($dep,$deps)){
$items=$this->agentService->getCodinseeByDep($dep);
//dd($items);
return new Response($this->twig->render("agent/codinsseChoose.html.twig",["items"=>$items,"topo"=>$topo]));
// }else{
// if(count($items)>0){
// $user = $this->getUser();
// $deps=$this->depRepo->getDepByUseridRes($user->getId());
// return new Response($this->twig->render('agent/interfaceDeValidation.html.twig',['items'=>$items,"deps"=>$deps, "depChoiced"=>$dep ]));
// }else
// return new Response($this->twig->render('agent/validationEnd.html.twig'));
// }
}
/**
* @Route("/pResto/specialDep", name="fiche_valid_for_special_rest_modif",methods={"GEt"})
*/
public function specialDep(){
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$session = $this->requestStack->getSession();
$codinsee=$session->get('codinsee');
$dep=$session->get('dep');
$items=$this->agentService->getFicheFromSpecialDepToValidate($dep,$codinsee);
if(count($items)>0){
$user = $this->getUser();
$deps=$this->depRepo->getDepByUseridRes($user->getId());
return new Response($this->twig->render('agent/interfaceDeValidation.html.twig',['items'=>$items,
"deps"=>$deps, "depChoiced"=>$dep,"codinsee"=>$codinsee ]));
}else
return new Response($this->twig->render('agent/validationEnd.html.twig'));
}
/**
* @Route ("/", name="home")
*
*/
public function homePage(){
if($this->isGranted('ROLE_ADMIN'))
return $this->redirectToRoute("admin_home");
else if($this->isGranted('ROLE_USER'))
return $this->redirectToRoute("choice_page");
else
return $this->redirectToRoute("login");
}
/**
* @Route("/validation/commune/done", name="valid.com.done", methods="POST")
*/
public function makeComDone(Request $request, DepDoneRepository $repo){
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$bodyJson=json_decode($request->getContent(),true);
$depDone=new DepDone();
$depDone->setDep($bodyJson['dep']);
$depDone->setDepName($bodyJson["nom_commune"]);
try{
$repo->add($depDone,true);
return $this->json(["message"=>"ok"],200);
}catch(\Exception $e){
return $this->json(["message"=>$e],500);
}
}
/** *
* @Route("/validation/get/commune/done/{dep}", name="get.valid.com.done", methods="get")
*/
public function getCommuneDone($dep, DepDoneRepository $repo){
$this->denyAccessUnlessGranted('ROLE_USER',null,'access denied');
$session = $this->requestStack->getSession();
$dep=$session->get('dep');
try{
$comDone=$repo->getCommDone($dep);
return $this->json($comDone,200);
}catch(\Exception $e){
return $this->json(["message"=>$e],500);
}
}
/**
* @Route("/admin/home", name="admin_home", methods="GET")
*/
public function adminHomePage(){
$this->denyAccessUnlessGranted('ROLE_ADMIN',null,'access denied');
return new Response($this->twig->render('admin/home.html.twig'));
}
}