src/Controller/Front/PageController.php line 151

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Config\Pages;
  4. use App\Entity\Page\Page;
  5. use App\Entity\Page\PageTranslation;
  6. use App\Factory\ApplicationFactoryInterface;
  7. use App\Provider\BlogProviderInterface;
  8. use App\Provider\FooterLinkProviderInterface;
  9. use App\Service\RecaptchaServiceInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use App\Utils\ResponseMessage\ResponseMessage;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use App\Provider\ItemCategoryProviderInterface;
  14. use App\Provider\Offer\OfferProviderInterface;
  15. use App\Provider\PageProviderInterface;
  16. use App\Provider\SeoIndustryProviderInterface;
  17. use App\Service\ApplicationServiceInterface;
  18. use Doctrine\ORM\NoResultException;
  19. use Exception;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  23. /**
  24.  * @Route("", name="front-")
  25.  */
  26. class PageController extends AbstractController
  27. {
  28.     private const OFFER_VIEW_TEMPLATE_PATH = 'front/offer/offer_preview/offer_preview.twig';
  29.     private const BLOG_VIEW_TEMPLATE_PATH = 'front/blog/blog_item.twig';
  30.     public function __construct(
  31.         private   PageProviderInterface         $pageProvider,
  32.         private   OfferProviderInterface        $offerProvider,
  33.         private   ApplicationFactoryInterface   $applicationFactory,
  34.         private   ApplicationServiceInterface   $applicationService,
  35.         private   ItemCategoryProviderInterface $itemCategoryProvider,
  36.         protected FooterLinkProviderInterface   $footerLinkProvider,
  37.         protected BlogProviderInterface         $blogProvider,
  38.         private   SeoIndustryProviderInterface  $seoIndustryProvider,
  39.         private RecaptchaServiceInterface $recaptchaService
  40.     ){}
  41.     /**
  42.      * @Route(
  43.      *      path="/{_locale}/{offer}/{temporaryId}/{branch_extra}",
  44.      *      name="offer",
  45.      *      requirements=
  46.      *     {
  47.      *          "_locale": "%app.supported_locales%",
  48.      *          "offer": "%app.supported_offer%"
  49.      *      }
  50.      * )
  51.      */
  52.     public function getOfferAction
  53.     (
  54.         Request $request,
  55.         string $_locale,
  56.         string $offer,
  57.         string $temporaryId
  58.     ): Response
  59.     {
  60.         $request->setLocale($_locale);
  61.         $isActive = false;
  62.         $isDone = false;
  63.         $error = '';
  64.         $branchExtra = $request->attributes->get('branch_extra');
  65.         $requestOffer = $offer;
  66.         try {
  67.             $offer = $this->offerProvider->getOfferPreViewByTemporaryId($temporaryId);
  68.             $isDone = $offer->getExtraInfo()['isDone'];
  69.             $isActive = $offer->getExtraInfo()['isActive'];
  70.             if(false === $isDone || false === $isActive){ $offer = null; }
  71.         } catch(NoResultException $e){
  72.             $offer = null;
  73.         }
  74.         if(is_null($offer)) {
  75.             if($isDone === false){
  76.                 $error = ResponseMessage::onOfferNotFound($_locale);
  77.             }
  78.             if($isActive === false) {
  79.                 $error = ResponseMessage::onOfferHasExpired($_locale);
  80.             }
  81.             return $this->pageNotFoundAction($error);
  82.         } else {
  83.             $industryId = $offer->getCategoryId();
  84.         }
  85.         $viewParams['links_in_footer'] = $this->footerLinkProvider->getAll();
  86.         $viewParams['offer'] = $offer;
  87.         $viewParams['front_page'] = false;
  88.         $viewParams['temporary_id'] = $temporaryId;
  89.         $viewParams['queryParams'] = $request->query->all();
  90.         $id = $offer->getCategoryId();
  91.         $itemCategory = $this->itemCategoryProvider->getOneById($offer->getCategoryId($id));
  92.         $namePL = is_null($itemCategory) ? '' : $itemCategory->getName();
  93.         $nameEN = is_null($itemCategory) ? '' : $itemCategory->getTranslation()['en']['nameCategory'] ?? '';
  94.         $viewParams['industry'] = ["namePL" =>  $namePL, "nameEn" =>  $nameEN];
  95.         $viewParams['seo'] = $this->seoIndustryProvider->getOneByIdIndustry($industryId);
  96.         if ($request->getMethod() === "POST") {
  97.             try {
  98.                $response =  $this->recaptchaService->getResponse($request);
  99.                 if ($response->success) {
  100.                     $locale      = $request->getLocale();
  101.                     $application = $this->applicationFactory->create($request);
  102.                     $this->applicationService->create($application, $locale);
  103.                     $this->addFlash('success', 'USER_ZONE.APPLICATION_MODAL.THANKS');
  104.                 }
  105.                 return $this->redirectToRoute(
  106.                     'front-offer',
  107.                     [
  108.                         '_locale' => $_locale,
  109.                         'offer' => $requestOffer,
  110.                         'temporaryId' => $temporaryId,
  111.                         'branch_extra' => $branchExtra
  112.                     ]
  113.                 );
  114.             } catch(Exception $e) {
  115.                 $this->addFlash('error', 'USER_ZONE.APPLICATION_MODAL.ERROR');
  116.                 return $this->redirectToRoute(
  117.                     'front-offer',
  118.                     [
  119.                         '_locale' => $_locale,
  120.                         'offer' => $requestOffer,
  121.                         'temporaryId' => $temporaryId,
  122.                         'branch_extra' => $branchExtra
  123.                     ]
  124.                 );
  125.             }
  126.         }
  127.         return $this->render(self::OFFER_VIEW_TEMPLATE_PATH, $viewParams);
  128.     }
  129.      /**
  130.      * @Route(
  131.      *      path="/{_locale}/{blog}/{url}",
  132.      *      name="blog",
  133.      *      requirements=
  134.      *     {
  135.      *          "_locale": "%app.supported_locales%",
  136.      *          "blog": "%app.supported_blog%"
  137.      *      }
  138.      * )
  139.      */
  140.     public function getBlogAction
  141.     (
  142.         Request $request,
  143.         string $_locale,
  144.         string $blog,
  145.         string $url,
  146.     ): Response
  147.     {
  148.         $request->setLocale($_locale);
  149.         $blog                      =   $this->pageProvider->getOneByUrl($url);
  150.         if(is_null($blog)) {
  151.             $error = ResponseMessage::onOfferNotFound($_locale);
  152.             return $this->pageNotFoundAction($error);
  153.         }
  154.         $viewParams['entity']       =   $blog->getPage();
  155.         $viewParams['links_in_footer'] = $this->footerLinkProvider->getAll();
  156.         $viewParams['blog']         =   $blog;
  157.         $viewParams['front_page']   =   true;
  158.         $viewParams['queryParams']  =   $request->query->all();
  159.        
  160.         return $this->render(self::BLOG_VIEW_TEMPLATE_PATH, $viewParams);
  161.     }
  162.      /**
  163.      * @Route(
  164.      *      path="/{_locale}/{prefix}/{code}",
  165.      *      name="code",
  166.      *      requirements={
  167.      *          "_locale": "%app.supported_locales%",
  168.      *          "code": "[^.]+"
  169.      *      }
  170.      * )
  171.      */
  172.     public function getOfferByCode
  173.     (
  174.         Request $request,
  175.         string $_locale,
  176.         string $code,
  177.     ):Response
  178.     {
  179.         $request->setLocale($_locale);
  180.         try {
  181.             $offer = $this->offerProvider->getOfferByCode($code);
  182.             $isDone = $offer->getExtraInfo()['isDone'];
  183.             $isActive = $offer->getExtraInfo()['isActive'];
  184.             if(false === $isDone || false === $isActive){ $offer = null; }
  185.         } catch(NoResultException $e){
  186.             $offer = null;
  187.         }
  188.         if(is_null($offer)) {
  189.             if($isDone   === false)  $error = ResponseMessage::onOfferNotFound($_locale);
  190.             if($isActive === false)  $error = ResponseMessage::onOfferHasExpired($_locale);
  191.             return $this->pageNotFoundAction($error);
  192.         }
  193.         $viewParams['links_in_footer'] = $this->footerLinkProvider->getAll();
  194.         $viewParams['offer']        =   $offer;
  195.         $viewParams['front_page']   =   false;
  196.         $viewParams['temporary_id'] =   $offer->getTemporaryId();
  197.         $viewParams['queryParams']  =   $request->query->all();
  198.         isset($offer)?$id           =   $offer->getCategoryId() : $id   =   '';
  199.         $viewParams['industry']     =   [
  200.         "namePL"                    =>  $this->itemCategoryProvider->getOneById($offer->getCategoryId($id))->getName(),
  201.         "nameEn"                    =>  $this->itemCategoryProvider->getOneById($offer->getCategoryId($id))->getTranslation()['en']['nameCategory']??''];
  202.         if($request->getMethod() === "POST"){
  203.             try{
  204.                 $locale      = $request->getLocale();
  205.                 $application = $this->applicationFactory->create($request);
  206.                 $this->applicationService->create($application, $locale);
  207.             }catch(Exception $e){}
  208.         }
  209.         return $this->render(self::OFFER_VIEW_TEMPLATE_PATH, $viewParams);
  210.     }
  211.     /**
  212.      * @Route(
  213.      *      path="/{_locale}/{url}",
  214.      *      name="page",
  215.      *      requirements={
  216.      *          "_locale": "%app.supported_locales%",
  217.      *          "url": "[^.]+"
  218.      *      }
  219.      * ),
  220.      * @ParamConverter(
  221.      *      "pageTranslation",
  222.      *      options={
  223.      *          "mapping": {
  224.      *              "_locale": "locale",
  225.      *              "url": "url"
  226.      *          }
  227.      *      }
  228.      * )
  229.      */
  230.     public function getPageAction
  231.     (
  232.         Request $request,
  233.         PageTranslation $pageTranslation,
  234.         string $_locale,
  235.         string $url = ''
  236.     ): Response
  237.     {
  238.         $request->setLocale($_locale);
  239.         $page = $pageTranslation->getTranslatable();
  240.         $templatePath = $page->getTemplatePath();
  241.         $forwardResponse = $this->getForwardResponseForPage($page, $request);
  242.         if (false === is_null($forwardResponse)) {
  243.             return $forwardResponse;
  244.         }
  245.         $viewParams = [
  246.             "entity" => $page,
  247.             "currentUrl" => [
  248.                 "locale" => $request->getLocale(),
  249.             ],
  250.             'queryParams' => $request->query->all(),
  251.             'links_in_footer' => $this->footerLinkProvider->getAll(),
  252.         ];
  253.         return $this->render($templatePath, $viewParams);
  254.     }
  255.     /**
  256.      * @Route("", name="hp", methods={"GET"})
  257.      */
  258.     public function homePageAction(Request $request): Response
  259.     {
  260.         return $this->pageNotFoundAction();
  261.     }
  262.     private function getForwardResponseForPage(Page $page, Request $request): ?Response
  263.     {
  264.         $controller = Pages::CONFIG_FOR_TYPES[$page->getMigrationId()] ?? null;
  265.         if (is_null($controller)) {
  266.             return null;
  267.         }
  268.         $viewParams['page'] = $page;
  269.         $viewParams['originalRequest'] = $request;
  270.         $viewParams['entity'] = $page;
  271.         $viewParams['queryParams'] = $request->query->all();
  272.         $viewParams['links_in_footer'] = $this->footerLinkProvider->getAll();
  273.         return $this->forward($controller, $viewParams);
  274.     }
  275.     private function pageNotFoundAction(string $error = null): Response
  276.     {
  277.         $page = $this->pageProvider->getOneByMigrationId(Pages::HOMEPAGE);
  278.         $viewParams['url'] = $page->getTranslations()['pl']->getUrl();
  279.         $viewParams['_locale'] = 'pl';
  280.         $viewParams['error']   = $error;
  281.         $viewParams['links_in_footer'] = $this->footerLinkProvider->getAll();
  282.         if (is_null($error) == false) {
  283.             $this->addFlash('response_message', $error);
  284.         }
  285.         return $this->redirectToRoute('front-page', $viewParams);
  286.     }
  287. }