/**
  * Recovers the $trashedLocation at its original place if possible.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
  *
  * If $newParentLocation is provided, $trashedLocation will be restored under it.
  *
  * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
  * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location
  */
 public function recover(APITrashItem $trashItem, Location $newParentLocation = null)
 {
     if (!is_numeric($trashItem->id)) {
         throw new InvalidArgumentValue("id", $trashItem->id, "TrashItem");
     }
     if ($newParentLocation === null && !is_numeric($trashItem->parentLocationId)) {
         throw new InvalidArgumentValue("parentLocationId", $trashItem->parentLocationId, "TrashItem");
     }
     if ($newParentLocation !== null && !is_numeric($newParentLocation->id)) {
         throw new InvalidArgumentValue("parentLocationId", $newParentLocation->id, "Location");
     }
     if ($this->repository->hasAccess('content', 'restore') !== true) {
         throw new UnauthorizedException('content', 'restore');
     }
     $this->repository->beginTransaction();
     try {
         $newParentLocationId = $newParentLocation ? $newParentLocation->id : $trashItem->parentLocationId;
         $newLocationId = $this->persistenceHandler->trashHandler()->recover($trashItem->id, $newParentLocationId);
         $content = $this->repository->getContentService()->loadContent($trashItem->contentId);
         $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
         // Publish URL aliases for recovered location
         foreach ($urlAliasNames as $languageCode => $name) {
             $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($newLocationId, $newParentLocationId, $name, $languageCode, $content->contentInfo->alwaysAvailable);
         }
         $this->repository->commit();
     } catch (Exception $e) {
         $this->repository->rollback();
         throw $e;
     }
     return $this->repository->getLocationService()->loadLocation($newLocationId);
 }
 /**
  * Publishes URL aliases for all locations of a given content.
  *
  * @param \eZ\Publish\API\Repository\Values\Content\Content $content
  * @param boolean $updatePathIdentificationString this parameter is legacy storage specific for updating
  *                      ezcontentobject_tree.path_identification_string, it is ignored by other storage engines
  *
  * @return void
  */
 protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true)
 {
     $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
     $locations = $this->repository->getLocationService()->loadLocations($content->getVersionInfo()->getContentInfo());
     foreach ($locations as $location) {
         foreach ($urlAliasNames as $languageCode => $name) {
             $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($location->id, $location->parentLocationId, $name, $languageCode, $content->contentInfo->alwaysAvailable, $updatePathIdentificationString ? $languageCode === $content->contentInfo->mainLanguageCode : false);
         }
     }
 }
 /**
  * Moves the subtree to $newParentLocation
  *
  * If a user has the permission to move the location to a target location
  * he can do it regardless of an existing descendant on which the user has no permission.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new parent is in a subtree of the location
  *
  * @param \eZ\Publish\API\Repository\Values\Content\Location $location
  * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
  */
 public function moveSubtree(APILocation $location, APILocation $newParentLocation)
 {
     $location = $this->loadLocation($location->id);
     $newParentLocation = $this->loadLocation($newParentLocation->id);
     // check create permission on target location
     if (!$this->repository->canUser('content', 'create', $location->getContentInfo(), $newParentLocation)) {
         throw new UnauthorizedException('content', 'create');
     }
     /** Check read access to whole source subtree
      * @var boolean|\eZ\Publish\API\Repository\Values\Content\Query\Criterion $contentReadCriterion
      */
     $contentReadCriterion = $this->permissionsCriterionHandler->getPermissionsCriterion();
     if ($contentReadCriterion === false) {
         throw new UnauthorizedException('content', 'read');
     } else {
         if ($contentReadCriterion !== true) {
             // Query if there are any content in subtree current user don't have access to
             $query = new Query(array('limit' => 0, 'filter' => new CriterionLogicalAnd(new CriterionSubtree($location->pathString), new CriterionLogicalNot($contentReadCriterion))));
             $result = $this->repository->getSearchService()->findContent($query, array(), false);
             if ($result->totalCount > 0) {
                 throw new UnauthorizedException('content', 'read');
             }
         }
     }
     if (strpos($newParentLocation->pathString, $location->pathString) === 0) {
         throw new InvalidArgumentException("\$newParentLocation", "new parent location is in a subtree of the given \$location");
     }
     $this->repository->beginTransaction();
     try {
         $this->persistenceHandler->locationHandler()->move($location->id, $newParentLocation->id);
         $content = $this->repository->getContentService()->loadContent($location->contentId);
         $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
         foreach ($urlAliasNames as $languageCode => $name) {
             $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($location->id, $newParentLocation->id, $name, $languageCode, $content->contentInfo->alwaysAvailable);
         }
         $this->persistenceHandler->urlAliasHandler()->locationMoved($location->id, $location->parentLocationId, $newParentLocation->id);
         $this->repository->commit();
     } catch (Exception $e) {
         $this->repository->rollback();
         throw $e;
     }
 }