src/Controller/PageController.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use JsonException;
  4. use App\Entity\Page;
  5. use App\Entity\PageBlock;
  6. use App\Repository\MenuRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Doctrine\Persistence\ObjectManager;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. abstract class PageController extends AbstractController
  12. {
  13.     protected ObjectManager $entityManager;
  14.     protected string  $locale;
  15.     protected array $datas = [];
  16.     protected MenuRepository $menuRepository;
  17.     protected $slug;
  18.     public function __construct(ManagerRegistry $doctrineMenuRepository $menuRepository)
  19.     {
  20.         $this->entityManager $doctrine->getManager();
  21.         $this->menuRepository $menuRepository;
  22.     }
  23.     protected function _getMetasPage($page)
  24.     {
  25.         if ($page->getMetas() && isset($page->getMetas()[$this->locale])) {
  26.             $this->datas['metas'] = $page->getMetas()[$this->locale];
  27.         }
  28.     }
  29.     protected function _getDataHeader(?string $locale null)
  30.     {
  31.         $page_blocks $this->entityManager->getRepository(PageBlock::class)->getbyNameByBlockType('header'$locale ?? $this->locale);
  32.         $this->datas['header'] = $this->_getData($page_blocks);
  33.         return $this->datas['header'];
  34.     }
  35.     protected function _getDataFooter(?string $locale null)
  36.     {
  37.         $page_blocks $this->entityManager->getRepository(PageBlock::class)->getbyNameByBlockType('footer'$locale ?? $this->locale);
  38.         $this->datas['footer'] = $this->_getData($page_blocks);
  39.         return $this->datas['footer'];
  40.     }
  41.     /**
  42.      * @return ?string
  43.      */
  44.     protected function _getSlug(?string $slug null): ?string
  45.     {
  46.         return $this->datas['slug'] = $slug;
  47.     }
  48.     protected function _getPage(string $slug)
  49.     {
  50.         return $this->datas['page'] = $this->entityManager->getRepository(Page::class)->getPageBySlugs($slug);
  51.     }
  52.     /**
  53.      * @throws JsonException
  54.      */
  55.     public function _getMenus(): array
  56.     {
  57.         $menu $this->menuRepository->findOneBy(['name' => 'main_menu']);
  58.         return $this->datas['menus'] = json_decode($menu->getJsonData(), null512JSON_THROW_ON_ERROR);
  59.     }
  60.     /**
  61.      * @throws JsonException
  62.      */
  63.     protected function _initDatas(Request $request, ?string $slug null)
  64.     {
  65.         $this->locale $request->getLocale();
  66.         $this->datas['locale'] = $this->locale;
  67.         $this->_getSlug($slug);
  68.         if (!isset($this->datas['header'])) {
  69.             $this->_getDataHeader();
  70.         }
  71.         if (!isset($this->datas['footer'])) {
  72.             $this->_getDataFooter();
  73.         }
  74.         if (!isset($this->datas['page']) && isset($this->datas['slug'])) {
  75.             $this->_getPage($this->datas['slug']);
  76.         }
  77.         if (!isset($this->datas['metas']) && isset($this->datas['page'])) {
  78.             $this->_getMetasPage($this->datas['page']);
  79.         }
  80.         if (!isset($this->datas['menus'])) {
  81.             $this->_getMenus();
  82.         }
  83.     }
  84.     /*
  85.      * 03/11/2021 update Manage Preview for draft
  86.      */
  87.     protected function _getData(array $page_blocks)
  88.     {
  89.         $request Request::createFromGlobals();
  90.         foreach ($page_blocks as $key => $pb) {
  91.             $jsonDatas $request->get('preview') ? $pb->getJsonDataPreview() : $pb->getJsonData();
  92.             $pb->datas json_decode($jsonDatas ?? $pb->getJsonData(), true512JSON_THROW_ON_ERROR);
  93.             if ($pb->getBlock() && true === $pb->getBlock()->getSubBlock()) {
  94.                 foreach ($pb->getBlockChildrens() as $p_b) {
  95.                     $p_b->json json_decode($p_b->getjsonData(), true512JSON_THROW_ON_ERROR);
  96.                 }
  97.             }
  98.         }
  99.         return $page_blocks;
  100.     }
  101.     public function getDatas(array $extraData = []): array
  102.     {
  103.         return array_merge($this->datas$extraData);
  104.     }
  105.     protected function getPageByType(string $type)
  106.     {
  107.         switch ($type) {
  108.             case 'vehicle':
  109.                 $page $this->entityManager->getRepository(Page::class)->getPageBySlug('account-vehicle');
  110.                 break;
  111.             case 'appointment':
  112.                 $page $this->entityManager->getRepository(Page::class)->getPageBySlug('account-appointment');
  113.                 break;
  114.             case 'profil':
  115.                 $page $this->entityManager->getRepository(Page::class)->getPageBySlug('account-profil');
  116.                 break;
  117.             case 'history':
  118.                 $page $this->entityManager->getRepository(Page::class)->getPageBySlug('account-history');
  119.                 break;
  120.             default:
  121.                 $page $this->entityManager->getRepository(Page::class)->getPageBySlug($type);
  122.                 break;
  123.         }
  124.         $this->datas['page'] = $page;
  125.         return $page;
  126.     }
  127. }