diff --git a/Services/FetchHelper.php b/Services/FetchHelper.php index f7a7f71..f2f7b7f 100644 --- a/Services/FetchHelper.php +++ b/Services/FetchHelper.php @@ -94,21 +94,20 @@ public function fetchChildren($parentLocation, $contentClass = null, $limit = se $parentLocationId = $parentLocation instanceof Location ? $parentLocation->id : $parentLocation; $params[] = new Criterion\ParentLocationId($parentLocationId); - return $this->fetchLocationList($params, $limit); + return $this->fetchLocationList($params, null, $limit); } /** * @param array $params * @param int $limit * @param int $offset - * @param string $sortClauses - * parametre which specifies the way the request will be sorted : "depth" or "prioriy" + * @param SortClause[]|null $sortClauses If null, results will be sorted by priority * @return Location[] * @throws InvalidArgumentException */ private function fetchLocationList( array $params, - $sortClauses = "priority", + $sortClauses = null, $limit = self::LIMIT, $offset = 0 ): array { @@ -127,16 +126,8 @@ private function fetchLocationList( $query->performCount = true; $query->limit = $limit; $query->offset = $offset; - if ($sortClauses == "depth") { - $query->sortClauses = [ - new SortClause\Location\Depth(Query::SORT_ASC), - ]; - } - if ($sortClauses == "priority") { - $query->sortClauses = [ - new SortClause\Location\Priority(Query::SORT_ASC), - ]; - } + $query->sortClauses = is_null($sortClauses) ? + [ new SortClause\Location\Priority(Query::SORT_ASC) ] : $sortClauses; $results = $this->searchService->findLocations($query); $items = []; @@ -231,9 +222,8 @@ public function fetchAncestors($location, string $contentType): ?array new Criterion\Ancestor($location->pathString), ]; - $sortClauses = "depth"; - $items = $this->fetchLocationList($params, $sortClauses); - return $items; + $sortClauses = [ new SortClause\Location\Depth(Query::SORT_ASC) ]; + return $this->fetchLocationList($params, $sortClauses); } /** @@ -242,17 +232,20 @@ public function fetchAncestors($location, string $contentType): ?array * @param Location|int $location * @param string $contentType * @param bool $highest - * param which specifies if fetchAncestor must return the most distant or closest ancestor + * param which specifies if fetchAncestor must return the highest ancestor in tree structure (smallest depth) * @return Location|null * @throws InvalidArgumentException */ public function fetchAncestor($location, string $contentType, bool $highest = true): ?Location { $results = $this->fetchAncestors($location, $contentType); - if ($highest) { - $itemHit = array_shift($results); - } elseif ($highest == false) { - $itemHit = array_pop($results); + $itemHit = null; + if (is_array($results) && count($results)) { + if ($highest) { + $itemHit = array_shift($results); + } else { + $itemHit = array_pop($results); + } } return ($itemHit instanceof Location) ? $itemHit : null; } @@ -264,7 +257,7 @@ public function fetchAncestor($location, string $contentType, bool $highest = tr */ private function fetchLocation(array $params): ?Location { - $results = $this->fetchLocationList($params, 1); + $results = $this->fetchLocationList($params, null, 1); $itemHit = reset($results); return ($itemHit instanceof Location) ? $itemHit : null; @@ -340,8 +333,7 @@ public function recursiveAncestorGetField(Location $location, string $fieldIdent $content = $location->getContent(); // Check if $location has a relation in field - if ( - $content->getContentType()->getFieldDefinition($fieldIdentifier) == null || + if ($content->getContentType()->getFieldDefinition($fieldIdentifier) == null || $this->fieldhelper->isFieldEmpty($content, $fieldIdentifier) ) { // No relation to a header object then check parent location diff --git a/Services/Twig/FetchExtension.php b/Services/Twig/FetchExtension.php index 4a46b5e..7b12b46 100644 --- a/Services/Twig/FetchExtension.php +++ b/Services/Twig/FetchExtension.php @@ -49,11 +49,11 @@ public function getFunctions() return [ new TwigFunction('render_children', [$this, 'renderChildren'], ['is_safe' => ['all']]), - new TwigFunction('fetch_children', [$this, 'fetchChildren']), - new TwigFunction('fetch_ancestor', [$this, 'fetchAncestor']), - new TwigFunction('fetch_ancestors', [$this, 'fetchAncestors']), - new TwigFunction('fetch_content', [$this, 'fetchContent']), - new TwigFunction('fetch_location', [$this, 'fetchLocation']), + new TwigFunction('fetch_children', [$this->fetchHelper, 'fetchChildren']), + new TwigFunction('fetch_ancestor', [$this->fetchHelper, 'fetchAncestor']), + new TwigFunction('fetch_ancestors', [$this->fetchHelper, 'fetchAncestors']), + new TwigFunction('fetch_content', [$this->repository->getContentService(), 'loadContent']), + new TwigFunction('fetch_location', [$this->repository->getLocationService(), 'loadLocation']), ]; } @@ -117,66 +117,6 @@ public function renderChildren( return $render; } - /** - * @param Location|int $parentLocation - * @param string|null $contentClass - * @return array - * @throws InvalidArgumentException - */ - public function fetchChildren($parentLocation, $contentClass = null): array - { - return $this->fetchHelper->fetchChildren($parentLocation, $contentClass); - } - - /** - * Fetch ancestor of $location with specified $contentType - * - * @param Location|int $location - * @param string $contentType - * @param bool $highest - * @return Location|null - * @throws InvalidArgumentException - */ - public function fetchAncestor($location, string $contentType, bool $highest): ?Location - { - return $this->fetchHelper->fetchAncestor($location, $contentType, $highest); - } - - /** - * Fetch ancestor of $location with specified $contentType - * - * @param Location|int $location - * @param string $contentType - * @return Location|null - * @throws InvalidArgumentException - */ - public function fetchAncestors($location, string $contentType): ?array - { - return $this->fetchHelper->fetchAncestors($location, $contentType); - } - - /** - * @param int $contentId - * @return Content - * @throws NotFoundException - * @throws UnauthorizedException - */ - public function fetchContent(int $contentId): Content - { - return $this->repository->getContentService()->loadContent(intval($contentId)); - } - - /** - * @param int $locationId - * @return Location - * @throws NotFoundException - * @throws UnauthorizedException - */ - public function fetchLocation(int $locationId): Location - { - return $this->repository->getLocationService()->loadLocation(intval($locationId)); - } - public function getName(): string { return 'sqli_twig_extension_fetch';