/** * @param Route $route * @param RouteCollectionAccessor $routes * @param string[] $entities * * @return string[] Entity requirements for the default controller */ protected function adjustRoutes(Route $route, RouteCollectionAccessor $routes, $entities) { $result = []; $routeName = $routes->getName($route); foreach ($entities as $className) { $entity = $this->entityAliasResolver->getPluralAlias($className); $result[] = $entity; $result[] = $this->entityClassNameHelper->getUrlSafeClassName($className); $existingRoute = $routes->getByPath(str_replace(self::ENTITY_PLACEHOLDER, $entity, $route->getPath()), $route->getMethods()); if ($existingRoute) { // move existing route before the current route $existingRouteName = $routes->getName($existingRoute); $routes->remove($existingRouteName); $routes->insert($existingRouteName, $existingRoute, $routeName, true); //additional route for entities which has api, but it not recognize urls like // /api/rest/latest/Oro_Bundle_AddressBundle_Entity_Country //TODO: This should be removed in scope of https://magecore.atlassian.net/browse/BAP-8650 $dictionaryRoute = $routes->cloneRoute($existingRoute); $dictionaryRoute->setPath(str_replace(self::ENTITY_PLACEHOLDER, $this->entityClassNameHelper->getUrlSafeClassName($className), $route->getPath())); $routes->insert($routes->generateRouteName($existingRouteName), $dictionaryRoute, $existingRouteName, true); } else { // add an additional strict route based on the base route and current entity $strictRoute = $routes->cloneRoute($route); $strictRoute->setPath(str_replace(self::ENTITY_PLACEHOLDER, $entity, $strictRoute->getPath())); $strictRoute->setDefault(self::ENTITY_ATTRIBUTE, $entity); $routes->insert($routes->generateRouteName($routeName), $strictRoute, $routeName, true); } } return $result; }
/** * {@inheritdoc} */ public function resolve(Route $route, RouteCollectionAccessor $routes) { if ($route->getOption('group') !== self::ROUTE_GROUP) { return; } if ($this->hasAttribute($route, self::ACTIVITY_PLACEHOLDER)) { $activities = array_map(function (ConfigInterface $config) { // convert to entity alias return $this->entityAliasResolver->getPluralAlias($config->getId()->getClassName()); }, $this->groupingConfigProvider->filter(function (ConfigInterface $config) { // filter activity entities $groups = $config->get('groups'); return !empty($groups) && in_array(ActivityScope::GROUP_ACTIVITY, $groups, true); })); if (!empty($activities)) { $activities = $this->adjustRoutes($route, $routes, $activities); if (!empty($activities)) { $route->setRequirement(self::ACTIVITY_ATTRIBUTE, implode('|', $activities)); } } $this->completeRouteRequirements($route); } elseif ($this->hasAttribute($route, self::ENTITY_PLACEHOLDER)) { $this->completeRouteRequirements($route); } }
/** * Get class alias of specified entity * * @param object $object * @param bool $isPlural * * @return null|string */ public function getClassAlias($object, $isPlural = false) { if (!is_object($object)) { return null; } $className = ClassUtils::getRealClass($object); return $isPlural ? $this->entityAliasResolver->getPluralAlias($className) : $this->entityAliasResolver->getAlias($className); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { if (!$this->isApplicable($options)) { return; } $className = $options['data_class']; $alias = $this->entityAliasResolver->getPluralAlias($className); $builder->add('contexts', 'oro_activity_contexts_select', ['label' => 'oro.activity.contexts.label', 'tooltip' => 'oro.activity.contexts.tooltip', 'required' => false, 'read_only' => false, 'mapped' => false, 'configs' => ['route_name' => 'oro_activity_form_autocomplete_search', 'route_parameters' => ['activity' => $alias, 'name' => $alias]]]); $builder->addEventListener(FormEvents::POST_SET_DATA, [$this, 'addDefaultContextListener']); }
/** * {@inheritdoc} */ public function transform($entityClass, $throwException = true) { try { return $this->entityAliasResolver->getPluralAlias($entityClass); } catch (EntityAliasNotFoundException $e) { if ($throwException) { throw $e; } } return null; }
/** * @return array [[entity plural alias, url safe class name], ...] */ protected function getSupportedEntities() { if (null === $this->supportedEntities) { $entities = $this->dictionaryProvider->getSupportedEntityClasses(); $this->supportedEntities = []; foreach ($entities as $className) { $this->supportedEntities[] = [$this->entityAliasResolver->getPluralAlias($className), $this->entityClassNameHelper->getUrlSafeClassName($className)]; } } return $this->supportedEntities; }
/** * @return string[] */ protected function getSupportedActivities() { if (null === $this->supportedActivities) { $this->supportedActivities = array_map(function (ConfigInterface $config) { // convert to entity alias return $this->entityAliasResolver->getPluralAlias($config->getId()->getClassName()); }, $this->groupingConfigProvider->filter(function (ConfigInterface $config) { // filter activity entities $groups = $config->get('groups'); return !empty($groups) && in_array(ActivityScope::GROUP_ACTIVITY, $groups, true); })); } return $this->supportedActivities; }
/** * @return array [[entity class, entity plural alias], ...] */ protected function getSupportedEntities() { if (null === $this->supportedEntities) { $resources = $this->resourcesLoader->getResources(Version::LATEST, [RequestType::REST, RequestType::JSON_API]); $this->supportedEntities = []; foreach ($resources as $resource) { $className = $resource->getEntityClass(); $pluralAlias = $this->entityAliasResolver->getPluralAlias($className); if (!empty($pluralAlias)) { $this->supportedEntities[] = [$className, $pluralAlias]; } } } return $this->supportedEntities; }
/** * 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); $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($targetClass); $item = $this->prepareItemTitle($item, $targetClass, $target, $targetId); $item['activityClassAlias'] = $this->entityAliasResolver->getPluralAlias($class); $item['entityId'] = $id; $item['targetId'] = $targetId; $item['targetClassName'] = $safeClassName; $item['icon'] = $config->get('icon'); $item['link'] = $this->getContextLink($targetClass, $targetId); $item = $this->dispatchContextTitle($item, $targetClass); $result[] = $item; } return $result; }
/** * @param Route $route * @param RouteCollectionAccessor $routes * @param string[] $entities */ protected function adjustRoutes(Route $route, RouteCollectionAccessor $routes, $entities) { $routeName = $routes->getName($route); foreach ($entities as $className) { $entity = $this->entityAliasResolver->getPluralAlias($className); if (empty($entity)) { continue; } $existingRoute = $routes->getByPath(str_replace(self::ENTITY_PLACEHOLDER, $entity, $route->getPath()), $route->getMethods()); if ($existingRoute) { // move existing route before the current route $existingRouteName = $routes->getName($existingRoute); $routes->remove($existingRouteName); $routes->insert($existingRouteName, $existingRoute, $routeName, true); } else { // add an additional strict route based on the base route and current entity $strictRoute = $routes->cloneRoute($route); $strictRoute->setPath(str_replace(self::ENTITY_PLACEHOLDER, $entity, $strictRoute->getPath())); $strictRoute->setDefault(self::ENTITY_ATTRIBUTE, $entity); $requirements = $strictRoute->getRequirements(); unset($requirements[self::ENTITY_ATTRIBUTE]); $strictRoute->setRequirements($requirements); if ($this->hasAttribute($route, self::ID_PLACEHOLDER)) { $this->setIdRequirement($strictRoute, $className); } $routes->insert($routes->generateRouteName($routeName), $strictRoute, $routeName, true); } } }
public function testGetPluralAliasCache() { $entityAliasProvider = $this->getMock('Oro\\Bundle\\EntityBundle\\Provider\\EntityAliasProviderInterface'); $entityAliasProvider->expects($this->once())->method('getEntityAlias')->with('Test\\Entity1')->willReturn(new EntityAlias('entity1_alias', 'entity1_plural_alias')); $this->entityAliasResolver->addProvider($entityAliasProvider); $this->assertEquals('entity1_plural_alias', $this->entityAliasResolver->getPluralAlias('Test\\Entity1')); // test that the result is cached $this->assertEquals('entity1_plural_alias', $this->entityAliasResolver->getPluralAlias('Test\\Entity1')); }
/** * 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 Route $route * @param RouteCollectionAccessor $routes * @param string[] $entities * * @return string[] The list of entities handled by the default controller */ protected function adjustRoutes(Route $route, RouteCollectionAccessor $routes, $entities) { $result = []; $routeName = $routes->getName($route); foreach ($entities as $className) { $entity = $this->entityAliasResolver->getPluralAlias($className); $existingRoute = $routes->getByPath(str_replace(self::ENTITY_PLACEHOLDER, $entity, $route->getPath()), $route->getMethods()); if ($existingRoute) { // move existing route before the current route $existingRouteName = $routes->getName($existingRoute); $routes->remove($existingRouteName); $routes->insert($existingRouteName, $existingRoute, $routeName, true); } else { // add an additional strict route based on the base route and current entity $strictRoute = $routes->cloneRoute($route); $strictRoute->setPath(str_replace(self::ENTITY_PLACEHOLDER, $entity, $strictRoute->getPath())); $strictRoute->setDefault(self::ENTITY_ATTRIBUTE, $entity); $routes->insert($routes->generateRouteName($routeName), $strictRoute, $routeName, true); $result[] = $entity; $result[] = $this->entityClassNameHelper->getUrlSafeClassName($className); } } 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 $entityClass * * @return string|null */ protected function getEntityResourceUri($entityClass) { $uri = null; if ($this->entityAliasResolver->hasAlias($entityClass)) { try { $uri = $this->router->generate('oro_rest_api_cget', ['entity' => $this->entityAliasResolver->getPluralAlias($entityClass)]); } catch (RoutingException $e) { // ignore any exceptions } } if ($uri) { $baseUrl = $this->router->getContext()->getBaseUrl(); if ($baseUrl) { $uri = substr($uri, strlen($baseUrl)); } } return $uri; }
/** * 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); $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($targetClass); if (!array_key_exists('title', $item) || !$item['title']) { 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'] = $this->getContextLink($targetClass, $targetId); $event = new PrepareContextTitleEvent($item, $targetClass); $this->dispatcher->dispatch(PrepareContextTitleEvent::EVENT_NAME, $event); $item = $event->getItem(); $result[] = $item; } return $result; }
/** * {@inheritdoc} */ public function transform($entityClass) { return $this->entityAliasResolver->getPluralAlias($entityClass); }