src/Controller/SousFamilleController.php line 50

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