/**
  * {@inheritdoc}
  */
 public function getEntityAlias($entityClass)
 {
     if ($this->configManager->hasConfig($entityClass)) {
         // check for enums
         $enumCode = $this->configManager->getProvider('enum')->getConfig($entityClass)->get('code');
         if ($enumCode) {
             $entityAlias = $this->getEntityAliasFromConfig($entityClass);
             if (null !== $entityAlias) {
                 return $entityAlias;
             }
             return $this->createEntityAlias(str_replace('_', '', $enumCode));
         }
         // check for dictionaries
         $groups = $this->configManager->getProvider('grouping')->getConfig($entityClass)->get('groups');
         if (!empty($groups) && in_array(GroupingScope::GROUP_DICTIONARY, $groups, true)) {
             // delegate aliases generation to default provider
             return null;
         }
         // exclude hidden entities
         if ($this->configManager->isHiddenModel($entityClass)) {
             return false;
         }
         // check for custom entities
         if (ExtendHelper::isCustomEntity($entityClass)) {
             $entityAlias = $this->getEntityAliasFromConfig($entityClass);
             if (null !== $entityAlias) {
                 return $entityAlias;
             }
             return $this->createEntityAlias('Extend' . ExtendHelper::getShortClassName($entityClass));
         }
     }
     return null;
 }
Ejemplo n.º 2
0
 /**
  * @param string $entityClassName
  * @param string $relationType
  *
  * @return array
  */
 protected function getEntityChoiceList($entityClassName, $relationType)
 {
     /** @var EntityConfigId[] $entityIds */
     $entityIds = $this->targetEntityClass ? [$this->configManager->getId('extend', $this->targetEntityClass)] : $this->configManager->getIds('extend');
     if (in_array($relationType, [RelationTypeBase::ONE_TO_MANY, RelationTypeBase::MANY_TO_MANY], true)) {
         $entityIds = array_filter($entityIds, function (EntityConfigId $configId) {
             $config = $this->configManager->getConfig($configId);
             return $config->is('is_extend');
         });
     }
     $entityIds = array_filter($entityIds, function (EntityConfigId $configId) {
         $config = $this->configManager->getConfig($configId);
         return !$config->is('state', ExtendScope::STATE_NEW) && ($this->targetEntityClass || !$config->is('is_deleted'));
     });
     $choices = [];
     foreach ($entityIds as $entityId) {
         $className = $entityId->getClassName();
         if (!$this->configManager->hasConfig($className, 'id') && !ExtendHelper::isCustomEntity($className)) {
             // @todo: temporary ignore entities that don't have PK with name 'id'
             // remove this in https://magecore.atlassian.net/browse/BAP-9713
             continue;
         }
         if ($className !== $entityClassName) {
             $entityConfig = $this->configManager->getProvider('entity')->getConfig($className);
             $choices[$className] = $entityConfig->get('label');
         }
     }
     return $choices;
 }
Ejemplo n.º 3
0
 /**
  * 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;
 }
Ejemplo n.º 4
0
 /**
  * Returns grouped search results
  *
  * @param string $string
  * @return array
  */
 public function getGroupedResults($string)
 {
     $search = $this->getResults($string);
     // empty key array contains all data
     $result = array('' => array('count' => 0, 'class' => '', 'config' => array(), 'icon' => '', 'label' => ''));
     /** @var $item Item */
     foreach ($search->getElements() as $item) {
         $config = $item->getEntityConfig();
         $alias = $config['alias'];
         if (!isset($result[$alias])) {
             $group = array('count' => 0, 'class' => $item->getEntityName(), 'config' => $config, 'icon' => '', 'label' => '');
             if (!empty($group['class']) && $this->configManager->hasConfig($group['class'])) {
                 $entityConfigId = new EntityConfigId('entity', $group['class']);
                 $entityConfig = $this->configManager->getConfig($entityConfigId);
                 if ($entityConfig->has('plural_label')) {
                     $group['label'] = $this->translator->trans($entityConfig->get('plural_label'));
                 }
                 if ($entityConfig->has('icon')) {
                     $group['icon'] = $entityConfig->get('icon');
                 }
             }
             $result[$alias] = $group;
         }
         $result[$alias]['count']++;
         $result['']['count']++;
     }
     uasort($result, function ($first, $second) {
         if ($first['label'] == $second['label']) {
             return 0;
         }
         return $first['label'] > $second['label'] ? 1 : -1;
     });
     return $result;
 }
Ejemplo n.º 5
0
 /**
  * @param FieldConfigModel $configModel
  * @return array
  */
 protected function writeItem(FieldConfigModel $configModel)
 {
     $className = $configModel->getEntity()->getClassName();
     $fieldName = $configModel->getFieldName();
     $state = ExtendScope::STATE_UPDATE;
     if (!$this->configManager->hasConfig($className, $fieldName)) {
         $this->configManager->createConfigFieldModel($className, $fieldName, $configModel->getType());
         $state = ExtendScope::STATE_NEW;
     }
     $translations = [];
     foreach ($this->configManager->getProviders() as $provider) {
         $scope = $provider->getScope();
         $data = $configModel->toArray($scope);
         if (!$data) {
             continue;
         }
         $translations = array_merge($translations, $this->processData($provider, $provider->getConfig($className, $fieldName), $data, $state));
     }
     $this->setExtendData($className, $fieldName, $state);
     $this->updateEntityState($className);
     if ($state === ExtendScope::STATE_UPDATE && in_array($configModel->getType(), ['enum', 'multiEnum'], true)) {
         $this->setEnumData($configModel->toArray('enum'), $className, $fieldName);
     }
     return $translations;
 }
Ejemplo n.º 6
0
 /**
  * @param string $className The entity class name
  * @param string $attrName  The entity config attribute name
  * @param string $scope     The entity config scope name
  *
  * @return mixed
  */
 public function getClassConfigValue($className, $attrName, $scope = 'entity')
 {
     if (!$this->configManager->hasConfig($className)) {
         return null;
     }
     $entityConfig = new EntityConfigId($scope, $className);
     return $this->configManager->getConfig($entityConfig)->get($attrName);
 }
Ejemplo n.º 7
0
 /**
  * Check if an association between a given entity type and activity type is ready to be used in a business logic.
  * It means that the association should exist and should not be marked as deleted.
  *
  * @param string $entityClass   The target entity class
  * @param string $activityClass The activity entity class
  *
  * @return bool
  */
 protected function isActivityAssociationAccessible($entityClass, $activityClass)
 {
     $associationName = ExtendHelper::buildAssociationName($entityClass, ActivityScope::ASSOCIATION_KIND);
     if (!$this->configManager->hasConfig($activityClass, $associationName)) {
         return false;
     }
     return ExtendHelper::isFieldAccessible($this->configManager->getFieldConfig('extend', $activityClass, $associationName));
 }
 /**
  * Check if an association between a given entity type and attachments is ready to be used in a business logic.
  * It means that the association should exist and should not be marked as deleted.
  *
  * @param string $entityClass The target entity class
  *
  * @return bool
  */
 protected function isAttachmentAssociationAccessible($entityClass)
 {
     $associationName = ExtendHelper::buildAssociationName($entityClass);
     if (!$this->configManager->hasConfig(self::ATTACHMENT_ENTITY, $associationName)) {
         return false;
     }
     return ExtendHelper::isFieldAccessible($this->configManager->getFieldConfig('extend', self::ATTACHMENT_ENTITY, $associationName));
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if (!$this->configManager->hasConfig($metadata->name, $associationName)) {
         return false;
     }
     $extendFieldConfig = $this->configManager->getFieldConfig('extend', $metadata->name, $associationName);
     return !ExtendHelper::isFieldAccessible($extendFieldConfig) || $this->configManager->isHiddenModel($metadata->name, $associationName) || $extendFieldConfig->has('target_entity') && !ExtendHelper::isEntityAccessible($this->configManager->getEntityConfig('extend', $extendFieldConfig->get('target_entity')));
 }
Ejemplo n.º 10
0
 /**
  * @param $className
  *
  * @return null|string
  */
 public function getGridName($className)
 {
     $className = $this->entityClassNameHelper->resolveEntityClass($className);
     if (!$this->configManager->hasConfig($className)) {
         return null;
     }
     return $this->configManager->getProvider('security')->getConfig($className)->get('share_grid');
 }
 /**
  * @param string $className
  *
  * @return bool
  */
 protected function isExtendEntity($className)
 {
     $result = false;
     if ($this->configManager->hasConfig($className)) {
         $extendProvider = $this->configManager->getProvider('extend');
         $result = $extendProvider->getConfig($className)->is('is_extend');
     }
     return $result;
 }
Ejemplo n.º 12
0
 /**
  * @param string $className
  * @param string $fieldName
  *
  * @return bool
  */
 protected function isMultiEnumField($className, $fieldName)
 {
     if ($this->configManager->hasConfig($className, $fieldName)) {
         /** @var FieldConfigId $fieldId */
         $fieldId = $this->configManager->getId('extend', $className, $fieldName);
         if ($fieldId->getFieldType() === 'multiEnum') {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if (!$this->configManager->hasConfig($metadata->name, $associationName)) {
         return false;
     }
     $groups = $this->configManager->getEntityConfig('grouping', $metadata->name)->get('groups');
     if (empty($groups) || !in_array(ActivityScope::GROUP_ACTIVITY, $groups, true)) {
         return false;
     }
     $mapping = $metadata->getAssociationMapping($associationName);
     if (!$mapping['isOwningSide'] || !($mapping['type'] & ClassMetadata::MANY_TO_MANY)) {
         return false;
     }
     $activityAssociationName = ExtendHelper::buildAssociationName($mapping['targetEntity'], ActivityScope::ASSOCIATION_KIND);
     return $associationName === $activityAssociationName;
 }
Ejemplo n.º 14
0
 /**
  * {@inheritdoc}
  */
 public function getMetadataFor($object)
 {
     $metadata = [];
     if ($object instanceof EntityManagerAwareInterface) {
         $entityFQCN = $object->getManager()->getMetadata()->name;
         $metadata['entity'] = [];
         $metadata['entity']['phpType'] = $entityFQCN;
         if ($this->cm->hasConfig($entityFQCN)) {
             $config = $this->cm->getConfig(new EntityConfigId('entity', $entityFQCN));
             $metadata['entity']['label'] = $this->translator->trans($config->get('label'));
             $metadata['entity']['pluralLabel'] = $this->translator->trans($config->get('plural_label'));
             $metadata['entity']['description'] = $this->translator->trans($config->get('description'));
         }
     }
     return $metadata;
 }
Ejemplo n.º 15
0
 /**
  * @param string $targetClassName
  * @param string $activityClassName
  *
  * @return bool
  */
 public function isApplicableTarget($targetClassName, $activityClassName)
 {
     if (!isset($this->providers[$activityClassName]) || !$this->configManager->hasConfig($targetClassName)) {
         return false;
     }
     return $this->providers[$activityClassName]->isApplicableTarget($this->configManager->getId('entity', $targetClassName), $this->configManager);
 }
Ejemplo n.º 16
0
 /**
  * @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);
 }
 /**
  * @param ConfigIdInterface $configId
  * @return bool
  */
 public function hasConfigById(ConfigIdInterface $configId)
 {
     if ($configId instanceof FieldConfigId) {
         return $this->configManager->hasConfig($configId->getClassName(), $configId->getFieldName());
     } else {
         return $this->configManager->hasConfig($configId->getClassName());
     }
 }
Ejemplo n.º 18
0
 /**
  * @param string $className
  * @return null|string
  */
 protected function getClassLabel($className)
 {
     if (!$this->configManager->hasConfig($className)) {
         return null;
     }
     $label = $this->configManager->getProvider('entity')->getConfig($className)->get('label');
     return $this->translator->trans($label);
 }
 /**
  * @param string $className
  * @return null|string
  */
 protected function getClassLabel($className)
 {
     if (!$this->configManager->hasConfig($className)) {
         return null;
     }
     $entityConfig = new EntityConfigId('entity', $className);
     $label = $this->configManager->getConfig($entityConfig)->get('label');
     return $this->translator->trans($label);
 }
Ejemplo n.º 20
0
 /**
  * @param BeforeGroupingChainWidgetEvent $event
  */
 public function isAllowedButton(BeforeGroupingChainWidgetEvent $event)
 {
     $entity = $event->getEntity();
     $pageType = $event->getPageType();
     if ($pageType === null || !is_object($entity) || !$this->configManager->hasConfig($this->doctrineHelper->getEntityClass($entity)) || !$this->isAllowedOnPage($this->doctrineHelper->getEntityClass($entity), $pageType)) {
         // Clear allowed widgets
         $event->setWidgets([]);
     }
 }
Ejemplo n.º 21
0
 /**
  * @param string        $className
  * @param ConfigManager $configManager
  *
  * @return bool
  */
 protected function isExtendEntity($className, ConfigManager $configManager)
 {
     $result = false;
     // check if an entity is marked as extended (both extended and custom entities are marked as extended)
     if ($configManager->hasConfig($className)) {
         $extendProvider = $configManager->getProvider('extend');
         $result = $extendProvider->getConfig($className)->is('is_extend');
     }
     return $result;
 }
Ejemplo n.º 22
0
 /**
  * @param string $entityClass
  * @throws WorkflowException
  */
 public function generateWorkflowFields($entityClass)
 {
     if ($this->entityConnector->isWorkflowAware($entityClass)) {
         return;
     }
     $extendConfigProvider = $this->configManager->getProvider('extend');
     $entityConfig = $extendConfigProvider->getConfig($entityClass);
     if (!$entityConfig || !$entityConfig->is('is_extend')) {
         throw new WorkflowException(sprintf('Class %s can not be extended', $entityClass));
     }
     $workflowItemClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowItem';
     $workflowStepClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowStep';
     // add fields
     $hasWorkflowItemField = $this->configManager->hasConfig($entityClass, self::PROPERTY_WORKFLOW_ITEM);
     if (!$hasWorkflowItemField) {
         $this->addRelationField($entityClass, self::PROPERTY_WORKFLOW_ITEM, ConfigHelper::getTranslationKey('entity', 'label', $workflowItemClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowItemClass, 'related_entity'), $workflowItemClass, 'id');
     }
     $hasWorkflowStepField = $this->configManager->hasConfig($entityClass, self::PROPERTY_WORKFLOW_STEP);
     if (!$hasWorkflowStepField) {
         $this->addRelationField($entityClass, self::PROPERTY_WORKFLOW_STEP, ConfigHelper::getTranslationKey('entity', 'label', $workflowStepClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowStepClass, 'related_entity'), $workflowStepClass, 'label');
     }
     // update entity config
     $entityConfig->set('state', ExtendScope::STATE_UPDATE);
     $entityConfig->set('upgradeable', true);
     $this->configManager->persist($entityConfig);
     $this->configManager->flush();
     // update database
     $this->entityProcessor->updateDatabase();
     // make fields hidden
     // TODO: Fields can be hidden only after schema update due to a bug in DoctrineSubscriber
     // TODO: Should be fixed in scope of https://magecore.atlassian.net/browse/BAP-3621
     // TODO: If make fields hidden then these fields will be created only for the first extended entity
     // TODO: Should be fixed in scope of https://magecore.atlassian.net/browse/BAP-3632
     /*
     if (!$hasWorkflowItemField) {
         $this->hideRelationField($entityClass, self::PROPERTY_WORKFLOW_ITEM);
     }
     if (!$hasWorkflowStepField) {
         $this->hideRelationField($entityClass, self::PROPERTY_WORKFLOW_STEP);
     }
     $this->configManager->flush();
     */
 }
Ejemplo n.º 23
0
 /**
  * @param string $className
  * @param string $fieldName
  * @param string $fieldType
  * @param bool   $force
  */
 protected function loadFieldConfigs($className, $fieldName, $fieldType, $force)
 {
     if ($this->configManager->hasConfig($className, $fieldName)) {
         $this->logger->info(sprintf('Update config for "%s" field.', $fieldName));
         $this->configManager->updateConfigFieldModel($className, $fieldName, $force);
     } else {
         $this->logger->info(sprintf('Create config for "%s" field.', $fieldName));
         $this->configManager->createConfigFieldModel($className, $fieldName, $fieldType);
     }
 }
Ejemplo n.º 24
0
 /**
  * @param FieldConfigId $fieldId
  *
  * @return string
  */
 protected function getManyToOneColumnName(FieldConfigId $fieldId)
 {
     $columnName = null;
     if ($this->configManager->hasConfig($fieldId->getClassName(), $fieldId->getFieldName())) {
         $columnName = $this->getFieldConfig($fieldId)->get('column_name');
     }
     if (!$columnName) {
         $columnName = $this->nameGenerator->generateRelationColumnName($fieldId->getFieldName());
     }
     return $columnName;
 }
Ejemplo n.º 25
0
 /**
  * @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;
 }
Ejemplo n.º 26
0
 /**
  * @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;
 }
Ejemplo n.º 27
0
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if (!$this->configManager->hasConfig($metadata->name, $associationName)) {
         return false;
     }
     $extendFieldConfig = $this->configManager->getFieldConfig('extend', $metadata->name, $associationName);
     if (!ExtendHelper::isFieldAccessible($extendFieldConfig)) {
         return true;
     }
     if ($this->excludeHiddenFields && $this->configManager->isHiddenModel($metadata->name, $associationName)) {
         return true;
     }
     if ($extendFieldConfig->has('target_entity')) {
         $targetEntity = $extendFieldConfig->get('target_entity');
         if (!ExtendHelper::isEntityAccessible($this->configManager->getEntityConfig('extend', $targetEntity))) {
             return true;
         }
         if ($this->excludeHiddenEntities && $this->configManager->isHiddenModel($targetEntity)) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 28
0
 /**
  * Renames configurable fields
  *
  * @param array $renameConfigs
  * @return bool TRUE if at least one field was renamed; otherwise, FALSE
  */
 protected function renameFields($renameConfigs)
 {
     $hasChanges = false;
     foreach ($renameConfigs as $className => $renameConfig) {
         foreach ($renameConfig as $fieldName => $newFieldName) {
             if ($this->configManager->hasConfig($className, $fieldName)) {
                 $renamed = $this->renameField($className, $fieldName, $newFieldName);
                 if ($renamed && !$hasChanges) {
                     $hasChanges = true;
                 }
             }
         }
     }
     return $hasChanges;
 }
 /**
  * Get list of dynamic fields to show
  *
  * @param string $entityName
  *
  * @return array
  */
 protected function getDynamicFields($entityName)
 {
     $fields = [];
     if ($this->cm->hasConfig($entityName)) {
         $entityProvider = $this->cm->getProvider('entity');
         $extendProvider = $this->cm->getProvider('extend');
         $datagridProvider = $this->cm->getProvider('datagrid');
         $fieldIds = $entityProvider->getIds($entityName);
         foreach ($fieldIds as $fieldId) {
             if ($extendProvider->getConfigById($fieldId)->is('owner', ExtendManager::OWNER_CUSTOM) && $datagridProvider->getConfigById($fieldId)->is('is_visible') && !$extendProvider->getConfigById($fieldId)->is('state', ExtendManager::STATE_NEW) && !$extendProvider->getConfigById($fieldId)->is('is_deleted')) {
                 $fields[] = $fieldId;
             }
         }
     }
     return $fields;
 }
Ejemplo n.º 30
0
 /**
  * @dataProvider hasConfigProvider
  */
 public function testHasConfig($expectedResult, $checkDatabaseResult, $cachedResult, $findModelResult, $className, $fieldName)
 {
     $this->modelManager->expects($this->any())->method('checkDatabase')->willReturn($checkDatabaseResult);
     if ($checkDatabaseResult) {
         $this->configCache->expects($this->once())->method('getConfigurable')->with($className, $fieldName)->willReturn($cachedResult);
         if (null === $cachedResult) {
             $this->configCache->expects($this->once())->method('saveConfigurable')->with($expectedResult, $className, $fieldName);
             if ($fieldName) {
                 $this->modelManager->expects($this->once())->method('findFieldModel')->with($className, $fieldName)->willReturn($findModelResult);
             } else {
                 $this->modelManager->expects($this->once())->method('findEntityModel')->with($className)->willReturn($findModelResult);
             }
         }
     }
     $result = $this->configManager->hasConfig($className, $fieldName);
     $this->assertEquals($expectedResult, $result);
 }