/**
  * Returns true if an object exists under the given root with the given name.
  * Returns false otherwise.
  *
  * @param int    $root
  * @param string $name
  *
  * @return bool
  */
 public function existsPlace($root, $name)
 {
     $query = new Query();
     $query->query = new Query\Criterion\LogicalAnd(array(new Query\Criterion\Field('name', Query\Criterion\Operator::EQ, $name), new Query\Criterion\ParentLocationId($root)));
     $searchService = $this->apiRepository->getSearchService();
     $searchResults = $searchService->findContent($query);
     return $searchResults->totalCount > 0;
 }
 /**
  * @param \eZ\Publish\API\Repository\Repository $repository
  * @param ConfigResolverInterface|\Psr\Log\LoggerInterface $resolver
  */
 public function __construct(Repository $repository, ConfigResolverInterface $resolver)
 {
     $this->repository = $repository;
     $this->searchService = $this->repository->getSearchService();
     $this->locationService = $this->repository->getLocationService();
     $this->contentService = $this->repository->getContentService();
     $this->languageService = $this->repository->getContentLanguageService();
     $this->userService = $this->repository->getUserService();
     $this->contentTypeService = $this->repository->getContentTypeService();
     $this->configResolver = $resolver;
 }
 /**
  * Returns latest published content that is located under $pathString and matching $contentTypeIdentifier.
  * The whole subtree will be passed through to find content.
  *
  * @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation Root location we want to start content search from.
  * @param string[] $includeContentTypeIdentifiers Array of ContentType identifiers we want content to match.
  * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion Additional criterion for filtering.
  * @param int|null $limit Max number of items to retrieve. If not provided, default limit will be used.
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Content[]
  */
 public function getLatestContent(Location $rootLocation, array $includeContentTypeIdentifiers = array(), Criterion $criterion = null, $limit = null)
 {
     $criteria = array(new Criterion\Subtree($rootLocation->pathString), new Criterion\Visibility(Criterion\Visibility::VISIBLE));
     if ($includeContentTypeIdentifiers) {
         $criteria[] = new Criterion\ContentTypeIdentifier($includeContentTypeIdentifiers);
     }
     if (!empty($criterion)) {
         $criteria[] = $criterion;
     }
     $query = new Query(array('criterion' => new Criterion\LogicalAnd($criteria), 'sortClauses' => array(new SortClause\DatePublished(Query::SORT_DESC))));
     $query->limit = $limit ?: $this->defaultMenuLimit;
     return $this->searchHelper->buildListFromSearchResult($this->repository->getSearchService()->findContent($query));
 }
Exemple #4
0
 /**
  * Get SearchService
  *
  * @return \eZ\Publish\API\Repository\SearchService
  */
 public function getSearchService()
 {
     if ($this->searchService !== null) {
         return $this->searchService;
     }
     $this->searchService = new SearchService($this->repository->getSearchService(), $this->signalDispatcher);
     return $this->searchService;
 }
 /**
  * Returns Location objects parent of the locationId provided
  *
  * Criterion can be passed as parameter to refine the search
  *
  * @param int $locationId
  * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion Additional criterion for filtering.
  * @param int $limit
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Location[] Location objects, indexed by their contentId.
  */
 public function getSubElementLocation($locationId, Criterion $criterion = null, $limit = null)
 {
     $criteria = array(new Criterion\ParentLocationId($locationId), new Criterion\Visibility(Criterion\Visibility::VISIBLE));
     if (!empty($criterion)) {
         $criteria[] = $criterion;
     }
     $query = new LocationQuery(array('criterion' => new Criterion\LogicalAnd($criteria), 'sortClauses' => array(new SortClause\Location\Priority(LocationQuery::SORT_ASC))));
     if ($limit != null) {
         $query->limit = $limit;
     }
     $searchResult = $this->repository->getSearchService()->findLocations($query);
     $list = array();
     foreach ($searchResult->searchHits as $searchHit) {
         $list[$searchHit->valueObject->id] = $searchHit->valueObject;
     }
     return $list;
 }
 public function __construct(Repository $repository, CategoryServiceInterface $categoryService, PostServiceInterface $postService)
 {
     $this->repository = $repository;
     $this->categoryService = $categoryService;
     $this->postService = $postService;
     $this->searchService = $repository->getSearchService();
     $this->contentService = $repository->getContentService();
     $this->locationService = $repository->getLocationService();
     $this->contentTypeService = $repository->getContentTypeService();
     $this->userService = $repository->getUserService();
 }
Exemple #7
0
 /**
  * Returns the number of content objects related to $tag.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to read tags
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified tag is not found
  *
  * @param \Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag
  *
  * @return int
  */
 public function getRelatedContentCount(Tag $tag)
 {
     if ($this->hasAccess('tags', 'read') !== true) {
         throw new UnauthorizedException('tags', 'read');
     }
     $spiTagInfo = $this->tagsHandler->loadTagInfo($tag->id);
     $relatedContentIds = $this->tagsHandler->loadRelatedContentIds($spiTagInfo->id);
     if (empty($relatedContentIds)) {
         return 0;
     }
     $searchResult = $this->repository->getSearchService()->findContent(new Query(array('limit' => 0, 'filter' => new ContentId($relatedContentIds))));
     return $searchResult->totalCount;
 }
 /**
  * Loads the users of a user group
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the users or user group
  *
  * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
  * @param int $offset
  * @param int $limit
  *
  * @return \eZ\Publish\API\Repository\Values\User\User[]
  */
 public function loadUsersOfUserGroup(APIUserGroup $userGroup, $offset = 0, $limit = -1)
 {
     $loadedUserGroup = $this->loadUserGroup($userGroup->id);
     if ($loadedUserGroup->getVersionInfo()->getContentInfo()->mainLocationId === null) {
         return array();
     }
     $mainGroupLocation = $this->repository->getLocationService()->loadLocation($loadedUserGroup->getVersionInfo()->getContentInfo()->mainLocationId);
     $searchQuery = new Query();
     $searchQuery->filter = new CriterionLogicalAnd(array(new CriterionContentTypeId($this->settings['userClassID']), new CriterionParentLocationId($mainGroupLocation->id)));
     $searchQuery->offset = $offset > 0 ? (int) $offset : 0;
     $searchQuery->limit = $limit >= 1 ? (int) $limit : null;
     $searchQuery->sortClauses = array($this->getSortClauseBySortField($mainGroupLocation->sortField, $mainGroupLocation->sortOrder));
     $searchResult = $this->repository->getSearchService()->findContent($searchQuery, array());
     $users = array();
     foreach ($searchResult->searchHits as $resultItem) {
         $spiUser = $this->userHandler->load($resultItem->valueObject->id);
         $users[] = $this->buildDomainUserObject($spiUser, $resultItem->valueObject);
     }
     return $users;
 }
 /**
  * Loads the users of a user group.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the users or user group
  *
  * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
  * @param int $offset the start offset for paging
  * @param int $limit the number of users returned
  *
  * @return \eZ\Publish\API\Repository\Values\User\User[]
  */
 public function loadUsersOfUserGroup(APIUserGroup $userGroup, $offset = 0, $limit = 25)
 {
     $loadedUserGroup = $this->loadUserGroup($userGroup->id);
     if ($loadedUserGroup->getVersionInfo()->getContentInfo()->mainLocationId === null) {
         return array();
     }
     $mainGroupLocation = $this->repository->getLocationService()->loadLocation($loadedUserGroup->getVersionInfo()->getContentInfo()->mainLocationId);
     $searchQuery = new LocationQuery();
     $searchQuery->filter = new CriterionLogicalAnd(array(new CriterionContentTypeId($this->settings['userClassID']), new CriterionParentLocationId($mainGroupLocation->id)));
     $searchQuery->offset = $offset;
     $searchQuery->limit = $limit;
     $searchQuery->performCount = false;
     $searchQuery->sortClauses = $mainGroupLocation->getSortClauses();
     $searchResult = $this->repository->getSearchService()->findLocations($searchQuery);
     $users = array();
     foreach ($searchResult->searchHits as $resultItem) {
         $users[] = $this->buildDomainUserObject($this->userHandler->load($resultItem->valueObject->contentInfo->id), $this->repository->getContentService()->internalLoadContent($resultItem->valueObject->contentInfo->id));
     }
     return $users;
 }
 /**
  * Returns SearchResult of the tested Locations for the given $sortClause.
  *
  * @param \eZ\Publish\API\Repository\Repository $repository
  * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause $sortClause
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
  */
 protected function sortLocations(Repository $repository, SortClause $sortClause)
 {
     $this->checkLocationFieldSearchSupport();
     $searchService = $repository->getSearchService();
     $query = new LocationQuery(array('filter' => new Criterion\ContentTypeIdentifier('test-' . $this->getTypeName()), 'sortClauses' => array($sortClause)));
     return $searchService->findLocations($query);
 }
 /**
  * Searches $searchText in the search database.
  *
  * @param string $searchText Search term
  * @param array $params Search parameters
  * @param array $searchTypes Search types
  *
  * @return array
  */
 public function search($searchText, $params = array(), $searchTypes = array())
 {
     $searchText = trim($searchText);
     if (empty($searchText)) {
         return array('SearchResult' => array(), 'SearchCount' => 0, 'StopWordArray' => array());
     }
     $doFullText = true;
     $query = new LocationQuery();
     $criteria = array();
     if (isset($params['SearchDate']) && (int) $params['SearchDate'] > 0) {
         $currentTimestamp = time();
         $dateSearchType = (int) $params['SearchDate'];
         $fromTimestamp = 0;
         if ($dateSearchType === 1) {
             // Last day
             $fromTimestamp = $currentTimestamp - 86400;
         } else {
             if ($dateSearchType === 2) {
                 // Last week
                 $fromTimestamp = $currentTimestamp - 7 * 86400;
             } else {
                 if ($dateSearchType === 3) {
                     // Last month
                     $fromTimestamp = $currentTimestamp - 30 * 86400;
                 } else {
                     if ($dateSearchType === 4) {
                         // Last three months
                         $fromTimestamp = $currentTimestamp - 3 * 30 * 86400;
                     } else {
                         if ($dateSearchType === 5) {
                             // Last year
                             $fromTimestamp = $currentTimestamp - 365 * 86400;
                         }
                     }
                 }
             }
         }
         $criteria[] = new Criterion\DateMetadata(Criterion\DateMetadata::CREATED, Criterion\Operator::GTE, $fromTimestamp);
     }
     if (isset($params['SearchSectionID']) && (int) $params['SearchSectionID'] > 0) {
         $criteria[] = new Criterion\SectionId((int) $params['SearchSectionID']);
     }
     if (isset($params['SearchContentClassID']) && (int) $params['SearchContentClassID'] > 0) {
         $criteria[] = new Criterion\ContentTypeId((int) $params['SearchContentClassID']);
         if (isset($params['SearchContentClassAttributeID']) && (int) $params['SearchContentClassAttributeID'] > 0) {
             $classAttribute = eZContentClassAttribute::fetch($params['SearchContentClassAttributeID']);
             if ($classAttribute instanceof eZContentClassAttribute) {
                 $criteria[] = new Criterion\Field($classAttribute->attribute('identifier'), Criterion\Operator::LIKE, $searchText);
                 $doFullText = false;
             }
         }
     }
     if (isset($params['SearchSubTreeArray']) && !empty($params['SearchSubTreeArray'])) {
         $subTreeArrayCriteria = array();
         foreach ($params['SearchSubTreeArray'] as $nodeId) {
             $node = eZContentObjectTreeNode::fetch($nodeId);
             $subTreeArrayCriteria[] = $node->attribute('path_string');
         }
         $criteria[] = new Criterion\Subtree($subTreeArrayCriteria);
     }
     if ($doFullText) {
         $criteria[] = new Criterion\FullText($searchText);
     }
     $query->query = new Criterion\LogicalAnd($criteria);
     $query->limit = isset($params['SearchLimit']) ? (int) $params['SearchLimit'] : 10;
     $query->offset = isset($params['SearchOffset']) ? (int) $params['SearchOffset'] : 0;
     $useLocationSearch = eZINI::instance('ezplatformsearch.ini')->variable('SearchSettings', 'UseLocationSearch') === 'true';
     if ($useLocationSearch) {
         $searchResult = $this->repository->getSearchService()->findLocations($query);
     } else {
         $searchResult = $this->repository->getSearchService()->findContentInfo($query);
     }
     $nodeIds = array();
     foreach ($searchResult->searchHits as $searchHit) {
         $nodeIds[] = $useLocationSearch ? $searchHit->valueObject->id : $searchHit->valueObject->mainLocationId;
     }
     $nodes = eZContentObjectTreeNode::fetch($nodeIds);
     if ($nodes instanceof eZContentObjectTreeNode) {
         $nodes = array($nodes);
     } else {
         if (!is_array($nodes)) {
             $nodes = array();
         }
     }
     return array('SearchResult' => $nodes, 'SearchCount' => $searchResult->totalCount, 'StopWordArray' => array());
 }