/** * {@inheritDoc} */ public function visitResult(DatagridConfiguration $config, ResultsObject $result) { $rows = $result->offsetGetByPath('[data]'); $rows = is_array($rows) ? $rows : []; $rows = array_map(function (ResultRecordInterface $record) { if ($rootEntity = $record->getRootEntity()) { return $rootEntity; } $entityName = $record->getValue('entityName'); $recordId = $record->getValue('recordId'); if ($entityName && $recordId) { return new ResultItem($this->em, $entityName, $recordId, null, null, null, $this->mapper->getEntityConfig($entityName)); } return null; }, $rows); $entities = $this->resultFormatter->getResultEntities($rows); $resultRows = []; /** @var ResultItem $item */ foreach ($rows as $item) { $entityName = $item->getEntityName(); $entityId = $item->getRecordId(); if (!isset($entities[$entityName][$entityId])) { continue; } $entity = $entities[$entityName][$entityId]; $this->dispatcher->dispatch(PrepareResultItemEvent::EVENT_NAME, new PrepareResultItemEvent($item, $entity)); $resultRows[] = new ResultRecord(['entity' => $entity, 'indexer_item' => $item]); } // set results $result->offsetSet('data', $resultRows); }
/** * {@inheritdoc} */ public function visitResult(DatagridConfiguration $config, ResultsObject $result) { $rows = $result->offsetGetByPath('[data]'); if (!is_array($rows)) { throw new UnexpectedTypeException($rows, 'array'); } $mappingConfig = $this->mapper->getMappingConfig(); $rows = array_map(function (ResultRecordInterface $record) use($mappingConfig) { $entityClass = $record->getValue('entityName'); $entityId = $record->getValue('recordId'); $entityConfig = array_key_exists($entityClass, $mappingConfig) ? $entityConfig = $this->mapper->getEntityConfig($entityClass) : []; return new ResultItem($this->em, $entityClass, $entityId, null, null, $entityConfig); }, $rows); $entities = $this->resultFormatter->getResultEntities($rows); $resultRows = []; /** @var ResultItem $item */ foreach ($rows as $item) { $entityClass = $item->getEntityName(); $entityId = $item->getRecordId(); $entity = $entities[$entityClass][$entityId]; $this->dispatcher->dispatch(PrepareResultItemEvent::EVENT_NAME, new PrepareResultItemEvent($item, $entity)); $resultRows[] = new ResultRecord(['entity' => $entity, 'indexer_item' => $item]); } $result->offsetSet('data', $resultRows); }
/** * {@inheritdoc} */ public function getResults($tagId) { $originResults = $this->em->createQueryBuilder()->select('t')->from('Oro\\Bundle\\TagBundle\\Entity\\Tagging', 't')->where('t.tag = :tag')->setParameter('tag', $tagId)->addGroupBy('t.entityName')->addGroupBy('t.recordId')->getQuery()->getResult(); $results = array(); /** @var Tagging $item */ foreach ($originResults as $item) { $results[] = new Item($this->em, $item->getEntityName(), $item->getRecordId(), null, null, null, $this->mapper->getEntityConfig($item->getEntityName())); } return new Result(new Query(), $results, count($results)); }
/** * {@inheritdoc} */ public function getResults($tagId) { $queryBuilder = $this->em->createQueryBuilder()->select('t.entityName', 't.recordId')->from('Oro\\Bundle\\TagBundle\\Entity\\Tagging', 't')->where('t.tag = :tag')->setParameter('tag', $tagId)->addGroupBy('t.entityName')->addGroupBy('t.recordId'); $this->securityProvider->applyAcl($queryBuilder, 't'); $originResults = $queryBuilder->getQuery()->getResult(); $results = []; /** @var Tagging $item */ foreach ($originResults as $item) { $entityName = $item['entityName']; $results[] = new Item($this->em, $entityName, $item['recordId'], null, null, $this->mapper->getEntityConfig($entityName)); } return new Result(new Query(), $results, count($results)); }
/** * {@inheritdoc} */ protected function doSearch(Query $query) { $results = []; $searchResults = $this->getIndexRepository()->search($query); if ($query->getCriteria()->getMaxResults() > 0 || $query->getCriteria()->getFirstResult() > 0) { $recordsCount = $this->getIndexRepository()->getRecordsCount($query); } else { $recordsCount = count($searchResults); } if ($searchResults) { foreach ($searchResults as $item) { if (is_array($item)) { $item = $item['item']; } /** * Search result can contains duplicates and we can not use HYDRATE_OBJECT because of performance issue. * @todo: update after fix BAP-7166. Remove check for existing result. */ $id = $item['id']; if (isset($results[$id])) { continue; } $results[$id] = new ResultItem($this->registry->getManagerForClass($item['entity']), $item['entity'], $item['recordId'], $item['title'], null, null, $this->mapper->getEntityConfig($item['entity'])); } } return ['results' => $results, 'records_count' => $recordsCount]; }
/** * @param Taggable $resource * @param ArrayCollection $tagsToAdd */ protected function persistTags(Taggable $resource, ArrayCollection $tagsToAdd) { foreach ($tagsToAdd as $tag) { if ($this->getUser() && (!$this->securityFacade->isGranted(self::ACL_RESOURCE_ASSIGN_ID_KEY) || !$this->securityFacade->isGranted(self::ACL_RESOURCE_CREATE_ID_KEY) && !$tag->getId())) { // skip tags that have not ID because user not granted to create tags continue; } $this->em->persist($tag); $alias = $this->mapper->getEntityConfig(ClassUtils::getClass($resource)); $tagging = $this->createTagging($tag, $resource)->setAlias($alias['alias']); $this->em->persist($tagging); } }
/** * Saves tags for the given taggable resource * * @param Taggable $resource Taggable resource * @param bool $flush Whether to flush the changes (default true) */ public function saveTagging(Taggable $resource, $flush = true) { $oldTags = $this->getTagging($resource, $this->getUser()->getId()); $newTags = $resource->getTags(); if (isset($newTags['all'], $newTags['owner'])) { $newOwnerTags = new ArrayCollection($newTags['owner']); $newAllTags = new ArrayCollection($newTags['all']); $manager = $this; $tagsToAdd = $newOwnerTags->filter(function ($tag) use($oldTags, $manager) { return !$oldTags->exists($manager->compareCallback($tag)); }); $tagsToDelete = $oldTags->filter(function ($tag) use($newOwnerTags, $manager) { return !$newOwnerTags->exists($manager->compareCallback($tag)); }); if (!$tagsToDelete->isEmpty() && $this->securityFacade->isGranted(self::ACL_RESOURCE_ASSIGN_ID_KEY)) { $this->deleteTaggingByParams($tagsToDelete, ClassUtils::getClass($resource), $resource->getTaggableId(), $this->getUser()->getId()); } // process if current user allowed to remove other's tag links if ($this->securityFacade->isGranted(self::ACL_RESOURCE_REMOVE_ID_KEY)) { // get 'not mine' taggings $oldTags = $this->getTagging($resource, $this->getUser()->getId(), true); $tagsToDelete = $oldTags->filter(function ($tag) use($newAllTags, $manager) { return !$newAllTags->exists($manager->compareCallback($tag)); }); if (!$tagsToDelete->isEmpty()) { $this->deleteTaggingByParams($tagsToDelete, ClassUtils::getClass($resource), $resource->getTaggableId()); } } foreach ($tagsToAdd as $tag) { if (!$this->securityFacade->isGranted(self::ACL_RESOURCE_ASSIGN_ID_KEY) || !$this->securityFacade->isGranted(self::ACL_RESOURCE_CREATE_ID_KEY) && !$tag->getId()) { // skip tags that have not ID because user not granted to create tags continue; } $this->em->persist($tag); $alias = $this->mapper->getEntityConfig(ClassUtils::getClass($resource)); $tagging = $this->createTagging($tag, $resource)->setAlias($alias['alias']); $this->em->persist($tagging); } if (!$tagsToAdd->isEmpty() && $flush) { $this->em->flush(); } } }
/** * {@inheritdoc} */ protected function doSearch(Query $query) { $results = array(); $searchResults = $this->getIndexRepository()->search($query); if ($query->getMaxResults() > 0 || $query->getFirstResult() > 0) { $recordsCount = $this->getIndexRepository()->getRecordsCount($query); } else { $recordsCount = count($searchResults); } if ($searchResults) { foreach ($searchResults as $item) { if (is_array($item)) { $item = $item['item']; } /** @var $item Item */ $results[] = new ResultItem($this->registry->getManagerForClass($item->getEntity()), $item->getEntity(), $item->getRecordId(), $item->getTitle(), null, $item->getRecordText(), $this->mapper->getEntityConfig($item->getEntity())); } } return array('results' => $results, 'records_count' => $recordsCount); }
public function testNonExistsConfig() { $this->assertEquals(false, $this->mapper->getEntityConfig('non exists entity')); }