<?php
namespace App\Controller;
use App\Form\DemandeDevisPromoType;
use App\Repository\Site\AlertEmailDevisRepository;
use App\Service\EmailService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Produit\FamilleProduit;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class FamilleProduitController extends AbstractController
{
/**
* @var HttpClientInterface
*/
private $httpClient;
/**
* @var AlertEmailDevisRepository
*/
private $alertEmailDevisRepository;
/**
* @var EmailService
*/
private $emailService;
public function __construct(HttpClientInterface $httpClient, AlertEmailDevisRepository $alertEmailDevisRepository, EmailService $emailService)
{
$this->httpClient = $httpClient;
$this->alertEmailDevisRepository = $alertEmailDevisRepository;
$this->emailService = $emailService;
}
/**
* @Route("/produit/{seoUrl}", name="famille_produit")
*/
public function index(FamilleProduit $familleProduit, Request $request)
{
$urlImageFamilleProduit = $this->getParameter('app.path.famille_produit_images').'/';
$urlImageSousFamille = $this->getParameter('app.path.sous_famille_produit_images').'/';
/*
$form = $this->createForm(DemandeDevisPromoType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$name = $form->get('name')->getData();
$phone = $form->get('phone')->getData();
$email = $form->get('email')->getData();
$address = $form->get('address')->getData();
$city = $form->get('city')->getData();
$postalCode = $form->get('postalCode')->getData();
$message = $form->get('message')->getData();
$movinetApiUrl = $this->getParameter('movinet.url.apiRest.prod') . 'json.api?www-command=createCodePromo';
$header = [
'Content-Type' => 'application/x-www-form-urlencoded',
'responseType' => 'json',
];
$json = [
'session' => $this->getParameter('movinet.auth.session'),
'uniqcode' => $this->getParameter('movinet.auth.uniqcode'),
'enseigne' => 'sogal',
'libelle' => 'PROMO-TV-MAR-25',
'client_final' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address . ' - ' . $postalCode . ' - ' . $city,
'observation' => $message
];
$response = $this->httpClient->request('POST', $movinetApiUrl, ['headers' => $header, 'body' => $json]);
$result = $response->toArray();
$codePromo = $result['code_promo'];
// Données google map
$addressForGoogle = $address."+".$postalCode."+".$city."+France";
$addressForGoogle = preg_replace('/\s+/', '+', $addressForGoogle);
$apiKey = $this->getParameter('app.apiKeyGoogleMapBO');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$addressForGoogle."&key=".$apiKey;
$client = HttpClient::create();
$jsonResults = $client->request('GET', $url);
$results = json_decode($jsonResults->getContent(), true);
$lat = $results["results"][0]["geometry"]["location"]["lat"];
$lng = $results["results"][0]["geometry"]["location"]["lng"];
$dataToSendApiSogal = [
"email" => $email,
"telephone"=> $phone,
"nom" => $name,
"adresse" => $address,
"cp" => $postalCode,
"ville" => $city,
"pays" => "France",
"code_promo" => $codePromo,
"libelle_promo" => 'PROMO-TV-MAR-25',
"message" => $message
];
// Données google map
!empty($lat) ? $dataToSendApiSogal["lat"] = $lat : null;
!empty($lng) ? $dataToSendApiSogal["lng"] = $lng : null;
// Envoi des données à l'API Sogal Expert
$keyApiSogal = $this->getParameter("api.sogalExpert.key");
if ($this->getParameter('kernel.environment') == "dev") {
$urlApiSogal = $this->getParameter("api.sogalExpert.url.localhost");
}else {
$urlApiSogal = $this->getParameter("api.sogalExpert.url");
}
$client = HttpClient::create(['headers' => [
'Content-Type' => 'application/json',
"responseType"=> 'json',
"Api-Token"=> $keyApiSogal
]]);
$baseUrl = $urlApiSogal."reception/devis-promo";
try {
$client->request('POST', $baseUrl, [
'json' => $dataToSendApiSogal,
]);
} catch (TransportExceptionInterface $e) {
$subject = $this->getParameter("app.name") . " - Problème lors de l'envoi de la configuration à l'api sogal-experts";
$errorArray = [
"exception message : " . $e->getMessage(),
'json : ' => json_encode($dataToSendApiSogal),
];
$admins = $this->alertEmailDevisRepository->findAll();
$adminEmails = [];
foreach ($admins as $key => $admin) {
$adminEmails[] = $admin->getEmail();
}
$this->emailService->send($adminEmails, "email/errorDevis.html.twig", $subject, ["errorArray" => $errorArray, "subject" => $subject]);
return $this->render('page/demandeDevisError.html.twig', [
"datas" => $dataToSendApiSogal,
]);
}
$this->addFlash('success', 'Votre demande a bien été envoyée');
return $this->redirectToRoute('homepage');
}
*/
$sousFamilleTypes = [];
foreach ($familleProduit->getSousFamilles() as $sousFamille) {
if($sousFamille->getSousFamilleTypes()){
foreach ($sousFamille->getSousFamilleTypes() as $sousFamilleType) {
$sousFamilleTypes[$familleProduit->getSeoUrl()][$sousFamilleType->getPlacement()][$sousFamille->getPlacement()] = [
'sousFamilleType' => $sousFamilleType,
'sousFamille' => $sousFamille
];
ksort($sousFamilleTypes[$familleProduit->getSeoUrl()][$sousFamilleType->getPlacement()]);
ksort($sousFamilleTypes[$familleProduit->getSeoUrl()]);
}
}
}
return $this->render('famille_produit/famille_produit.html.twig', [
// 'titre' => $familleProduit->getDesignation(),
// 'produits' => $produits,
// 'partitions' => $partitions,
// 'collections' => $collections,
// 'sousFamilles' => $sousFamilles,
// 'filtrePersonaliseBis' => $filtrePersonaliseBis,
'familleProduit' => $familleProduit,
// 'urlImageProduit' => $urlImageProduit,
'urlImageSousFamille' => $urlImageSousFamille,
// 'urlPartitionProduit' => $urlPartitionProduit,
'urlImageFamilleProduit' => $urlImageFamilleProduit,
'sousFamilleTypes' => $sousFamilleTypes,
//'form' => $form->createView()
]);
}
}