<?php
namespace App\Controller;
use App\Entity\StaticPage;
use App\Service\StaticPageBlockEntityService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class StaticPageController extends AbstractController
{
/**
* @Route("/page/{slug}", name="static_page.view", methods="GET")
*/
public function index(
$slug,
StaticPageBlockEntityService $blockEntityService,
EntityManagerInterface $manager,
Request $request
): Response
{
$page = $manager->getRepository(StaticPage::class)->findOneBy(['slug' => $slug, 'deleted_at' => null]);
if (!$page) {
throw $this->createNotFoundException();
}
$blocks = $blockEntityService->index($page);
usort($blocks, function ($a, $b) {
return $a['position'] - $b['position'];
});
// dd(json_decode($blocks[0]["data"]));
// BLOCK_HEADER sorting
$header = null;
$types = array_column($blocks, 'type');
if (\in_array('STATIC_PAGE_BLOCK_HEADER', $types, true)) {
$header_key = array_search('STATIC_PAGE_BLOCK_HEADER', $types, true);
$header = $blocks[$header_key]; // Retrieve BLOCK_HEADER
unset($blocks[$header_key]); // Remove it from the $blocks array
$blocks = array_values($blocks); // Redindexing array
// Get children
$parent_id = $header['id'];
$ids = array_column($blocks, 'id');
$header_children_media = [];
foreach ($blocks as $block) {
if ($block['parent'] === $parent_id && 'STATIC_PAGE_BLOCK_MEDIA' === $block['type']) {
$child_id = $block['id'];
$child_key = array_search($child_id, $ids, true);
unset($blocks[$child_key]); // Remove child from $blocks array
$header_children_media[] = $block['media']['file']; // Retrieve BLOCK_HEADER's media children
}
}
$header['media'] = $header_children_media;
}
$blocks = array_map(function ($block) {
$block['data'] = @json_decode($block['data'], true);
if (!\is_array($block['data'])) {
$block['data'] = [];
}
return $block;
}, $blocks);
return $this->render('static_page/static_page.html.twig', [
'page' => $page,
'blocks' => $blocks,
'block_header' => $header,
'homepage_static_page' => $request->get('homepage'),
]);
}
/**
* @Route("/theme/page/{slug}", name="theme-static_page.view", methods="GET")
*/
public function show($slug, StaticPageBlockEntityService $blockEntityService, Request $request)
{
$page = $this->getDoctrine()->getRepository(StaticPage::class)->findOneBy(['slug' => $slug, 'deleted_at' => null]);
if (!$page) {
throw $this->createNotFoundException();
}
$blocks = $blockEntityService->index($page);
usort($blocks, function ($a, $b) {
return $a['position'] - $b['position'];
});
// BLOCK_HEADER sorting
$header = null;
$types = array_column($blocks, 'type');
if (\in_array('STATIC_PAGE_BLOCK_HEADER', $types, true)) {
$header_key = array_search('STATIC_PAGE_BLOCK_HEADER', $types, true);
$header = $blocks[$header_key]; // Retrieve BLOCK_HEADER
unset($blocks[$header_key]); // Remove it from the $blocks array
$blocks = array_values($blocks); // Redindexing array
// Get children
$parent_id = $header['id'];
$ids = array_column($blocks, 'id');
$header_children_media = [];
foreach ($blocks as $block) {
if ($block['parent'] === $parent_id && 'STATIC_PAGE_BLOCK_MEDIA' === $block['type']) {
$child_id = $block['id'];
$child_key = array_search($child_id, $ids, true);
unset($blocks[$child_key]); // Remove child from $blocks array
$header_children_media[] = $block['media']['file']; // Retrieve BLOCK_HEADER's media children
}
}
$header['media'] = $header_children_media;
}
$blocks = array_map(function ($block) {
$block['data'] = @json_decode($block['data'], true);
if (!\is_array($block['data'])) {
$block['data'] = [];
}
return $block;
}, $blocks);
return $this->render('static_page/theme_static_page.html.twig', [
'page' => $page,
'blocks' => $blocks,
'block_header' => $header,
'homepage_static_page' => $request->get('homepage'),
]);
}
}