From 99427aac0d893933e8a326aee59a02c11701377b Mon Sep 17 00:00:00 2001 From: julian ladjani Date: Fri, 1 Feb 2019 18:18:44 +0100 Subject: [PATCH 1/6] Add multiple select type min and max configuration constrain --- src/Enumerations/CustomerOptionTypeEnum.php | 4 ++ src/Form/Product/ShopCustomerOptionType.php | 77 +++++++++++---------- src/Services/ConstraintCreator.php | 13 +++- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/Enumerations/CustomerOptionTypeEnum.php b/src/Enumerations/CustomerOptionTypeEnum.php index dec5a243..d0c7bbb2 100644 --- a/src/Enumerations/CustomerOptionTypeEnum.php +++ b/src/Enumerations/CustomerOptionTypeEnum.php @@ -118,6 +118,10 @@ public static function getFormTypeArray(): array public static function getConfigurationArray(): array { return [ + self::MULTI_SELECT => [ + 'brille24.form.config.min.choice_number' => ['type' => 'number', 'value' => 0], + 'brille24.form.config.max.choice_number' => ['type' => 'number', 'value' => 1000], + ], self::TEXT => [ 'brille24.form.config.min.length' => ['type' => 'number', 'value' => 0], 'brille24.form.config.max.length' => ['type' => 'number', 'value' => 255], diff --git a/src/Form/Product/ShopCustomerOptionType.php b/src/Form/Product/ShopCustomerOptionType.php index fc943696..db2bc6d3 100644 --- a/src/Form/Product/ShopCustomerOptionType.php +++ b/src/Form/Product/ShopCustomerOptionType.php @@ -49,11 +49,12 @@ public function __construct( CurrencyContextInterface $currencyContext, MoneyFormatterInterface $moneyFormatter, LocaleContextInterface $localeContext - ) { - $this->channelContext = $channelContext; + ) + { + $this->channelContext = $channelContext; $this->currencyContext = $currencyContext; - $this->moneyFormatter = $moneyFormatter; - $this->localeContext = $localeContext; + $this->moneyFormatter = $moneyFormatter; + $this->localeContext = $localeContext; } public function buildForm(FormBuilderInterface $builder, array $options): void @@ -69,11 +70,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void foreach ($customerOptions as $customerOption) { $customerOptionType = $customerOption->getType(); - $fieldName = $customerOption->getCode(); + $fieldName = $customerOption->getCode(); [$class, $formOptions] = CustomerOptionTypeEnum::getFormTypeArray()[$customerOptionType]; - $fieldConfig = $this->getFormConfiguration($formOptions, $customerOption, $product); + $fieldConfig = $this->getFormConfiguration($formOptions, $customerOption, $product); $fieldConfig['mapped'] = $options['mapped']; $builder->add($fieldName, $class, $fieldConfig); @@ -81,15 +82,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->addEventListener( FormEvents::POST_SET_DATA, function (FormEvent $event) { - $data = $event->getData(); - $form = $event->getForm(); - if (!is_array($data)) { - return; - } - foreach ($data as $key => $value) { - $form->get($key)->setData($value); - } + $data = $event->getData(); + $form = $event->getForm(); + if (!is_array($data)) { + return; + } + foreach ($data as $key => $value) { + $form->get($key)->setData($value); } + } ); } @@ -109,9 +110,9 @@ public function getBlockPrefix(): string /** * Gets the settings for the form type based on the type that the form field is for * - * @param array $formOptions + * @param array $formOptions * @param CustomerOptionInterface $customerOption - * @param ProductInterface $product + * @param ProductInterface $product * * @return array */ @@ -119,9 +120,10 @@ private function getFormConfiguration( array $formOptions, CustomerOptionInterface $customerOption, ProductInterface $product - ): array { + ): array + { $defaultOptions = [ - 'label' => $customerOption->getName(), + 'label' => $customerOption->getName(), 'required' => $customerOption->isRequired(), ]; @@ -131,31 +133,30 @@ private function getFormConfiguration( $customerOptionType = $customerOption->getType(); if (CustomerOptionTypeEnum::isSelect($customerOptionType)) { $configuration = [ - 'choices' => $customerOption->getValues()->toArray(), + 'choices' => $customerOption->getValues()->toArray(), 'choice_label' => function (CustomerOptionValueInterface $value) use ($product) { return $this->buildValueString($value, $product); }, 'choice_value' => 'code', ]; - } else { - $constraint = ConstraintCreator::createFromConfiguration( - $customerOptionType, - $customerOption->getConfiguration() - ); + } + $constraint = ConstraintCreator::createFromConfiguration( + $customerOptionType, + $customerOption->getConfiguration() + ); - if ($constraint !== null) { - $constraint->groups = ['sylius']; - $configuration = ['constraints' => [$constraint]]; - } + if ($constraint !== null) { + $constraint->groups = ['sylius']; + $configuration = ['constraints' => [$constraint]]; + } - if ($customerOption->isRequired()) { - /** @var NotBlank $requiredConstraint */ - $requiredConstraint = ConstraintCreator::createRequiredConstraint(); - $requiredConstraint->message = 'brille24.form.customer_options.required'; + if ($customerOption->isRequired()) { + /** @var NotBlank $requiredConstraint */ + $requiredConstraint = ConstraintCreator::createRequiredConstraint(); + $requiredConstraint->message = 'brille24.form.customer_options.required'; - $requiredConstraint->groups = ['sylius']; - $configuration['constraints'][] = $requiredConstraint; - } + $requiredConstraint->groups = ['sylius']; + $configuration['constraints'][] = $requiredConstraint; } return array_merge($formOptions, $defaultOptions, $configuration); @@ -163,7 +164,7 @@ private function getFormConfiguration( /** * @param CustomerOptionValueInterface $value - * @param ProductInterface $product + * @param ProductInterface $product * * @return string * @@ -188,7 +189,7 @@ private function buildValueString(CustomerOptionValueInterface $value, ProductIn /** @var ChannelInterface $channel */ $channel = $this->channelContext->getChannel(); - $price = $price ?? $value->getPriceForChannel($channel); + $price = $price ?? $value->getPriceForChannel($channel); // No price was found for the current channel, probably because the values weren't updated after adding a new channel if ($price === null) { @@ -204,7 +205,7 @@ private function buildValueString(CustomerOptionValueInterface $value, ProductIn $valueString = $price->getValueString( $this->currencyContext->getCurrencyCode(), $this->localeContext->getLocaleCode(), $this->moneyFormatter ); - $name = $value->getName(); + $name = $value->getName(); return "{$name} ($valueString)"; } diff --git a/src/Services/ConstraintCreator.php b/src/Services/ConstraintCreator.php index 14ca2b99..dd4f0d12 100644 --- a/src/Services/ConstraintCreator.php +++ b/src/Services/ConstraintCreator.php @@ -17,7 +17,7 @@ class ConstraintCreator /** * Gets the value from the Customer Option value configuration * - * @param array $configuration + * @param array $configuration * @param string $key * * @return mixed @@ -35,7 +35,7 @@ public static function getValueFromConfiguration(array $configuration, string $k * Creates a constraint form the configuration based on the type of Custom Option * * @param string $type - * @param array $configuration + * @param array $configuration * * @return Constraint|null */ @@ -53,6 +53,13 @@ public static function createFromConfiguration(string $type, array $configuratio ]; return new Length($lengthRange); + case CustomerOptionTypeEnum::MULTI_SELECT: + $choiceNumberRange = [ + 'min' => $getFromConfiguration('brille24.form.config.min.choice_number'), + 'max' => $getFromConfiguration('brille24.form.config.max.choice_number'), + 'multiple' => true, + ]; + return new Choice($choiceNumberRange); case CustomerOptionTypeEnum::FILE: return new File(['maxSize' => $getFromConfiguration('brille24.form.config.max.file_size')]); case CustomerOptionTypeEnum::DATE: @@ -78,7 +85,7 @@ public static function createFromConfiguration(string $type, array $configuratio public static function createConditionalConstraint(array $conditions, array $constraints): Constraint { return new ConditionalConstraint([ - 'conditions' => $conditions, + 'conditions' => $conditions, 'constraints' => $constraints, ]); } From 213c1d8ef6a1de30110266c25e04a7fa739cd6b9 Mon Sep 17 00:00:00 2001 From: julian ladjani Date: Fri, 1 Feb 2019 18:51:50 +0100 Subject: [PATCH 2/6] Add multiple select configuration in ressource and in entity, add translation, fix select type configuration error --- src/Entity/CustomerOptions/CustomerOption.php | 14 +++++--------- src/Enumerations/CustomerOptionTypeEnum.php | 2 ++ src/Resources/translations/messages.en.yml | 2 ++ src/Resources/views/CustomerOption/_form.html.twig | 3 +-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Entity/CustomerOptions/CustomerOption.php b/src/Entity/CustomerOptions/CustomerOption.php index 7b278c98..06164936 100644 --- a/src/Entity/CustomerOptions/CustomerOption.php +++ b/src/Entity/CustomerOptions/CustomerOption.php @@ -54,7 +54,7 @@ public function __construct() { $this->initializeTranslationsCollection(); - $this->values = new ArrayCollection(); + $this->values = new ArrayCollection(); $this->groupAssociations = new ArrayCollection(); } @@ -76,12 +76,8 @@ public function setType(?string $type): void } $this->type = $type; - - if (CustomerOptionTypeEnum::isSelect($type)) { - $this->configuration = []; - } else { - $this->configuration = CustomerOptionTypeEnum::getConfigurationArray()[$type]; - } + + $this->configuration = CustomerOptionTypeEnum::getConfigurationArray()[$type]; } /** @@ -94,7 +90,7 @@ public function getType(): string public function getTypeCode(): ?string { - $type = $this->getType(); + $type = $this->getType(); $translations = CustomerOptionTypeEnum::getTranslateArray(); if (array_key_exists($type, $translations)) { return $translations[$type]; @@ -191,7 +187,7 @@ public function setConfiguration(array $configuration): void { // Setting the new values foreach ($configuration as $key => $value) { - $optionKey = str_replace('_', '.', $key); + $optionKey = str_replace('_', '.', $key); $this->configuration[$optionKey]['value'] = $value; } diff --git a/src/Enumerations/CustomerOptionTypeEnum.php b/src/Enumerations/CustomerOptionTypeEnum.php index d0c7bbb2..ad6437f8 100644 --- a/src/Enumerations/CustomerOptionTypeEnum.php +++ b/src/Enumerations/CustomerOptionTypeEnum.php @@ -122,6 +122,8 @@ public static function getConfigurationArray(): array 'brille24.form.config.min.choice_number' => ['type' => 'number', 'value' => 0], 'brille24.form.config.max.choice_number' => ['type' => 'number', 'value' => 1000], ], + self::SELECT => [ + ], self::TEXT => [ 'brille24.form.config.min.length' => ['type' => 'number', 'value' => 0], 'brille24.form.config.max.length' => ['type' => 'number', 'value' => 255], diff --git a/src/Resources/translations/messages.en.yml b/src/Resources/translations/messages.en.yml index 3f4c7a02..99504ae2 100644 --- a/src/Resources/translations/messages.en.yml +++ b/src/Resources/translations/messages.en.yml @@ -50,10 +50,12 @@ brille24: config: min: length: Minimum length + choice_number: Minimum number of selected items file_size: Minimum size date: Minimum date number: Minimum number max: + choice_number: Maximum number of selected items length: Maximum length file_size: Maximum size date: Maximum date diff --git a/src/Resources/views/CustomerOption/_form.html.twig b/src/Resources/views/CustomerOption/_form.html.twig index 476f3efc..c8d3ce2d 100644 --- a/src/Resources/views/CustomerOption/_form.html.twig +++ b/src/Resources/views/CustomerOption/_form.html.twig @@ -16,7 +16,6 @@ {% set customerOptionType = form.type.vars.value %} {% if customerOptionType == 'select' or customerOptionType == 'multi_select' %} {% include 'Brille24SyliusCustomerOptionsPlugin:CustomerOption:_values.html.twig' with {'form': form.values} only %} - {% else %} - {% include 'Brille24SyliusCustomerOptionsPlugin:CustomerOption:_configuration.html.twig' with {'form': form.configuration} only %} {% endif %} + {% include 'Brille24SyliusCustomerOptionsPlugin:CustomerOption:_configuration.html.twig' with {'form': form.configuration} only %} From 640dc241bbd3ee67b0313ee940da18f9080d3536 Mon Sep 17 00:00:00 2001 From: julian ladjani Date: Fri, 1 Feb 2019 19:36:33 +0100 Subject: [PATCH 3/6] Multiple Select fix configuration override by contrains --- src/Form/Product/ShopCustomerOptionType.php | 2 +- src/Services/ConstraintCreator.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Form/Product/ShopCustomerOptionType.php b/src/Form/Product/ShopCustomerOptionType.php index db2bc6d3..26485fb9 100644 --- a/src/Form/Product/ShopCustomerOptionType.php +++ b/src/Form/Product/ShopCustomerOptionType.php @@ -147,7 +147,7 @@ private function getFormConfiguration( if ($constraint !== null) { $constraint->groups = ['sylius']; - $configuration = ['constraints' => [$constraint]]; + $configuration['constraints'] = [$constraint]; } if ($customerOption->isRequired()) { diff --git a/src/Services/ConstraintCreator.php b/src/Services/ConstraintCreator.php index dd4f0d12..052d9f1a 100644 --- a/src/Services/ConstraintCreator.php +++ b/src/Services/ConstraintCreator.php @@ -11,6 +11,7 @@ use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Range; +use Symfony\Component\Validator\Constraints\Choice; class ConstraintCreator { From feee41c2ff1f2fd01b7b6b37a4e9086396096c93 Mon Sep 17 00:00:00 2001 From: julian ladjani Date: Sat, 2 Feb 2019 16:36:30 +0100 Subject: [PATCH 4/6] Multiple Select fix Choice constrain error --- src/Form/Product/ShopCustomerOptionType.php | 2 +- src/Services/ConstraintCreator.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Form/Product/ShopCustomerOptionType.php b/src/Form/Product/ShopCustomerOptionType.php index 26485fb9..a238b2c6 100644 --- a/src/Form/Product/ShopCustomerOptionType.php +++ b/src/Form/Product/ShopCustomerOptionType.php @@ -142,7 +142,7 @@ private function getFormConfiguration( } $constraint = ConstraintCreator::createFromConfiguration( $customerOptionType, - $customerOption->getConfiguration() + array_merge($customerOption->getConfiguration(), $configuration) ); if ($constraint !== null) { diff --git a/src/Services/ConstraintCreator.php b/src/Services/ConstraintCreator.php index 052d9f1a..1b56cda7 100644 --- a/src/Services/ConstraintCreator.php +++ b/src/Services/ConstraintCreator.php @@ -58,6 +58,7 @@ public static function createFromConfiguration(string $type, array $configuratio $choiceNumberRange = [ 'min' => $getFromConfiguration('brille24.form.config.min.choice_number'), 'max' => $getFromConfiguration('brille24.form.config.max.choice_number'), + 'choices' => $configuration['choices'], 'multiple' => true, ]; return new Choice($choiceNumberRange); From d11978aa4b84d8be0fa5972dfb5de1a666429598 Mon Sep 17 00:00:00 2001 From: julian ladjani Date: Thu, 7 Feb 2019 15:40:13 +0100 Subject: [PATCH 5/6] Fix multi select configuration form fields name --- src/Enumerations/CustomerOptionTypeEnum.php | 4 ++-- src/Resources/translations/messages.en.yml | 4 ++-- src/Services/ConstraintCreator.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Enumerations/CustomerOptionTypeEnum.php b/src/Enumerations/CustomerOptionTypeEnum.php index ad6437f8..32607daf 100644 --- a/src/Enumerations/CustomerOptionTypeEnum.php +++ b/src/Enumerations/CustomerOptionTypeEnum.php @@ -119,8 +119,8 @@ public static function getConfigurationArray(): array { return [ self::MULTI_SELECT => [ - 'brille24.form.config.min.choice_number' => ['type' => 'number', 'value' => 0], - 'brille24.form.config.max.choice_number' => ['type' => 'number', 'value' => 1000], + 'brille24.form.config.min.choice' => ['type' => 'number', 'value' => 0], + 'brille24.form.config.max.choice' => ['type' => 'number', 'value' => 1000], ], self::SELECT => [ ], diff --git a/src/Resources/translations/messages.en.yml b/src/Resources/translations/messages.en.yml index 99504ae2..86f502bc 100644 --- a/src/Resources/translations/messages.en.yml +++ b/src/Resources/translations/messages.en.yml @@ -50,12 +50,12 @@ brille24: config: min: length: Minimum length - choice_number: Minimum number of selected items + choice: Minimum number of selected items file_size: Minimum size date: Minimum date number: Minimum number max: - choice_number: Maximum number of selected items + choice: Maximum number of selected items length: Maximum length file_size: Maximum size date: Maximum date diff --git a/src/Services/ConstraintCreator.php b/src/Services/ConstraintCreator.php index 1b56cda7..856cd4b5 100644 --- a/src/Services/ConstraintCreator.php +++ b/src/Services/ConstraintCreator.php @@ -56,8 +56,8 @@ public static function createFromConfiguration(string $type, array $configuratio return new Length($lengthRange); case CustomerOptionTypeEnum::MULTI_SELECT: $choiceNumberRange = [ - 'min' => $getFromConfiguration('brille24.form.config.min.choice_number'), - 'max' => $getFromConfiguration('brille24.form.config.max.choice_number'), + 'min' => $getFromConfiguration('brille24.form.config.min.choice'), + 'max' => $getFromConfiguration('brille24.form.config.max.choice'), 'choices' => $configuration['choices'], 'multiple' => true, ]; From 4c4ffbb42c9d26550e482c2050c372fe0dc8ce4b Mon Sep 17 00:00:00 2001 From: julian ladjani Date: Sun, 19 May 2019 00:11:31 +0200 Subject: [PATCH 6/6] Fix merge error (function part duplication) --- src/Form/Product/ShopCustomerOptionType.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Form/Product/ShopCustomerOptionType.php b/src/Form/Product/ShopCustomerOptionType.php index 6c98103c..f9050811 100644 --- a/src/Form/Product/ShopCustomerOptionType.php +++ b/src/Form/Product/ShopCustomerOptionType.php @@ -92,10 +92,6 @@ function (FormEvent $event) { $form->get($key)->setData($value); } } - foreach ($data as $key => $value) { - $form->get($key)->setData($value); - } - } ); }