<?php
/**
*
* Copyright (c) 2017. The Cocktail
*
* This file is part of the Movistar Originales package.
*
*/
namespace AppBundle\QueryType;
use AppBundle\Exception\ContentNotFoundException;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\Core\QueryType\QueryType;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\Core\Repository\ContentTypeService;
class BlockQueryType implements QueryType
{
const CONTAINER_EXCLUDED = ['internal_dist'];
/** @var string[] */
private $languages;
private $searchService;
private $contentTypeService;
public function __construct(
SearchService $searchService,
ContentTypeService $contentTypeService
) {
$this->searchService = $searchService;
$this->contentTypeService = $contentTypeService;
}
/**
* @param string[] $value
*/
public function setLanguages(array $value)
{
$this->languages = $value;
}
/**
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function getQuery(array $parameters = [])
{
$locationId = $parameters['locationId'];
$container = $parameters['container'];
$contentTypesCriterias = $queryCriterias = [];
if (!$container) {
$locationId = $this->loadContainerBlocks($locationId);
} else {
$blockContentTypes = array_merge($this->loadBlockContentTypes(), $this->loadInternalContentTypes());
foreach ($blockContentTypes as $blockContentType) {
if (!in_array($blockContentType->identifier, self::CONTAINER_EXCLUDED)) {
$contentTypesCriterias[] = new Query\Criterion\ContentTypeIdentifier($blockContentType->identifier);
}
}
$queryCriterias[] = new Query\Criterion\LogicalOr($contentTypesCriterias);
}
$locationQuery = $this->getLocationQuery($locationId, $queryCriterias ?? []);
return $locationQuery;
}
public function getQueryByRenderer($renderer, array $parameters = [])
{
$locationId = $parameters['locationId'];
$locationId = $this->loadContainerBlocks($locationId);
if (!$locationId) {
return new ContentNotFoundException("Not Blocks Created");
}
$renderer = $this->rendererRepository->getRenderer($renderer);
$queryCriterias = $sortClauses = $orQueryCriterias = [];
$orQuery = new Query\Criterion\LogicalOr([
new Query\Criterion\ContentTypeIdentifier($renderer->getBlockType())
]);
$queryCriterias[] = $orQuery;
$locationQuery = $this->getLocationQuery($locationId, $queryCriterias);
$locationQuery->limit = 1;
return $locationQuery;
}
private function getLocationQuery($locationId, array $andCriterias = [])
{
$criterias = array_filter(array_merge($andCriterias, [
new Query\Criterion\ParentLocationId($locationId),
new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE),
new Query\Criterion\LanguageCode($this->languages),
new Query\Criterion\LogicalNot(new Query\Criterion\ContentTypeIdentifier('Folder'))
]));
$filter = new Query\Criterion\LogicalAnd($criterias);
$sortClauses[] = new SortClause\Location\Priority(LocationQuery::SORT_ASC);
$locationQuery = new LocationQuery();
$locationQuery->filter = $filter;
$locationQuery->sortClauses = $sortClauses;
return $locationQuery;
}
public function loadContainerBlocks($locationId)
{
$queryCriterias = new Query\Criterion\LogicalAnd([
new Query\Criterion\Field('name', Query\Criterion\Operator::EQ, 'Bloques'),
new Query\Criterion\ContentTypeIdentifier("folder"),
new Query\Criterion\ParentLocationId($locationId),
new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE),
]);
$locationQuery = new LocationQuery();
$locationQuery->filter = $queryCriterias;
$locationSearchResults = $this->searchService->findLocations($locationQuery);
$hits = $locationSearchResults->searchHits;
$locationId = count($hits) ? $hits[0]->valueObject->id : null;
return $locationId;
}
public static function getName()
{
return 'AppBundle:Block';
}
public function getSupportedParameters()
{
return [];
}
/**
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType[]
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
private function loadBlockContentTypes()
{
$group = $this->contentTypeService->loadContentTypeGroupByIdentifier('Bloques');
return $this->contentTypeService->loadContentTypes($group);
}
/**
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType[]
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
private function loadInternalContentTypes()
{
$group = $this->contentTypeService->loadContentTypeGroupByIdentifier('Internal');
return $this->contentTypeService->loadContentTypes($group);
}
}