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

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