src/Controller/FamilleProduitController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\DemandeDevisPromoType;
  4. use App\Repository\Site\AlertEmailDevisRepository;
  5. use App\Service\EmailService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpClient\HttpClient;
  8. use Symfony\Contracts\HttpClient\HttpClientInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Entity\Produit\FamilleProduit;
  12. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  13. class FamilleProduitController extends AbstractController
  14. {
  15.     /**
  16.      * @var HttpClientInterface
  17.      */
  18.     private $httpClient;
  19.     /**
  20.      * @var AlertEmailDevisRepository
  21.      */
  22.     private $alertEmailDevisRepository;
  23.     /**
  24.      * @var EmailService
  25.      */
  26.     private $emailService;
  27.     public function __construct(HttpClientInterface $httpClientAlertEmailDevisRepository $alertEmailDevisRepositoryEmailService $emailService)
  28.     {
  29.         $this->httpClient $httpClient;
  30.         $this->alertEmailDevisRepository $alertEmailDevisRepository;
  31.         $this->emailService $emailService;
  32.     }
  33.     /**
  34.      * @Route("/produit/{seoUrl}", name="famille_produit")
  35.      */
  36.     public function index(FamilleProduit $familleProduitRequest $request)
  37.     {
  38.         $urlImageFamilleProduit $this->getParameter('app.path.famille_produit_images').'/';
  39.         $urlImageSousFamille $this->getParameter('app.path.sous_famille_produit_images').'/';
  40.         /*
  41.         $form = $this->createForm(DemandeDevisPromoType::class);
  42.         $form->handleRequest($request);
  43.         if ($form->isSubmitted() && $form->isValid()) {
  44.             $name = $form->get('name')->getData();
  45.             $phone = $form->get('phone')->getData();
  46.             $email = $form->get('email')->getData();
  47.             $address = $form->get('address')->getData();
  48.             $city = $form->get('city')->getData();
  49.             $postalCode = $form->get('postalCode')->getData();
  50.             $message = $form->get('message')->getData();
  51.             $movinetApiUrl = $this->getParameter('movinet.url.apiRest.prod') . 'json.api?www-command=createCodePromo';
  52.             $header = [
  53.                 'Content-Type' => 'application/x-www-form-urlencoded',
  54.                 'responseType' => 'json',
  55.             ];
  56.             $json = [
  57.                 'session' => $this->getParameter('movinet.auth.session'),
  58.                 'uniqcode' => $this->getParameter('movinet.auth.uniqcode'),
  59.                 'enseigne' => 'sogal',
  60.                 'libelle' => 'PROMO-TV-MAR-25',
  61.                 'client_final' => $name,
  62.                 'phone' => $phone,
  63.                 'email' => $email,
  64.                 'address' => $address . ' - ' . $postalCode . ' - ' . $city,
  65.                 'observation' => $message
  66.             ];
  67.             $response = $this->httpClient->request('POST', $movinetApiUrl, ['headers' => $header, 'body' => $json]);
  68.             $result = $response->toArray();
  69.             $codePromo = $result['code_promo'];
  70.             // Données google map
  71.             $addressForGoogle = $address."+".$postalCode."+".$city."+France";
  72.             $addressForGoogle = preg_replace('/\s+/', '+', $addressForGoogle);
  73.             $apiKey = $this->getParameter('app.apiKeyGoogleMapBO');
  74.             $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$addressForGoogle."&key=".$apiKey;
  75.             $client = HttpClient::create();
  76.             $jsonResults = $client->request('GET', $url);
  77.             $results = json_decode($jsonResults->getContent(), true);
  78.             $lat = $results["results"][0]["geometry"]["location"]["lat"];
  79.             $lng = $results["results"][0]["geometry"]["location"]["lng"];
  80.             $dataToSendApiSogal = [
  81.                 "email" => $email,
  82.                 "telephone"=> $phone,
  83.                 "nom" => $name,
  84.                 "adresse" => $address,
  85.                 "cp" => $postalCode,
  86.                 "ville" => $city,
  87.                 "pays" => "France",
  88.                 "code_promo" => $codePromo,
  89.                 "libelle_promo" => 'PROMO-TV-MAR-25',
  90.                 "message" => $message
  91.             ];
  92.             // Données google map
  93.             !empty($lat) ? $dataToSendApiSogal["lat"] = $lat : null;
  94.             !empty($lng) ? $dataToSendApiSogal["lng"] = $lng : null;
  95.             // Envoi des données à l'API Sogal Expert
  96.             $keyApiSogal = $this->getParameter("api.sogalExpert.key");
  97.             if ($this->getParameter('kernel.environment') == "dev") {
  98.                 $urlApiSogal = $this->getParameter("api.sogalExpert.url.localhost");
  99.             }else {
  100.                 $urlApiSogal = $this->getParameter("api.sogalExpert.url");
  101.             }
  102.             $client = HttpClient::create(['headers' => [
  103.                 'Content-Type' => 'application/json',
  104.                 "responseType"=> 'json',
  105.                 "Api-Token"=> $keyApiSogal
  106.             ]]);
  107.             $baseUrl = $urlApiSogal."reception/devis-promo";
  108.             try {
  109.                 $client->request('POST', $baseUrl, [
  110.                     'json' => $dataToSendApiSogal,
  111.                 ]);
  112.             } catch (TransportExceptionInterface $e) {
  113.                 $subject = $this->getParameter("app.name") . " - Problème lors de l'envoi de la configuration  à l'api sogal-experts";
  114.                 $errorArray = [
  115.                     "exception message : " . $e->getMessage(),
  116.                     'json : ' => json_encode($dataToSendApiSogal),
  117.                 ];
  118.                 $admins = $this->alertEmailDevisRepository->findAll();
  119.                 $adminEmails = [];
  120.                 foreach ($admins as $key => $admin) {
  121.                     $adminEmails[] = $admin->getEmail();
  122.                 }
  123.                 $this->emailService->send($adminEmails, "email/errorDevis.html.twig", $subject, ["errorArray" => $errorArray, "subject" => $subject]);
  124.                 return $this->render('page/demandeDevisError.html.twig', [
  125.                     "datas" => $dataToSendApiSogal,
  126.                 ]);
  127.             }
  128.             $this->addFlash('success', 'Votre demande a bien été envoyée');
  129.             return $this->redirectToRoute('homepage');
  130.         }
  131.         */
  132.         $sousFamilleTypes = [];
  133.         foreach ($familleProduit->getSousFamilles() as $sousFamille) {
  134.             if($sousFamille->getSousFamilleTypes()){
  135.                 foreach ($sousFamille->getSousFamilleTypes() as $sousFamilleType) {
  136.                     $sousFamilleTypes[$familleProduit->getSeoUrl()][$sousFamilleType->getPlacement()][$sousFamille->getPlacement()] = [
  137.                         'sousFamilleType' => $sousFamilleType,
  138.                         'sousFamille' => $sousFamille
  139.                     ];
  140.                     ksort($sousFamilleTypes[$familleProduit->getSeoUrl()][$sousFamilleType->getPlacement()]);
  141.                     ksort($sousFamilleTypes[$familleProduit->getSeoUrl()]);
  142.                 }
  143.             }
  144.         }
  145.         return $this->render('famille_produit/famille_produit.html.twig', [
  146.             // 'titre' => $familleProduit->getDesignation(),
  147.             // 'produits' => $produits,
  148.             // 'partitions' => $partitions,
  149.             // 'collections' => $collections,
  150.             // 'sousFamilles' => $sousFamilles,
  151.             // 'filtrePersonaliseBis' => $filtrePersonaliseBis,
  152.             'familleProduit' => $familleProduit,
  153.             // 'urlImageProduit' => $urlImageProduit,
  154.             'urlImageSousFamille' => $urlImageSousFamille,
  155.             // 'urlPartitionProduit' => $urlPartitionProduit,
  156.             'urlImageFamilleProduit' => $urlImageFamilleProduit,
  157.             'sousFamilleTypes' => $sousFamilleTypes,
  158.             //'form' => $form->createView()
  159.         ]);
  160.     }
  161. }