<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Service\PlatformService;
use Firebase\JWT\JWT;
/**
* Class CartoController
* @package App\Controller
*/
class CartoController extends AbstractController
{
/**
* @param Request $request
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @Route("/", name="home")
*/
public function renderInterface(Request $request) : Response
{
$atlasParams = $this->getParameter('atlasparam');
return $this->render('index.html.twig', [
'atlas' => $atlasParams
]
);
}
/**
* @param Request $request
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @Route("/query", name="query")
*/
public function searchQuery(Request $request, PlatformService $platformService) : Response
{
$q = $request->query->get('q');
$dataCompCommune = $this->getParameter('dc_commune');
$q = str_replace("'", "''", $q);
$query = "select \"id\", \"com_nom\", \"code_dep\" from com_dept where REPLACE(unaccent(LOWER(com_nom)), '-', ' ') like REPLACE(unaccent(LOWER('".$q."%')), '-', ' ')";
$response = $platformService->callApiDC($dataCompCommune, $query);
$output['results'] = [];
foreach($response as $data){
$output['results'][] = ['id' => $data[0], 'text' => $data[1] . " (". $data[2] .")", 'search' => $data[2]];
}
$output['pagination'] = ["more" => false ];
return new JsonResponse($output);
}
/**
* @param Request $request
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @Route("/carto", name="carto")
*/
public function renderInterfaceCarto(Request $request, PlatformService $platformService) : Response
{
$atlasParams = $this->getParameter('atlasparam');
$token = $this->getParameter('token');
$atlasParams['token'] = $token;
$atlasParams['search'] = $request->query->get('q');
$dataCompBase = $this->getParameter('dc_base');
$query ="select \"code_insee\", \"Name\" ,\"code_dep\" from \"BA_DA_EAR\"";
$response = $platformService->callApiDC($dataCompBase, $query);
$output = [];
$output['bases'] = [];
foreach($response as $data){
$output['bases'][] = ['id' => $data[0], 'text' => $data[1], 'search' => $data[2]];
}
$dataCompSSAFem = $this->getParameter('dc_ssa_fem');
$query2 = "select \"Code Insee /FEM-Geo\", \"Libelle Long /FEM\", code_dep from \"Sites_SSA_FEM\" order by \"Libelle Long /FEM\"";
$response2 = $platformService->callApiDC($dataCompSSAFem, $query2);
$output['fem'] = [];
foreach($response2 as $data){
$output['fem'][] = ['id' => $data[0], 'text' => $data[1], 'search' => $data[2]];
}
$dataCompSSAFer = $this->getParameter('dc_ssa_fer');
$query3 = "select \"Code Insee /FER-Geo\", \"Libelle Long /FER\", code_dep from \"Sites_SSA_FER\" order by \"Libelle Long /FER\"";
$response3 = $platformService->callApiDC($dataCompSSAFer, $query3);
$output['fer'] = [];
foreach($response3 as $data){
$output['fer'][] = ['id' => $data[0], 'text' => $data[1], 'search' => $data[2]];
}
return $this->render('carto.html.twig',
[
'atlas' => $atlasParams,
'bases' => json_encode($output['bases']),
'ssa_fem' => json_encode($output['fem']),
'ssa_fer' => json_encode($output['fer'])
]);
}
/**
* @param Request $request
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @Route("/sources", name="sources")
*/
public function renderSourcesPage(Request $request) : Response
{
return $this->render('sources.html.twig');
}
}