src/Controller/Front/LandingPage/LandingPageController.php line 79

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\LandingPage;
  3. use App\Entity\Prospect;
  4. use App\Form\ProspectStepType;
  5. use App\Form\ProspectType;
  6. use App\Form\StoreChoiceType;
  7. use App\Repository\BlogPostRepository;
  8. use App\Repository\FreelancerRepository;
  9. use App\Repository\HearingBrandRepository;
  10. use App\Service\ProspectService;
  11. use Doctrine\ORM\EntityManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Exception\ORMException;
  14. use Doctrine\ORM\OptimisticLockException;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. class LandingPageController extends AbstractProspectCreationController
  21. {
  22.     // array contains LP path ['url' => 'template_path']
  23.     protected array $lpTemplatePaths = [
  24.         'solution-acouphene' => 'front_v4/mca/landing_page/prospect/index_acq_acouphene.html.twig',
  25.         'demande-de-bilan-1' => 'front_v4/mca/landing_page/prospect/index_acq_seo.html.twig',
  26.         'mon-bilan-auditif' => 'front_v4/mca/landing_page/prospect/index_acq_seo.html.twig',
  27.         'demande-de-bilan-2' => 'front_v4/mca/landing_page/prospect/index_acq_seo.html.twig',
  28.         'demande-de-bilan-3' => 'front_v4/mca/landing_page/prospect/index_acq_seo.html.twig',
  29.         'demande-de-bilan-4' => 'front_v4/mca/landing_page/prospect/index_acq_seo.html.twig',
  30.         'demande-de-bilan-5' => 'front_v4/mca/landing_page/prospect/index_demande_bilan_5.html.twig',
  31.         'eml_acq_wstep' => 'front_v4/mca/landing_page/prospect/index_acq_seo.html.twig',
  32.         'mon-essai-gratuit' => 'front_v4/mca/landing_page/prospect/index_free_try.html.twig',
  33.         'demande-de-bilan-auditif-depistage' => 'front_v4/mca/landing_page/prospect/acq_native/index_acq_native_1.html.twig',
  34.         'demande-de-bilan-auditif-depistage-conversation' => 'front_v4/mca/landing_page/prospect/acq_native/index_acq_native_2.html.twig',
  35.         'demande-de-bilan-auditif-depistage-son-television' => 'front_v4/mca/landing_page/prospect/acq_native/index_acq_native_3.html.twig',
  36.         'demande-de-bilan-auditif-depistage-environnement-bruyant' => 'front_v4/mca/landing_page/prospect/acq_native/index_acq_native_4.html.twig',
  37.         'appareils-totalement-rembourses' => 'front_v4/mca/landing_page/prospect/index_free_hearing_aid.html.twig',
  38.         'sante-pour-tous' => 'front_v4/mca/landing_page/prospect/index_acq_campaign_sms.html.twig',
  39.     ];
  40.     /**
  41.      *  Méthode appelée lorsqu'un prospect valide le form d'une lp
  42.      *
  43.      * @Route("/success/{id}", name="lp_form_success")
  44.      *
  45.      * @param BlogPostRepository $blogPostRepository
  46.      * @param Prospect $prospect
  47.      * @param string $origin // route d'où est émise la requete
  48.      * @return Response
  49.      */
  50.     public function landingPageSuccess(BlogPostRepository $blogPostRepositoryProspect $prospectstring $origin "default"): Response
  51.     {
  52.         $pos $prospect->getProspectsOnStore()->last();
  53.         $store $pos->getStore();
  54.         return $this->render("front_v4/mca/landing_page/prospect/form_success.html.twig", [
  55.             'store' => $store,
  56.             'prospect' => $prospect,
  57.             'pos_id' => empty($pos) ? null $pos->getId(),
  58.             'origin' => $origin '-success',
  59.             'blogPosts' => $blogPostRepository->getLastedArticle(),
  60.             'is_freelancer' => $store->getAdministrator()->isFreelancer()
  61.         ]);
  62.     }
  63.     /**
  64.      * @param Request $request
  65.      * @param EntityManager $entityManager
  66.      * @param LoggerInterface $logger
  67.      * @param HearingBrandRepository $hearingBrandRepository
  68.      * @param FreelancerRepository $freelancerRepository
  69.      * @return Response
  70.      * @throws OptimisticLockException
  71.      * @throws ORMException
  72.      */
  73.     public function landingPageShow(Request $requestEntityManagerInterface $entityManagerLoggerInterface $loggerHearingBrandRepository $hearingBrandRepositoryFreelancerRepository $freelancerRepository): Response
  74.     {
  75.         // with url get path of template
  76.         $pathInfo $request->getPathInfo();
  77.         $templatePath $this->lpTemplatePaths[ltrim($pathInfo'/')];
  78.         if (empty($templatePath)) {
  79.             throw $this->createNotFoundException();
  80.         }
  81.         $logger->info('get prospect from landing page path : ' $pathInfo);
  82.         $prospect = new Prospect();
  83.         $this->prefiledProspectWithGetParam($request$prospect);
  84.         $prospectForm $this->createForm(ProspectType::class, $prospect);
  85.         $prospectForm->handleRequest($request);
  86.         if ($prospectForm->isSubmitted() && $prospectForm->isValid()) {
  87.             $this->get("security.csrf.token_manager")->refreshToken("prospect_token");
  88.             // already TYPE_CHECKUP on landing page
  89.             $prospect->setRequest(Prospect::TYPE_CHECKUP);
  90.             // create a prospect or redirect to a specific page if duplicate has been found
  91.             $redirection $this->createProspectOrRedirect($prospect);
  92.             // apply redirection if one is returned
  93.             if (!is_null($redirection)) {
  94.                 return $redirection;
  95.             }
  96.             $entityManager->flush();
  97.             // redirect to success page
  98.             return $this->redirectToSuccessPage($prospect$pathInfo);
  99.         }
  100.         return $this->render($templatePath, [
  101.             'formProspect' => $prospectForm->createView(),
  102.             'hearingBrands' => $hearingBrandRepository->findBy(['presentable' => true]),
  103.             'freelancers' => $freelancerRepository->getOnlyWithLogo(),
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/demande-de-bilan-auditif-gratuit", name="index_acquisition_step", methods={"GET", "POST"})
  108.      * @param Request $request
  109.      * @param LoggerInterface $logger
  110.      * @param EntityManagerInterface $entityManager
  111.      * @return Response
  112.      */
  113.     public function landingPageStepShow(Request $requestLoggerInterface $loggerEntityManagerInterface $entityManager): Response
  114.     {
  115.         $logger->info('get prospect from landing page path : indexAcquisitionStep');
  116.         $prospect = new Prospect();
  117.         $this->prefiledProspectWithGetParam($request$prospect);
  118.         $prospectForm $this->createForm(ProspectStepType::class, $prospect);
  119.         $prospectForm->handleRequest($request);
  120.         if ($prospectForm->isSubmitted() && $prospectForm->isValid()) {
  121.             $this->get("security.csrf.token_manager")->refreshToken("prospect_token");
  122.             // already TYPE_CHECKUP on landing page
  123.             $prospect->setRequest(Prospect::TYPE_CHECKUP);
  124.             // patch for Google API postal code
  125.             if (empty($prospect->getPostalcode())) {
  126.                 $logger->info('Google APi can\'t get postalcode');
  127.                 $prospect->setPostalcode('00000');
  128.             }
  129.             // create a prospect or redirect to a specific page if duplicate has been found
  130.             $redirection $this->createProspectOrRedirect($prospect);
  131.             // apply redirection if one is returned
  132.             if (!is_null($redirection)) {
  133.                 return $redirection;
  134.             }
  135.             $entityManager->flush();
  136.             // redirect to success page
  137.             return $this->redirectToSuccessPage($prospect'index_acquisition_step');
  138.         }
  139.         return $this->render('front_v4/mca/landing_page/prospect/index_acq_step.html.twig', [
  140.             'formProspect' => $prospectForm->createView()
  141.         ]);
  142.     }
  143.     /**
  144.      * Hearing Test redirect to another LP (test scoring hearing online)
  145.      * @Route("/test-auditif", name="index_hearing_test", methods={"GET", "POST"})
  146.      * @param LoggerInterface $logger
  147.      * @return Response
  148.      */
  149.     public function indexHearingTest(LoggerInterface $logger): Response
  150.     {
  151.         $logger->info('Landing page path : indexHearingTest');
  152.         return $this->render('front_v4/mca/landing_page/prospect/index_hearing_test.html.twig');
  153.     }
  154.     /**
  155.      * LP with choice store from user
  156.      * @Route("/trouver-votre-centre-auditif", name="index_lp_choice", methods={"GET", "POST"})
  157.      * @param Request $request
  158.      * @param LoggerInterface $logger
  159.      * @param HearingBrandRepository $hearingBrandRepository
  160.      * @param FreelancerRepository $freelancerRepository
  161.      * @param SessionInterface $session
  162.      * @return Response
  163.      */
  164.     public function landingPageStoreChoice(
  165.         Request  $request,
  166.         LoggerInterface $logger,
  167.         HearingBrandRepository $hearingBrandRepository,
  168.         FreelancerRepository $freelancerRepository,
  169.         SessionInterface $session
  170.     ): Response
  171.     {
  172.         $logger->info('get prospect from LP Store Choice path : indexLpChoicePrefill');
  173.         $prospect = new Prospect();
  174.         $this->prefiledProspectWithGetParam($request$prospect);
  175.         $prospectForm $this->createForm(ProspectType::class, $prospect);
  176.         $prospectForm->handleRequest($request);
  177.         if ($prospectForm->isSubmitted() && $prospectForm->isValid()) {
  178.             $this->get("security.csrf.token_manager")->refreshToken("prospect_token");
  179.             // already TYPE_CHECKUP on landing page
  180.             $prospect->setRequest(Prospect::TYPE_CHECKUP);
  181.             // patch for Google API postal code
  182.             if (empty($prospect->getPostalcode())) {
  183.                 $logger->info('Google APi can\'t get postalcode');
  184.                 $prospect->setPostalcode('00000');
  185.             }
  186.             // store prospect date in session (persist after choosing store)
  187.             $session->set('prospect_data'$prospect);
  188.             // Store choice redirection
  189.             return $this->redirectToRoute('store_choice');
  190.         }
  191.         return $this->render('front_v4/mca/landing_page/prospect/index_acq_seo.html.twig', [
  192.             'formProspect' => $prospectForm->createView(),
  193.             'hearingBrands' => $hearingBrandRepository->findBy(['presentable' => true]),
  194.             'freelancers' => $freelancerRepository->getOnlyWithLogo(),
  195.         ]);
  196.     }
  197.     /**
  198.      * Store Choice
  199.      * @Route("/store-choice", name="store_choice", methods={"GET", "POST"})
  200.      */
  201.     public function storeChoice(
  202.         Request $request,
  203.         LoggerInterface $logger,
  204.         EntityManagerInterface $entityManager,
  205.         HearingBrandRepository $hearingBrandRepository,
  206.         FreelancerRepository $freelancerRepository,
  207.         ProspectService $prospectService,
  208.         SessionInterface $session
  209.     ): Response
  210.     {
  211.         $logger->info('Store choice for prospect data.');
  212.         // get prospect data form session
  213.         $prospect $session->get('prospect_data');
  214.         if (!$prospect) {
  215.             throw $this->createNotFoundException('No prospect data found in session.');
  216.         }
  217.         $searchResultStores $prospectService->pickStoresBelongingToADifferentAdmin($prospect5);
  218.         if (empty($searchResultStores)) {
  219.             $logger->info('No stores available for the given prospect.');
  220.             return $this->render('front_v4/mca/landing_page/prospect/index_store_choice.html.twig', [
  221.                 'noStoreAvailable' => true,
  222.                 'searchResultStores' => $searchResultStores,
  223.                 'prospect' => $prospect,
  224.                 'hearingBrands' => $hearingBrandRepository->findBy(['presentable' => true]),
  225.                 'freelancers' => $freelancerRepository->getOnlyWithLogo(),
  226.             ]);
  227.         }
  228.         $storeForm $this->createForm(StoreChoiceType::class, null, ['stores' => $searchResultStores]);
  229.         $storeForm->handleRequest($request);
  230.         if ($storeForm->isSubmitted() && $storeForm->isValid()) {
  231.             $selectedStore $storeForm->get('store')->getData();
  232.             // create a prospect or redirect to a specific page if duplicate has been found
  233.             $redirection $this->createProspectOrRedirect($prospect$selectedStore->getStore());
  234.             // apply redirection if one is returned
  235.             if (!is_null($redirection)) {
  236.                 return $redirection;
  237.             }
  238.             $entityManager->flush();
  239.             // redirect to success page
  240.             return $this->redirectToSuccessPage($prospect'index_acquisition_step');
  241.         }
  242.         // return a page with store list
  243.         return $this->render('front_v4/mca/landing_page/prospect/index_store_choice.html.twig', [
  244.             'storeForm' => $storeForm->createView(),
  245.             'searchResultStores' => $searchResultStores,
  246.             'prospect' => $prospect,
  247.             'hearingBrands' => $hearingBrandRepository->findBy(['presentable' => true]),
  248.             'freelancers' => $freelancerRepository->getOnlyWithLogo(),
  249.         ]);
  250.     }
  251. }