<?php
/**
*
* Copyright (c) 2017. The Cocktail
*
* This file is part of the Movistar Originales package.
*
*/
namespace AppBundle\Controller;
use AppBundle\QueryType\FolderQueryType;
use AppBundle\QueryType\LocationQueryType;
use eZ\Publish\API\Repository\Values\Content\Location;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MenuController extends Controller
{
const IDENTIFIERS = [0 => 'serie', 1 => 'page'];
private $locationQueryType;
private $folderQueryType;
public function __construct(
LocationQueryType $locationQueryType,
FolderQueryType $folderQueryType
) {
$this->folderQueryType = $folderQueryType;
$this->locationQueryType = $locationQueryType;
}
public function headerAction($locationId)
{
$pages = $this->findChildrensLocations($locationId, 'page');
$subpages = [];
foreach ($pages as $page) {
$subpages[$page->valueObject->id] = $this->findChildrensLocations($page->valueObject->id, 'season');
}
$response = $this->render('@ezdesign/menu.html.twig', ['pages' => $pages, 'subpages' => $subpages]);
$response->setMaxAge(68000);
$response->setPublic();
$response->headers->add(['X-Location-Id' => $locationId]);
return $response;
}
public function footerAction(Location $location)
{
$locationId = $location->id;
if (!$footerLocationId = $this->findFooterLocationId($locationId)) {
// Hardcoded Originales footer
$footerLocationId = $this->findFooterLocationId(240);
}
$contentService = $this->get('ezpublish.api.service.content');
$content = $contentService->loadContentByContentInfo($location->contentInfo);
$tree = $this->footerTree($footerLocationId, $locationId);
return $this->render('@ezdesign/footer.html.twig', ['tree' => $tree, 'content' => $content]);
}
public function callToActionButtonsAction(Location $location)
{
$contentService = $this->get('ezpublish.api.service.content');
$content = $contentService->loadContentByContentInfo($location->contentInfo);
return $this->render('@ezdesign/header_button.html.twig', ['content' => $content]);
}
public function callToActionMobileButtonsAction(Location $location)
{
$contentService = $this->get('ezpublish.api.service.content');
$content = $contentService->loadContentByContentInfo($location->contentInfo);
return $this->render('@ezdesign/header_mobile_button.html.twig', ['content' => $content]);
}
private function footerTree($footerLocationId, $mainLocationId): array
{
$contentService = $this->get('ezpublish.api.service.content');
$tree = $childLocations = [];
foreach ($this->findChildrensLocations($footerLocationId, 'menu') as $menu) {
$menu = $contentService->loadContent($menu->valueObject->contentInfo->id);
$auto = $menu->getFieldValue('auto')->bool;
$childLocations = $childs = [];
if ($auto) {
$identifiers = $menu->getFieldValue('ezselection')->selection;
foreach ($identifiers as $identifier) {
$childLocations = array_merge($childLocations, $this->findChildrensLocations($mainLocationId, self::IDENTIFIERS[$identifier]));
}
} else {
$childLocations = $this->findChildrensLocations($menu->contentInfo->mainLocationId, 'menu_item');
}
foreach ($childLocations as $child) {
$childs[] = $contentService->loadContent($child->valueObject->contentInfo->id);
}
$tree[] = ['menu' => $menu, 'childs' => $childs];
}
return $tree;
}
private function findChildrensLocations($locationId, $identifier)
{
$searchService = $this->get('ezpublish.api.service.search');
$parameters = ['locationId' => $locationId, 'identifier' => $identifier];
$locationQuery = $this->locationQueryType->getQuery($parameters);
$results = $searchService->findLocations($locationQuery);
return $results->searchHits;
}
private function findFooterLocationId($locationId)
{
$parameters = ['locationId' => $locationId, 'name' => 'Footer'];
$folderQuery = $this->folderQueryType->getQuery($parameters);
$searchService = $this->get('ezpublish.api.service.search');
$results = $searchService->findLocations($folderQuery);
$hits = $results->searchHits;
$locationId = count($hits) ? $hits[0]->valueObject->id : null;
return $locationId;
}
}