src/Controller/ShopController.php line 36

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Shop;
  4. use App\Entity\User;
  5. use App\Form\ShopType;
  6. use App\Form\AdditionalShopType;
  7. use App\Repository\ShopRepository;
  8. use Symfony\Component\Mime\Address;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  11. use Symfony\Component\HttpClient\HttpClient;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  20. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  21. #[Route('/shop')]
  22. class ShopController extends AbstractController
  23. {
  24.     #[Route('/'name'app_shop_index'methods: ['GET'])]
  25.     public function index(ShopRepository $shopRepository): Response
  26.     {
  27.         return $this->render('shop/index.html.twig', [
  28.             'shops' => $shopRepository->findAll(),
  29.         ]);
  30.     }
  31.     
  32.     #[Route('/new'name'app_shop_new'methods: ['GET''POST'])]
  33. public function new(
  34.     Request $request,
  35.     ShopRepository $shopRepository,
  36.     ValidatorInterface $validator,
  37.     EntityManagerInterface $entityManager,
  38.     MailerInterface $mailer,
  39.     UserPasswordHasherInterface $passwordHasher
  40. ): Response {
  41.     $shop = new Shop();
  42.     $user = new User();
  43.     
  44.     $shop->setUsers($user); // Lie les deux entités ensemble
  45.     $form $this->createForm(ShopType::class, $shop);
  46.     $form->handleRequest($request);
  47.     if ($form->isSubmitted() && $form->isValid()) {
  48.         $plaintextPassword $user->getPassword();
  49.         $hashedPassword $passwordHasher->hashPassword($user$plaintextPassword);
  50.         $user->setPassword($hashedPassword);
  51.         // Récupérer les coordonnées géographiques
  52.         if ($shop->getMyBusiness() !== null) {
  53.             $apiKey 'AIzaSyCM1n8TZCulTKvvFnaznr_sEodEXxrL44A'// Clé API Google Places
  54.             // Récupération de l'ID du commerce
  55.             $placeId $shop->getMyBusiness(); // Assurez-vous d'avoir la méthode getMyBusiness() dans l'entité Shop
  56.             // Construction de l'URL de l'API Google Places
  57.             $apiUrl "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$apiKey&language=fr";
  58.             //dd($apiUrl);
  59.             // Utilisation de cURL pour effectuer la requête HTTP
  60.             $httpClient HttpClient::create();
  61.             $response $httpClient->request('GET'$apiUrl);
  62.             if ($response->getStatusCode() === 200) {
  63.                 $data json_decode($response->getContent(), true);
  64.                 $latitude $data['result']['geometry']['location']['lat'];
  65.                 $longitude $data['result']['geometry']['location']['lng'];
  66.                 $phoneNumber $data['result']['formatted_phone_number'];
  67.                 $website $data['result']['website'];
  68.                 // Pour la mise en place du persist de l'adresse
  69.                 $addressComponents $data['result']['address_components'];
  70.                 $address_street '';
  71.                 $address_postal_code '';
  72.                 $address_city '';
  73.                 if (in_array("street_number"$addressComponents[0]['types'])) {
  74.                     $address_street $addressComponents[0]['long_name'] . " " $addressComponents[1]['long_name'];
  75.                 } else {
  76.                     $address_street $addressComponents[0]['long_name'];
  77.                 }
  78.                 foreach ($addressComponents as $component) {
  79.                     $types $component['types'];
  80.                     if (in_array('postal_code'$types)) {
  81.                         $address_postal_code $component['long_name'];
  82.                     } elseif (in_array('locality'$types)) {
  83.                         $address_city $component['long_name'];
  84.                     }
  85.                 }
  86.                 // Persistez dans l'entité Shop
  87.                 $shop->setLatitude($latitude);
  88.                 $shop->setLongitude($longitude);
  89.                 $shop->setPhoneNumber($phoneNumber);
  90.                 $shop->setWebsite($website);
  91.                 $shop->setAdressStreet($address_street);
  92.                 $shop->setAddressPostalCode($address_postal_code);
  93.                 $shop->setAddressCity($address_city);
  94.             }
  95.             $shopRepository->save($shoptrue);
  96.             // début de l'envoi de l'email de confirmation
  97.             $email = (new TemplatedEmail())
  98.             ->from(new Address('contact@bestof.ovh''Bestof contact'))
  99.             ->to($user->getEmail())
  100.             ->subject('Bienvenue sur Best of Sarlat')
  101.             ->htmlTemplate('shop/emailInscription.html.twig')
  102.             ->context(['user' => $user]);
  103.             $mailer->send($email);
  104.             // fin de l'envoi de l'email de confirmation
  105.             // Afficher le message de confirmation
  106.             $this->addFlash('success''Merci pour votre inscription !');
  107.             return $this->redirectToRoute('app_home_list', [], Response::HTTP_SEE_OTHER);
  108.         }
  109.     }
  110.     return $this->render('shop/new.html.twig', [
  111.         'shop' => $shop,
  112.         'form' => $form->createView(),
  113.         'user' => $user,
  114.     ]);
  115. }
  116. #[Route('/nouveau-commerce'name'app_shop_additional'methods: ['GET''POST'])]
  117. public function newAdditional(
  118.     Request $request,
  119.     ShopRepository $shopRepository,
  120. ): Response {
  121.     $shop = new Shop();
  122.     $form $this->createForm(AdditionalShopType::class, $shop);
  123.     $form->handleRequest($request);
  124.     // Récupérer les coordonnées géographiques
  125.     if ($shop->getMyBusiness() !== null) {
  126.         $apiKey 'AIzaSyCM1n8TZCulTKvvFnaznr_sEodEXxrL44A'// Clé API Google Places
  127.         // Récupération de l'ID du commerce
  128.         $placeId $shop->getMyBusiness(); // Assurez-vous d'avoir la méthode getMyBusiness() dans l'entité Shop
  129.         // Construction de l'URL de l'API Google Places
  130.         $apiUrl "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$apiKey&language=fr";
  131.         //dd($apiUrl);
  132.         // Utilisation de cURL pour effectuer la requête HTTP
  133.         $httpClient HttpClient::create();
  134.         $response $httpClient->request('GET'$apiUrl);
  135.         if ($response->getStatusCode() === 200) {
  136.             $data json_decode($response->getContent(), true);
  137.             $latitude $data['result']['geometry']['location']['lat'];
  138.             $longitude $data['result']['geometry']['location']['lng'];
  139.             $phoneNumber $data['result']['formatted_phone_number'];
  140.             $website $data['result']['website'];
  141.             // Pour la mise en place du persist de l'adresse
  142.             $addressComponents $data['result']['address_components'];
  143.             $address_street '';
  144.             $address_postal_code '';
  145.             $address_city '';
  146.             if (in_array("street_number"$addressComponents[0]['types'])) {
  147.                 $address_street $addressComponents[0]['long_name'] . " " $addressComponents[1]['long_name'];
  148.             } else {
  149.                 $address_street $addressComponents[0]['long_name'];
  150.             }
  151.             foreach ($addressComponents as $component) {
  152.                 $types $component['types'];
  153.                 if (in_array('postal_code'$types)) {
  154.                     $address_postal_code $component['long_name'];
  155.                 } elseif (in_array('locality'$types)) {
  156.                     $address_city $component['long_name'];
  157.                 }
  158.             }
  159.             // Persistez dans l'entité Shop
  160.             $shop->setLatitude($latitude);
  161.             $shop->setLongitude($longitude);
  162.             $shop->setPhoneNumber($phoneNumber);
  163.             $shop->setWebsite($website);
  164.             $shop->setAdressStreet($address_street);
  165.             $shop->setAddressPostalCode($address_postal_code);
  166.             $shop->setAddressCity($address_city);
  167.         }
  168.         $shopRepository->save($shoptrue);
  169.         // Afficher le message de confirmation
  170.         $this->addFlash('success''Merci pour votre ajout de commerce !');
  171.         return $this->redirectToRoute('app_home_list', [], Response::HTTP_SEE_OTHER);
  172.     }
  173.     return $this->render('shop/newShopOnly.html.twig', [
  174.         'shop' => $shop,
  175.         'form' => $form->createView(),
  176.     ]);
  177. }
  178.     
  179.     #[Route('/{id}'name'app_shop_show'methods: ['GET'])]
  180.     public function show(Shop $shop): Response
  181.     {
  182.         return $this->render('shop/show.html.twig', [
  183.             'shop' => $shop,
  184.         ]);
  185.     }
  186.     #[Route('/{id}/edit'name'app_shop_edit'methods: ['GET''POST'])]
  187.     public function edit(Request $requestShop $shopShopRepository $shopRepository): Response
  188.     {
  189.         $form $this->createForm(ShopType::class, $shop);
  190.         $form->handleRequest($request);
  191.         if ($form->isSubmitted() && $form->isValid()) {
  192.             $shopRepository->save($shoptrue);
  193.             return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
  194.         }
  195.         return $this->render('shop/edit.html.twig', [
  196.             'shop' => $shop,
  197.             'form' => $form,
  198.         ]);
  199.     }
  200.     #[Route('/{id}'name'app_shop_delete'methods: ['POST'])]
  201.     public function delete(Request $requestShop $shopShopRepository $shopRepository): Response
  202.     {
  203.         if ($this->isCsrfTokenValid('delete'.$shop->getId(), $request->request->get('_token'))) {
  204.             $shopRepository->remove($shoptrue);
  205.         }
  206.         return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
  207.     }
  208. }