src/AppBundle/Controller/BlockController.php line 122

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\Repository\BlockRepository;
  11. use AppBundle\Pagination\PageableBlock;
  12. use eZ\Publish\API\Repository\ContentService;
  13. use eZ\Publish\API\Repository\LocationService;
  14. use eZ\Publish\Core\MVC\Symfony\View\ContentView;
  15. use Pagerfanta\Exception\OutOfRangeCurrentPageException;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  20. use eZ\Publish\API\Repository\Exceptions;
  21. class BlockController extends AbstractController
  22. {
  23.     const PAGEABLE = ['m9_block'];
  24.     private $contentService;
  25.     private $locationService;
  26.     private $blockRepository;
  27.     private $pageableBlock;
  28.     private $requestStack;
  29.     public function __construct(
  30.         ContentService $contentService,
  31.         LocationService $locationService,
  32.         BlockRepository $blockRepository,
  33.         PageableBlock $pageableBlock,
  34.         RequestStack $requestStack
  35.     ) {
  36.         $this->contentService $contentService;
  37.         $this->locationService $locationService;
  38.         $this->blockRepository $blockRepository;
  39.         $this->pageableBlock $pageableBlock;
  40.         $this->requestStack $requestStack;
  41.     }
  42.     /**
  43.      * @param $locationId
  44.      * @param bool $container
  45.      *
  46.      * @return Response
  47.      *
  48.      * @throws Exceptions\InvalidArgumentException
  49.      * @throws Exceptions\NotFoundException
  50.      * @throws Exceptions\UnauthorizedException
  51.      */
  52.     public function renderBlocksAction(int $locationIdbool $container false)
  53.     {
  54.         $modules $this->blockRepository->loadBlocksByParentLocation($locationId$container);
  55.         return $this->render('@ezdesign/block/list.html.twig', ['modules' => $modules'locationId' => $locationId]);
  56.     }
  57.     /**
  58.      * @param ContentView $view
  59.      *
  60.      * @return ContentView
  61.      *
  62.      * @throws Exceptions\NotFoundException
  63.      * @throws Exceptions\UnauthorizedException
  64.      */
  65.     public function pageableBlockAction(ContentView $view)
  66.     {
  67.         $blockContent $view->getContent();
  68.         if ($blockContent->getFieldValue('auto')->bool) {
  69.             try {
  70.                 $masterRequest $this->requestStack->getMasterRequest();
  71.                 $page $masterRequest->get('page'1);
  72.                 $max $blockContent->getFieldValue('auto_limit')->value ?? 6;
  73.                 $path $view->getLocation()->path;
  74.                 array_splice($path, -2);
  75.                 $pathString '/'.implode("/"$path).'/';
  76.                 $parent $this->locationService->loadLocation(end($path));
  77.                 $pager $this->pageableBlock->getPager($page$maxnull$pathString);
  78.                 $view->setParameters(['pager' =>  $pager'parent' => $parent]);
  79.             } catch (OutOfRangeCurrentPageException $exception) {
  80.                 throw new NotFoundHttpException('Page not found');
  81.             }
  82.         }
  83.         return $view;
  84.     }
  85.     /**
  86.      * @param int $contentId
  87.      * @return Response
  88.      * @throws Exceptions\NotFoundException
  89.      * @throws Exceptions\UnauthorizedException
  90.      */
  91.     public function renderIconBlockAction(int $contentId)
  92.     {
  93.         $rootContent $this->contentService->loadContent($contentId);
  94.         return $this->render(':content:_icon.html.twig', ['content' => $rootContent]);
  95.     }
  96.     /**
  97.      * @api Busca los bloques paginables y renderiza metas
  98.      *
  99.      * @param int $locationId
  100.      *
  101.      * @return Response
  102.      *
  103.      * @throws Exceptions\InvalidArgumentException
  104.      * @throws Exceptions\NotFoundException
  105.      * @throws Exceptions\UnauthorizedException
  106.      */
  107.     public function paginationMetaAction(int $locationId)
  108.     {
  109.         $masterRequest $this->requestStack->getMasterRequest();
  110.         $modules $this->blockRepository->loadBlocksByParentLocation($locationId);
  111.         foreach ($modules as $module) {
  112.             $identifier $module->getContentType()->identifier;
  113.             if (\in_array($identifierself::PAGEABLE)) {
  114.                 try {
  115.                     $page $masterRequest->get('page'1);
  116.                     $max $module->getFieldValue('auto_limit')->value ?? 6;
  117.                     $location $this->locationService->loadLocation($locationId);
  118.                     $pager $this->pageableBlock->getPager($page$maxnull$location->pathString);
  119.                     $next $pager->hasNextPage() ? $pager->getNextPage(): null;
  120.                     $previous $pager->hasPreviousPage() ? $pager->getPreviousPage(): null;
  121.                 } catch (OutOfRangeCurrentPageException $exception) {
  122.                     throw new NotFoundHttpException('Page not found');
  123.                 }
  124.             }
  125.         }
  126.         $route $masterRequest->attributes->get("_route");
  127.         $routeParams $masterRequest->attributes->get("_route_params");
  128.         return $this->render(':content:_metapagination.html.twig', [
  129.             'block' => $identifier ?? null,
  130.             'next' => $next ?? null,
  131.             'previous' => $previous ?? null,
  132.             'route' => $route,
  133.             'routeParams' => $routeParams,
  134.         ]);
  135.     }
  136. }