<?php
/**
*
* Copyright (c) 2017. The Cocktail
*
* This file is part of the Movistar Originales package.
*
*/
namespace AppBundle\Controller;
use AppBundle\Repository\BlockRepository;
use AppBundle\Pagination\PageableBlock;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use eZ\Publish\API\Repository\Exceptions;
class BlockController extends AbstractController
{
const PAGEABLE = ['m9_block'];
private $contentService;
private $locationService;
private $blockRepository;
private $pageableBlock;
private $requestStack;
public function __construct(
ContentService $contentService,
LocationService $locationService,
BlockRepository $blockRepository,
PageableBlock $pageableBlock,
RequestStack $requestStack
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->blockRepository = $blockRepository;
$this->pageableBlock = $pageableBlock;
$this->requestStack = $requestStack;
}
/**
* @param $locationId
* @param bool $container
*
* @return Response
*
* @throws Exceptions\InvalidArgumentException
* @throws Exceptions\NotFoundException
* @throws Exceptions\UnauthorizedException
*/
public function renderBlocksAction(int $locationId, bool $container = false)
{
$modules = $this->blockRepository->loadBlocksByParentLocation($locationId, $container);
return $this->render('@ezdesign/block/list.html.twig', ['modules' => $modules, 'locationId' => $locationId]);
}
/**
* @param ContentView $view
*
* @return ContentView
*
* @throws Exceptions\NotFoundException
* @throws Exceptions\UnauthorizedException
*/
public function pageableBlockAction(ContentView $view)
{
$blockContent = $view->getContent();
if ($blockContent->getFieldValue('auto')->bool) {
try {
$masterRequest = $this->requestStack->getMasterRequest();
$page = $masterRequest->get('page', 1);
$max = $blockContent->getFieldValue('auto_limit')->value ?? 6;
$path = $view->getLocation()->path;
array_splice($path, -2);
$pathString = '/'.implode("/", $path).'/';
$parent = $this->locationService->loadLocation(end($path));
$pager = $this->pageableBlock->getPager($page, $max, null, $pathString);
$view->setParameters(['pager' => $pager, 'parent' => $parent]);
} catch (OutOfRangeCurrentPageException $exception) {
throw new NotFoundHttpException('Page not found');
}
}
return $view;
}
/**
* @param int $contentId
* @return Response
* @throws Exceptions\NotFoundException
* @throws Exceptions\UnauthorizedException
*/
public function renderIconBlockAction(int $contentId)
{
$rootContent = $this->contentService->loadContent($contentId);
return $this->render(':content:_icon.html.twig', ['content' => $rootContent]);
}
/**
* @api Busca los bloques paginables y renderiza metas
*
* @param int $locationId
*
* @return Response
*
* @throws Exceptions\InvalidArgumentException
* @throws Exceptions\NotFoundException
* @throws Exceptions\UnauthorizedException
*/
public function paginationMetaAction(int $locationId)
{
$masterRequest = $this->requestStack->getMasterRequest();
$modules = $this->blockRepository->loadBlocksByParentLocation($locationId);
foreach ($modules as $module) {
$identifier = $module->getContentType()->identifier;
if (\in_array($identifier, self::PAGEABLE)) {
try {
$page = $masterRequest->get('page', 1);
$max = $module->getFieldValue('auto_limit')->value ?? 6;
$location = $this->locationService->loadLocation($locationId);
$pager = $this->pageableBlock->getPager($page, $max, null, $location->pathString);
$next = $pager->hasNextPage() ? $pager->getNextPage(): null;
$previous = $pager->hasPreviousPage() ? $pager->getPreviousPage(): null;
} catch (OutOfRangeCurrentPageException $exception) {
throw new NotFoundHttpException('Page not found');
}
}
}
$route = $masterRequest->attributes->get("_route");
$routeParams = $masterRequest->attributes->get("_route_params");
return $this->render(':content:_metapagination.html.twig', [
'block' => $identifier ?? null,
'next' => $next ?? null,
'previous' => $previous ?? null,
'route' => $route,
'routeParams' => $routeParams,
]);
}
}