示例#1
0
 /**
  * @param ObjectManager    $manager
  * @param FieldConfigModel $fieldModel
  *
  * @return array
  */
 protected function getLeadSourceOptions(ObjectManager $manager, FieldConfigModel $fieldModel)
 {
     $options = [];
     try {
         $options = $manager->getRepository('OroEntityConfigBundle:OptionSet')->findOptionsByField($fieldModel->getId());
     } catch (\Exception $e) {
     }
     return $options;
 }
 public function testEvent()
 {
     $entityConfigModel = new EntityConfigModel('Test\\Class');
     $fieldConfigModel = new FieldConfigModel('testField', 'string');
     $fieldConfigModel->setEntity($entityConfigModel);
     $event = new NewFieldConfigModelEvent($fieldConfigModel, $this->configManager);
     $this->assertEquals('Test\\Class', $event->getClassName());
     $this->assertEquals('testField', $event->getFieldName());
     $this->assertEquals('string', $event->getFieldType());
     $this->assertEquals($this->configManager, $event->getConfigManager());
 }
 /**
  * @param string|null $type
  * @param array $data
  * @return FieldConfigModel
  */
 protected function createFieldConfigModel($type = null, array $data = [])
 {
     $entity = new FieldConfigModel();
     if ($type) {
         $entity->setType($type)->setFieldName('field_' . $type);
     }
     foreach ($data as $scope => $values) {
         $entity->fromArray($scope, $values, []);
     }
     return $entity;
 }
示例#4
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;
 }
示例#5
0
 public function testFieldConfigModel()
 {
     $fieldName = 'testField';
     $fieldType = 'integer';
     $entityModel = new EntityConfigModel('Test\\TestClass');
     $fieldModel = new FieldConfigModel($fieldName, $fieldType);
     $fieldModel->setEntity($entityModel);
     $this->assertEmpty($fieldModel->getId());
     $this->assertEquals($fieldName, $fieldModel->getFieldName());
     $this->assertEquals($fieldType, $fieldModel->getType());
     $this->assertSame($entityModel, $fieldModel->getEntity());
     $fieldName1 = 'testField';
     $fieldType1 = 'integer';
     $fieldModel->setFieldName($fieldName1);
     $fieldModel->setType($fieldType1);
     $this->assertEquals($fieldName1, $fieldModel->getFieldName());
     $this->assertEquals($fieldType1, $fieldModel->getType());
 }
 /**
  * Checks whether a field can be restored.
  * Unessential symbols, like _ or upper case letters, in a field name are ignored.
  *
  * @param FieldConfigModel $field
  *
  * @return bool
  */
 public function canFieldBeRestored(FieldConfigModel $field)
 {
     $normalizedFieldName = $this->normalizeFieldName($field->getFieldName());
     $configs = $this->extendConfigProvider->getConfigs($field->getEntity()->getClassName(), true);
     foreach ($configs as $config) {
         /** @var FieldConfigId $configId */
         $configId = $config->getId();
         $fieldName = $configId->getFieldName();
         if ($field->getFieldName() === $fieldName) {
             // skip current field
             continue;
         }
         if ($normalizedFieldName === $this->normalizeFieldName($fieldName) && !$config->is('is_deleted') && !$config->is('state', ExtendScope::STATE_DELETE)) {
             // an active field with similar name exists
             return false;
         }
     }
     return true;
 }
 public function testToFromArray()
 {
     $this->configValue->setCode('doctrine')->setScope('datagrid')->setValue('a:2:{s:4:"code";s:8:"test_001";s:4:"type";s:6:"string";}');
     $values = array('is_searchable' => true, 'is_sortable' => false, 'doctrine' => $this->configValue);
     $serializable = array('doctrine' => true);
     $this->configField->addValue(new ConfigModelValue('is_searchable', 'datagrid', false));
     $this->configField->fromArray('datagrid', $values, $serializable);
     $this->assertEquals(array('is_searchable' => 1, 'is_sortable' => 0, 'doctrine' => $this->configValue), $this->configField->toArray('datagrid'));
     $this->configEntity->addValue(new ConfigModelValue('is_searchable', 'datagrid', false));
     $this->configEntity->fromArray('datagrid', $values, $serializable);
     $this->assertEquals(array('is_searchable' => 1, 'is_sortable' => 0, 'doctrine' => $this->configValue), $this->configEntity->toArray('datagrid'));
 }
示例#8
0
 public function testFieldConfigModel()
 {
     $fieldName = 'testField';
     $fieldType = 'integer';
     $entityModel = new EntityConfigModel('Test\\TestClass');
     $fieldModel = new FieldConfigModel($fieldName, $fieldType);
     $fieldModel->setEntity($entityModel);
     $this->assertEmpty($fieldModel->getId());
     $this->assertEquals($fieldName, $fieldModel->getFieldName());
     $this->assertEquals($fieldType, $fieldModel->getType());
     $this->assertSame($entityModel, $fieldModel->getEntity());
     $fieldName1 = 'testField';
     $fieldType1 = 'integer';
     $fieldModel->setFieldName($fieldName1);
     $fieldModel->setType($fieldType1);
     $this->assertEquals($fieldName1, $fieldModel->getFieldName());
     $this->assertEquals($fieldType1, $fieldModel->getType());
     $this->assertCount(0, $fieldModel->getOptions());
     $optionSet = new OptionSet();
     $fieldModel->setOptions(new ArrayCollection([$optionSet]));
     $this->assertCount(1, $fieldModel->getOptions());
     $this->assertSame($optionSet, $fieldModel->getOptions()->first());
 }
 /**
  * @return string
  */
 public function getFieldType()
 {
     return $this->configModel->getType();
 }
 public function testCreateFieldModel()
 {
     $entityModel = $this->createEntityModel(self::TEST_ENTITY);
     $expectedResult = new FieldConfigModel(self::TEST_FIELD, 'int');
     $expectedResult->setMode(ConfigModelManager::MODE_DEFAULT);
     $expectedResult->setEntity($entityModel);
     $this->createRepositoryMock([$entityModel], [UnitOfWork::STATE_MANAGED, UnitOfWork::STATE_MANAGED, UnitOfWork::STATE_MANAGED]);
     $result = $this->configModelManager->createFieldModel(self::TEST_ENTITY, self::TEST_FIELD, 'int', ConfigModelManager::MODE_DEFAULT);
     $this->assertEquals($expectedResult, $result);
     // test that the created model is stored in a local cache
     $this->assertSame($result, $this->configModelManager->getFieldModel(self::TEST_ENTITY, self::TEST_FIELD));
 }
 /**
  * @param FieldConfigModel $entity
  * @return array
  */
 protected function validateEntityFields(FieldConfigModel $entity)
 {
     $errors = [];
     $fieldProperties = $this->fieldTypeProvider->getFieldProperties($entity->getType());
     foreach ($fieldProperties as $scope => $properties) {
         $scopeData = $entity->toArray($scope);
         foreach ($properties as $code => $config) {
             if (!isset($scopeData[$code])) {
                 continue;
             }
             if ($scope === 'enum') {
                 foreach ($scopeData[$code] as $key => $enumFields) {
                     $result = $this->strategyHelper->validateEntity(EnumValue::createFromArray($enumFields));
                     if ($result) {
                         $errors[] = sprintf('%s.%s.%s: %s', $scope, $code, $key, implode(' ', $result));
                     }
                 }
             } elseif (isset($config['constraints'])) {
                 $result = $this->strategyHelper->validateEntity($scopeData[$code], $this->constraintFactory->parse($config['constraints']));
                 if ($result) {
                     $errors[] = sprintf('%s.%s: %s', $scope, $code, implode(' ', $result));
                 }
             }
         }
     }
     return $errors;
 }
 /**
  * @return FieldConfigModel[]
  */
 protected function getFieldConfigModels()
 {
     $models = [];
     for ($i = 1; $i <= self::NUMBER_OF_FIELDS; $i++) {
         $model = new FieldConfigModel('field' . $i, 'string');
         if ($i % 5 === 0) {
             $model->setMode(ConfigModel::MODE_HIDDEN);
         }
         $models[] = $model;
     }
     return $models;
 }
示例#13
0
 /**
  * @param FieldConfigModel $model
  * @param array $options
  */
 protected function updateModelConfig(FieldConfigModel $model, array $options)
 {
     $fieldProperties = $this->fieldTypeProvider->getFieldProperties($model->getType());
     foreach ($fieldProperties as $scope => $properties) {
         $values = [];
         foreach ($properties as $code => $config) {
             if (!isset($options[$scope][$code])) {
                 continue;
             }
             $value = $this->denormalizeFieldValue(isset($config['options']) ? $config['options'] : [], $options[$scope][$code], $model->getType());
             if ($value !== null) {
                 $values[$code] = $value;
             }
         }
         $model->fromArray($scope, $values, []);
     }
 }
示例#14
0
 /**
  * @param FieldConfigModel $field
  * @return $this
  */
 public function addField($field)
 {
     $field->setEntity($this);
     $this->fields->add($field);
     return $this;
 }
 /**
  * @Route(
  *      "/unremove/{id}",
  *      name="oro_entityextend_field_unremove",
  *      requirements={"id"="\d+"},
  *      defaults={"id"=0}
  * )
  * Acl(
  *      id="oro_entityextend_field_unremove",
  *      label="UnRemove custom field",
  *      type="action",
  *      group_name=""
  * )
  */
 public function unremoveAction(FieldConfigModel $field)
 {
     if (!$field) {
         throw $this->createNotFoundException('Unable to find FieldConfigModel entity.');
     }
     $className = $field->getEntity()->getClassName();
     /** @var ExtendManager $extendManager */
     $extendManager = $this->get('oro_entity_extend.extend.extend_manager');
     /** @var ConfigManager $configManager */
     $configManager = $this->get('oro_entity_config.config_manager');
     $fieldConfig = $extendManager->getConfigProvider()->getConfig($className, $field->getFieldName());
     if (!$fieldConfig->is('owner', ExtendManager::OWNER_CUSTOM)) {
         return new Response('', Codes::HTTP_FORBIDDEN);
     }
     $fieldConfig->set('state', ExtendManager::STATE_UPDATED);
     $configManager->persist($fieldConfig);
     $entityConfig = $extendManager->getConfigProvider()->getConfig($className);
     $entityConfig->set('upgradeable', true);
     $configManager->persist($entityConfig);
     $configManager->flush();
     return new JsonResponse(array('message' => 'Item was restored', 'successful' => true), Codes::HTTP_OK);
 }
示例#16
0
 /**
  * @param string $className
  * @param string $fieldName
  * @param string $fieldType
  * @param string $mode
  *
  * @return FieldConfigModel
  *
  * @throws \InvalidArgumentException if $className is empty or $mode is invalid
  * @throws RuntimeException if models are locked
  */
 public function createFieldModel($className, $fieldName, $fieldType, $mode = ConfigModel::MODE_DEFAULT)
 {
     if (empty($className)) {
         throw new \InvalidArgumentException('$className must not be empty');
     }
     if (!$this->isValidMode($mode)) {
         throw new \InvalidArgumentException(sprintf('Invalid $mode: "%s"', $mode));
     }
     if ($this->lockObject->isLocked()) {
         throw new RuntimeException(sprintf('Cannot create field model for "%s::%s" because config models are locked.', $className, $fieldName));
     }
     $entityModel = $this->getEntityModel($className);
     $fieldModel = new FieldConfigModel($fieldName, $fieldType);
     $fieldModel->setMode($mode);
     $entityModel->addField($fieldModel);
     if (!empty($fieldName)) {
         $this->ensureFieldCacheWarmed($className);
         $this->fields[$className][$fieldName] = $fieldModel;
     }
     return $fieldModel;
 }
 /**
  * @param string $className
  * @param string $fieldName
  * @param string $fieldType
  * @param string $mode
  * @throws \InvalidArgumentException
  * @return FieldConfigModel
  */
 public function createFieldModel($className, $fieldName, $fieldType, $mode = self::MODE_DEFAULT)
 {
     if (!in_array($mode, array(self::MODE_DEFAULT, self::MODE_HIDDEN, self::MODE_READONLY))) {
         throw new \InvalidArgumentException(sprintf('FieldConfigModel give invalid parameter "mode" : "%s"', $mode));
     }
     /** @var EntityConfigModel $entityModel */
     $entityModel = $this->getModel($className);
     $fieldModel = new FieldConfigModel($fieldName, $fieldType);
     $fieldModel->setMode($mode);
     $entityModel->addField($fieldModel);
     $this->localCache->set($className . $fieldName, $fieldModel);
     return $fieldModel;
 }
示例#18
0
 /**
  * @param string $className
  * @param string $fieldName
  * @param string $fieldType
  * @param string $mode
  * @return FieldConfigModel
  * @throws \InvalidArgumentException
  */
 public function createFieldModel($className, $fieldName, $fieldType, $mode = self::MODE_DEFAULT)
 {
     if (empty($className)) {
         throw new \InvalidArgumentException('$className must not be empty');
     }
     if (!in_array($mode, array(self::MODE_DEFAULT, self::MODE_HIDDEN, self::MODE_READONLY))) {
         throw new \InvalidArgumentException(sprintf('Invalid $mode: "%s"', $mode));
     }
     $entityModel = $this->getEntityModel($className);
     $fieldModel = new FieldConfigModel($fieldName, $fieldType);
     $fieldModel->setMode($mode);
     $entityModel->addField($fieldModel);
     if (!empty($fieldName)) {
         $this->ensureFieldLocalCacheWarmed($className);
         $this->fieldLocalCache[$className][$fieldName] = $fieldModel;
     }
     return $fieldModel;
 }
示例#19
0
 /**
  * Gets config id for the given model
  *
  * @param EntityConfigModel|FieldConfigModel $model
  * @param string                             $scope
  * @return ConfigIdInterface
  */
 public function getConfigIdByModel($model, $scope)
 {
     if ($model instanceof FieldConfigModel) {
         return new FieldConfigId($scope, $model->getEntity()->getClassName(), $model->getFieldName(), $model->getType());
     } else {
         return new EntityConfigId($scope, $model->getClassName());
     }
 }
 /**
  * @param ConfigManager    $configManager
  * @param FieldConfigModel $fieldModel
  * @param array            $options
  */
 protected function updateFieldConfigs(ConfigManager $configManager, FieldConfigModel $fieldModel, $options)
 {
     $className = $fieldModel->getEntity()->getClassName();
     $fieldName = $fieldModel->getFieldName();
     foreach ($options as $scope => $scopeValues) {
         $configProvider = $configManager->getProvider($scope);
         $config = $configProvider->getConfig($className, $fieldName);
         $hasChanges = false;
         foreach ($scopeValues as $code => $val) {
             if (!$config->is($code, $val)) {
                 $config->set($code, $val);
                 $hasChanges = true;
             }
         }
         if ($hasChanges) {
             $configProvider->persist($config);
             $indexedValues = $configProvider->getPropertyConfig()->getIndexedValues($config->getId());
             $fieldModel->fromArray($config->getId()->getScope(), $config->all(), $indexedValues);
         }
     }
 }
示例#21
0
 /**
  * Returns default options for the given option set
  *
  * @param FieldConfigModel $configFieldModel
  * @return int[]
  */
 protected function getDefaultOptionIds(FieldConfigModel $configFieldModel)
 {
     $defaultOptionIds = [];
     foreach ($configFieldModel->getOptions()->toArray() as $option) {
         if ($option->getIsDefault()) {
             $defaultOptionIds[] = $option->getId();
         }
     }
     return $defaultOptionIds;
 }
 public function testCreateFieldModel()
 {
     $meta = $this->em->getClassMetadata(EntityConfigModel::ENTITY_NAME);
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $em->expects($this->any())->method('getRepository')->will($this->returnValue(new FoundEntityConfigRepository($em, $meta)));
     $serviceLink = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\DependencyInjection\\Utils\\ServiceLink')->disableOriginalConstructor()->getMock();
     $serviceLink->expects($this->any())->method('getService')->will($this->returnValue($em));
     $configModelManager = new ConfigModelManager($serviceLink);
     $entityModel = FoundEntityConfigRepository::getResultConfigEntity();
     $result = new FieldConfigModel('test', 'string');
     $result->setMode(ConfigModelManager::MODE_DEFAULT);
     $entityModel->addField($result);
     $this->assertEquals($result, $configModelManager->createFieldModel(DemoEntity::ENTITY_NAME, 'test', 'string', ConfigModelManager::MODE_DEFAULT));
 }
示例#23
0
 protected function createFieldConfigModel(EntityConfigModel $entityConfigModel, $fieldName, $fieldType, $mode = ConfigModel::MODE_DEFAULT)
 {
     $result = new FieldConfigModel($fieldName, $fieldType);
     $result->setEntity($entityConfigModel);
     $result->setMode($mode);
     return $result;
 }
 /**
  * @return array
  */
 public function processProvider()
 {
     $field = new FieldConfigModel('field_name', 'type1');
     $fieldWrongType = new FieldConfigModel('field_name', 'wrongType');
     $fieldSystem = new FieldConfigModel('field_name', 'type1');
     $fieldSystem->fromArray('extend', ['owner' => ExtendScope::OWNER_SYSTEM], []);
     return ['empty' => ['field' => new FieldConfigModel(), 'isExist' => false, 'expected' => null], 'filled' => ['field' => $field, 'isExist' => false, 'expected' => $field], 'wrong type' => ['field' => $fieldWrongType, 'isExist' => false, 'expected' => null], 'system' => ['field' => $fieldSystem, 'isExist' => true, 'expected' => null]];
 }