src/Controller/HomeController.php line 65

  1. <?php
  2. namespace App\Controller;
  3. use App\Data\HomeData;
  4. use App\Form\HomeForm;
  5. use App\Data\SearchData;
  6. use App\Form\SearchForm;
  7. use App\Repository\ShopRepository;
  8. use App\Repository\CategoryRepository;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpClient\HttpClient;
  14. class HomeController extends AbstractController
  15. {
  16.     /**
  17.      * route par défaut
  18.      * @Route("/", name="default")
  19.      * @Route("/home", name="app_home_index")
  20.      * 
  21.      * @return Response
  22.      */
  23.     public function index(ShopRepository $shopRepositoryCategoryRepository $categoryRepositoryRequest $request$id null): Response
  24.     {
  25.         $data = new SearchData();
  26.         $form $this->createForm(SearchForm::class, $data, [
  27.             'action' => $this->generateUrl('app_home_list'),
  28.             'method' => 'POST',
  29.         ]);
  30.         
  31.         // Récupérer les informations pour la page
  32.         $offset 0// position de départ
  33.         $limit 6// nombre d'éléments à afficher
  34.         $allCategory $categoryRepository->findBy(
  35.             [], 
  36.             ["name" => "ASC"], 
  37.             $limit// nombre maximal d'éléments à retourner
  38.             $offset // position de départ
  39.         );
  40.         $shop $shopRepository->findAll();
  41.         return $this->render('home/index.html.twig', [
  42.             'controller_name' => 'HomeController',
  43.             "allCategory" => $allCategory,
  44.             "shopForView" => $shop,
  45.             'form' => $form->createView(),
  46.         ]);
  47.     }
  48.      /**
  49.      * route par défaut
  50.      * @Route("/listing", name="app_home_list")
  51.      * @Route("/listing/{id}", name="app_home_list_category", requirements={"id"="\d+"})
  52.      * 
  53.      * @return Response
  54.      */
  55.     public function list(ShopRepository $shopRepositoryCategoryRepository $categoryRepositoryRequest $request$id null): Response
  56.     {
  57.         $data = new SearchData();
  58.         if ($id !== null){
  59.             // j'ai reçu un ID de la route
  60.             $category $categoryRepository->find($id);
  61.             // je le met dans le data pour qu'il soit "dans la recherche"
  62.             $data->Category = [$category];
  63.         }
  64.     
  65.         $form $this->createForm(SearchForm::class, $data);
  66.         $form->handleRequest($request);
  67.         //dd($data);
  68.     
  69.         $sortBy $request->query->get('sortby');
  70.     
  71.         if ($sortBy == 'newest') {
  72.             $shops $shopRepository->findBy([], ['createdAt' => 'DESC']);
  73.         } elseif ($sortBy == 'oldest') {
  74.             $shops $shopRepository->findBy([], ['createdAt' => 'ASC']);
  75.         } else {
  76.             $shops $shopRepository->findSearch($data);
  77.         }
  78.     
  79.         $allShops $shopRepository->findBy(['available' => true]);;
  80.         $allCategories $categoryRepository->findAll();
  81.         $apiKey 'AIzaSyCM1n8TZCulTKvvFnaznr_sEodEXxrL44A'// Clé API Google Places
  82.     
  83.         $placeIds = [];
  84.         foreach ($allShops as $shop) {
  85.             $placeIds[] = $shop->getMyBusiness();
  86.         }
  87.         foreach ($placeIds as $placeId) {
  88.             $apiUrl "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$apiKey&language=fr";
  89.             // Effectuer votre requête API et le traitement des données ici
  90.         }
  91.         //dd($placeIds);
  92.     
  93.         // Utilisation de cURL pour effectuer la requête HTTP
  94.         $httpClient HttpClient::create();
  95.         $response $httpClient->request('GET'$apiUrl);
  96.         $data json_decode($response->getContent(), true);
  97.     
  98.         //dd($allShops);
  99.         return $this->render('home/listing.html.twig', [
  100.             'allShops' => $allShops,
  101.             'allCategories' => $allCategories,
  102.             'form' => $form->createView(),
  103.             'shops' => $shops,
  104.             "apiData" => $data,
  105.         ]);
  106.     }
  107.     /**
  108.      * route par défaut
  109.      * @Route("/listing-grid", name="app_home_list_grid")
  110.      * @Route("/listing-grid/{id}", name="app_home_list_category_grid", requirements={"id"="\d+"})
  111.      * 
  112.      * @return Response
  113.      */
  114.     public function listGrid(ShopRepository $shopRepositoryCategoryRepository $categoryRepositoryRequest $request$id null): Response
  115.     {
  116.         $data = new SearchData();
  117.         if ($id !== null){
  118.             // j'ai reçu un ID de la route
  119.             $category $categoryRepository->find($id);
  120.             // je le met dans le data pour qu'il soit "dans la recherche"
  121.             $data->Category = [$category];
  122.         }
  123.     
  124.         $form $this->createForm(SearchForm::class, $data);
  125.         $form->handleRequest($request);
  126.         //dd($data);
  127.     
  128.         $sortBy $request->query->get('sortby');
  129.     
  130.         if ($sortBy == 'newest') {
  131.             $shops $shopRepository->findBy([], ['createdAt' => 'DESC']);
  132.         } elseif ($sortBy == 'oldest') {
  133.             $shops $shopRepository->findBy([], ['createdAt' => 'ASC']);
  134.         } else {
  135.             $shops $shopRepository->findSearch($data);
  136.         }
  137.     
  138.         $allShops $shopRepository->findAll();
  139.         $allCategories $categoryRepository->findAll();
  140.         //dd($allShops);
  141.         return $this->render('home/listingGrid.html.twig', [
  142.             'allShops' => $allShops,
  143.             'allCategories' => $allCategories,
  144.             'form' => $form->createView(),
  145.             'shops' => $shops,
  146.         ]);
  147.     }
  148.     
  149.     
  150.     // TODO : route show : doit afficher le détails d'un commerce
  151.     /**
  152.      * affichage des détails d'un commerce
  153.      *
  154.      * @Route("/commerce/{slug}",name="app_home_show")
  155.      * @return Response
  156.      */
  157.     public function show($slugShopRepository $shopRepositoryCategoryRepository $categoryRepository): Response
  158.     {
  159.         $allCategories $categoryRepository->findAll();
  160.         //dump($allCategories);
  161.         $shop $shopRepository->findOneBy(['slug' => $slug]);
  162.         $categories $shop->getCategories();
  163.         $categories->initialize();
  164.         $categories->toArray();
  165.         //dump($categories);
  166.         $offset 0// position de départ
  167.         $limit 2// nombre d'éléments à afficher
  168.         
  169.         $results = [];
  170.         foreach ($allCategories as $cat) {
  171.             if (in_array($cat->getName(), $shop->getCategories()->toArray())) {
  172.                 $cat->getShops()->initialize();
  173.                 $results[] = $cat->getShops();
  174.             }
  175.         }
  176.         $mainCategory $results[0];
  177.         //dump($mainCategory);
  178.         for ($i 0$i min(count($mainCategory), $limit); $i++) {
  179.             $twoShops[] = $mainCategory[$i];
  180.         }
  181.         //TODO Pour chaque catégorie, si $categories === catégorie name, return les shops, sinon, continuer
  182.  /*           $twoShops = $mainCategory->findBy(
  183.                 [],
  184.                 ["name" => "ASC"],
  185.                 $limit, // nombre maximal d'éléments à retourner
  186.                 $offset // position de départ
  187.             );
  188. */
  189.         //dump($categoriesTwo);
  190.         // TODO si le commerce n'existe pas, je dois renvoyer vers une 404.
  191.         if ($shop === null) {
  192.             throw $this->createNotFoundException("Le commerce n'existe pas");
  193.         }
  194.     
  195.         $apiKey 'AIzaSyCM1n8TZCulTKvvFnaznr_sEodEXxrL44A'// Clé API Google Places
  196.     
  197.         // Récupération de l'ID du commerce
  198.         $placeId $shop->getMyBusiness(); // Assure-toi d'avoir la méthode getMyBusiness() dans l'entité Shop
  199.     
  200.         // Construction de l'URL de l'API Google Places
  201.         $apiUrl "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$apiKey&language=fr";
  202.         
  203.         // Utilisation de cURL pour effectuer la requête HTTP
  204.         $httpClient HttpClient::create();
  205.         $response $httpClient->request('GET'$apiUrl);
  206.         $data json_decode($response->getContent(), true);
  207.         //dd($shop);
  208.         return $this->render("home/show.html.twig"
  209.         [
  210.             "shopId" => $slug,
  211.             "shopForView" => $shop,
  212.             'twoShops' => $twoShops,
  213.             "allCategories" => $allCategories,
  214.             "apiData" => $data,
  215.         ]);
  216.     }
  217. }