|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 4 | + */ |
| 5 | +namespace BD\SandboxBundle\QueryType; |
| 6 | + |
| 7 | +use BD\SandboxBundle\QueryType\Sorter\LocationOptionsSorter; |
| 8 | +use eZ\Publish\Core\QueryType\OptionsResolverBasedQueryType; |
| 9 | +use eZ\Publish\Core\QueryType\QueryType; |
| 10 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 11 | +use eZ\Publish\API\Repository\Values\Content\Location; |
| 12 | +use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
| 13 | + |
| 14 | +class LocationChildrenQueryType extends OptionsResolverBasedQueryType implements QueryType |
| 15 | +{ |
| 16 | + private $languages; |
| 17 | + |
| 18 | + private $excludedContentTypes; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var Sorter\LocationOptionsSorter |
| 22 | + */ |
| 23 | + private $sorter; |
| 24 | + |
| 25 | + public function __construct(LocationOptionsSorter $sorter, array $languages, array $excludedContentTypes) |
| 26 | + { |
| 27 | + $this->languages = $languages; |
| 28 | + $this->excludedContentTypes = $excludedContentTypes; |
| 29 | + $this->sorter = $sorter; |
| 30 | + } |
| 31 | + |
| 32 | + protected function configureOptions(OptionsResolver $optionsResolver) |
| 33 | + { |
| 34 | + $optionsResolver->setRequired('location'); |
| 35 | + $optionsResolver->setDefault('types', []); |
| 36 | + $optionsResolver->setAllowedValues('location', function ($value) { return $value instanceof Location; }); |
| 37 | + $optionsResolver->setAllowedTypes('types', 'array'); |
| 38 | + } |
| 39 | + |
| 40 | + protected function doGetQuery(array $parameters) |
| 41 | + { |
| 42 | + $query = new \eZ\Publish\API\Repository\Values\Content\LocationQuery(); |
| 43 | + |
| 44 | + $criteria = [ |
| 45 | + new Criterion\Visibility(Criterion\Visibility::VISIBLE), |
| 46 | + new Criterion\ParentLocationId($parameters['location']->id), |
| 47 | + new Criterion\LanguageCode($this->languages), |
| 48 | + ]; |
| 49 | + |
| 50 | + if (isset($parameters['types'])) { |
| 51 | + $criteria[] = new Criterion\ContentTypeIdentifier($parameters['types']); |
| 52 | + } |
| 53 | + |
| 54 | + if (!empty($this->excludedContentTypes)) { |
| 55 | + $criteria[] = new Criterion\LogicalNot( |
| 56 | + new Criterion\ContentTypeIdentifier($this->excludedContentTypes) |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + $query->filter = $criteria; |
| 61 | + |
| 62 | + $this->sorter->sortFromLocation($query, $parameters['location']); |
| 63 | + |
| 64 | + return $query; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the QueryType name. |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + public static function getName() |
| 72 | + { |
| 73 | + return 'LocationChildren'; |
| 74 | + } |
| 75 | +} |
0 commit comments