src/Controller/HomeController.php line 173
<?php
namespace App\Controller;
use App\Data\HomeData;
use App\Form\HomeForm;
use App\Data\SearchData;
use App\Form\SearchForm;
use App\Repository\ShopRepository;
use App\Repository\CategoryRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
class HomeController extends AbstractController
{
/**
* route par défaut
* @Route("/", name="default")
* @Route("/home", name="app_home_index")
*
* @return Response
*/
public function index(ShopRepository $shopRepository, CategoryRepository $categoryRepository, Request $request, $id = null): Response
{
$data = new SearchData();
$form = $this->createForm(SearchForm::class, $data, [
'action' => $this->generateUrl('app_home_list'),
'method' => 'POST',
]);
// Récupérer les informations pour la page
$offset = 0; // position de départ
$limit = 6; // nombre d'éléments à afficher
$allCategory = $categoryRepository->findBy(
[],
["name" => "ASC"],
$limit, // nombre maximal d'éléments à retourner
$offset // position de départ
);
$shop = $shopRepository->findAll();
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
"allCategory" => $allCategory,
"shopForView" => $shop,
'form' => $form->createView(),
]);
}
/**
* route par défaut
* @Route("/listing", name="app_home_list")
* @Route("/listing/{id}", name="app_home_list_category", requirements={"id"="\d+"})
*
* @return Response
*/
public function list(ShopRepository $shopRepository, CategoryRepository $categoryRepository, Request $request, $id = null): Response
{
$data = new SearchData();
if ($id !== null){
// j'ai reçu un ID de la route
$category = $categoryRepository->find($id);
// je le met dans le data pour qu'il soit "dans la recherche"
$data->Category = [$category];
}
$form = $this->createForm(SearchForm::class, $data);
$form->handleRequest($request);
//dd($data);
$sortBy = $request->query->get('sortby');
if ($sortBy == 'newest') {
$shops = $shopRepository->findBy([], ['createdAt' => 'DESC']);
} elseif ($sortBy == 'oldest') {
$shops = $shopRepository->findBy([], ['createdAt' => 'ASC']);
} else {
$shops = $shopRepository->findSearch($data);
}
$allShops = $shopRepository->findBy(['available' => true]);;
$allCategories = $categoryRepository->findAll();
$apiKey = 'AIzaSyCM1n8TZCulTKvvFnaznr_sEodEXxrL44A'; // Clé API Google Places
$placeIds = [];
foreach ($allShops as $shop) {
$placeIds[] = $shop->getMyBusiness();
}
foreach ($placeIds as $placeId) {
$apiUrl = "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$apiKey&language=fr";
// Effectuer votre requête API et le traitement des données ici
}
//dd($placeIds);
// Utilisation de cURL pour effectuer la requête HTTP
$httpClient = HttpClient::create();
$response = $httpClient->request('GET', $apiUrl);
$data = json_decode($response->getContent(), true);
//dd($allShops);
return $this->render('home/listing.html.twig', [
'allShops' => $allShops,
'allCategories' => $allCategories,
'form' => $form->createView(),
'shops' => $shops,
"apiData" => $data,
]);
}
/**
* route par défaut
* @Route("/listing-grid", name="app_home_list_grid")
* @Route("/listing-grid/{id}", name="app_home_list_category_grid", requirements={"id"="\d+"})
*
* @return Response
*/
public function listGrid(ShopRepository $shopRepository, CategoryRepository $categoryRepository, Request $request, $id = null): Response
{
$data = new SearchData();
if ($id !== null){
// j'ai reçu un ID de la route
$category = $categoryRepository->find($id);
// je le met dans le data pour qu'il soit "dans la recherche"
$data->Category = [$category];
}
$form = $this->createForm(SearchForm::class, $data);
$form->handleRequest($request);
//dd($data);
$sortBy = $request->query->get('sortby');
if ($sortBy == 'newest') {
$shops = $shopRepository->findBy([], ['createdAt' => 'DESC']);
} elseif ($sortBy == 'oldest') {
$shops = $shopRepository->findBy([], ['createdAt' => 'ASC']);
} else {
$shops = $shopRepository->findSearch($data);
}
$allShops = $shopRepository->findAll();
$allCategories = $categoryRepository->findAll();
//dd($allShops);
return $this->render('home/listingGrid.html.twig', [
'allShops' => $allShops,
'allCategories' => $allCategories,
'form' => $form->createView(),
'shops' => $shops,
]);
}
// TODO : route show : doit afficher le détails d'un commerce
/**
* affichage des détails d'un commerce
*
* @Route("/commerce/{slug}",name="app_home_show")
* @return Response
*/
public function show($slug, ShopRepository $shopRepository, CategoryRepository $categoryRepository): Response
{
$allCategories = $categoryRepository->findAll();
//dump($allCategories);
$shop = $shopRepository->findOneBy(['slug' => $slug]);
$categories = $shop->getCategories();
$categories->initialize();
$categories->toArray();
//dump($categories);
$offset = 0; // position de départ
$limit = 2; // nombre d'éléments à afficher
$results = [];
foreach ($allCategories as $cat) {
if (in_array($cat->getName(), $shop->getCategories()->toArray())) {
$cat->getShops()->initialize();
$results[] = $cat->getShops();
}
}
$mainCategory = $results[0];
//dump($mainCategory);
for ($i = 0; $i < min(count($mainCategory), $limit); $i++) {
$twoShops[] = $mainCategory[$i];
}
//TODO Pour chaque catégorie, si $categories === catégorie name, return les shops, sinon, continuer
/* $twoShops = $mainCategory->findBy(
[],
["name" => "ASC"],
$limit, // nombre maximal d'éléments à retourner
$offset // position de départ
);
*/
//dump($categoriesTwo);
// TODO si le commerce n'existe pas, je dois renvoyer vers une 404.
if ($shop === null) {
throw $this->createNotFoundException("Le commerce n'existe pas");
}
$apiKey = 'AIzaSyCM1n8TZCulTKvvFnaznr_sEodEXxrL44A'; // Clé API Google Places
// Récupération de l'ID du commerce
$placeId = $shop->getMyBusiness(); // Assure-toi d'avoir la méthode getMyBusiness() dans l'entité Shop
// Construction de l'URL de l'API Google Places
$apiUrl = "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$apiKey&language=fr";
// Utilisation de cURL pour effectuer la requête HTTP
$httpClient = HttpClient::create();
$response = $httpClient->request('GET', $apiUrl);
$data = json_decode($response->getContent(), true);
//dd($shop);
return $this->render("home/show.html.twig",
[
"shopId" => $slug,
"shopForView" => $shop,
'twoShops' => $twoShops,
"allCategories" => $allCategories,
"apiData" => $data,
]);
}
}