/** * Finds Locations for the given query. * * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid * * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query * @param boolean $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. * * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult */ public function findLocations( LocationQuery $query, $filterOnUserPermissions = true ) { $query = clone $query; $query->filter = $query->filter ?: new Criterion\MatchAll(); $this->validateSortClauses( $query ); if ( $filterOnUserPermissions && !$this->permissionsCriterionHandler->addPermissionsCriterion( $query->filter ) ) { return new SearchResult( array( 'time' => 0, 'totalCount' => 0 ) ); } if ( $query->limit === null ) { $query->limit = self::MAX_LIMIT; } $result = $this->locationSearchHandler->findLocations( $query ); foreach ( $result->searchHits as $hit ) { $hit->valueObject = $this->domainMapper->buildLocationDomainObject( $hit->valueObject ); } return $result; }
/** * Creates the new $location in the content repository for the given content * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent * or the parent is a sub location of the location of the content * or if set the remoteId exists already * * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo * * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct * * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location */ public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) { $content = $this->repository->getContentService()->loadContent($contentInfo->id); $parentLocation = $this->loadLocation($locationCreateStruct->parentLocationId); if (!$this->repository->canUser('content', 'create', $content->contentInfo, $parentLocation)) { throw new UnauthorizedException('content', 'create'); } // Check if the parent is a sub location of one of the existing content locations (this also solves the // situation where parent location actually one of the content locations), // or if the content already has location below given location create struct parent $existingContentLocations = $this->loadLocations($content->contentInfo); if (!empty($existingContentLocations)) { foreach ($existingContentLocations as $existingContentLocation) { if (stripos($parentLocation->pathString, $existingContentLocation->pathString) !== false) { throw new InvalidArgumentException("\$locationCreateStruct", "Specified parent is a sub location of one of the existing content locations."); } if ($parentLocation->id == $existingContentLocation->parentLocationId) { throw new InvalidArgumentException("\$locationCreateStruct", "Content is already below the specified parent."); } } } $spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct($locationCreateStruct, $parentLocation, $content->contentInfo->mainLocationId !== null ? $content->contentInfo->mainLocationId : true, $content->contentInfo->id, $content->contentInfo->currentVersionNo); $this->repository->beginTransaction(); try { $newLocation = $this->persistenceHandler->locationHandler()->create($spiLocationCreateStruct); $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); foreach ($urlAliasNames as $languageCode => $name) { $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($newLocation->id, $newLocation->parentId, $name, $languageCode, $content->contentInfo->alwaysAvailable, $languageCode === $content->contentInfo->mainLanguageCode); } $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->domainMapper->buildLocationDomainObject($newLocation); }