src/AppBundle/QueryType/BlockQueryType.php line 164

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\QueryType;
  10. use AppBundle\Exception\ContentNotFoundException;
  11. use eZ\Publish\API\Repository\SearchService;
  12. use eZ\Publish\API\Repository\Values\Content\LocationQuery;
  13. use eZ\Publish\Core\QueryType\QueryType;
  14. use eZ\Publish\API\Repository\Values\Content\Query;
  15. use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
  16. use eZ\Publish\Core\Repository\ContentTypeService;
  17. class BlockQueryType implements QueryType
  18. {
  19.     const CONTAINER_EXCLUDED = ['internal_dist'];
  20.     /** @var string[] */
  21.     private $languages;
  22.     private $searchService;
  23.     private $contentTypeService;
  24.     public function __construct(
  25.         SearchService $searchService,
  26.         ContentTypeService $contentTypeService
  27.     ) {
  28.         $this->searchService $searchService;
  29.         $this->contentTypeService $contentTypeService;
  30.     }
  31.     /**
  32.      * @param string[] $value
  33.      */
  34.     public function setLanguages(array $value)
  35.     {
  36.         $this->languages $value;
  37.     }
  38.     /**
  39.      * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
  40.      */
  41.     public function getQuery(array $parameters = [])
  42.     {
  43.         $locationId $parameters['locationId'];
  44.         $container $parameters['container'];
  45.         $contentTypesCriterias $queryCriterias = [];
  46.         if (!$container) {
  47.             $locationId $this->loadContainerBlocks($locationId);
  48.         } else {
  49.             $blockContentTypes array_merge($this->loadBlockContentTypes(), $this->loadInternalContentTypes());
  50.             foreach ($blockContentTypes as $blockContentType) {
  51.                 if (!in_array($blockContentType->identifierself::CONTAINER_EXCLUDED)) {
  52.                     $contentTypesCriterias[] = new Query\Criterion\ContentTypeIdentifier($blockContentType->identifier);
  53.                 }
  54.             }
  55.             $queryCriterias[] = new Query\Criterion\LogicalOr($contentTypesCriterias);
  56.         }
  57.         $locationQuery $this->getLocationQuery($locationId$queryCriterias ?? []);
  58.         return $locationQuery;
  59.     }
  60.     public function getQueryByRenderer($renderer, array $parameters = [])
  61.     {
  62.         $locationId $parameters['locationId'];
  63.         $locationId $this->loadContainerBlocks($locationId);
  64.         if (!$locationId) {
  65.             return new ContentNotFoundException("Not Blocks Created");
  66.         }
  67.         $renderer $this->rendererRepository->getRenderer($renderer);
  68.         $queryCriterias $sortClauses $orQueryCriterias = [];
  69.         $orQuery = new Query\Criterion\LogicalOr([
  70.             new Query\Criterion\ContentTypeIdentifier($renderer->getBlockType())
  71.         ]);
  72.         $queryCriterias[] = $orQuery;
  73.         $locationQuery $this->getLocationQuery($locationId$queryCriterias);
  74.         $locationQuery->limit 1;
  75.         return $locationQuery;
  76.     }
  77.     private function getLocationQuery($locationId, array $andCriterias = [])
  78.     {
  79.         $criterias array_filter(array_merge($andCriterias, [
  80.             new Query\Criterion\ParentLocationId($locationId),
  81.             new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE),
  82.             new Query\Criterion\LanguageCode($this->languages),
  83.             new Query\Criterion\LogicalNot(new Query\Criterion\ContentTypeIdentifier('Folder'))
  84.         ]));
  85.         $filter = new Query\Criterion\LogicalAnd($criterias);
  86.         $sortClauses[] = new SortClause\Location\Priority(LocationQuery::SORT_ASC);
  87.         $locationQuery = new LocationQuery();
  88.         $locationQuery->filter $filter;
  89.         $locationQuery->sortClauses $sortClauses;
  90.         return $locationQuery;
  91.     }
  92.     public function loadContainerBlocks($locationId)
  93.     {
  94.         $queryCriterias = new Query\Criterion\LogicalAnd([
  95.             new Query\Criterion\Field('name'Query\Criterion\Operator::EQ'Bloques'),
  96.             new Query\Criterion\ContentTypeIdentifier("folder"),
  97.             new Query\Criterion\ParentLocationId($locationId),
  98.             new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE),
  99.         ]);
  100.         $locationQuery = new LocationQuery();
  101.         $locationQuery->filter $queryCriterias;
  102.         $locationSearchResults $this->searchService->findLocations($locationQuery);
  103.         $hits $locationSearchResults->searchHits;
  104.         $locationId count($hits) ? $hits[0]->valueObject->id null;
  105.         return $locationId;
  106.     }
  107.     public static function getName()
  108.     {
  109.         return 'AppBundle:Block';
  110.     }
  111.     public function getSupportedParameters()
  112.     {
  113.         return [];
  114.     }
  115.     /**
  116.      * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType[]
  117.      * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
  118.      */
  119.     private function loadBlockContentTypes()
  120.     {
  121.         $group $this->contentTypeService->loadContentTypeGroupByIdentifier('Bloques');
  122.         return $this->contentTypeService->loadContentTypes($group);
  123.     }
  124.     /**
  125.      * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType[]
  126.      * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
  127.      */
  128.     private function loadInternalContentTypes()
  129.     {
  130.         $group $this->contentTypeService->loadContentTypeGroupByIdentifier('Internal');
  131.         return $this->contentTypeService->loadContentTypes($group);
  132.     }
  133. }