src/Controller/Home/IndexController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Home;
  3. use App\Entity\Block;
  4. use App\Entity\Content;
  5. use App\Entity\Newsletter;
  6. use App\Entity\Page;
  7. use App\Entity\PageBlock;
  8. use App\Form\NewsLetterType;
  9. use App\Repository\ContentRepository;
  10. use App\Repository\MenuRepository;
  11. use App\Service\LangService;
  12. use App\Service\Mail;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class IndexController extends AbstractController
  18. {
  19.     /** @var array */
  20.     private $classe = [
  21.         //'contact' => \App\Form\ContactType::class,
  22.     ];
  23.     /**
  24.      * page -> entity form.
  25.      *
  26.      * @var array
  27.      */
  28.     public $page_form = [
  29.         //'contact' => ['contact'],
  30.     ];
  31.     /** @var LangService */
  32.     private $langService;
  33.     /** @var ContentRepository */
  34.     private $contentRepository;
  35.     public function __construct(LangService $langServiceContentRepository $contentRepository)
  36.     {
  37.         $this->langService $langService;
  38.         $this->contentRepository $contentRepository;
  39.     }
  40.     /**
  41.      * @param $slug
  42.      * @param Request $request
  43.      * @param MenuRepository $menuRepository
  44.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  45.      *
  46.      * @Route("/{slug}", name="front_index_page", defaults={"slug": "accueil"})
  47.      */
  48.     public function page($slugRequest $requestMenuRepository $menuRepository)
  49.     {
  50.         /** @var Page|null $page */
  51.         $page $this->getDoctrine()->getRepository(Page::class)->getPageBySlug($slug);
  52.         $locale $request->getLocale();
  53.         if ($page && $page->getSlugs()[$locale] === $slug) {
  54.             $array = [];
  55.             $datas['slug'] = $slug;
  56.             $datas['page'] = $page;
  57.             $datas['locale'] = $locale;
  58.             $datas['blocks'] = $this->_getDataPage($locale$page->getId());
  59.             if (empty($datas['blocks'])) {
  60.                 return new Response(
  61.                     $this->render('bundles/TwigBundle/Exception/error404.html.twig'),
  62.                     404
  63.                 );
  64.             }
  65.             //get Header & Footer
  66.             $datas['header'] = $this->_getDataHeader($locale);
  67.             $datas['footer'] = $this->_getDataFooter($locale);
  68.             $menu $menuRepository->findOneBy(['name' => 'main_menu']);
  69.             $datas['menus'] = json_decode($menu->getJsonData());
  70.             // Metas
  71.             $datas['metas'] = [];
  72.             if ($page->getMetas() && isset($page->getMetas()[$locale])) {
  73.                 $datas['metas'] = $page->getMetas()[$locale];
  74.             }
  75.             foreach ($datas['blocks'] as $key => $dt) {
  76.                 if (isset($dt->datas) && array_key_exists('meta_title'$dt->datas)) {
  77.                     //$datas['metas'] = $dt->datas;
  78.                 } else {
  79.                     $sub = [];
  80.                     if (true == $dt->getBlock()->getSubBlock()) {
  81.                         foreach ($dt->getBlockChildrens() as $children) {
  82.                             $templateChildren $children->getBlock()->getPath();
  83.                             $children->json['template'] = $templateChildren;
  84.                             $sub[] = $children->json;
  85.                         }
  86.                         $dt->datas['sub_blocks'] = $sub;
  87.                     }
  88.                     $template $dt->getBlock()->getPath();
  89.                     $module null;
  90.                     if ($dt->getBlock()->getModule()) {
  91.                         if ($dt->getBlock()->getModule()->getIsActivated()) {
  92.                             $module $dt->getBlock()->getModule();
  93.                         } else {
  94.                             $template false;
  95.                         }
  96.                     }
  97.                     if (isset($dt->datas['lang_block']) && $dt->datas['lang_block'] === $locale) {
  98.                         $array['blocks'][] = [
  99.                             'data' => $dt->datas,
  100.                             'template' => $template,
  101.                             'module' => $module
  102.                         ];
  103.                     }
  104.                 }
  105.             }
  106.             unset($datas['blocks']);
  107.             $datas array_merge($datas$array);
  108.         } else {
  109.             // If !$page redirect to welcome_page
  110.             $pages $this->getDoctrine()->getRepository(Page::class)->findAll();
  111.             if (!empty($pages)) {
  112.                 throw $this->createNotFoundException('Page Not exist');
  113.             }
  114.             return $this->render('home/welcome_page.html.twig', [
  115.                 'header' => 'header_default',
  116.                 'footer' => 'footer_default',
  117.             ]);
  118.         }
  119.         if (array_key_exists($slug$this->page_form)) {
  120.             $classe $this->page_form[$slug][0];
  121.             $model 'App\\Entity\\'.ucfirst($classe);
  122.             $model = new $model();
  123.             $form $this->createForm($this->classe[$slug], $model, [
  124.                     'action' => $this->generateUrl('ajax_form'),
  125.                     'slug' => $slug ?? null,
  126.                 ]
  127.             );
  128.             $datas['form'] = $form->createView();
  129.         }
  130.         //newsletter
  131.         if ($page) {
  132.             $newsLetter = new NewsLetter();
  133.             $newsletter_form $this->createForm(NewsletterType::class, $newsLetter, [
  134.                     'action' => $this->generateUrl('ajax_form'),
  135.                 ]
  136.             );
  137.             $datas['newsletter_form'] = $newsletter_form->createView();
  138.             $datas['has_newsletter'] = $page->getHasNewsletter();
  139.         }
  140.         return $this->render('home/page/_generic-page.html.twig'$datas);
  141.     }
  142.     /**
  143.      * @param string $locale
  144.      * @param int $pageId
  145.      * @return array
  146.      */
  147.     private function _getDataPage(string $localeint $pageId)
  148.     {
  149.         $lang $this->langService->getCodeLanguage($locale);
  150.         $contents $this->contentRepository->getLangExist($lang$pageId);
  151.         $arrayPageBlocks = [];
  152.         /** @var Content $content */
  153.         foreach ($contents as $content) {
  154.             $json $content->getJson();
  155.             if ($content->getPageBlock()) {
  156.                 $content->getPageBlock()->setJsonData($json);
  157.                 $arrayPageBlocks[$content->getPageBlock()->getItemOrder()] = $content->getPageBlock();
  158.             } else {
  159.                 $content->getBlockChildren()->setJsonData($json);
  160.                 $arrayPageBlocks[$content->getBlockChildren()->getPageBlock()->getItemOrder()] = $content->getBlockChildren()->getPageBlock();
  161.             }
  162.         }
  163.         ksort($arrayPageBlocks);
  164.         return $this->_getData($arrayPageBlocks);
  165.     }
  166.     protected function _getDataSite(int $locale)
  167.     {
  168.         $page_blocks $this->getDoctrine()->getRepository(PageBlock::class)->getByPageNameBy('site'$locale);
  169.         return $this->_getData($page_blocks);
  170.     }
  171.     protected function _getDataHeader(string $locale)
  172.     {
  173.         $page_blocks $this->getDoctrine()->getRepository(PageBlock::class)->getbyNameByBlockType('header'$locale);
  174.         return $this->_getData($page_blocks);
  175.     }
  176.     protected function _getDataFooter(string $locale)
  177.     {
  178.         $page_blocks $this->getDoctrine()->getRepository(PageBlock::class)->getbyNameByBlockType('footer'$locale);
  179.         return $this->_getData($page_blocks);
  180.     }
  181.     protected function _getDayliMessage()
  182.     {
  183.         return $this->getDoctrine()->getRepository(Block::class)->findByType(6);
  184.     }
  185.     /*
  186.      * 03/11/2021 update Manage Preview for draft
  187.      */
  188.     protected function _getData(array $page_blocks)
  189.     {
  190.         $request Request::createFromGlobals();
  191.         foreach ($page_blocks as $key => $pb) {
  192.             $jsonDatas $request->get('preview') ? $pb->getJsonDataPreview() : $pb->getJsonData();
  193.             $pb->datas json_decode($jsonDatas ?? $pb->getJsonData(), true);
  194.             if ($pb->getBlock() && true === $pb->getBlock()->getSubBlock()) {
  195.                 foreach ($pb->getBlockChildrens() as $p_b) {
  196.                     $p_b->json json_decode($p_b->getjsonData(), true);
  197.                 }
  198.             }
  199.         }
  200.         return $page_blocks;
  201.     }
  202.     /**
  203.      * @Route("/send/newsletter", name="index_home_send_newsletter")
  204.      */
  205.     public function sendNewsletter(Request $requestMail $mail)
  206.     {
  207.         dd($request->request->all());
  208.     }
  209. }