src/Controller/Home/IndexController.php line 65

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Home;
  4. use JsonException;
  5. use App\Controller\Front\SecurityTokenTrait;
  6. use App\Controller\PageController;
  7. use App\Entity\Block;
  8. use App\Entity\Content;
  9. use App\Entity\Newsletter;
  10. use App\Entity\Page;
  11. use App\Entity\PageBlock;
  12. use App\Form\ApplicationType;
  13. use App\Form\NewsLetterType;
  14. use App\Model\ApplicationDTO;
  15. use App\Repository\ContentRepository;
  16. use App\Repository\MenuRepository;
  17. use App\Service\LangService;
  18. use App\Service\Mail;
  19. use Doctrine\Persistence\ManagerRegistry;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Contracts\Translation\TranslatorInterface;
  25. class IndexController extends PageController
  26. {
  27.     use SecurityTokenTrait;
  28.     private array $classe = [
  29.         // 'contact' => \App\Form\ContactType::class,
  30.     ];
  31.     /**
  32.      * page -> entity form.
  33.      *
  34.      * @var array
  35.      */
  36.     public $page_form = [
  37.         // 'contact' => ['contact'],
  38.     ];
  39.     private LangService $langService;
  40.     private ContentRepository $contentRepository;
  41.     private TranslatorInterface $translator;
  42.     public function __construct(LangService $langServiceContentRepository $contentRepositoryManagerRegistry $doctrineMenuRepository $menuRepositoryTranslatorInterface $translator)
  43.     {
  44.         parent::__construct($doctrine$menuRepository);
  45.         $this->langService $langService;
  46.         $this->contentRepository $contentRepository;
  47.         $this->translator $translator;
  48.     }
  49.     /**
  50.      * @return RedirectResponse|Response
  51.      *
  52.      * @Route("/{slug}", name="front_index_page", defaults={"slug": "accueil"}, options={"expose"=true})
  53.      * @throws JsonException
  54.      */
  55.     public function page($slugRequest $requestMail $mail): Response
  56.     {
  57.         $datas = [];
  58.         $this->_initDatas($request$slug);
  59.         $page $this->datas['page'];
  60.         /** @var Page|null $page */
  61.         $locale $request->getLocale();
  62.         if ($page && $page->getSlugs()[$locale] === $slug) {
  63.             if ($page->getHasFormApplication()) {
  64.                 $applicationDTO = new ApplicationDTO();
  65.                 $form $this->createForm(ApplicationType::class, $applicationDTO)->handleRequest($request);
  66.                 $datas['formApplication'] = $form->createView();
  67.                 if ($form->isSubmitted() && $form->isValid()) {
  68.                     $mail->sendApplicationMail($applicationDTO$slug);
  69.                     $message 'Merci pour ta candidature et l’intérêt que tu portes à CarFit. Nos équipes ont sorti les loupes et se posent sur ton profil. On revient vers toi très vite.';
  70.                     if ('de' === $locale) {
  71.                         $message 'Vielen Dank für deine Bewerbung und dein Interesse an CarFit. Unser Team hat die Lupen ausgepackt und prüft dein Profil. Wir melden uns zeitnah bei dir zurück.';
  72.                     }
  73.                     $this->addFlash('success'$message);
  74.                     return $this->redirectToRoute('front_index_page', ['slug' => $slug]);
  75.                 }
  76.             }
  77.             $array = [];
  78.             $datas['blocks'] = $this->_getDataPage($locale$page->getId());
  79.             if (empty($datas['blocks'])) {
  80.                 return $this->render('bundles/TwigBundle/Exception/error404.html.twig');
  81.             }
  82.             foreach ($datas['blocks'] as $key => $dt) {
  83.                 if (isset($dt->datas) && array_key_exists('meta_title'$dt->datas)) {
  84.                     // $datas['metas'] = $dt->datas;
  85.                 } else {
  86.                     $sub = [];
  87.                     if (true == $dt->getBlock()->getSubBlock()) {
  88.                         foreach ($dt->getBlockChildrens() as $children) {
  89.                             $templateChildren $children->getBlock()->getPath();
  90.                             $children->json['template'] = $templateChildren;
  91.                             $sub[] = $children->json;
  92.                         }
  93.                         $dt->datas['sub_blocks'] = $sub;
  94.                     }
  95.                     $template $dt->getBlock()->getPath();
  96.                     $module null;
  97.                     if ($dt->getBlock()->getModule()) {
  98.                         if ($dt->getBlock()->getModule()->getIsActivated()) {
  99.                             $module $dt->getBlock()->getModule();
  100.                         } else {
  101.                             $template false;
  102.                         }
  103.                     }
  104.                     if (isset($dt->datas['lang_block']) && $dt->datas['lang_block'] === $locale) {
  105.                         $array['blocks'][] = [
  106.                             'data' => $dt->datas,
  107.                             'template' => $template,
  108.                             'module' => $module,
  109.                         ];
  110.                         if ($dt->getBlock()->getFormType()) {
  111.                             $formType $dt->getBlock()->getFormType();
  112.                             $class explode('Type'$formType);
  113.                             $class $class[0];
  114.                             $model 'App\\Entity\\'.$class;
  115.                             $model = new $model();
  116.                             $form 'App\\Form\\'.$formType;
  117.                             $form $this->createForm(
  118.                                 ucfirst($form),
  119.                                 $model,
  120.                                 [
  121.                                     'action' => $this->generateUrl('ajax_form'),
  122.                                 ]
  123.                             );
  124.                             $datas['form'] = $form->createView();
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.             unset($datas['blocks']);
  130.             $datas array_merge($datas$array);
  131.         } else {
  132.             // If !$page redirect to welcome_page
  133.             $pages $this->getDoctrine()->getRepository(Page::class)->findAll();
  134.             if (!empty($pages)) {
  135.                 throw $this->createNotFoundException('Page Not exist');
  136.             }
  137.             return $this->render('home/welcome_page.html.twig', [
  138.                 'header' => 'header_default',
  139.                 'footer' => 'footer_default',
  140.             ]);
  141.         }
  142.         if (array_key_exists($slug$this->page_form)) {
  143.             $classe $this->page_form[$slug][0];
  144.             $model 'App\\Entity\\'.ucfirst($classe);
  145.             $model = new $model();
  146.             $form $this->createForm(
  147.                 $this->classe[$slug],
  148.                 $model,
  149.                 [
  150.                     'action' => $this->generateUrl('ajax_form'),
  151.                     'slug' => $slug ?? null,
  152.                 ]
  153.             );
  154.             $datas['form'] = $form->createView();
  155.         }
  156.         // newsletter
  157.         if ($page) {
  158.             $newsLetter = new NewsLetter();
  159.             $newsletter_form $this->createForm(
  160.                 NewsletterType::class,
  161.                 $newsLetter,
  162.                 [
  163.                     'action' => $this->generateUrl('ajax_form'),
  164.                 ]
  165.             );
  166.             $datas['newsletter_form'] = $newsletter_form->createView();
  167.             $datas['has_newsletter'] = $page->getHasNewsletter();
  168.         }
  169.         return $this->render('home/page/_generic-page.html.twig'$this->getDatas($datas));
  170.     }
  171.     /**
  172.      * @return array
  173.      */
  174.     private function _getDataPage(string $localeint $pageId)
  175.     {
  176.         $lang $this->langService->getCodeLanguage($locale);
  177.         $contents $this->contentRepository->getLangExist($lang$pageId);
  178.         $arrayPageBlocks = [];
  179.         /** @var Content $content */
  180.         foreach ($contents as $content) {
  181.             $json $content->getJson();
  182.             if ($content->getPageBlock()) {
  183.                 $content->getPageBlock()->setJsonData($json);
  184.                 $arrayPageBlocks[$content->getPageBlock()->getItemOrder()] = $content->getPageBlock();
  185.             } else {
  186.                 $content->getBlockChildren()->setJsonData($json);
  187.                 $arrayPageBlocks[$content->getBlockChildren()->getPageBlock()->getItemOrder()] = $content->getBlockChildren()->getPageBlock();
  188.             }
  189.         }
  190.         ksort($arrayPageBlocks);
  191.         return $this->_getData($arrayPageBlocks);
  192.     }
  193.     protected function _getDataSite(int $locale)
  194.     {
  195.         $page_blocks $this->getDoctrine()->getRepository(PageBlock::class)->getByPageNameBy('site'$locale);
  196.         return $this->_getData($page_blocks);
  197.     }
  198.     protected function _getDayliMessage()
  199.     {
  200.         return $this->getDoctrine()->getRepository(Block::class)->findByType(6);
  201.     }
  202.     /**
  203.      * @Route("/send/newsletter", name="index_home_send_newsletter")
  204.      */
  205.     public function sendNewsletter(Request $requestMail $mail): Response
  206.     {
  207.         dd($request->request->all());
  208.     }
  209. }