/**
  * Pre set data event handler
  *
  * @param FormEvent $event
  */
 public function preSetData(FormEvent $event)
 {
     $form = $event->getForm();
     $configModel = $form->getConfig()->getOption('config_model');
     if (!$configModel instanceof FieldConfigModel) {
         return;
     }
     if (!in_array($configModel->getType(), ['enum', 'multiEnum'])) {
         return;
     }
     $enumConfig = $configModel->toArray('enum');
     if (empty($enumConfig['enum_code'])) {
         // new enum - a form already has a all data because on submit them are not removed from a config
         return;
     }
     $enumCode = $enumConfig['enum_code'];
     $data = $event->getData();
     $data['enum']['enum_name'] = $this->translator->trans(ExtendHelper::getEnumTranslationKey('label', $enumCode));
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $enumConfigProvider = $this->configManager->getProvider('enum');
     if ($enumConfigProvider->hasConfig($enumValueClassName)) {
         $enumEntityConfig = $enumConfigProvider->getConfig($enumValueClassName);
         $data['enum']['enum_public'] = $enumEntityConfig->get('public');
         $data['enum']['enum_options'] = $this->enumSynchronizer->getEnumOptions($enumValueClassName);
     }
     $event->setData($data);
 }
 /**
  * @dataProvider enumTypeProvider
  */
 public function testPreSetDataForExistingEnum($dataType)
 {
     $enumCode = 'test_enum';
     $enumLabel = ExtendHelper::getEnumTranslationKey('label', $enumCode);
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $configModel = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Entity\\FieldConfigModel')->disableOriginalConstructor()->getMock();
     $configModel->expects($this->once())->method('getType')->will($this->returnValue($dataType));
     $configModel->expects($this->once())->method('toArray')->with('enum')->will($this->returnValue(['enum_code' => $enumCode]));
     $event = $this->getFormEventMock($configModel);
     $initialData = [];
     $enumOptions = [['id' => 'val1', 'label' => 'Value 1', 'priority' => 1]];
     $expectedData = ['enum' => ['enum_name' => $enumLabel, 'enum_public' => true, 'enum_options' => $enumOptions]];
     $event->expects($this->once())->method('getData')->will($this->returnValue($initialData));
     $enumConfig = new Config(new EntityConfigId('enum', $enumValueClassName));
     $enumConfig->set('public', true);
     $this->enumSynchronizer->expects($this->once())->method('getEnumOptions')->with($enumValueClassName)->will($this->returnValue($enumOptions));
     $enumConfigProvider = $this->getConfigProviderMock();
     $this->configManager->expects($this->once())->method('getProvider')->with('enum')->will($this->returnValue($enumConfigProvider));
     $enumConfigProvider->expects($this->once())->method('hasConfig')->with($enumValueClassName)->will($this->returnValue(true));
     $enumConfigProvider->expects($this->once())->method('getConfig')->with($enumValueClassName)->will($this->returnValue($enumConfig));
     $event->expects($this->once())->method('setData')->with($expectedData);
     $this->extension->preSetData($event);
 }
 /**
  * @param string    $enumValueClassName The full class name of an entity is used to store enum values
  * @param string    $enumCode           The unique identifier of an enum
  * @param bool      $isMultiple         Indicates whether several options can be selected for this enum
  *                                      or it supports only one selected option
  * @param bool|null $isPublic           Indicates whether this enum can be used by any entity or
  *                                      it is designed to use in one entity only
  *                                      NULL means unspecified. In this case this attribute will not be
  *                                      changed for existing enum entity and will be set to FALSE
  *                                      for new enum entity
  */
 protected function createEnumValueConfigEntityModel($enumValueClassName, $enumCode, $isMultiple, $isPublic)
 {
     if ($this->configManager->hasConfigEntityModel($enumValueClassName)) {
         if (null !== $isPublic) {
             $this->relationBuilder->updateEntityConfigs($enumValueClassName, ['enum' => ['public' => $isPublic]]);
         }
         return;
     }
     if (null === $isPublic) {
         $isPublic = false;
     }
     // create entity
     $this->configManager->createConfigEntityModel($enumValueClassName, ConfigModelManager::MODE_HIDDEN);
     $this->relationBuilder->updateEntityConfigs($enumValueClassName, ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode), 'plural_label' => ExtendHelper::getEnumTranslationKey('plural_label', $enumCode), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode)], 'extend' => ['owner' => ExtendScope::OWNER_SYSTEM, 'is_extend' => true, 'table' => $this->nameGenerator->generateEnumTableName($enumCode, true), 'inherit' => ExtendHelper::BASE_ENUM_VALUE_CLASS], 'enum' => ['code' => $enumCode, 'public' => $isPublic, 'multiple' => $isMultiple]]);
     // create fields
     $this->configManager->createConfigFieldModel($enumValueClassName, 'id', 'string');
     $this->relationBuilder->updateFieldConfigs($enumValueClassName, 'id', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'id'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'id')], 'importexport' => ['identity' => true]]);
     $this->configManager->createConfigFieldModel($enumValueClassName, 'name', 'string');
     $this->relationBuilder->updateFieldConfigs($enumValueClassName, 'name', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'name'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'name')], 'datagrid' => ['is_visible' => false]]);
     $this->configManager->createConfigFieldModel($enumValueClassName, 'priority', 'integer');
     $this->relationBuilder->updateFieldConfigs($enumValueClassName, 'priority', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'priority'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'priority')], 'datagrid' => ['is_visible' => false]]);
     $this->configManager->createConfigFieldModel($enumValueClassName, 'default', 'boolean');
     $this->relationBuilder->updateFieldConfigs($enumValueClassName, 'default', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'default'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'default')], 'datagrid' => ['is_visible' => false]]);
 }
 /**
  * @param string $enumCode
  * @param string $enumName
  * @param string $locale
  *
  * @throws \InvalidArgumentException
  */
 public function applyEnumNameTrans($enumCode, $enumName, $locale)
 {
     if (strlen($enumCode) === 0) {
         throw new \InvalidArgumentException('$enumCode must not be empty.');
     }
     if (strlen($enumName) === 0) {
         throw new \InvalidArgumentException('$enumName must not be empty.');
     }
     if (empty($locale)) {
         throw new \InvalidArgumentException('$locale must not be empty.');
     }
     // add translations for an entity will be used to store enum values
     $labelsToBeUpdated = [];
     $labelKey = ExtendHelper::getEnumTranslationKey('label', $enumCode);
     $pluralLabelKey = ExtendHelper::getEnumTranslationKey('plural_label', $enumCode);
     $descriptionKey = ExtendHelper::getEnumTranslationKey('description', $enumCode);
     $currentLabelTrans = $this->translator->trans($labelKey, [], null, $locale);
     if ($currentLabelTrans === $labelKey) {
         // labels initialization
         $labelsToBeUpdated[$labelKey] = $enumName;
         $labelsToBeUpdated[$pluralLabelKey] = $enumName;
         if ($locale === Translation::DEFAULT_LOCALE) {
             // set empty description only for default locale
             $labelsToBeUpdated[$descriptionKey] = '';
         }
     } elseif ($enumName != $currentLabelTrans) {
         // update existing labels
         $labelsToBeUpdated[$labelKey] = $enumName;
         $labelsToBeUpdated[$pluralLabelKey] = $enumName;
     }
     if (!empty($labelsToBeUpdated)) {
         /** @var EntityManager $em */
         $em = $this->doctrine->getManagerForClass(Translation::ENTITY_NAME);
         /** @var TranslationRepository $translationRepo */
         $translationRepo = $em->getRepository(Translation::ENTITY_NAME);
         $transValues = [];
         foreach ($labelsToBeUpdated as $labelKey => $labelText) {
             // save into translation table
             $transValues[] = $translationRepo->saveValue($labelKey, $labelText, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI);
         }
         // flush translations to db
         $em->flush($transValues);
         // mark translation cache dirty
         $this->dbTranslationMetadataCache->updateTimestamp($locale);
     }
 }
 public function testApplyEnumNameTransNoTransForDefaultLocale()
 {
     $enumCode = 'test_enum';
     $enumName = 'Test Enum New';
     $locale = Translation::DEFAULT_LOCALE;
     $this->translator->expects($this->once())->method('trans')->with(ExtendHelper::getEnumTranslationKey('label', $enumCode), [], null, $locale)->will($this->returnValue(ExtendHelper::getEnumTranslationKey('label', $enumCode)));
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $this->doctrine->expects($this->once())->method('getManagerForClass')->with(Translation::ENTITY_NAME)->will($this->returnValue($em));
     $transRepo = $this->getMockBuilder('Oro\\Bundle\\TranslationBundle\\Entity\\Repository\\TranslationRepository')->disableOriginalConstructor()->getMock();
     $em->expects($this->once())->method('getRepository')->with(Translation::ENTITY_NAME)->will($this->returnValue($transRepo));
     $transLabelObj = new \stdClass();
     $transPluralLabelObj = new \stdClass();
     $transDescriptionObj = new \stdClass();
     $transRepo->expects($this->at(0))->method('saveValue')->with(ExtendHelper::getEnumTranslationKey('label', $enumCode), $enumName, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI)->will($this->returnValue($transLabelObj));
     $transRepo->expects($this->at(1))->method('saveValue')->with(ExtendHelper::getEnumTranslationKey('plural_label', $enumCode), $enumName, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI)->will($this->returnValue($transPluralLabelObj));
     $transRepo->expects($this->at(2))->method('saveValue')->with(ExtendHelper::getEnumTranslationKey('description', $enumCode), '', $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI)->will($this->returnValue($transDescriptionObj));
     $em->expects($this->once())->method('flush')->with($this->identicalTo([$transLabelObj, $transPluralLabelObj, $transDescriptionObj]));
     $this->dbTranslationMetadataCache->expects($this->once())->method('updateTimestamp')->with($locale);
     $this->synchronizer->applyEnumNameTrans($enumCode, $enumName, $locale);
 }
 /**
  * @param $enumValueClassName
  * @param $enumCode
  * @param $configManagerAt
  * @param $relationBuilderAt
  */
 protected function setAddEnumValueEntityFieldsExpectations($enumValueClassName, $enumCode, &$configManagerAt, &$relationBuilderAt)
 {
     $this->configManager->expects($this->at($configManagerAt++))->method('createConfigFieldModel')->with($enumValueClassName, 'id', 'string');
     $this->configManager->expects($this->at($configManagerAt++))->method('createConfigFieldModel')->with($enumValueClassName, 'name', 'string');
     $this->configManager->expects($this->at($configManagerAt++))->method('createConfigFieldModel')->with($enumValueClassName, 'priority', 'integer');
     $this->configManager->expects($this->at($configManagerAt++))->method('createConfigFieldModel')->with($enumValueClassName, 'default', 'boolean');
     $this->relationBuilder->expects($this->at($relationBuilderAt++))->method('updateFieldConfigs')->with($enumValueClassName, 'id', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'id'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'id')], 'importexport' => ['identity' => true]]);
     $this->relationBuilder->expects($this->at($relationBuilderAt++))->method('updateFieldConfigs')->with($enumValueClassName, 'name', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'name'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'name')], 'datagrid' => ['is_visible' => false]]);
     $this->relationBuilder->expects($this->at($relationBuilderAt++))->method('updateFieldConfigs')->with($enumValueClassName, 'priority', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'priority'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'priority')], 'datagrid' => ['is_visible' => false]]);
     $this->relationBuilder->expects($this->at($relationBuilderAt++))->method('updateFieldConfigs')->with($enumValueClassName, 'default', ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'default'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'default')], 'datagrid' => ['is_visible' => false]]);
 }
Exemple #7
0
 /**
  * Creates a table that is used to store enum values for the enum with the given code.
  *
  * @param Schema        $schema
  * @param string        $enumCode   The unique identifier of an enum
  * @param bool          $isMultiple Indicates whether several options can be selected for this enum
  *                                  or it supports only one selected option
  * @param bool          $isPublic   Indicates whether this enum can be used by any entity or
  *                                  it is designed to use in one entity only
  * @param bool|string[] $immutable  Indicates whether the changing the list of enum values and
  *                                  public flag is allowed or not. More details can be found
  *                                  in entity_config.yml
  * @param array         $options
  *
  * @return Table A table that is used to store enum values
  *
  * @throws \InvalidArgumentException
  *
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function createEnum(Schema $schema, $enumCode, $isMultiple = false, $isPublic = false, $immutable = false, array $options = [])
 {
     if ($enumCode !== ExtendHelper::buildEnumCode($enumCode)) {
         new \InvalidArgumentException(sprintf('The enum code "%s" must contain only lower alphabetical symbols, numbers and underscore.', $enumCode));
     }
     $tableName = $this->nameGenerator->generateEnumTableName($enumCode);
     $className = ExtendHelper::buildEnumValueClassName($enumCode);
     $options = array_replace_recursive([ExtendOptionsManager::MODE_OPTION => ConfigModelManager::MODE_HIDDEN, ExtendOptionsManager::ENTITY_CLASS_OPTION => $className, 'entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode), 'plural_label' => ExtendHelper::getEnumTranslationKey('plural_label', $enumCode), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode)], 'extend' => ['owner' => ExtendScope::OWNER_SYSTEM, 'is_extend' => true, 'table' => $tableName, 'inherit' => ExtendHelper::BASE_ENUM_VALUE_CLASS], 'enum' => ['code' => $enumCode, 'public' => $isPublic, 'multiple' => $isMultiple]], $options);
     if ($immutable) {
         $options['enum']['immutable'] = true;
     }
     $table = $schema->createTable($tableName);
     $this->entityMetadataHelper->registerEntityClass($tableName, $className);
     $table->addOption(OroOptions::KEY, $options);
     $table->addColumn('id', 'string', ['length' => ExtendHelper::MAX_ENUM_VALUE_ID_LENGTH, OroOptions::KEY => ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'id'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'id')], 'importexport' => ['identity' => true]]]);
     $table->addColumn('name', 'string', ['length' => 255, OroOptions::KEY => ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'name'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'name')], 'datagrid' => ['is_visible' => false]]]);
     $table->addColumn('priority', 'integer', [OroOptions::KEY => ['entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'priority'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'priority')], 'datagrid' => ['is_visible' => false]]]);
     $table->addColumn('is_default', 'boolean', [OroOptions::KEY => [ExtendOptionsManager::FIELD_NAME_OPTION => 'default', 'entity' => ['label' => ExtendHelper::getEnumTranslationKey('label', $enumCode, 'default'), 'description' => ExtendHelper::getEnumTranslationKey('description', $enumCode, 'default')], 'datagrid' => ['is_visible' => false]]]);
     $table->setPrimaryKey(['id']);
     return $table;
 }
 /**
  * @dataProvider getEnumTranslationKeyProvider
  */
 public function testGetEnumTranslationKey($propertyName, $enumCode, $fieldName, $expected)
 {
     $this->assertEquals($expected, ExtendHelper::getEnumTranslationKey($propertyName, $enumCode, $fieldName));
 }