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

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