public function testGetConfig()
 {
     $this->assertEquals('Test\\Class', $this->fieldId->getClassName());
     $this->assertEquals('testScope', $this->fieldId->getScope());
     $this->assertEquals('testField', $this->fieldId->getFieldName());
     $this->assertEquals('string', $this->fieldId->getFieldType());
     $this->assertEquals('field_testScope_Test-Class_testField', $this->fieldId->toString());
     $this->fieldId->setFieldType('integer');
     $this->assertEquals('integer', $this->fieldId->getFieldType());
 }
 /**
  * Process field
  *
  * @param array           $mapConfig
  * @param ConfigInterface $searchConfig
  * @param FieldConfigId   $fieldId
  * @param string          $className
  */
 protected function processFields(&$mapConfig, ConfigInterface $searchConfig, FieldConfigId $fieldId, $className)
 {
     $extendConfigProvider = $this->configManager->getProvider('extend');
     $fieldName = $fieldId->getFieldName();
     if ($searchConfig->is('searchable')) {
         $fieldType = $this->transformCustomType($fieldId->getFieldType());
         if (in_array($fieldType, [Indexer::RELATION_ONE_TO_ONE, Indexer::RELATION_MANY_TO_ONE])) {
             $config = $extendConfigProvider->getConfig($className, $fieldName);
             $targetEntity = $config->get('target_entity');
             $targetField = $config->get('target_field');
             $targetType = $this->transformCustomType($this->configManager->getId('extend', $targetEntity, $targetField)->getFieldType());
             $field = ['name' => $fieldName, 'relation_type' => $fieldType, 'relation_fields' => [['name' => $targetField, 'target_type' => $targetType, 'target_fields' => [strtolower($fieldName . '_' . $targetField)]]]];
         } elseif (in_array($fieldType, [Indexer::RELATION_MANY_TO_MANY, Indexer::RELATION_ONE_TO_MANY])) {
             $config = $extendConfigProvider->getConfig($className, $fieldName);
             $targetEntity = $config->get('target_entity');
             $targetFields = array_unique(array_merge($config->get('target_grid'), $config->get('target_title'), $config->get('target_detailed')));
             $fields = [];
             foreach ($targetFields as $targetField) {
                 $targetType = $this->transformCustomType($this->configManager->getId('extend', $targetEntity, $targetField)->getFieldType());
                 $fields[] = ['name' => $targetField, 'target_type' => $targetType, 'target_fields' => [strtolower($fieldName . '_' . $targetField)]];
             }
             $field = ['name' => $fieldName, 'relation_type' => $fieldType, 'relation_fields' => $fields];
         } else {
             $field = ['name' => $fieldName, 'target_type' => $fieldType, 'target_fields' => [strtolower($fieldName)]];
         }
         $mapConfig[$className][self::FIELDS_PATH][] = $field;
     }
 }
 /**
  * @param FieldConfigId $selfFieldId
  * @param string        $targetEntity
  * @return string
  */
 public static function generateManyToManyJoinTableName(FieldConfigId $selfFieldId, $targetEntity)
 {
     $selfClassArray = explode('\\', $selfFieldId->getClassName());
     $selfClassName = array_pop($selfClassArray);
     $targetClassArray = explode('\\', $targetEntity);
     $targetClassName = array_pop($targetClassArray);
     return strtolower('oro_extend_' . $selfClassName . '_' . $targetClassName . '_' . $selfFieldId->getFieldName());
 }
 /**
  * @param ConfigInterface $extendConfig
  * @param FieldConfigId   $fieldConfigId
  *
  * @return array
  */
 protected function getOptions(ConfigInterface $extendConfig, FieldConfigId $fieldConfigId)
 {
     $className = $fieldConfigId->getClassName();
     $fieldName = $fieldConfigId->getFieldName();
     $options = [];
     switch ($fieldConfigId->getFieldType()) {
         case 'boolean':
             $options['empty_value'] = false;
             $options['choices'] = ['No', 'Yes'];
             break;
         case 'optionSet':
             $options['entityClassName'] = $className;
             $options['entityFieldName'] = $fieldName;
             break;
         case 'enum':
             $options['enum_code'] = $this->enumConfigProvider->getConfig($className, $fieldName)->get('enum_code');
             break;
         case 'multiEnum':
             $options['expanded'] = true;
             $options['enum_code'] = $this->enumConfigProvider->getConfig($className, $fieldName)->get('enum_code');
             break;
         case RelationType::MANY_TO_ONE:
             $options['entity_class'] = $extendConfig->get('target_entity');
             $options['configs'] = ['placeholder' => 'oro.form.choose_value', 'component' => 'relation', 'target_entity' => str_replace('\\', '_', $extendConfig->get('target_entity')), 'target_field' => $extendConfig->get('target_field'), 'properties' => [$extendConfig->get('target_field')]];
             break;
         case RelationType::ONE_TO_MANY:
         case RelationType::MANY_TO_MANY:
             $classArray = explode('\\', $extendConfig->get('target_entity'));
             $blockName = array_pop($classArray);
             $options['block'] = $blockName;
             $options['block_config'] = [$blockName => ['title' => null, 'subblocks' => [['useSpan' => false]]]];
             $options['class'] = $extendConfig->get('target_entity');
             $options['selector_window_title'] = 'Select ' . $blockName;
             $options['initial_elements'] = null;
             if (!$extendConfig->is('without_default')) {
                 $options['default_element'] = ExtendConfigDumper::DEFAULT_PREFIX . $fieldName;
             }
             break;
     }
     return $options;
 }
Example #5
0
 public function testNewEnumNameValidators()
 {
     $configId = new FieldConfigId('enum', 'Test\\Entity', 'testField', 'enum');
     $this->typeHelper->expects($this->any())->method('hasEnumCode')->with($configId->getClassName(), $configId->getFieldName())->will($this->returnValue(false));
     $resolver = $this->getOptionsResolver();
     $this->type->setDefaultOptions($resolver);
     $resolvedOptions = $resolver->resolve(['config_id' => $configId]);
     $this->assertCount(5, $resolvedOptions['constraints']);
     $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\NotBlank', $resolvedOptions['constraints'][0]);
     $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\Length', $resolvedOptions['constraints'][1]);
     $this->assertEquals($this->nameGenerator->getMaxEnumCodeSize(), $resolvedOptions['constraints'][1]->max);
     $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\Regex', $resolvedOptions['constraints'][2]);
     $this->assertEquals('/^[\\w- ]*$/', $resolvedOptions['constraints'][2]->pattern);
     $this->assertEquals(EnumNameType::INVALID_NAME_MESSAGE, $resolvedOptions['constraints'][2]->message);
     $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\Callback', $resolvedOptions['constraints'][3]);
     $context = $this->getMockBuilder('Symfony\\Component\\Validator\\ExecutionContext')->disableOriginalConstructor()->getMock();
     $context->expects($this->once())->method('addViolation')->with(EnumNameType::INVALID_NAME_MESSAGE);
     call_user_func($resolvedOptions['constraints'][3]->methods[0], '!@#$', $context);
     $this->assertInstanceOf('Oro\\Bundle\\EntityExtendBundle\\Validator\\Constraints\\UniqueEnumName', $resolvedOptions['constraints'][4]);
     $this->assertEquals($configId->getClassName(), $resolvedOptions['constraints'][4]->entityClassName);
     $this->assertEquals($configId->getFieldName(), $resolvedOptions['constraints'][4]->fieldName);
 }
Example #6
0
 public function testClearFieldCache()
 {
     $configId = new FieldConfigId('entity', self::ENTITY_CLASS, 'field');
     $this->configCache->expects($this->once())->method('deleteFieldConfig')->with($configId->getClassName(), $configId->getFieldName());
     $this->configManager->clearCache($configId);
 }
 /**
  * @param ClassMetadataBuilder $metadataBuilder
  * @param FieldConfigId        $fieldId
  * @param string               $targetEntity
  */
 protected function buildDefaultRelation(ClassMetadataBuilder $metadataBuilder, FieldConfigId $fieldId, $targetEntity)
 {
     $builder = $metadataBuilder->createOneToOne(ExtendConfigDumper::DEFAULT_PREFIX . $fieldId->getFieldName(), $targetEntity);
     $builder->addJoinColumn($this->nameGenerator->generateRelationDefaultColumnName($fieldId->getFieldName()), 'id', true, false, 'SET NULL');
     $builder->build();
 }
 protected function renameExtendField(Schema $schema, QueryBag $queries, Table $table, FieldConfigId $fieldConfigId, ConfigManager $configManager, EntityMetadataHelper $entityMetadataHelper)
 {
     switch ($fieldConfigId->getFieldType()) {
         case RelationType::MANY_TO_ONE:
             $this->renameManyToOneExtendField($schema, $queries, $table, $fieldConfigId->getFieldName());
             break;
         case RelationType::ONE_TO_MANY:
             $config = $configManager->getConfig($fieldConfigId);
             $targetEntityClassName = $config->get('target_entity');
             $this->renameOneToManyExtendField($schema, $queries, $table, $fieldConfigId->getFieldName(), $targetEntityClassName, $entityMetadataHelper);
             break;
         case RelationType::MANY_TO_MANY:
             break;
         default:
             $oldColumnName = 'field_' . $fieldConfigId->getFieldName();
             if ($table->hasColumn($oldColumnName)) {
                 $this->renameExtension->renameColumn($schema, $queries, $table, $oldColumnName, $fieldConfigId->getFieldName());
             }
             break;
     }
 }
 /**
  * @param ConfigInterface $fieldConfig
  */
 protected function createSelfRelation(ConfigInterface $fieldConfig)
 {
     /** @var FieldConfigId $fieldConfigId */
     $fieldConfigId = $fieldConfig->getId();
     $targetEntityClass = $fieldConfig->get('target_entity');
     $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType()));
     $selfConfig = $this->extendConfigProvider->getConfig($selfFieldId->getClassName());
     $relationKey = ExtendHelper::buildRelationKey($selfFieldId->getClassName(), $selfFieldId->getFieldName(), $selfFieldId->getFieldType(), $targetEntityClass);
     /**
      * in case of oneToMany relation
      * automatically create target field (type: manyToOne)
      */
     $targetFieldId = false;
     $owner = true;
     $targetOwner = false;
     if (in_array($selfFieldId->getFieldType(), RelationType::$toManyRelations)) {
         $classNameArray = explode('\\', $selfFieldId->getClassName());
         $relationFieldName = strtolower(array_pop($classNameArray)) . '_' . $selfFieldId->getFieldName();
         if ($selfFieldId->getFieldType() === RelationType::ONE_TO_MANY) {
             $owner = false;
             $targetOwner = true;
         }
         $targetFieldId = new FieldConfigId('extend', $targetEntityClass, $relationFieldName, ExtendHelper::getReverseRelationType($selfFieldId->getFieldType()));
     }
     $selfRelationConfig = ['assign' => false, 'field_id' => $selfFieldId, 'owner' => $owner, 'target_entity' => $targetEntityClass, 'target_field_id' => $targetFieldId];
     if ($fieldConfig->has('cascade')) {
         $selfRelationConfig['cascade'] = $fieldConfig->get('cascade');
     }
     $selfRelations = $selfConfig->get('relation') ?: [];
     $selfRelations[$relationKey] = $selfRelationConfig;
     $selfConfig->set('relation', $selfRelations);
     $this->extendConfigProvider->persist($selfConfig);
     $targetConfig = $this->extendConfigProvider->getConfig($targetEntityClass);
     $targetRelationConfig = ['assign' => false, 'field_id' => $targetFieldId, 'owner' => $targetOwner, 'target_entity' => $selfFieldId->getClassName(), 'target_field_id' => $selfFieldId];
     $targetRelations = $targetConfig->get('relation') ?: [];
     $targetRelations[$relationKey] = $targetRelationConfig;
     $targetConfig->set('relation', $targetRelations);
     $fieldConfig->set('relation_key', $relationKey);
     $this->extendConfigProvider->persist($targetConfig);
 }
Example #10
0
 /**
  * @param FieldConfigId $fieldConfigId
  *
  * @return string
  */
 protected function getRelationKey(FieldConfigId $fieldConfigId)
 {
     return ExtendHelper::buildRelationKey($fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), 'manyToOne', 'Oro\\Bundle\\AttachmentBundle\\Entity\\File');
 }
 /**
  * @param string        $scope
  * @param FieldConfigId $fieldId
  *
  * @return ConfigInterface
  */
 protected function getFieldConfig($scope, FieldConfigId $fieldId)
 {
     return $this->configManager->getProvider($scope)->getConfig($fieldId->getClassName(), $fieldId->getFieldName());
 }
 /**
  * @param FieldConfigId $fieldId
  *
  * @return ConfigInterface
  */
 protected function getFieldConfig(FieldConfigId $fieldId)
 {
     return $this->configManager->getFieldConfig('extend', $fieldId->getClassName(), $fieldId->getFieldName());
 }
Example #13
0
 public function testDeleteFieldConfig()
 {
     $configId = new FieldConfigId(self::SCOPE, self::ENTITY_CLASS, self::FIELD_NAME, self::FIELD_TYPE);
     $cacheKey = self::ENTITY_CLASS . '.' . self::FIELD_NAME;
     $this->cache->expects($this->at(0))->method('delete')->with(ConfigCache::FIELD_NAMES_KEY . self::ENTITY_CLASS)->willReturn(true);
     $this->cache->expects($this->at(1))->method('delete')->with($cacheKey)->willReturn(true);
     $this->assertTrue($this->configCache->deleteFieldConfig($configId->getClassName(), $configId->getFieldName()));
 }
 /**
  * @param object $entity
  * @param FieldConfigId $fieldConfig
  * @return OptionSetRelation[]
  */
 protected function getValueForOptionSet($entity, FieldConfigId $fieldConfig)
 {
     /** @var $optionSetRepository OptionSetRelationRepository */
     $optionSetRepository = $this->configManager->getEntityManager()->getRepository(OptionSetRelation::ENTITY_NAME);
     $model = $this->configManager->getConfigFieldModel($fieldConfig->getClassName(), $fieldConfig->getFieldName());
     $value = $optionSetRepository->findByFieldId($model->getId(), $entity->getId());
     array_walk($value, function (OptionSetRelation &$item) {
         $item = array('title' => $item->getOption()->getLabel());
     });
     $value['values'] = $value;
     return $value;
 }
Example #15
0
 /**
  * Changes a type of a field
  *
  * @param string $className
  * @param string $fieldName
  * @param string $newFieldName
  * @return bool TRUE if the name was changed; otherwise, FALSE
  */
 public function changeFieldName($className, $fieldName, $newFieldName)
 {
     $result = $this->modelManager->changeFieldName($className, $fieldName, $newFieldName);
     if ($result) {
         $this->eventDispatcher->dispatch(Events::RENAME_FIELD, new RenameFieldEvent($className, $fieldName, $newFieldName, $this));
         foreach ($this->getProviders() as $provider) {
             /** @var FieldConfigId $newConfigId */
             $newConfigId = $this->getId($provider->getScope(), $className, $newFieldName);
             $newConfigKey = $this->buildConfigKey($newConfigId);
             $configId = new FieldConfigId($newConfigId->getScope(), $newConfigId->getClassName(), $fieldName, $newConfigId->getFieldType());
             $cachedConfig = $this->cache->getFieldConfig($configId->getScope(), $configId->getClassName(), $configId->getFieldName(), true);
             if ($cachedConfig) {
                 $this->cache->saveConfig($this->changeConfigFieldName($cachedConfig, $newFieldName), true);
                 $this->cache->deleteFieldConfig($configId->getClassName(), $configId->getFieldName(), true);
             }
             $configKey = $this->buildConfigKey($configId);
             if (isset($this->persistConfigs[$configKey])) {
                 $this->persistConfigs[$newConfigKey] = $this->changeConfigFieldName($this->persistConfigs[$configKey], $newFieldName);
                 unset($this->persistConfigs[$configKey]);
             }
             if (isset($this->originalConfigs[$configKey])) {
                 $this->originalConfigs[$newConfigKey] = $this->changeConfigFieldName($this->originalConfigs[$configKey], $newFieldName);
                 unset($this->originalConfigs[$configKey]);
             }
             if (isset($this->configChangeSets[$configKey])) {
                 $this->configChangeSets[$newConfigKey] = $this->configChangeSets[$configKey];
                 unset($this->configChangeSets[$configKey]);
             }
         }
     }
     return $result;
 }