src/AppBundle/Controller/MenuController.php line 60

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * Copyright (c) 2017. The Cocktail
  5.  *
  6.  * This file is part of the Movistar Originales package.
  7.  *
  8.  */
  9. namespace AppBundle\Controller;
  10. use AppBundle\QueryType\FolderQueryType;
  11. use AppBundle\QueryType\LocationQueryType;
  12. use eZ\Publish\API\Repository\Values\Content\Location;
  13. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  14. class MenuController extends Controller
  15. {
  16.     const IDENTIFIERS = [=> 'serie'=> 'page'];
  17.     private $locationQueryType;
  18.     private $folderQueryType;
  19.     public function __construct(
  20.         LocationQueryType $locationQueryType,
  21.         FolderQueryType $folderQueryType
  22.     ) {
  23.         $this->folderQueryType $folderQueryType;
  24.         $this->locationQueryType $locationQueryType;
  25.     }
  26.     public function headerAction($locationId)
  27.     {
  28.         $pages $this->findChildrensLocations($locationId'page');
  29.         $subpages = [];
  30.         foreach ($pages as $page) {
  31.             $subpages[$page->valueObject->id] = $this->findChildrensLocations($page->valueObject->id'season');
  32.         }
  33.         $response $this->render('@ezdesign/menu.html.twig', ['pages' => $pages'subpages' => $subpages]);
  34.         $response->setMaxAge(68000);
  35.         $response->setPublic();
  36.         $response->headers->add(['X-Location-Id' => $locationId]);
  37.         return $response;
  38.     }
  39.     public function footerAction(Location $location)
  40.     {
  41.         $locationId $location->id;
  42.         if (!$footerLocationId $this->findFooterLocationId($locationId)) {
  43.             // Hardcoded Originales footer
  44.             $footerLocationId $this->findFooterLocationId(240);
  45.         }
  46.         $contentService $this->get('ezpublish.api.service.content');
  47.         $content $contentService->loadContentByContentInfo($location->contentInfo);
  48.         $tree $this->footerTree($footerLocationId$locationId);
  49.         return $this->render('@ezdesign/footer.html.twig', ['tree' => $tree'content' => $content]);
  50.     }
  51.     public function callToActionButtonsAction(Location $location)
  52.     {
  53.         $contentService $this->get('ezpublish.api.service.content');
  54.         $content $contentService->loadContentByContentInfo($location->contentInfo);
  55.         return $this->render('@ezdesign/header_button.html.twig', ['content' => $content]);
  56.     }
  57.     public function callToActionMobileButtonsAction(Location $location)
  58.     {
  59.         $contentService $this->get('ezpublish.api.service.content');
  60.         $content $contentService->loadContentByContentInfo($location->contentInfo);
  61.         return $this->render('@ezdesign/header_mobile_button.html.twig', ['content' => $content]);
  62.     }
  63.     private function footerTree($footerLocationId$mainLocationId): array
  64.     {
  65.         $contentService $this->get('ezpublish.api.service.content');
  66.         $tree $childLocations = [];
  67.         foreach ($this->findChildrensLocations($footerLocationId'menu') as $menu) {
  68.             $menu $contentService->loadContent($menu->valueObject->contentInfo->id);
  69.             $auto $menu->getFieldValue('auto')->bool;
  70.             $childLocations $childs = [];
  71.             if ($auto) {
  72.                 $identifiers $menu->getFieldValue('ezselection')->selection;
  73.                 foreach ($identifiers as $identifier) {
  74.                     $childLocations array_merge($childLocations$this->findChildrensLocations($mainLocationIdself::IDENTIFIERS[$identifier]));
  75.                 }
  76.             } else {
  77.                 $childLocations $this->findChildrensLocations($menu->contentInfo->mainLocationId'menu_item');
  78.             }
  79.             foreach ($childLocations as $child) {
  80.                 $childs[] = $contentService->loadContent($child->valueObject->contentInfo->id);
  81.             }
  82.             $tree[] = ['menu' => $menu'childs' => $childs];
  83.         }
  84.         return $tree;
  85.     }
  86.     private function findChildrensLocations($locationId$identifier)
  87.     {
  88.         $searchService $this->get('ezpublish.api.service.search');
  89.         $parameters = ['locationId' => $locationId'identifier' => $identifier];
  90.         $locationQuery $this->locationQueryType->getQuery($parameters);
  91.         $results $searchService->findLocations($locationQuery);
  92.         return $results->searchHits;
  93.     }
  94.     private function findFooterLocationId($locationId)
  95.     {
  96.         $parameters = ['locationId' => $locationId'name' => 'Footer'];
  97.         $folderQuery $this->folderQueryType->getQuery($parameters);
  98.         $searchService $this->get('ezpublish.api.service.search');
  99.         $results $searchService->findLocations($folderQuery);
  100.         $hits $results->searchHits;
  101.         $locationId count($hits) ? $hits[0]->valueObject->id null;
  102.         return $locationId;
  103.     }
  104. }