<?php
namespace App\Controller;
use JsonException;
use App\Entity\Page;
use App\Entity\PageBlock;
use App\Repository\MenuRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
abstract class PageController extends AbstractController
{
protected ObjectManager $entityManager;
protected string $locale;
protected array $datas = [];
protected MenuRepository $menuRepository;
protected $slug;
public function __construct(ManagerRegistry $doctrine, MenuRepository $menuRepository)
{
$this->entityManager = $doctrine->getManager();
$this->menuRepository = $menuRepository;
}
protected function _getMetasPage($page)
{
if ($page->getMetas() && isset($page->getMetas()[$this->locale])) {
$this->datas['metas'] = $page->getMetas()[$this->locale];
}
}
protected function _getDataHeader(?string $locale = null)
{
$page_blocks = $this->entityManager->getRepository(PageBlock::class)->getbyNameByBlockType('header', $locale ?? $this->locale);
$this->datas['header'] = $this->_getData($page_blocks);
return $this->datas['header'];
}
protected function _getDataFooter(?string $locale = null)
{
$page_blocks = $this->entityManager->getRepository(PageBlock::class)->getbyNameByBlockType('footer', $locale ?? $this->locale);
$this->datas['footer'] = $this->_getData($page_blocks);
return $this->datas['footer'];
}
/**
* @return ?string
*/
protected function _getSlug(?string $slug = null): ?string
{
return $this->datas['slug'] = $slug;
}
protected function _getPage(string $slug)
{
return $this->datas['page'] = $this->entityManager->getRepository(Page::class)->getPageBySlugs($slug);
}
/**
* @throws JsonException
*/
public function _getMenus(): array
{
$menu = $this->menuRepository->findOneBy(['name' => 'main_menu']);
return $this->datas['menus'] = json_decode($menu->getJsonData(), null, 512, JSON_THROW_ON_ERROR);
}
/**
* @throws JsonException
*/
protected function _initDatas(Request $request, ?string $slug = null)
{
$this->locale = $request->getLocale();
$this->datas['locale'] = $this->locale;
$this->_getSlug($slug);
if (!isset($this->datas['header'])) {
$this->_getDataHeader();
}
if (!isset($this->datas['footer'])) {
$this->_getDataFooter();
}
if (!isset($this->datas['page']) && isset($this->datas['slug'])) {
$this->_getPage($this->datas['slug']);
}
if (!isset($this->datas['metas']) && isset($this->datas['page'])) {
$this->_getMetasPage($this->datas['page']);
}
if (!isset($this->datas['menus'])) {
$this->_getMenus();
}
}
/*
* 03/11/2021 update Manage Preview for draft
*/
protected function _getData(array $page_blocks)
{
$request = Request::createFromGlobals();
foreach ($page_blocks as $key => $pb) {
$jsonDatas = $request->get('preview') ? $pb->getJsonDataPreview() : $pb->getJsonData();
$pb->datas = json_decode($jsonDatas ?? $pb->getJsonData(), true, 512, JSON_THROW_ON_ERROR);
if ($pb->getBlock() && true === $pb->getBlock()->getSubBlock()) {
foreach ($pb->getBlockChildrens() as $p_b) {
$p_b->json = json_decode($p_b->getjsonData(), true, 512, JSON_THROW_ON_ERROR);
}
}
}
return $page_blocks;
}
public function getDatas(array $extraData = []): array
{
return array_merge($this->datas, $extraData);
}
protected function getPageByType(string $type)
{
switch ($type) {
case 'vehicle':
$page = $this->entityManager->getRepository(Page::class)->getPageBySlug('account-vehicle');
break;
case 'appointment':
$page = $this->entityManager->getRepository(Page::class)->getPageBySlug('account-appointment');
break;
case 'profil':
$page = $this->entityManager->getRepository(Page::class)->getPageBySlug('account-profil');
break;
case 'history':
$page = $this->entityManager->getRepository(Page::class)->getPageBySlug('account-history');
break;
default:
$page = $this->entityManager->getRepository(Page::class)->getPageBySlug($type);
break;
}
$this->datas['page'] = $page;
return $page;
}
}