コード例 #1
0
ファイル: IndexerQuery.php プロジェクト: Maksold/platform
 /**
  * @return Result
  */
 protected function getResult()
 {
     if (!$this->result) {
         $this->result = $this->indexer->query($this->query);
     }
     return $this->result;
 }
コード例 #2
0
 /**
  * Get search aliases for specified entity class(es). By default returns all associated entities.
  *
  * @param string[] $entities
  *
  * @return string[]
  */
 protected function getSearchAliases(array $entities)
 {
     if (empty($entities)) {
         $entities = array_flip($this->activityManager->getActivityTargets($this->class));
     }
     return array_values($this->searchIndexer->getEntityAliases($entities));
 }
コード例 #3
0
 /**
  * Adjust query for tag-results-grid (tag search result grid)
  * after datasource has been built
  *
  * @param BuildAfter $event
  */
 public function onBuildAfter(BuildAfter $event)
 {
     $datasource = $event->getDatagrid()->getDatasource();
     if ($datasource instanceof SearchDatasource) {
         /** @var $query Query */
         $query = new IndexerQuery($this->indexer, $this->indexer->select());
         $searchEntity = $this->requestParams->get('from', '*');
         $searchEntity = empty($searchEntity) ? '*' : $searchEntity;
         $searchString = $this->requestParams->get('search', '');
         $query->from($searchEntity)->andWhere(Indexer::TEXT_ALL_DATA_FIELD, '~', $searchString, 'text');
         $datasource->setQuery($query);
     }
 }
コード例 #4
0
 /**
  * Get search aliases for specified entity class(es). By default returns all search aliases
  * for all entities which can be associated with an activity this manager id work with.
  *
  * @param string[] $entities
  *
  * @return string[]
  */
 protected function getSearchAliases(array $entities)
 {
     if (empty($entities)) {
         $entities = array_flip($this->activityManager->getActivityTargets($this->class));
     }
     $aliases = [];
     foreach ($entities as $targetEntityClass) {
         $alias = $this->searchIndexer->getEntityAlias($targetEntityClass);
         if (null !== $alias) {
             $aliases[] = $alias;
         }
     }
     return $aliases;
 }
コード例 #5
0
ファイル: SecurityIndexer.php プロジェクト: kstupak/platform
 /**
  * Returns objects extracted from simple search
  *
  * @param User $user
  * @param string $entityClass
  * @param string $searchString
  * @param int $offset
  * @param int $maxResults
  *
  * @return array
  */
 protected function getObjects(User $user, $entityClass, $searchString, $offset, $maxResults)
 {
     $objects = [];
     if (!$this->configManager->hasConfig($entityClass)) {
         return $objects;
     }
     $classNames = $this->shareScopeProvider->getClassNamesBySharingScopeConfig($entityClass);
     if (!$classNames) {
         return $objects;
     }
     $tables = [];
     foreach ($classNames as $className) {
         $metadata = $this->em->getClassMetadata($className);
         $tables[] = $metadata->getTableName();
     }
     $searchResults = $this->indexer->simpleSearch($searchString, $offset, $maxResults, $tables);
     list($userIds, $buIds, $orgIds) = $this->getIdsByClass($searchResults, $user);
     if ($orgIds) {
         $organizations = $this->em->getRepository('OroOrganizationBundle:Organization')->getEnabledOrganizations($orgIds);
         $objects = array_merge($objects, $organizations);
     }
     if ($buIds) {
         $businessUnits = $this->em->getRepository('OroOrganizationBundle:BusinessUnit')->getBusinessUnits($buIds);
         $objects = array_merge($objects, $businessUnits);
     }
     if ($userIds) {
         $users = $this->em->getRepository('OroUserBundle:User')->findUsersByIds($userIds);
         $objects = array_merge($objects, $users);
     }
     return $objects;
 }
コード例 #6
0
ファイル: SearchHandler.php プロジェクト: hugeval/platform
 /**
  * @param string $search
  * @param int    $firstResult
  * @param int    $maxResults
  * @return array
  */
 protected function searchIds($search, $firstResult, $maxResults)
 {
     $result = $this->indexer->simpleSearch($search, $firstResult, $maxResults, $this->entitySearchAlias);
     $elements = $result->getElements();
     $ids = [];
     foreach ($elements as $element) {
         $ids[] = $element->getRecordId();
     }
     return $ids;
 }
コード例 #7
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->hasResult()) {
         // result data are already retrieved
         return;
     }
     $query = $context->getQuery();
     if (!$query instanceof SearchQuery) {
         // unsupported query
         return;
     }
     $searchResult = $this->searchIndex->query($query);
     $context->setResult($searchResult->toArray());
     // set callback to be used to calculate total count
     $context->setTotalCountCallback(function () use($searchResult) {
         return $searchResult->getRecordsCount();
     });
 }
コード例 #8
0
 /**
  * Get search aliases for all entities which can be associated with specified activity.
  *
  * @return string[]
  */
 protected function getSearchAliases()
 {
     $class = $this->entityClassNameHelper->resolveEntityClass($this->class, true);
     $aliases = [];
     foreach ($this->activityManager->getActivityTargets($class) as $targetEntityClass => $fieldName) {
         $alias = $this->indexer->getEntityAlias($targetEntityClass);
         if (null !== $alias) {
             $aliases[] = $alias;
         }
     }
     return $aliases;
 }
コード例 #9
0
ファイル: IndexerTest.php プロジェクト: hugeval/platform
 public function testAdvancedSearch()
 {
     $searchResults = ['one', 'two', 'three'];
     $this->engine->expects($this->any())->method('search')->will($this->returnCallback(function (Query $query) use($searchResults) {
         return new Result($query, $searchResults, count($searchResults));
     }));
     $sourceQuery = 'from test_product' . ' where (name ~ "test string" and integer count > 10 and (decimal price = 10 or integer qty in (2, 5)))' . ' order_by name offset 10 max_results 5';
     $expectedQuery = 'select from test_product where ((integer qty in (2, 5) or decimal price = 10) ' . 'and integer count > 10 and text name ~ "test string") order by name ASC limit 5 offset 10';
     $result = $this->indexService->advancedSearch($sourceQuery);
     $actualQuery = $result->getQuery()->getStringQuery();
     $this->assertEquals($searchResults, $result->getElements());
     $this->assertEquals(count($searchResults), $result->getRecordsCount());
     $this->assertEquals($expectedQuery, $actualQuery);
 }
コード例 #10
0
ファイル: IndexerTest.php プロジェクト: xamin123/platform
 public function testAdvancedSearch()
 {
     $searchResults = array('one', 'two', 'three');
     $this->engine->expects($this->any())->method('search')->will($this->returnCallback(function (Query $query) use($searchResults) {
         return new Result($query, $searchResults, count($searchResults));
     }));
     $sourceQuery = 'from (test_product, test_category)' . ' where name ~ "test string" and integer count = 10 and decimal price in (10, 12, 15)' . ' order_by name offset 10 max_results 5';
     $expectedQuery = 'select from test_product' . ' where and((text)name ~ test string) and((integer)count = 10) and((decimal)price in (10, 12, 15))' . ' order by name asc limit 5 offset 10';
     $result = $this->indexService->advancedSearch($sourceQuery);
     $actualQuery = $this->combineQueryString($result->getQuery());
     $this->assertEquals($searchResults, $result->getElements());
     $this->assertEquals(count($searchResults), $result->getRecordsCount());
     $this->assertEquals($expectedQuery, $actualQuery);
 }
コード例 #11
0
 /**
  * @param string $search
  * @param int $page
  * @param int $perPage
  * @param array $foundElements
  * @param array $resultData
  * @param array $expectedIds
  * @return \PHPUnit_Framework_MockObject_MockObject
  */
 protected function assertSearchCall($search, $page, $perPage, array $foundElements, array $resultData, array $expectedIds)
 {
     $searchResult = $this->getMockBuilder('Oro\\Bundle\\SearchBundle\\Query\\Result')->disableOriginalConstructor()->getMock();
     $searchResult->expects($this->once())->method('getElements')->will($this->returnValue($foundElements));
     $this->indexer->expects($this->once())->method('simpleSearch')->with($search, $page - 1, $perPage + 1, 'alias')->will($this->returnValue($searchResult));
     $queryBuilder = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $query = $this->getMockBuilder('Doctrine\\ORM\\AbstractQuery')->disableOriginalConstructor()->setMethods(['getResult'])->getMockForAbstractClass();
     $query->expects($this->once())->method('getResult')->will($this->returnValue($resultData));
     $expr = $this->getMockBuilder('Doctrine\\ORM\\Query\\Expr')->disableOriginalConstructor()->getMock();
     $expr->expects($this->once())->method('in')->with('e.id', $expectedIds)->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('expr')->will($this->returnValue($expr));
     $queryBuilder->expects($this->once())->method('where')->with($expr)->will($this->returnSelf());
     $this->aclHelper->expects($this->once())->method('apply')->with($queryBuilder, 'VIEW')->will($this->returnValue($query));
     $this->entityRepository->expects($this->any())->method('createQueryBuilder')->will($this->returnValue($queryBuilder));
     return $searchResult;
 }
コード例 #12
0
 /**
  * Get search aliases for all entities which can be associated with specified activity.
  *
  * @return string[]
  */
 protected function getSearchAliases()
 {
     $class = $this->entityClassNameHelper->resolveEntityClass($this->class, true);
     $aliases = [];
     $targetEntityClasses = array_keys($this->activityManager->getActivityTargets($class));
     foreach ($targetEntityClasses as $targetEntityClass) {
         $alias = $this->indexer->getEntityAlias($targetEntityClass);
         if (null !== $alias) {
             $aliases[] = $alias;
         }
     }
     /** dispatch oro_activity.search_aliases event */
     $event = new SearchAliasesEvent($aliases, $targetEntityClasses);
     $this->dispatcher->dispatch(SearchAliasesEvent::EVENT_NAME, $event);
     $aliases = $event->getAliases();
     return $aliases;
 }
コード例 #13
0
 /**
  * @param string $search
  * @param int $page
  * @param int $perPage
  * @param array $foundElements
  * @param array $resultData
  * @param array $expectedIds
  * @return \PHPUnit_Framework_MockObject_MockObject
  */
 protected function assertSearchCall($search, $page, $perPage, array $foundElements, array $resultData, array $expectedIds)
 {
     /* @var $searchResult Result|\PHPUnit_Framework_MockObject_MockObject */
     $searchResult = $this->getMockBuilder('Oro\\Bundle\\SearchBundle\\Query\\Result')->disableOriginalConstructor()->getMock();
     $searchResult->expects($this->once())->method('getElements')->willReturn($foundElements);
     $this->indexer->expects($this->once())->method('simpleSearch')->with($search, $page - 1, $perPage + 1, 'alias')->willReturn($searchResult);
     /* @var $queryBuilder QueryBuilder|\PHPUnit_Framework_MockObject_MockObject */
     $queryBuilder = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     /* @var $query AbstractQuery|\PHPUnit_Framework_MockObject_MockObject */
     $query = $this->getMockBuilder('Doctrine\\ORM\\AbstractQuery')->disableOriginalConstructor()->setMethods(['getResult'])->getMockForAbstractClass();
     $query->expects($this->once())->method('getResult')->willReturn($resultData);
     /* @var $expr Expr|\PHPUnit_Framework_MockObject_MockObject */
     $expr = $this->getMockBuilder('Doctrine\\ORM\\Query\\Expr')->disableOriginalConstructor()->getMock();
     $expr->expects($this->once())->method('in')->with('e.id', $expectedIds)->will($this->returnSelf());
     $expr->expects($this->once())->method('asc')->with('e.email')->will($this->returnSelf());
     $queryBuilder->expects($this->exactly(2))->method('expr')->willReturn($expr);
     $queryBuilder->expects($this->once())->method('where')->with($expr)->will($this->returnSelf());
     $queryBuilder->expects($this->once())->method('addOrderBy')->with($expr)->will($this->returnSelf());
     $queryBuilder->expects($this->any())->method('andWhere')->with('e.account = :account')->will($this->returnSelf());
     $this->aclHelper->expects($this->once())->method('apply')->with($queryBuilder, 'VIEW')->willReturn($query);
     $this->entityRepository->expects($this->any())->method('createQueryBuilder')->willReturn($queryBuilder);
     return $searchResult;
 }
コード例 #14
0
 /**
  *
  * @param $query
  * @return \Oro\Bundle\SearchBundle\Query\Result
  */
 public function getResults($query)
 {
     return $this->indexer->simpleSearch($query);
 }
コード例 #15
0
 /**
  * Returns search aliases for all the Customer entities
  *
  * @return string[]
  */
 protected function getCustomerSearchAliases()
 {
     return array_values($this->searchIndexer->getEntityAliases($this->getCustomerEntities()));
 }
コード例 #16
0
ファイル: SearchDatasource.php プロジェクト: Maksold/platform
 /**
  * {@inheritDoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $this->query = new IndexerQuery($this->indexer, $this->indexer->select());
     $grid->setDatasource(clone $this);
 }
コード例 #17
0
 /**
  * Get search aliases for specified entity class(es). By default returns all associated entities.
  *
  * @param string[] $from
  *
  * @return array
  */
 protected function getSearchAliases(array $from)
 {
     $entities = empty($from) ? $this->activityManager->getActivityTargets($this->class) : array_flip($from);
     $aliases = array_intersect_key($this->searchIndexer->getEntitiesListAliases(), $entities);
     return array_values($aliases);
 }