src/Controller/StaticPageController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\StaticPage;
  4. use App\Service\StaticPageBlockEntityService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class StaticPageController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/page/{slug}", name="static_page.view", methods="GET")
  14.      */
  15.     public function index(
  16.         $slug,
  17.         StaticPageBlockEntityService $blockEntityService,
  18.         EntityManagerInterface $manager,
  19.         Request $request
  20.     ): Response
  21.     {
  22.         $page $manager->getRepository(StaticPage::class)->findOneBy(['slug' => $slug'deleted_at' => null]);
  23.         if (!$page) {
  24.             throw $this->createNotFoundException();
  25.         }
  26.         $blocks $blockEntityService->index($page);
  27.         usort($blocks, function ($a$b) {
  28.             return $a['position'] - $b['position'];
  29.         });
  30. //        dd(json_decode($blocks[0]["data"]));
  31.         // BLOCK_HEADER sorting
  32.         $header null;
  33.         $types array_column($blocks'type');
  34.         if (\in_array('STATIC_PAGE_BLOCK_HEADER'$typestrue)) {
  35.             $header_key array_search('STATIC_PAGE_BLOCK_HEADER'$typestrue);
  36.             $header $blocks[$header_key]; // Retrieve BLOCK_HEADER
  37.             unset($blocks[$header_key]); // Remove it from the $blocks array
  38.             $blocks array_values($blocks); // Redindexing array
  39.             // Get children
  40.             $parent_id $header['id'];
  41.             $ids array_column($blocks'id');
  42.             $header_children_media = [];
  43.             foreach ($blocks as $block) {
  44.                 if ($block['parent'] === $parent_id && 'STATIC_PAGE_BLOCK_MEDIA' === $block['type']) {
  45.                     $child_id $block['id'];
  46.                     $child_key array_search($child_id$idstrue);
  47.                     unset($blocks[$child_key]); // Remove child from $blocks array
  48.                     $header_children_media[] = $block['media']['file']; // Retrieve BLOCK_HEADER's media children
  49.                 }
  50.             }
  51.             $header['media'] = $header_children_media;
  52.         }
  53.         $blocks array_map(function ($block) {
  54.             $block['data'] = @json_decode($block['data'], true);
  55.             if (!\is_array($block['data'])) {
  56.                 $block['data'] = [];
  57.             }
  58.             return $block;
  59.         }, $blocks);
  60.         return $this->render('static_page/static_page.html.twig', [
  61.             'page' => $page,
  62.             'blocks' => $blocks,
  63.             'block_header' => $header,
  64.             'homepage_static_page' => $request->get('homepage'),
  65.         ]);
  66.     }
  67.     /**
  68.      * @Route("/theme/page/{slug}", name="theme-static_page.view", methods="GET")
  69.      */
  70.     public function show($slugStaticPageBlockEntityService $blockEntityServiceRequest $request)
  71.     {
  72.         $page $this->getDoctrine()->getRepository(StaticPage::class)->findOneBy(['slug' => $slug'deleted_at' => null]);
  73.         if (!$page) {
  74.             throw $this->createNotFoundException();
  75.         }
  76.         $blocks $blockEntityService->index($page);
  77.         usort($blocks, function ($a$b) {
  78.             return $a['position'] - $b['position'];
  79.         });
  80.         // BLOCK_HEADER sorting
  81.         $header null;
  82.         $types array_column($blocks'type');
  83.         if (\in_array('STATIC_PAGE_BLOCK_HEADER'$typestrue)) {
  84.             $header_key array_search('STATIC_PAGE_BLOCK_HEADER'$typestrue);
  85.             $header $blocks[$header_key]; // Retrieve BLOCK_HEADER
  86.             unset($blocks[$header_key]); // Remove it from the $blocks array
  87.             $blocks array_values($blocks); // Redindexing array
  88.             // Get children
  89.             $parent_id $header['id'];
  90.             $ids array_column($blocks'id');
  91.             $header_children_media = [];
  92.             foreach ($blocks as $block) {
  93.                 if ($block['parent'] === $parent_id && 'STATIC_PAGE_BLOCK_MEDIA' === $block['type']) {
  94.                     $child_id $block['id'];
  95.                     $child_key array_search($child_id$idstrue);
  96.                     unset($blocks[$child_key]); // Remove child from $blocks array
  97.                     $header_children_media[] = $block['media']['file']; // Retrieve BLOCK_HEADER's media children
  98.                 }
  99.             }
  100.             $header['media'] = $header_children_media;
  101.         }
  102.         $blocks array_map(function ($block) {
  103.             $block['data'] = @json_decode($block['data'], true);
  104.             if (!\is_array($block['data'])) {
  105.                 $block['data'] = [];
  106.             }
  107.             return $block;
  108.         }, $blocks);
  109.         return $this->render('static_page/theme_static_page.html.twig', [
  110.             'page' => $page,
  111.             'blocks' => $blocks,
  112.             'block_header' => $header,
  113.             'homepage_static_page' => $request->get('homepage'),
  114.         ]);
  115.     }
  116. }