public function testGetEntityMetadata() { $this->assertNull($this->configManager->getEntityMetadata('SomeUndefinedClass')); $metadata = $this->getEntityMetadata(self::ENTITY_CLASS); $this->metadataFactory->expects($this->once())->method('getMetadataForClass')->with(self::ENTITY_CLASS)->willReturn($metadata); $this->assertSame($metadata, $this->configManager->getEntityMetadata(self::ENTITY_CLASS)); }
/** * {@inheritdoc} */ public function getConfiguration() { $configuration = parent::getConfiguration(); $className = $this->source->getEntity(); $metadata = $this->configManager->getEntityMetadata($className); if (!$metadata || empty($metadata->routeView)) { return $configuration; } $fromPart = $configuration->offsetGetByPath('[source][query][from]'); $entityAlias = null; $doctrineMetadata = $this->doctrine->getManagerForClass($className)->getClassMetadata($className); $identifiers = $doctrineMetadata->getIdentifier(); $primaryKey = array_shift($identifiers); foreach ($fromPart as $piece) { if ($piece['table'] == $className) { $entityAlias = $piece['alias']; break; } } if (!$entityAlias || !$primaryKey || count($identifiers) > 1 || !$this->isActionSupported($primaryKey)) { return $configuration; } $viewAction = ['view' => ['type' => 'navigate', 'label' => 'oro.report.datagrid.view', 'acl_resource' => 'VIEW;entity:' . $className, 'icon' => 'eye-open', 'link' => 'view_link', 'rowAction' => true]]; $properties = [$primaryKey => null, 'view_link' => ['type' => 'url', 'route' => $metadata->routeView, 'params' => [$primaryKey]]]; $configuration->offsetAddToArrayByPath('[source][query][select]', ["{$entityAlias}.{$primaryKey}"]); $configuration->offsetAddToArrayByPath('[properties]', $properties); $configuration->offsetAddToArrayByPath('[actions]', $viewAction); return $configuration; }
/** * @param Email $email * * @return bool|string */ protected function getFromNameLink(Email $email) { $path = false; if ($email->getFromEmailAddress() && $email->getFromEmailAddress()->getOwner()) { $className = $email->getFromEmailAddress()->getOwner()->getClass(); $routeName = $this->configManager->getEntityMetadata($className)->getRoute('view', false); $path = $this->router->generate($routeName, ['id' => $email->getFromEmailAddress()->getOwner()->getId()]); } return $path; }
/** * Generate rout for entity (using entity config) * * @param Reminder $reminder * @return string */ public function getUrl(Reminder $reminder) { $metadata = $this->manager->getEntityMetadata($reminder->getRelatedEntityClassName()); if (isset($metadata)) { if (!empty($metadata->routeView)) { return $this->router->generate($metadata->routeView, array('id' => $reminder->getRelatedEntityId())); } if (!empty($metadata->routeName)) { return $this->router->generate($metadata->routeName); } } return ''; }
/** * {@inheritdoc} */ public function getData(ActivityList $activityListEntity) { /** @var Email $email */ $email = $headEmail = $this->doctrineRegistryLink->getService()->getRepository($activityListEntity->getRelatedActivityClass())->find($activityListEntity->getRelatedActivityId()); if ($email->isHead() && $email->getThread()) { $headEmail = $this->emailThreadProvider->getHeadEmail($this->doctrineHelper->getEntityManager($activityListEntity->getRelatedActivityClass()), $email); } $data = ['ownerName' => $email->getFromName(), 'ownerLink' => null, 'entityId' => $email->getId(), 'headOwnerName' => $headEmail->getFromName(), 'headSubject' => $headEmail->getSubject(), 'headSentAt' => $headEmail->getSentAt()->format('c'), 'isHead' => $email->isHead() && $email->getThread(), 'treadId' => $email->getThread() ? $email->getThread()->getId() : null]; if ($email->getThread()) { $emails = $email->getThread()->getEmails(); // if there are just two email - add replayedEmailId to use on client side if (count($emails) === 2) { $data['replayedEmailId'] = $emails[0]->getId(); } } if ($email->getFromEmailAddress()->getHasOwner()) { $owner = $email->getFromEmailAddress()->getOwner(); $data['headOwnerName'] = $data['ownerName'] = $this->entityNameResolver->getName($owner); $route = $this->configManager->getEntityMetadata(ClassUtils::getClass($owner))->getRoute('view'); $securityFacade = $this->securityFacadeLink->getService(); if (null !== $route && $securityFacade->isGranted('VIEW', $owner)) { $id = $this->doctrineHelper->getSingleEntityIdentifier($owner); try { $data['ownerLink'] = $this->router->generate($route, ['id' => $id]); } catch (RouteNotFoundException $e) { // Do not set owner link if route is not found. } } } return $data; }
/** * @param string $className The entity class name * @param string $routeType Route Type * @param bool $strict Should exception be thrown if no route of given type found * * @return string */ public function getClassRoute($className, $routeType = 'view', $strict = false) { if (!$this->configManager->hasConfig($className)) { return null; } return $this->configManager->getEntityMetadata($className)->getRoute($routeType, $strict); }
/** * Returns the context for the given email * * @param Email $email * * @return array */ public function getEmailContext(Email $email) { $criteria = Criteria::create(); $criteria->andWhere(Criteria::expr()->eq('id', $email->getId())); $qb = $this->activityManager->getActivityTargetsQueryBuilder($this->class, $criteria); if (null === $qb) { return []; } $result = $qb->getQuery()->getResult(); if (empty($result)) { return $result; } $currentUser = $this->securityFacade->getLoggedUser(); $currentUserClass = ClassUtils::getClass($currentUser); $currentUserId = $currentUser->getId(); $result = array_values(array_filter($result, function ($item) use($currentUserClass, $currentUserId) { return !($item['entity'] === $currentUserClass && $item['id'] == $currentUserId); })); foreach ($result as &$item) { $route = $this->configManager->getEntityMetadata($item['entity'])->getRoute(); $item['entityId'] = $email->getId(); $item['targetId'] = $item['id']; $item['targetClassName'] = $this->entityClassNameHelper->getUrlSafeClassName($item['entity']); $item['icon'] = $this->configManager->getProvider('entity')->getConfig($item['entity'])->get('icon'); $item['link'] = $route ? $this->router->generate($route, ['id' => $item['id']]) : null; unset($item['id'], $item['entity']); } return $result; }
/** * Returns the context for the given activity class and id * * @param string $class The FQCN of the activity entity * @param $id * * @return array */ public function getActivityContext($class, $id) { $currentUser = $this->securityTokenStorage->getToken()->getUser(); $userClass = ClassUtils::getClass($currentUser); $entity = $this->doctrineHelper->getEntity($class, $id); $result = []; if (!$entity || !$entity instanceof ActivityInterface) { return $result; } $targets = $entity->getActivityTargetEntities(); $entityProvider = $this->configManager->getProvider('entity'); foreach ($targets as $target) { $targetClass = ClassUtils::getClass($target); $targetId = $target->getId(); if ($userClass === $targetClass && $currentUser->getId() === $targetId) { continue; } $item = []; $config = $entityProvider->getConfig($targetClass); $metadata = $this->configManager->getEntityMetadata($targetClass); $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($targetClass); $link = null; if ($metadata) { $link = $this->router->generate($metadata->getRoute(), ['id' => $targetId]); } elseif ($link === null && ExtendHelper::isCustomEntity($targetClass)) { // Generate view link for the custom entity $link = $this->router->generate('oro_entity_view', ['id' => $targetId, 'entityName' => $safeClassName]); } if ($fields = $this->mapper->getEntityMapParameter($targetClass, 'title_fields')) { $text = []; foreach ($fields as $field) { $text[] = $this->mapper->getFieldValue($target, $field); } $item['title'] = implode(' ', $text); } else { $item['title'] = $this->translator->trans('oro.entity.item', ['%id%' => $targetId]); } $item['activityClassAlias'] = $this->entityAliasResolver->getPluralAlias($class); $item['entityId'] = $id; $item['targetId'] = $targetId; $item['targetClassName'] = $safeClassName; $item['icon'] = $config->get('icon'); $item['link'] = $link; $result[] = $item; } return $result; }
/** * @param string $entityClassName * @param mixed $id * * @return array */ protected function getRegularEntityViewRouteOptions($entityClassName, $id = null) { $routeOptions = ['route' => false, 'route_params' => false]; /** @var EntityMetadata $metadata */ $metadata = $this->configManager->getEntityMetadata($entityClassName); if ($metadata && $metadata->routeView) { $routeOptions['route'] = $metadata->routeView; $routeOptions['route_params'] = ['id' => $id]; } return $routeOptions; }
/** * Returns the context for the given activity class and id * * @param string $class The FQCN of the activity entity * @param $id * * @return array */ public function getActivityContext($class, $id) { $criteria = Criteria::create(); $criteria->andWhere(Criteria::expr()->eq('id', $id)); $currentUser = $this->securityTokenStorage->getToken()->getUser(); $userClass = ClassUtils::getClass($currentUser); $queryBuilder = $this->activityManager->getActivityTargetsQueryBuilder($class, $criteria, null, null, null, null, function (QueryBuilder $qb, $targetEntityClass) use($currentUser, $userClass) { if ($targetEntityClass === $userClass) { // Exclude current user from result $qb->andWhere($qb->expr()->neq(QueryUtils::getSelectExprByAlias($qb, 'entityId'), $currentUser->getId())); } }); if (null === $queryBuilder) { return []; } $result = $queryBuilder->getQuery()->getResult(); if (empty($result)) { return $result; } $entityProvider = $this->configManager->getProvider('entity'); foreach ($result as &$item) { $config = $entityProvider->getConfig($item['entity']); $metadata = $this->configManager->getEntityMetadata($item['entity']); $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($item['entity']); $link = null; if ($metadata) { $link = $this->router->generate($metadata->getRoute(), ['id' => $item['id']]); } elseif ($link === null && ExtendHelper::isCustomEntity($item['entity'])) { // Generate view link for the custom entity $link = $this->router->generate('oro_entity_view', ['id' => $item['id'], 'entityName' => $safeClassName]); } $item['activityClassAlias'] = $this->entityAliasResolver->getPluralAlias($class); $item['entityId'] = $id; $item['targetId'] = $item['id']; $item['targetClassName'] = $safeClassName; $item['icon'] = $config->get('icon'); $item['link'] = $link; unset($item['id'], $item['entity']); } return $result; }
/** * @param string $className The entity class name * @param string $routeType Route Type * @param bool $strict Should exception be thrown if no route of given type found * * @return string|null */ public function getClassRoute($className, $routeType = 'view', $strict = false) { if (!$this->configManager->hasConfig($className)) { return null; } $entityMetadata = $this->configManager->getEntityMetadata($className); if (!$entityMetadata) { return null; } $route = $entityMetadata->getRoute($routeType, $strict); return $route && $this->hasRoute($route) ? $route : null; }
/** * Search entities by primary key * * @param $keys[] * * @return array */ public function findValueByPrimaryKey($keys) { $entityMetadata = $this->entityConfigManager->getEntityMetadata($this->class); $keyField = $this->dictionaryHelper->getNamePrimaryKeyField($this->getMetadata()); $searchFields = $this->dictionaryHelper->getSearchFields($this->getMetadata(), $entityMetadata); $representationField = $this->dictionaryHelper->getRepresentationField($this->getMetadata(), $entityMetadata); $qb = $this->getListQueryBuilder(-1, 1, [], null, []); $qb->andWhere('e.' . $keyField . ' in (:keys)'); $qb->setParameter('keys', $keys); $query = $qb->getQuery(); $results = $query->getResult(); return $this->prepareData($results, $keyField, $searchFields, $representationField); }
/** * @param string $className The entity class name * @param string $routeType Route Type * @param bool $strict Should exception be thrown if no route of given type found * * @return string */ public function getClassRoute($className, $routeType = 'view', $strict = false) { if (!$this->configManager->hasConfig($className)) { return null; } $route = $this->configManager->getEntityMetadata($className)->getRoute($routeType, $strict); if ($collection = $this->router->getRouteCollection()) { if ($collection->get($route) === null) { return null; } } return $route; }
/** * @param EmailOwnerInterface $owner * @param $data * * @return mixed */ protected function setOwnerLink($owner, $data) { $route = $this->configManager->getEntityMetadata(ClassUtils::getClass($owner))->getRoute('view'); $securityFacade = $this->securityFacadeLink->getService(); if (null !== $route && $securityFacade->isGranted('VIEW', $owner)) { $id = $this->doctrineHelper->getSingleEntityIdentifier($owner); try { $data['ownerLink'] = $this->router->generate($route, ['id' => $id]); } catch (RouteNotFoundException $e) { // Do not set owner link if route is not found. } } return $data; }
/** * @param string $entityClassName * * @return array */ protected function getEntityRouteOptions($entityClassName) { $route = false; $routeParams = false; if (class_exists($entityClassName)) { /** @var EntityMetadata $metadata */ $metadata = $this->configManager->getEntityMetadata($entityClassName); if ($metadata && $metadata->routeView) { $route = $metadata->routeView; $routeParams = ['id' => null]; } $relationExtendConfig = $this->extendProvider->getConfig($entityClassName); if ($relationExtendConfig->is('owner', ExtendScope::OWNER_CUSTOM)) { $route = $this->entityViewRoute; $routeParams = ['entityName' => str_replace('\\', '_', $entityClassName), 'id' => null]; } } return ['route' => $route, 'route_params' => $routeParams]; }
/** * @param string $targetClass The FQCN of the activity target entity * @param int $targetId The identifier of the activity target entity * * @return string|null */ protected function getContextLink($targetClass, $targetId) { $metadata = $this->configManager->getEntityMetadata($targetClass); $link = null; if ($metadata) { try { $route = $metadata->getRoute('view', true); } catch (\LogicException $exception) { // Need for cases when entity does not have route. return null; } $link = $this->router->generate($route, ['id' => $targetId]); } elseif (ExtendHelper::isCustomEntity($targetClass)) { $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($targetClass); // Generate view link for the custom entity $link = $this->router->generate('oro_entity_view', ['id' => $targetId, 'entityName' => $safeClassName]); } return $link; }
/** * @param ClassMetadata $metadata * * @return bool */ protected function hasEntityConfigs(ClassMetadata $metadata) { $classMetadata = $this->configManager->getEntityMetadata($metadata->getName()); return $classMetadata && $classMetadata->configurable; }