Ejemplo n.º 1
0
 /**
  * Checks if the given constraint is applied or not
  *
  * @param Options     $options
  * @param string|null $constraintName Can be: null, 'add', 'delete'
  *
  * @return bool
  */
 protected function isDisabled($options, $constraintName = null)
 {
     /** @var ConfigIdInterface $configId */
     $configId = $options['config_id'];
     $className = $configId->getClassName();
     if (empty($className)) {
         return false;
     }
     $fieldName = $this->typeHelper->getFieldName($configId);
     if (empty($fieldName)) {
         return false;
     }
     $enumCode = $this->typeHelper->getEnumCode($className, $fieldName);
     if (!empty($enumCode)) {
         if ($options['config_is_new']) {
             // a new field reuses public enum
             return true;
         }
         if ($constraintName) {
             $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
             if ($this->typeHelper->isImmutable('enum', $enumValueClassName, null, $constraintName)) {
                 // is immutable
                 return true;
             }
         }
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     $defaultFieldOptions = ['multiple' => true];
     $resolver->setDefaults(['dictionary_code' => null, 'class' => null, 'field_options' => $defaultFieldOptions]);
     $resolver->setNormalizers(['class' => function (Options $options, $value) {
         if ($value !== null) {
             return $value;
         }
         if (empty($options['dictionary_code'])) {
             throw new InvalidOptionsException('Either "class" or "dictionary_code" must option must be set.');
         }
         $class = ExtendHelper::buildEnumValueClassName($options['dictionary_code']);
         if (!is_a($class, 'Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue', true)) {
             throw new InvalidOptionsException(sprintf('"%s" must be a child of "%s"', $class, 'Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue'));
         }
         return $class;
     }, 'field_options' => function (Options $options, $value) use(&$defaultFieldOptions) {
         if (isset($options['class'])) {
             $nullValue = null;
             if ($options->has('null_value')) {
                 $nullValue = $options->get('null_value');
             }
             $value['choices'] = $this->getChoices($options['class'], $nullValue);
         } else {
             $value['choices'] = [];
         }
         return array_merge($defaultFieldOptions, $value);
     }]);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $defaultFieldOptions = ['multiple' => true];
     $resolver->setDefaults(['enum_code' => null, 'class' => null, 'field_options' => $defaultFieldOptions, 'operator_choices' => [self::TYPE_IN => $this->translator->trans('oro.filter.form.label_type_in'), self::TYPE_NOT_IN => $this->translator->trans('oro.filter.form.label_type_not_in')]]);
     $resolver->setNormalizer('class', function (Options $options, $value) {
         if ($value !== null) {
             return $value;
         }
         if (empty($options['enum_code'])) {
             throw new InvalidOptionsException('Either "class" or "enum_code" must option must be set.');
         }
         $class = ExtendHelper::buildEnumValueClassName($options['enum_code']);
         if (!is_a($class, 'Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue', true)) {
             throw new InvalidOptionsException(sprintf('"%s" must be a child of "%s"', $class, 'Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue'));
         }
         return $class;
     });
     // this normalizer allows to add/override field_options options outside
     $resolver->setNormalizer('field_options', function (Options $options, $value) use(&$defaultFieldOptions) {
         if (isset($options['class'])) {
             $nullValue = null;
             if ($options->offsetExists('null_value')) {
                 $nullValue = $options->offsetGet('null_value');
             }
             $value['choices'] = $this->getChoices($options['class'], $nullValue);
         } else {
             $value['choices'] = [];
         }
         return array_merge($defaultFieldOptions, $value);
     });
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $internalRatings = $manager->getRepository(ExtendHelper::buildEnumValueClassName(Account::INTERNAL_RATING_CODE))->findAll();
     /** @var \Oro\Bundle\UserBundle\Entity\User $accountOwner */
     $accountOwner = $manager->getRepository('OroUserBundle:User')->findOneBy([]);
     foreach ($this->accounts as $accountName => $accountData) {
         /** @var \OroB2B\Bundle\AccountBundle\Entity\AccountGroup $accountGroup */
         $accountGroup = $this->getReference(LoadAccountGroupDemoData::ACCOUNT_GROUP_REFERENCE_PREFIX . $accountData['group']);
         $account = new Account();
         $account->setName($accountName)->setGroup($accountGroup)->setParent(null)->setOrganization($accountOwner->getOrganization())->setOwner($accountOwner)->setInternalRating($internalRatings[array_rand($internalRatings)]);
         $manager->persist($account);
         $this->addReference(static::ACCOUNT_REFERENCE_PREFIX . $account->getName(), $account);
         if (isset($accountData['subsidiaries'])) {
             foreach ($accountData['subsidiaries'] as $subsidiaryName => $subsidiaryData) {
                 /** @var \OroB2B\Bundle\AccountBundle\Entity\AccountGroup $subsidiaryGroup */
                 $subsidiaryGroup = $this->getReference(LoadAccountGroupDemoData::ACCOUNT_GROUP_REFERENCE_PREFIX . $subsidiaryData['group']);
                 $subsidiary = new Account();
                 $subsidiary->setName($subsidiaryName)->setGroup($subsidiaryGroup)->setParent($account)->setOrganization($accountOwner->getOrganization())->setOwner($accountOwner)->setInternalRating($internalRatings[array_rand($internalRatings)]);
                 $manager->persist($subsidiary);
                 $this->addReference(static::ACCOUNT_REFERENCE_PREFIX . $subsidiary->getName(), $subsidiary);
             }
         }
     }
     $manager->flush();
 }
Ejemplo n.º 5
0
 /**
  * Checks if the form type should be read-only or not
  *
  * @param Options $options
  *
  * @return bool
  */
 protected function isReadOnly($options)
 {
     /** @var ConfigIdInterface $configId */
     $configId = $options['config_id'];
     $className = $configId->getClassName();
     if (empty($className)) {
         return false;
     }
     $fieldName = $this->typeHelper->getFieldName($configId);
     if (empty($fieldName)) {
         return false;
     }
     if ($this->typeHelper->isSystem($className, $fieldName)) {
         // it is a system field
         return true;
     }
     $enumCode = $this->typeHelper->getEnumCode($className, $fieldName);
     if (!empty($enumCode)) {
         if ($options['config_is_new']) {
             // a new field reuses public enum
             return true;
         }
         $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
         if ($this->typeHelper->isImmutable('enum', $enumValueClassName, null, 'public')) {
             // is immutable
             return true;
         }
         if ($this->typeHelper->hasOtherReferences($enumCode, $className, $fieldName)) {
             // an enum is reused by other fields
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     /** @var AccountUser[] $accountUsers */
     $accountUsers = $manager->getRepository('OroB2BCustomerBundle:AccountUser')->findAll();
     $internalRatings = $manager->getRepository(ExtendHelper::buildEnumValueClassName(Customer::INTERNAL_RATING_CODE))->findAll();
     $rootCustomer = null;
     $firstLevelCustomer = null;
     // create customer groups
     $rootGroup = $this->createCustomerGroup('Root');
     $firstLevelGroup = $this->createCustomerGroup('First');
     $secondLevelGroup = $this->createCustomerGroup('Second');
     $manager->persist($rootGroup);
     $manager->persist($firstLevelGroup);
     $manager->persist($secondLevelGroup);
     // create customers
     foreach ($accountUsers as $index => $accountUser) {
         $customer = $accountUser->getCustomer();
         switch ($index % 3) {
             case 0:
                 $customer->setGroup($rootGroup);
                 $rootCustomer = $customer;
                 break;
             case 1:
                 $customer->setGroup($firstLevelGroup)->setParent($rootCustomer);
                 $firstLevelCustomer = $customer;
                 break;
             case 2:
                 $customer->setGroup($secondLevelGroup)->setParent($firstLevelCustomer);
                 break;
         }
         $customer->setInternalRating($internalRatings[array_rand($internalRatings)]);
     }
     $manager->flush();
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     /** @var UserManager $userManager */
     $userManager = $this->container->get('oro_user.manager');
     $admin = $userManager->findUserByEmail(LoadAdminUserData::DEFAULT_ADMIN_EMAIL);
     $organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
     /** @var Store $store */
     $store = $this->getReference('store');
     /** @var Channel $channel */
     $channel = $this->getReference('default_channel');
     /** @var Integration $integration */
     $integration = $this->getReference('integration');
     $className = ExtendHelper::buildEnumValueClassName('mage_subscr_status');
     $enumRepo = $manager->getRepository($className);
     foreach ($this->subscriberData as $data) {
         $subscriber = new NewsletterSubscriber();
         $date = new \DateTime();
         $date->modify('-1 day');
         /** @var AbstractEnumValue $status */
         $status = $enumRepo->find($data['status']);
         $subscriber->setEmail($data['email'])->setStatus($status)->setConfirmCode(uniqid())->setStore($store)->setOwner($admin)->setOrganization($organization)->setOriginId($data['originId'])->setChangeStatusAt($date)->setCreatedAt($date)->setUpdatedAt($date)->setChannel($integration)->setDataChannel($channel);
         if (!empty($data['customer'])) {
             /** @var Customer $customer */
             $customer = $this->getReference($data['customer']);
             $subscriber->setCustomer($customer);
         }
         $this->setReference($data['reference'], $subscriber);
         $manager->persist($subscriber);
     }
     $manager->flush();
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     parent::load($manager);
     $className = ExtendHelper::buildEnumValueClassName($this->getEnumCode());
     /** @var AbstractEnumValue[] $enumData */
     $enumData = $manager->getRepository($className)->findAll();
     foreach ($enumData as $enumItem) {
         $this->addReference($enumItem->getName(), $enumItem);
     }
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     $resolver->setDefaults(['enum_code' => null, 'class' => null, 'query_builder' => function (EntityRepository $repo) {
         return $repo->createQueryBuilder('o')->orderBy('o.priority');
     }, 'property' => 'name', 'multiple' => null]);
     $resolver->setNormalizers(['class' => function (Options $options, $value) {
         return !empty($value) ? $value : ExtendHelper::buildEnumValueClassName($options['enum_code']);
     }, 'multiple' => function (Options $options, $value) {
         return $value !== null ? $value : $this->configManager->getProvider('enum')->getConfig($options['class'])->is('multiple');
     }]);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     $defaultFieldOptions = ['multiple' => true];
     $resolver->setDefaults(['enum_code' => null, 'class' => null, 'field_options' => $defaultFieldOptions]);
     $resolver->setNormalizers(['class' => function (Options $options, $value) {
         return $value !== null ? $value : ExtendHelper::buildEnumValueClassName($options['enum_code']);
     }, 'field_options' => function (Options $options, $value) use(&$defaultFieldOptions) {
         $value['choices'] = $options['class'] !== null ? $this->getChoices($options['class'], $options['null_value']) : [];
         return array_merge($defaultFieldOptions, $value);
     }]);
 }
Ejemplo n.º 11
0
 /**
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $className = ExtendHelper::buildEnumValueClassName('lead_source');
     /** @var EnumValueRepository $enumRepo */
     $enumRepo = $manager->getRepository($className);
     $priority = 1;
     foreach ($this->data as $name => $isDefault) {
         $enumOption = $enumRepo->createEnumValue($name, $priority++, $isDefault);
         $manager->persist($enumOption);
     }
     $manager->flush();
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $className = ExtendHelper::buildEnumValueClassName('mage_subscr_status');
     /** @var EnumValueRepository $enumValueRepository */
     $enumValueRepository = $manager->getRepository($className);
     $priority = 1;
     foreach ($this->data as $id => $name) {
         $enumOption = $enumValueRepository->createEnumValue($name, $priority++, false, $id);
         $manager->persist($enumOption);
     }
     $manager->flush();
 }
Ejemplo n.º 13
0
 protected function initSupportingEntities(ObjectManager $manager = null)
 {
     if ($manager) {
         $this->em = $manager;
     }
     $this->users = $this->em->getRepository('OroUserBundle:User')->findAll();
     $this->countries = $this->em->getRepository('OroAddressBundle:Country')->findAll();
     $className = ExtendHelper::buildEnumValueClassName('lead_source');
     $enumRepo = $manager->getRepository($className);
     $this->sources = $enumRepo->findAll();
     $this->channel = $this->channelBuilderFactory->createBuilder()->setChannelType(DefaultChannelData::B2B_CHANNEL_TYPE)->setStatus(Channel::STATUS_ACTIVE)->setEntities()->getChannel();
     $manager->persist($this->channel);
     $manager->flush($this->channel);
 }
Ejemplo n.º 14
0
 /**
  * {@inheritdoc}
  */
 public function init($name, array $params)
 {
     $params[FilterUtility::FRONTEND_TYPE_KEY] = 'dictionary';
     if (isset($params['class'])) {
         $params[FilterUtility::FORM_OPTIONS_KEY]['class'] = $params['class'];
         unset($params['class']);
     }
     if (isset($params['enum_code'])) {
         $params[FilterUtility::FORM_OPTIONS_KEY] = ['enum_code' => $params['enum_code'], 'class' => ExtendHelper::buildEnumValueClassName($params['enum_code'])];
         $params['class'] = ExtendHelper::buildEnumValueClassName($params['enum_code']);
         unset($params['enum_code']);
     }
     parent::init($name, $params);
 }
 /**
  * {@inheritdoc}
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function preUpdate()
 {
     $enumConfigProvider = $this->configManager->getProvider('enum');
     $extendConfigProvider = $this->configManager->getProvider('extend');
     $entityConfigs = $extendConfigProvider->getConfigs();
     foreach ($entityConfigs as $entityConfig) {
         if (!$entityConfig->is('is_extend')) {
             continue;
         }
         $fieldConfigs = $extendConfigProvider->getConfigs($entityConfig->getId()->getClassName());
         foreach ($fieldConfigs as $fieldConfig) {
             if (!$fieldConfig->in('state', [ExtendScope::STATE_NEW, ExtendScope::STATE_UPDATE])) {
                 continue;
             }
             /** @var FieldConfigId $fieldConfigId */
             $fieldConfigId = $fieldConfig->getId();
             $fieldType = $fieldConfigId->getFieldType();
             if (!in_array($fieldType, ['enum', 'multiEnum'])) {
                 continue;
             }
             // prepare input parameters
             $fieldOptions = [];
             $enumFieldConfig = $enumConfigProvider->getConfig($fieldConfigId->getClassName(), $fieldConfigId->getFieldName());
             $enumCode = $enumFieldConfig->get('enum_code');
             $enumName = $enumFieldConfig->get('enum_name');
             $isPublic = $enumFieldConfig->get('enum_public');
             if (empty($enumCode) && $isPublic && empty($enumName)) {
                 throw new \LogicException(sprintf('Both "enum_code" and "enum_name" cannot be empty for a public enum. Field: %s::%s.', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName()));
             }
             if (empty($enumCode)) {
                 $enumCode = $enumName !== null ? ExtendHelper::buildEnumCode($enumName) : ExtendHelper::generateEnumCode($fieldConfigId->getClassName(), $fieldConfigId->getFieldName());
                 $fieldOptions['enum']['enum_code'] = $enumCode;
             }
             $isMultiple = $this->fieldTypeHelper->getUnderlyingType($fieldType) === RelationType::MANY_TO_MANY;
             $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
             // create an entity is used to store enum values
             $this->createEnumValueConfigEntityModel($enumValueClassName, $enumCode, $isMultiple, $isPublic);
             // create a relation
             if ($isMultiple) {
                 $fieldOptions['extend']['without_default'] = true;
                 $this->relationBuilder->addManyToManyRelation($entityConfig, $enumValueClassName, $fieldConfigId->getFieldName(), ['name'], ['name'], ['name'], $fieldOptions, $fieldType);
             } else {
                 $this->relationBuilder->addManyToOneRelation($entityConfig, $enumValueClassName, $fieldConfigId->getFieldName(), 'name', $fieldOptions, $fieldType);
             }
         }
     }
 }
Ejemplo n.º 16
0
 /**
  * @param ObjectManager $manager
  */
 public function loadSources(ObjectManager $manager)
 {
     $className = ExtendHelper::buildEnumValueClassName('lead_source');
     /** @var EnumValueRepository $enumRepo */
     $enumRepo = $manager->getRepository($className);
     /** @var AbstractEnumValue[] $sources */
     $sources = $enumRepo->findAll();
     $randomSource = count($sources) - 1;
     $leads = $this->em->getRepository('OroCRMSalesBundle:Lead')->findAll();
     foreach ($leads as $lead) {
         /** @var Lead $lead */
         $source = $sources[mt_rand(0, $randomSource)];
         $lead->setSource($source);
         $manager->persist($lead);
     }
     $manager->flush();
 }
Ejemplo n.º 17
0
 /**
  * {@inheritdoc}
  */
 public function up(Schema $schema, QueryBag $queries)
 {
     $existingEnumTables = $this->getExistingEnumTables($schema);
     $optionSets = $this->dataStorageExtension->get('existing_option_sets');
     foreach ($optionSets as &$optionSet) {
         $entityTableName = $this->extendExtension->getTableNameByEntityClass($optionSet['class_name']);
         $data = $optionSet['data'];
         $enumCode = $this->buildEnumCode($optionSet['class_name'], $optionSet['field_name'], $existingEnumTables);
         $enumTable = $this->extendExtension->addEnumField($schema, $entityTableName, $optionSet['field_name'], $enumCode, $data['extend']['set_expanded'], false, ['extend' => ['owner' => $data['extend']['owner']]]);
         $existingEnumTables[] = $enumTable->getName();
         $optionSet['table_name'] = $entityTableName;
         $optionSet['enum_class_name'] = ExtendHelper::buildEnumValueClassName($enumCode);
         $optionSet['enum_table_name'] = $enumTable->getName();
     }
     $this->fillEnumValues($queries, $optionSets);
     $this->assignEnumValues($queries, $optionSets);
     $this->removeOptionSetAttributes($queries, $optionSets);
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->objectManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $this->doctrineHelper = $this->getMockBuilder('Oro\\Bundle\\EntityBundle\\ORM\\DoctrineHelper')->disableOriginalConstructor()->getMock();
     $this->eventDispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')->disableOriginalConstructor()->getMock();
     $this->skuIncrementor = $this->getMockBuilder('OroB2B\\Bundle\\ProductBundle\\Duplicator\\SkuIncrementorInterface')->disableOriginalConstructor()->getMock();
     $this->attachmentManager = $this->getMockBuilder('Oro\\Bundle\\AttachmentBundle\\Manager\\AttachmentManager')->disableOriginalConstructor()->getMock();
     $this->attachmentProvider = $this->getMockBuilder('Oro\\Bundle\\AttachmentBundle\\Provider\\AttachmentProvider')->disableOriginalConstructor()->getMock();
     $this->productStatusRepository = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $this->productStatusDisabled = $this->getMockBuilder('Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue')->disableOriginalConstructor()->getMockForAbstractClass();
     $this->connection = $this->getMockBuilder('Doctrine\\DBAL\\Connection')->disableOriginalConstructor()->getMock();
     $this->doctrineHelper->expects($this->once())->method('getEntityRepository')->with(ExtendHelper::buildEnumValueClassName('prod_status'))->will($this->returnValue($this->productStatusRepository));
     $this->doctrineHelper->expects($this->any())->method('getEntityManager')->with($this->anything())->will($this->returnValue($this->objectManager));
     $this->objectManager->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection));
     $this->productStatusRepository->expects($this->once())->method('find')->with(Product::STATUS_DISABLED)->will($this->returnValue($this->productStatusDisabled));
     $this->duplicator = new ProductDuplicator($this->doctrineHelper, $this->eventDispatcher, $this->attachmentManager, $this->attachmentProvider);
     $this->duplicator->setSkuIncrementor($this->skuIncrementor);
 }
Ejemplo n.º 19
0
 /**
  * @dataProvider setDefaultOptionsProvider
  */
 public function testSetDefaultOptions(ConfigIdInterface $configId, $isNewConfig, $enumCode, $isSystem, $isImmutablePublic, $hasOtherReferences, $options, $expectedOptions)
 {
     $fieldName = $configId instanceof FieldConfigId ? $configId->getFieldName() : null;
     $enumValueClassName = $enumCode ? ExtendHelper::buildEnumValueClassName($enumCode) : null;
     $this->typeHelper->expects($this->any())->method('getEnumCode')->with($configId->getClassName(), $fieldName)->will($this->returnValue($enumCode));
     $this->typeHelper->expects($this->any())->method('isSystem')->with($configId->getClassName(), $fieldName)->will($this->returnValue($isSystem));
     $this->typeHelper->expects($this->any())->method('isImmutable')->with('enum', $enumValueClassName, null, 'public')->will($this->returnValue($isImmutablePublic));
     $this->typeHelper->expects($this->any())->method('hasOtherReferences')->with($enumCode, $configId->getClassName(), $fieldName)->will($this->returnValue($hasOtherReferences));
     $resolver = $this->getOptionsResolver();
     $this->type->setDefaultOptions($resolver);
     $options['config_id'] = $configId;
     $options['config_is_new'] = $isNewConfig;
     $resolvedOptions = $resolver->resolve($options);
     $this->assertSame($configId, $resolvedOptions['config_id']);
     unset($resolvedOptions['config_id']);
     $this->assertEquals($isNewConfig, $resolvedOptions['config_is_new']);
     unset($resolvedOptions['config_is_new']);
     $this->assertEquals($expectedOptions, $resolvedOptions);
 }
Ejemplo n.º 20
0
 public function testTransEnumLocalCache()
 {
     $enumCode1 = 'test_enum1';
     $enumCode2 = 'test_enum2';
     $enumValueEntityClass1 = ExtendHelper::buildEnumValueClassName($enumCode1);
     $enumValueEntityClass2 = ExtendHelper::buildEnumValueClassName($enumCode2);
     $repo1 = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $repo2 = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $this->doctrine->expects($this->exactly(2))->method('getRepository')->will($this->returnValueMap([[$enumValueEntityClass1, null, $repo1], [$enumValueEntityClass2, null, $repo2]]));
     $repo1->expects($this->once())->method('findAll')->will($this->returnValue([]));
     $repo2->expects($this->once())->method('findAll')->will($this->returnValue([]));
     $this->extension->transEnum('val1', $enumValueEntityClass1);
     $this->extension->transEnum('val1', $enumValueEntityClass2);
     // call one more time to check local cache
     $this->extension->transEnum('val1', $enumValueEntityClass1);
     $this->extension->transEnum('val1', $enumValueEntityClass2);
     // call with enum code to check local cache keys
     $this->extension->transEnum('val1', $enumCode1);
     $this->extension->transEnum('val1', $enumCode2);
 }
 /**
  * @dataProvider setDefaultOptionsProvider
  */
 public function testSetDefaultOptions(ConfigIdInterface $configId, $isNewConfig, $enumCode, $isImmutableAdd, $isImmutableDelete, $options, $expectedOptions)
 {
     $enumValueClassName = $enumCode ? ExtendHelper::buildEnumValueClassName($enumCode) : null;
     $this->typeHelper->expects($this->any())->method('getEnumCode')->with($configId->getClassName(), $configId instanceof FieldConfigId ? $configId->getFieldName() : null)->will($this->returnValue($enumCode));
     $this->typeHelper->expects($this->any())->method('isImmutable')->will($this->returnValueMap([['enum', $enumValueClassName, null, 'add', $isImmutableAdd], ['enum', $enumValueClassName, null, 'delete', $isImmutableDelete]]));
     $resolver = $this->getOptionsResolver();
     $this->type->setDefaultOptions($resolver);
     $options['config_id'] = $configId;
     $options['config_is_new'] = $isNewConfig;
     $resolvedOptions = $resolver->resolve($options);
     $this->assertSame($configId, $resolvedOptions['config_id']);
     unset($resolvedOptions['config_id']);
     $this->assertEquals($isNewConfig, $resolvedOptions['config_is_new']);
     unset($resolvedOptions['config_is_new']);
     $this->assertFalse($resolvedOptions['handle_primary']);
     unset($resolvedOptions['handle_primary']);
     $this->assertEquals('oro_entity_extend_enum_value', $resolvedOptions['type']);
     unset($resolvedOptions['type']);
     $this->assertEquals($expectedOptions, $resolvedOptions);
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $newsletterSubscribers = [];
     $customers = new ArrayCollection($manager->getRepository('OroCRMMagentoBundle:Customer')->findAll());
     $className = ExtendHelper::buildEnumValueClassName('mage_subscr_status');
     $statuses = new ArrayCollection($manager->getRepository($className)->findAll());
     /** @var Customer $customer */
     foreach ($customers as $customer) {
         $newsletterSubscriber = new NewsletterSubscriber();
         $newsletterSubscriber->setOwner($customer->getOwner())->setOrganization($customer->getOrganization())->setChangeStatusAt($customer->getCreatedAt())->setCreatedAt($customer->getCreatedAt())->setUpdatedAt($customer->getUpdatedAt())->setConfirmCode(uniqid('', true))->setChannel($customer->getChannel())->setEmail($customer->getEmail())->setStore($customer->getStore())->setStatus($statuses->get(array_rand($statuses->toArray())))->setDataChannel($customer->getDataChannel());
         if (rand(0, 1)) {
             $newsletterSubscriber->setCustomer($customer);
         }
         $newsletterSubscribers[] = $newsletterSubscriber;
     }
     foreach ($newsletterSubscribers as $newsletterSubscriber) {
         $manager->persist($newsletterSubscriber);
     }
     if ($newsletterSubscribers) {
         $manager->flush($newsletterSubscribers);
     }
 }
Ejemplo n.º 23
0
 /**
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /** @var ConfigManager $configManager */
     $configManager = $this->container->get('oro_entity_config.config_manager');
     $configFieldModel = $configManager->getConfigFieldModel('OroCRM\\Bundle\\SalesBundle\\Entity\\Lead', 'extend_source');
     if (is_null($configFieldModel)) {
         // no old options found
         return;
     }
     $enumOptions = [];
     $options = $this->getLeadSourceOptions($manager, $configFieldModel);
     if (empty($options)) {
         // no old options found
         return;
     }
     $className = ExtendHelper::buildEnumValueClassName('lead_source');
     /** @var EnumValueRepository $enumRepo */
     $enumRepo = $manager->getRepository($className);
     /** @var OptionSet $option */
     foreach ($options as $option) {
         $enumOption = $enumRepo->createEnumValue($option->getLabel(), $option->getPriority(), $option->getIsDefault());
         $manager->persist($enumOption);
         $enumOptions[$option->getId()] = $enumOption;
     }
     $qBuilder = $manager->getRepository('OroCRMSalesBundle:Lead')->createQueryBuilder('lead');
     $relRepo = $manager->getRepository('OroEntityConfigBundle:OptionSetRelation');
     $paginator = new Paginator($qBuilder, false);
     /** @var Lead $lead */
     foreach ($paginator as $lead) {
         /** @var OptionSetRelation $relEntity */
         $relEntity = $relRepo->createQueryBuilder('r')->select('IDENTITY(r.option) as option_id')->where('r.field = ?0', 'r.entity_id = ?1')->getQuery()->execute([$configFieldModel, $lead->getId()]);
         $sourceOptionId = $relEntity[0]['option_id'];
         $source = empty($enumOptions[$sourceOptionId]) ? null : $enumOptions[$sourceOptionId];
         $lead->setSource($source);
     }
     $manager->flush();
 }
Ejemplo n.º 24
0
 /**
  * {@inheritdoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     $resolver->setDefaults(['enum_code' => null, 'class' => null, 'query_builder' => function (EnumValueRepository $repo) {
         return $repo->getValuesQueryBuilder();
     }, 'property' => 'name', 'multiple' => null]);
     $resolver->setNormalizers(['class' => function (Options $options, $value) {
         if ($value !== null) {
             return $value;
         }
         if (empty($options['enum_code'])) {
             throw new InvalidOptionsException('Either "class" or "enum_code" must option must be set.');
         }
         $class = ExtendHelper::buildEnumValueClassName($options['enum_code']);
         if (!is_a($class, 'Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue', true)) {
             throw new InvalidOptionsException(sprintf('"%s" must be a child of "%s"', $class, 'Oro\\Bundle\\EntityExtendBundle\\Entity\\AbstractEnumValue'));
         }
         return $class;
     }, 'multiple' => function (Options $options, $value) {
         if ($value === null && !empty($options['class'])) {
             $value = $this->configManager->getProvider('enum')->getConfig($options['class'])->is('multiple');
         }
         return $value;
     }]);
 }
Ejemplo n.º 25
0
 protected function doTestSetDefaultOptions(AbstractType $type, OptionsResolver $resolver, $enumCode, $multiple = false, $expanded = false, array $options = [])
 {
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $enumConfig = new Config(new EntityConfigId('enum', $enumValueClassName));
     $enumConfig->set('multiple', $multiple);
     $enumConfigProvider = $this->getConfigProviderMock();
     $this->configManager->expects($this->once())->method('getProvider')->with('enum')->will($this->returnValue($enumConfigProvider));
     $enumConfigProvider->expects($this->once())->method('getConfig')->with($enumValueClassName)->will($this->returnValue($enumConfig));
     $type->setDefaultOptions($resolver);
     $resolvedOptions = $resolver->resolve(array_merge($options, ['enum_code' => $enumCode, 'expanded' => $expanded]));
     $this->assertEquals($multiple, $resolvedOptions['multiple']);
     $this->assertEquals($expanded, $resolvedOptions['expanded']);
     $this->assertEquals($enumCode, $resolvedOptions['enum_code']);
     $this->assertEquals($enumValueClassName, $resolvedOptions['class']);
     $this->assertEquals('name', $resolvedOptions['property']);
     $this->assertNotNull($resolvedOptions['query_builder']);
     unset($resolvedOptions['multiple']);
     unset($resolvedOptions['expanded']);
     unset($resolvedOptions['enum_code']);
     unset($resolvedOptions['class']);
     unset($resolvedOptions['property']);
     unset($resolvedOptions['query_builder']);
     return $resolvedOptions;
 }
Ejemplo n.º 26
0
 /**
  * @param array $data
  * @param string $className
  * @param string $fieldName
  */
 protected function setEnumData(array $data, $className, $fieldName)
 {
     $provider = $this->configManager->getProvider('enum');
     if (!$provider) {
         return;
     }
     $enumCode = $provider->getConfig($className, $fieldName)->get('enum_code');
     if (!$enumCode || !isset($data['enum_options'])) {
         return;
     }
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $enumOptions = array_map(function ($option) {
         if (!isset($option['id'])) {
             $option['id'] = $option['label'];
         }
         return $option;
     }, $data['enum_options']);
     if ($provider->hasConfig($enumValueClassName)) {
         $this->enumSynchronizer->applyEnumOptions($enumValueClassName, $enumOptions, $this->translationHelper->getLocale());
     }
 }
Ejemplo n.º 27
0
 protected function doTestSetDefaultOptions(AbstractEnumType $type, OptionsResolver $resolver, $enumCode, $multiple = false, $expanded = false, array $options = [])
 {
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     // AbstractEnumType require class to be instance of AbstractEnumValue
     // This may be achieved with Stub. Stub namespace does not reflect Stub path. So we have to load it manually
     $fileName = ExtendHelper::getShortClassName($enumValueClassName) . '.php';
     require_once realpath(__DIR__ . DIRECTORY_SEPARATOR . 'Stub' . DIRECTORY_SEPARATOR . $fileName);
     $enumConfig = new Config(new EntityConfigId('enum', $enumValueClassName));
     $enumConfig->set('multiple', $multiple);
     $enumConfigProvider = $this->getConfigProviderMock();
     $this->configManager->expects($this->once())->method('getProvider')->with('enum')->will($this->returnValue($enumConfigProvider));
     $enumConfigProvider->expects($this->once())->method('getConfig')->with($enumValueClassName)->will($this->returnValue($enumConfig));
     $type->setDefaultOptions($resolver);
     $resolvedOptions = $resolver->resolve(array_merge($options, ['enum_code' => $enumCode, 'expanded' => $expanded]));
     $this->assertEquals($multiple, $resolvedOptions['multiple']);
     $this->assertEquals($expanded, $resolvedOptions['expanded']);
     $this->assertEquals($enumCode, $resolvedOptions['enum_code']);
     $this->assertEquals($enumValueClassName, $resolvedOptions['class']);
     $this->assertEquals('name', $resolvedOptions['property']);
     $this->assertNotNull($resolvedOptions['query_builder']);
     unset($resolvedOptions['multiple'], $resolvedOptions['expanded'], $resolvedOptions['enum_code'], $resolvedOptions['class'], $resolvedOptions['property'], $resolvedOptions['query_builder']);
     return $resolvedOptions;
 }
Ejemplo n.º 28
0
 /**
  * Post submit event handler
  *
  * @param FormEvent $event
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function postSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     $configModel = $form->getConfig()->getOption('config_model');
     if (!$configModel instanceof FieldConfigModel) {
         return;
     }
     if (!in_array($configModel->getType(), ['enum', 'multiEnum'])) {
         return;
     }
     if (!$form->isValid()) {
         return;
     }
     $data = $event->getData();
     $enumConfig = $configModel->toArray('enum');
     $enumName = $this->getValue($data['enum'], 'enum_name');
     $enumCode = $this->getValue($enumConfig, 'enum_code');
     if (empty($enumCode)) {
         $enumCode = $enumName !== null ? ExtendHelper::buildEnumCode($enumName) : ExtendHelper::generateEnumCode($configModel->getEntity()->getClassName(), $configModel->getFieldName());
     }
     $locale = $this->translator->getLocale();
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $enumConfigProvider = $this->configManager->getProvider('enum');
     if ($enumConfigProvider->hasConfig($enumValueClassName)) {
         // existing enum
         if ($configModel->getId()) {
             if ($enumName !== null) {
                 $this->enumSynchronizer->applyEnumNameTrans($enumCode, $enumName, $locale);
             }
             $enumOptions = $this->getValue($data['enum'], 'enum_options');
             if ($enumOptions !== null) {
                 $this->enumSynchronizer->applyEnumOptions($enumValueClassName, $enumOptions, $locale);
             }
             $enumPublic = $this->getValue($data['enum'], 'enum_public');
             if ($enumPublic !== null) {
                 $this->enumSynchronizer->applyEnumEntityOptions($enumValueClassName, $enumPublic);
             }
         }
         unset($data['enum']['enum_name']);
         unset($data['enum']['enum_options']);
         unset($data['enum']['enum_public']);
         $event->setData($data);
     } else {
         // new enum
         $this->sortOptions($data['enum']['enum_options']);
         $data['enum']['enum_locale'] = $locale;
         $event->setData($data);
     }
 }
Ejemplo n.º 29
0
 /**
  * @param string $columnName column name
  * @param string $dataName   property path of the field, e.g. entity.enum_field
  * @param string $enumCode   enum code
  * @param bool   $isMultiple allow to filter by several values
  *
  * @return DatagridConfiguration
  */
 public function addEnumFilter($columnName, $dataName, $enumCode, $isMultiple = false)
 {
     $this->addFilter($columnName, ['type' => 'entity', 'data_name' => $dataName, 'options' => ['field_options' => ['class' => ExtendHelper::buildEnumValueClassName($enumCode), 'property' => 'name', 'query_builder' => function (EntityRepository $entityRepository) {
         return $entityRepository->createQueryBuilder('c')->orderBy('c.name', 'ASC');
     }, 'multiple' => $isMultiple]]]);
     return $this;
 }
Ejemplo n.º 30
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function setDefaultOptionsProvider()
 {
     return [['enumCode' => 'test_enum', 'class' => null, 'nullValue' => null, 'values' => ['val1' => 'Value1'], 'fieldOptions' => null, 'expectedOptions' => ['enum_code' => 'test_enum', 'class' => ExtendHelper::buildEnumValueClassName('test_enum'), 'null_value' => null, 'field_options' => ['multiple' => true, 'choices' => ['val1' => 'Value1']]]], ['enumCode' => 'test_enum', 'class' => null, 'nullValue' => ':empty:', 'values' => ['val1' => 'Value1'], 'fieldOptions' => null, 'expectedOptions' => ['enum_code' => 'test_enum', 'class' => ExtendHelper::buildEnumValueClassName('test_enum'), 'null_value' => ':empty:', 'field_options' => ['multiple' => true, 'choices' => [':empty:' => 'None', 'val1' => 'Value1']]]], ['enumCode' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'nullValue' => null, 'values' => ['val1' => 'Value1'], 'fieldOptions' => null, 'expectedOptions' => ['enum_code' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'null_value' => null, 'field_options' => ['multiple' => true, 'choices' => ['val1' => 'Value1']]]], ['enumCode' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'nullValue' => ':empty:', 'values' => ['val1' => 'Value1'], 'fieldOptions' => null, 'expectedOptions' => ['enum_code' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'null_value' => ':empty:', 'field_options' => ['multiple' => true, 'choices' => [':empty:' => 'None', 'val1' => 'Value1']]]], ['enumCode' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'nullValue' => null, 'values' => ['val1' => 'Value1'], 'fieldOptions' => ['multiple' => false], 'expectedOptions' => ['enum_code' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'null_value' => null, 'field_options' => ['multiple' => false, 'choices' => ['val1' => 'Value1']]]], 'numeric choice keys' => ['enumCode' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'nullValue' => ':empty:', 'values' => [1 => 'Value1'], 'fieldOptions' => ['multiple' => false], 'expectedOptions' => ['enum_code' => null, 'class' => 'Extend\\Entity\\EV_Test_Enum', 'null_value' => ':empty:', 'field_options' => ['multiple' => false, 'choices' => [':empty:' => 'None', 1 => 'Value1']]]]];
 }