/** * POST_SUBMIT event handler * * @param FormEvent $event */ public function postSubmit(FormEvent $event) { $form = $event->getForm(); $options = $form->getConfig()->getOptions(); /** @var ConfigIdInterface $configId */ $configId = $options['config_id']; if (!$form->isValid()) { return; } // change the entity state to "Requires update" if the attribute has "require_schema_update" option // and the value of the attribute was changed $configProvider = $this->configManager->getProvider($configId->getScope()); if ($configProvider->getPropertyConfig()->isSchemaUpdateRequired($form->getName(), $configId)) { $newVal = $form->getData(); $oldVal = $this->configManager->getConfig($configId)->get($form->getName()); if ($this->isSchemaUpdateRequired($newVal, $oldVal)) { $extendConfigProvider = $this->configManager->getProvider('extend'); $extendConfig = $extendConfigProvider->getConfig($configId->getClassName()); if ($extendConfig->is('state', ExtendScope::STATE_ACTIVE)) { $extendConfig->set('state', ExtendScope::STATE_UPDATE); $this->configManager->persist($extendConfig); } } } }
/** * 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; }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $configModel = $options['config_model']; $data = array(); if ($configModel instanceof FieldConfigModel) { $className = $configModel->getEntity()->getClassName(); $fieldName = $configModel->getFieldName(); $fieldType = $configModel->getType(); $configType = PropertyConfigContainer::TYPE_FIELD; /** * Add read only field name and field type */ $builder->add('fieldName', 'text', array('label' => 'oro.entity_config.form.name.label', 'block' => 'general', 'disabled' => true, 'data' => $fieldName)); $builder->add('type', 'choice', array('label' => 'oro.entity_config.form.type.label', 'choices' => [], 'block' => 'general', 'disabled' => true, 'empty_value' => 'oro.entity_extend.form.data_type.' . $fieldType)); } else { $className = $configModel->getClassName(); $fieldName = null; $fieldType = null; $configType = PropertyConfigContainer::TYPE_ENTITY; } foreach ($this->configManager->getProviders() as $provider) { if ($provider->getPropertyConfig()->hasForm($configType, $fieldType)) { $config = $this->configManager->getConfig($provider->getId($className, $fieldName, $fieldType)); $builder->add($provider->getScope(), new ConfigScopeType($provider->getPropertyConfig()->getFormItems($configType, $fieldType), $config, $this->configManager, $configModel), array('block_config' => $this->getFormBlockConfig($provider, $configType))); $data[$provider->getScope()] = $config->all(); } } $builder->setData($data); $builder->addEventSubscriber(new ConfigSubscriber($this->doctrine, $this->configManager, $this->translator, $this->dbTranslationMetadataCache)); }
/** * @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; }
protected function getEntityChoiceList($entityClassName, $relationType) { $choices = array(); $extendEntityConfig = $this->configManager->getProvider('extend'); /** @var EntityConfigId[] $entityIds */ $entityIds = $this->targetEntity ? array($extendEntityConfig->getId($this->targetEntity)) : $extendEntityConfig->getIds(); if (in_array($relationType, array(RelationTypeBase::ONE_TO_MANY, RelationTypeBase::MANY_TO_MANY))) { $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('is_extend', false) || !$config->is('state', ExtendScope::STATE_NEW); }); foreach ($entityIds as $entityId) { $className = $entityId->getClassName(); if ($className != $entityClassName) { $entityConfig = $this->configManager->getProvider('entity')->getConfig($className); $choices[$className] = new ChoiceListItem($entityConfig->get('label'), array('data-icon' => $entityConfig->get('icon'))); } } return $choices; }
protected function getEntityChoiceList($entityClassName, $relationType) { $choices = []; $extendEntityConfig = $this->configManager->getProvider('extend'); /** @var EntityConfigId[] $entityIds */ $entityIds = $this->targetEntity ? [$extendEntityConfig->getId($this->targetEntity)] : $extendEntityConfig->getIds(); 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->targetEntity || !$config->is('is_deleted')); }); foreach ($entityIds as $entityId) { $className = $entityId->getClassName(); if ($className !== $entityClassName) { $entityConfig = $this->configManager->getProvider('entity')->getConfig($className); $choices[$className] = $entityConfig->get('label'); } } return $choices; }
/** * @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); }
public function testCreateConfigFieldModel() { $configId = new FieldConfigId('entity', self::ENTITY_CLASS, 'id', 'int'); $model = $this->createFieldConfigModel($this->createEntityConfigModel(self::ENTITY_CLASS), 'id', 'int'); $this->modelManager->expects($this->once())->method('findFieldModel')->with(self::ENTITY_CLASS, 'id')->will($this->returnValue(null)); $this->modelManager->expects($this->once())->method('createFieldModel')->with(self::ENTITY_CLASS, 'id', 'int', ConfigModelManager::MODE_DEFAULT)->will($this->returnValue($model)); $metadata = new EntityMetadata(self::ENTITY_CLASS); $idFieldMetadata = new FieldMetadata(self::ENTITY_CLASS, 'id'); $metadata->addPropertyMetadata($idFieldMetadata); $this->metadataFactory->expects($this->once())->method('getMetadataForClass')->with(self::ENTITY_CLASS)->will($this->returnValue($metadata)); $idFieldMetadata->defaultValues['entity'] = ['translatable' => 'labelVal', 'other' => 'otherVal']; $this->metadataFactory->expects($this->once())->method('getMetadataForClass')->with(self::ENTITY_CLASS)->will($this->returnValue($metadata)); $propertyConfigContainer = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\PropertyConfigContainer')->disableOriginalConstructor()->getMock(); $propertyConfigContainer->expects($this->once())->method('getDefaultValues')->with(PropertyConfigContainer::TYPE_FIELD, 'int')->will($this->returnValue(['translatable10' => 'labelVal10', 'other10' => 'otherVal10'])); $propertyConfigContainer->expects($this->once())->method('getTranslatableValues')->with(PropertyConfigContainer::TYPE_FIELD)->will($this->returnValue(['translatable', 'translatable10', 'auto_generated'])); $this->configProvider->expects($this->any())->method('getPropertyConfig')->will($this->returnValue($propertyConfigContainer)); $this->eventDispatcher->expects($this->once())->method('dispatch')->with(Events::NEW_FIELD_CONFIG, new FieldConfigEvent(self::ENTITY_CLASS, 'id', $this->configManager)); $config = new Config($configId); $config->set('other10', 'otherVal10'); $config->set('translatable10', 'labelVal10'); $config->set('other', 'otherVal'); $config->set('translatable', 'labelVal'); $config->set('auto_generated', 'oro.entityconfig.tests.unit.fixture.demoentity.id.auto_generated'); $result = $this->configManager->createConfigFieldModel(self::ENTITY_CLASS, 'id', 'int'); $this->assertEquals($model, $result); $this->assertEquals([$config], $this->configManager->getUpdateConfig()); // test that a config for a created model is stored in a local cache $result = $this->configManager->getConfig($configId); $this->assertEquals($config, $result); }
/** * {@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; }
/** * {@inheritdoc} */ public function getConfigById(ConfigIdInterface $configId) { if ($configId instanceof FieldConfigId) { return $this->configManager->getConfig($this->getId($configId->getClassName(), $configId->getFieldName())); } else { return $this->configManager->getConfig($this->getId($configId->getClassName())); } }
/** * @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); }
public function testConfigChangeSet() { $configId = new EntityConfigId('entity', self::ENTITY_CLASS); $originalConfig = $this->getConfig($configId, ['item1' => true, 'item11' => true, 'item12' => true, 'item2' => 123, 'item21' => 123, 'item22' => 123, 'item3' => 'val2', 'item4' => 'val4', 'item6' => null, 'item7' => 'val7']); $this->configCache->expects($this->once())->method('getEntityConfig')->willReturn($originalConfig); $this->configManager->getConfig($configId); $changedConfig = $this->getConfig($configId, ['item1' => true, 'item11' => 1, 'item12' => false, 'item2' => 123, 'item21' => '123', 'item22' => 456, 'item3' => 'val21', 'item5' => 'val5', 'item6' => 'val6', 'item7' => null]); $this->configManager->persist($changedConfig); $this->configManager->calculateConfigChangeSet($changedConfig); $this->assertEquals(['item12' => [true, false], 'item22' => [123, 456], 'item3' => ['val2', 'val21'], 'item5' => [null, 'val5'], 'item6' => [null, 'val6'], 'item7' => ['val7', null]], $this->configManager->getConfigChangeSet($changedConfig)); }
/** * @param FormEvent $event * @return array */ protected function prepareEvent(FormEvent $event) { $formData = $event->getForm()->getRoot()->getData(); if (!$formData) { return; } $entityId = $formData->getId(); $fieldConfigId = $event->getForm()->getConfig()->getOption('config_id'); $extendConfig = $this->configManager->getConfig($fieldConfigId); $model = $this->configManager->getConfigFieldModel($fieldConfigId->getClassName(), $fieldConfigId->getFieldName()); return [$entityId, $model, $extendConfig]; }
/** * Gets a default option of an enum associated with the given field * This method must be public because it is used as a callback * * @param string $fieldName * * @return string|null */ public function getEnumDefaultValue($fieldName) { if (isset($this->computedDefaultValues[$fieldName]) || array_key_exists($fieldName, $this->computedDefaultValues)) { return $this->computedDefaultValues[$fieldName]; } $fieldConfig = $this->configManager->getConfig(new FieldConfigId('extend', self::CALENDAR_PROPERTY_CLASS, $fieldName, 'enum')); $repo = $this->doctrineHelper->getEntityRepository($fieldConfig->get('target_entity')); $data = $repo->createQueryBuilder('e')->select('e.id')->where('e.default = true')->getQuery()->getArrayResult(); if ($data) { $data = array_shift($data); } $result = $data ? $data['id'] : null; $this->computedDefaultValues[$fieldName] = $result; return $result; }
/** * @dataProvider getConfigNotCachedProvider */ public function testGetConfigNotCached($configId, $getModelResult, $expectedConfig) { $this->modelManager->expects($this->any())->method('checkDatabase')->willReturn(true); $this->configCache->expects($this->once())->method('getConfigurable')->with(self::ENTITY_CLASS)->willReturn(true); $this->configCache->expects($this->once())->method('getConfig')->with($this->identicalTo($configId))->willReturn(null); $this->configCache->expects($this->once())->method('saveConfig')->with($this->equalTo($expectedConfig)); if ($configId instanceof FieldConfigId) { $this->modelManager->expects($this->never())->method('getEntityModel'); $this->modelManager->expects($this->once())->method('getFieldModel')->with($configId->getClassName(), $configId->getFieldName())->willReturn($getModelResult); } else { $this->modelManager->expects($this->once())->method('getEntityModel')->with($configId->getClassName())->willReturn($getModelResult); $this->modelManager->expects($this->never())->method('getFieldModel'); } $result = $this->configManager->getConfig($configId); $this->assertEquals($expectedConfig, $result); }
/** * @dataProvider getConfigNotCachedProvider */ public function testGetConfigNotCached(ConfigIdInterface $configId, $getModelResult, $expectedConfig) { $this->modelManager->expects($this->any())->method('checkDatabase')->willReturn(true); $this->configCache->expects($this->once())->method('getConfigurable')->with(self::ENTITY_CLASS)->willReturn(true); if ($configId instanceof FieldConfigId) { $this->configCache->expects($this->once())->method('getFieldConfig')->with($configId->getScope(), $configId->getClassName(), $configId->getFieldName())->willReturn(null); } else { $this->configCache->expects($this->once())->method('getEntityConfig')->with($configId->getScope(), $configId->getClassName())->willReturn(null); } $this->configCache->expects($this->once())->method('saveConfig')->with($this->equalTo($expectedConfig)); if ($configId instanceof FieldConfigId) { $this->modelManager->expects($this->never())->method('getEntityModel'); $this->modelManager->expects($this->once())->method('getFieldModel')->with($configId->getClassName(), $configId->getFieldName())->willReturn($getModelResult); } else { $this->modelManager->expects($this->once())->method('getEntityModel')->with($configId->getClassName())->willReturn($getModelResult); $this->modelManager->expects($this->never())->method('getFieldModel'); } $result = $this->configManager->getConfig($configId); $this->assertEquals($expectedConfig, $result); $this->assertArrayHasKey($this->buildConfigKey($configId), $this->readAttribute($this->configManager, 'originalConfigs')); }
/** * Gets configuration data for an object (class or field) which is represented by the given id. * * @param ConfigIdInterface $configId * @return ConfigInterface */ public function getConfigById(ConfigIdInterface $configId) { return $this->configManager->getConfig($this->copyId($configId)); }
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; } }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { foreach ($this->items as $code => $config) { if (isset($config['form']['type'])) { $options = isset($config['form']['options']) ? $config['form']['options'] : array(); $options['config_id'] = $this->config->getId(); $options['config_is_new'] = $this->configModel->getId() == false; /** * Disable field on editAction */ if (isset($config['options']['create_only']) && $this->configModel->getId()) { $options['disabled'] = true; $this->appendClassAttr($options, 'disabled-' . $config['form']['type']); } $propertyOnForm = false; $properties = []; if (isset($config['options']['required_property'])) { $properties[] = $config['options']['required_property']; } if (isset($config['options']['required_properties'])) { $properties = array_merge($properties, $config['options']['required_properties']); } if (!empty($properties)) { foreach ($properties as $property) { if (isset($property['config_id'])) { $configId = $property['config_id']; $fieldName = array_key_exists('field_name', $configId) ? $configId['field_name'] : false; if ($fieldName === false && $this->config->getId() instanceof FieldConfigId) { $fieldName = $this->config->getId()->getFieldName(); } $className = isset($configId['class_name']) ? $configId['class_name'] : $this->config->getId()->getClassName(); $scope = isset($configId['scope']) ? $configId['scope'] : $this->config->getId()->getScope(); if ($fieldName) { $configId = new FieldConfigId($scope, $className, $fieldName, $this->config->getId()->getFieldType()); } else { $configId = new EntityConfigId($scope, $className); } //check if requirement property is set in this form if ($className == $this->config->getId()->getClassName()) { if ($fieldName) { if ($this->config->getId() instanceof FieldConfigId && $this->config->getId()->getFieldName() == $fieldName) { $propertyOnForm = true; } } else { $propertyOnForm = true; } } } else { $propertyOnForm = true; $configId = $this->config->getId(); } $requireConfig = $this->configManager->getConfig($configId); if ($requireConfig->get($property['code']) != $property['value']) { if ($propertyOnForm) { $this->appendClassAttr($options, 'hide'); } else { continue; } } } if ($propertyOnForm) { $this->setAttr($options, 'data-requireProperty', $configId->toString() . $property['code']); $this->setAttr($options, 'data-requireValue', $property['value']); } } if (isset($config['constraints'])) { $options['constraints'] = $this->parseValidator($config['constraints']); } $this->setAttr($options, 'data-property_id', $this->config->getId()->toString() . $code); $builder->add($code, $config['form']['type'], $options); } } }