<?php
namespace App\Controller;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class rechercheController extends AbstractController
{
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
/**
* @Route("/search")
*/
public function renderRecherchePage(Request $request)
{
// Creating a variable to store the
// server address
$ip_server = $_SERVER['SERVER_ADDR'];
//dd($ip_server);
/* $formattedAddress = "21,avenue du Maine,70015 Paris";
$response = $this->client->request('GET','https://maps.googleapis.com/maps/api/geocode/json', [
'query' => [
'address' => $formattedAddress,
'key' => 'AIzaSyCRmNV4VerjHHrsOoQOMmRWwpKL_ekzJrc'
]
]);
$data = $response->toArray();
// dd($data);*/
if (isset($_GET['latitude'])) {
$page = $_GET['page'] ?? 1; // Get the current page from the request, default to 1 if not provided
$limit = 80; // Number of results per page
$queryParams = [
//'name' => $_GET['name'],
'latitude' => $_GET['latitude'],
'longitude' => $_GET['longitude'],
'ville' => $_GET['ville'],
'page' => $page,
'limit' => $limit
];
$url = $_ENV['BASE_API'] . 'centres?' . http_build_query($queryParams);
$response = $this->client->request('GET', $url);
$data = $response->toArray();
$centresImported = $data['centerImported'];
$totalPages = $data['totalPages'];
$centres = $response->toArray()["content"];
// dd($centresImported);
}
else if (isset($_GET['address'])) {
$page = $_GET['page'] ?? 1; // Get the current page from the request, default to 1 if not provided
$limit = 80; // Number of results per page
if($_GET['address'] == "")
{
$queryParams = [
'latitude' => "48.856614",
'address' => $_GET['address'],
'longitude' => "2.3522219",
'ville' => "paris",
'page' => $page,
'limit' => $limit
];
}
else{
$queryParams = [
//'name' => $_GET['name'],
'address' => $_GET['address'],
//'longitude' => $_GET['longitude'],
// 'ville' => $_GET['ville'],
'page' => $page,
'limit' => $limit
];
}
$url = $_ENV['BASE_API'] . 'centres?' . http_build_query($queryParams);
$response = $this->client->request('GET', $url);
$data = $response->toArray();
$centresImported = $data['centerImported'];
$totalPages = $data['totalPages'];
$centres = $response->toArray()["content"];
}
else if (isset($_GET['name']) && isset($_GET['latitude'])){
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?name=".$_GET['name']."&latitude=".$_GET['latitude']."&longitude=".$_GET['longitude']."&ville=".$_GET['ville']
);
}
else {
if (isset($_GET['name'])){
/* $response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?name=".$_GET['name']
);*/
$page = $_GET['page'] ?? 1; // Get the current page from the request, default to 1 if not provided
$limit = 80; // Number of results per page
$queryParams = [
'name' => $_GET['name'],
//'latitude' => $_GET['latitude'],
//'longitude' => $_GET['longitude'],
//'ville' => $_GET['ville'],
'page' => $page,
'limit' => $limit
];
$url = $_ENV['BASE_API'] . 'centres?' . http_build_query($queryParams);
$response = $this->client->request('GET', $url);
$data = $response->toArray();
$centresImported = $data['centerImported'];
$totalPages = $data['totalPages'];
$centres = $response->toArray()["content"];
} elseif (isset($_GET['latitude'])) {
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?latitude=".$_GET['latitude']."&longitude=".$_GET['longitude']."&ville=".$_GET['ville']
);
$centres = $response->toArray()["content"];
} else {
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres"
);
$data = $response->toArray();
$totalPages = $data['totalPages'];
$centres = $response->toArray()["content"];
}
}
$centresImported = $response->toArray()["centerImported"];
// dd($response->toArray());
$resp= $this->client->request(
'GET',
"{$_ENV['BASE_API']}specialites"
);
$specialites = $resp->toArray()["specialites"];
$presta= $this->client->request(
'GET',
"{$_ENV['BASE_API']}prestations"
);
$prestations = $presta->toArray()["prestations"];
$page = $request->query->getInt('page', 1);
$limit = 10; // Number of results per page
//dd($centresImported);
return $this->render('./recherche/recherche.html.twig', array(
"centres" => $centres,
"specialites" => $specialites,
"prestations" => $prestations,
"centresImported" => $centresImported,
'page' => $page,
'limit' => $limit,
'totalPages' => $totalPages
));
}
/**
* @Route("/searchResult")
*/
public function renderRechercheResultPage()
{
$specialites = '';
$prestations ='';
if(isset($_GET['specialites'])){
$specialites = $_GET['specialites'];
}
if(isset($_GET['prestations'])){
$prestations = $_GET['prestations'];
}
if ((isset($_GET['name'])) && (isset($_GET['latitude']))){
$page = $_GET['page'] ?? 1; // Get the current page from the request, default to 1 if not provided
$limit = 80; // Number of results per page
$queryParams = [
'name' => $_GET['name'],
'specialities' => $specialites,
'prestations' => $prestations,
'isDomicile' => $_GET['isDomicile'],
'day' => $_GET['disponibilite'],
'ville' => $_GET['ville'],
'page' => $page,
'limit' => $limit
];
$url = $_ENV['BASE_API'] . 'centres?' . http_build_query($queryParams);
$response = $this->client->request('GET', $url);
$data = $response->toArray();
$centresImported = $data['centerImported'];
$totalPages = $data['totalPages'];
}
/*if ((isset($_GET['name'])) && (isset($_GET['latitude']))){
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?name=".$_GET['name']."&latitude=".$_GET['latitude']."&longitude=".$_GET['longitude']."&specialities=".$specialites."&prestations=".$prestations."&isDomicile=".$_GET['isDomicile']."&disponibilite=".$_GET['disponibilite']
);
}*/ else
/*if (isset($_GET['name'])) {
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?name=".$_GET['name']."&specialities=".$specialites."&prestations=".$prestations."&isDomicile=".$_GET['isDomicile']."&disponibilite=".$_GET['disponibilite']
);
} */
if (isset($_GET['name'])){
/* $response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?name=".$_GET['name']
);*/
$page = $_GET['page'] ?? 1; // Get the current page from the request, default to 1 if not provided
$limit = 80; // Number of results per page
$queryParams = [
'name' => $_GET['name'],
'specialities' => $specialites,
'prestations' => $prestations,
'isDomicile' => $_GET['isDomicile'],
'day' => $_GET['disponibilite'],
'ville' => $_GET['ville'],
'page' => $page,
'limit' => $limit
];
$url = $_ENV['BASE_API'] . 'centres?' . http_build_query($queryParams);
$response = $this->client->request('GET', $url);
$data = $response->toArray();
$centresImported = $data['centerImported'];
$totalPages = $data['totalPages'];
}
else
/*if (isset($_GET['latitude'])){
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?latitude=".$_GET['latitude']."&longitude=".$_GET['longitude']."&specialities=".$specialites."&prestations=".$prestations."&isDomicile=".$_GET['isDomicile']."&disponibilite=".$_GET['disponibilite']
);
} */
if (isset($_GET['latitude'])){
$page = $_GET['page'] ?? 1; // Get the current page from the request, default to 1 if not provided
$limit = 80; // Number of results per page
$queryParams = [
//'name' => $_GET['name'],
'latitude' => $_GET['latitude'],
'longitude' => $_GET['longitude'],
'specialities' => $specialites,
'prestations' => $prestations,
'isDomicile' => $_GET['isDomicile'],
'day' => $_GET['disponibilite'],
'ville' => $_GET['ville'],
'page' => $page,
'limit' => $limit
];
$url = $_ENV['BASE_API'] . 'centres?' . http_build_query($queryParams);
$response = $this->client->request('GET', $url);
$data = $response->toArray();
$centresImported = $data['centerImported'];
$totalPages = $data['totalPages'];
}
else {
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}centres?specialities=".$specialites."&prestations=".$prestations."&isDomicile=".$_GET['isDomicile']."&disponibilite=".$_GET['disponibilite']
);
}
$centres = $response->toArray()["content"];
$centresImported = $data['centerImported'];
// dd($centresImported);
// Extract the "codePostal" values into a separate array for sorting
// Sort the $centresImported array based on the "codePostal" values
$postalCodes = array_column($centresImported, 'codePostal');
//dd($postalCodes);
// array_multisort($postalCodes, SORT_ASC, $centresImported);
// dd($centresImported);
// dd($$totalPages);
return $this->render('./recherche/recherchMap.html.twig', array(
"centres" => $centres,
"centresImported" => $centresImported,
'page' => $page,
'limit' => $limit,
'totalPages' => $totalPages
));
}
}
?>