<?php
/**
* Created by PhpStorm.
* User: michael
* Date: 03/07/2019
* Time: 10:06
*/
namespace App\Controller\Front;
use App\Controller\Front\LandingPage\AbstractProspectCreationController;
use App\Entity\HearingBrand;
use App\Entity\Prospect;
use App\Form\FilterHearingBrandType;
use App\Form\Model\FilterHearingBrandModel;
use App\Form\ProspectType;
use App\Repository\HearingBrandRepository;
use App\Repository\StoreRepository;
use App\Service\GmapsService;
use Doctrine\ORM\EntityManagerInterface;
use Geocoder\Exception\Exception;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class HearingBrandController extends AbstractProspectCreationController
{
/**
* @Route("/enseignes-auditives", name="hearing_brand_list")
* @param HearingBrandRepository $hearingBrandRepository
* @param PaginatorInterface $paginator
* @param Request $request
* @return Response
*/
public function showAllHearingBrand(HearingBrandRepository $hearingBrandRepository, PaginatorInterface $paginator, Request $request): Response
{
$search = new FilterHearingBrandModel();
$form = $this->createForm(FilterHearingBrandType::class, $search);
$form->handleRequest($request);
$pagination = $paginator->paginate(
$hearingBrandRepository->getFilteredHearingBrand($search),
$request->query->getInt('page', 1)
);
return $this->render('front_v4/mca/hearing_brand/list.html.twig', [
'pagination' => $pagination,
'form' => $form->createView()
]);
}
/**
* @Route("/enseigne-auditive/{slug}", name="hearing_brand_show", methods={"GET", "POST"})
* @param Request $request
* @param HearingBrand $hearingBrand
* @param GmapsService $gmapsService
* @param StoreRepository $storeRepository
* @param EntityManagerInterface $em
* @return Response
* @throws Exception
*/
public function indexHearingBrand(Request $request,
HearingBrand $hearingBrand,
GmapsService $gmapsService,
StoreRepository $storeRepository,
EntityManagerInterface $em
): Response
{
if (!$request->get('slug')) {
throw $this->createNotFoundException();
}
// Autoscroll to lead_form
$scrollToLeadForm = false;
$prospect = new Prospect();
$prospectForm = $this->createForm(ProspectType::class, $prospect);
$prospectForm->handleRequest($request);
$nbOfCenter = $hearingBrand->getStores()->count();
// Autoscroll to lead_form
if ($prospectForm->isSubmitted()) {
$scrollToLeadForm = true;
}
if ($prospectForm->isSubmitted() && $prospectForm->isValid()) {
$result = null;
$prospect = $prospectForm->getData();
// Lead type = Demande de bilan
$prospect->setRequest('checkup');
// Récupérer la position de l'utilisateur en fonction de son code postal
$geocode = $gmapsService->getLatitudeLongitudeFromAddress($prospect->getPostalcode());
// Recherche d'un store correspondant à la marque à proximité
// Critère de geociblage
if (!empty($geocode)) {
$result = $storeRepository->findStoreAroundLocationForHearingBrand($geocode[0], $geocode[1], -1, $hearingBrand);
}
if ($result != null) {
$store = $result[0];
//Create a prospect or redirect to a specific page if duplicate has been found
$redirection = $this->createProspectOrRedirect($prospect);
//apply redirection if one is returned
if (!is_null($redirection)) {
return $redirection;
}
}
$em->flush();
// Redirection page de confirmation
return $this->render('front_v4/mca/hearing_brand/single.html.twig', [
'hearingBrand' => $hearingBrand,
'nbOfCenter' => $nbOfCenter,
'status' => 'send_success',
'lead_number' => md5($prospect->getEmail()),
'scroll_to_lead_form' => $scrollToLeadForm,
]);
}
return $this->render('front_v4/mca/hearing_brand/single.html.twig', [
'hearingBrand' => $hearingBrand,
'nbOfCenter' => $nbOfCenter,
'prospectForm' => $prospectForm->createView(),
'scroll_to_lead_form' => $scrollToLeadForm,
]);
}
}