diff --git a/core/lib/Thelia/Condition/Implementation/AbstractMatchCountries.php b/core/lib/Thelia/Condition/Implementation/AbstractMatchCountries.php new file mode 100644 index 000000000..0e35b16d1 --- /dev/null +++ b/core/lib/Thelia/Condition/Implementation/AbstractMatchCountries.php @@ -0,0 +1,150 @@ + + * + */ +abstract class AbstractMatchCountries extends ConditionAbstract +{ + /** Condition 1st parameter : quantity */ + CONST COUNTRIES_LIST = 'countries'; + + /** + * @inheritdoc + */ + public function __construct(FacadeInterface $facade) + { + $this->availableOperators = [ + self::COUNTRIES_LIST => [ + Operators::IN, + Operators::OUT + ] + ]; + + parent::__construct($facade); + } + + protected abstract function getSummaryLabel($cntryStrList, $i18nOperator); + + protected abstract function getFormLabel(); + + /** + * @inheritdoc + */ + public function setValidatorsFromForm(array $operators, array $values) + { + $this->checkComparisonOperatorValue($operators, self::COUNTRIES_LIST); + + // Use default values if data is not defined. + if (! isset($operators[self::COUNTRIES_LIST]) || ! isset($values[self::COUNTRIES_LIST])) { + $operators[self::COUNTRIES_LIST] = Operators::IN; + $values[self::COUNTRIES_LIST] = []; + } + + // Be sure that the value is an array, make one if required + if (! is_array($values[self::COUNTRIES_LIST])) { + $values[self::COUNTRIES_LIST] = array($values[self::COUNTRIES_LIST]); + } + + // Check that at least one category is selected + if (empty($values[self::COUNTRIES_LIST])) { + throw new InvalidConditionValueException( + get_class(), self::COUNTRIES_LIST + ); + } + + $this->operators = [ self::COUNTRIES_LIST => $operators[self::COUNTRIES_LIST] ]; + $this->values = [ self::COUNTRIES_LIST => $values[self::COUNTRIES_LIST] ]; + + return $this; + } + + /** + * @inheritdoc + */ + public function isMatching() + { + // The delivery address should match one of the selected countries. + + /* TODO !!!! */ + return $this->conditionValidator->variableOpComparison( + $this->facade->getNbArticlesInCart(), + $this->operators[self::COUNTRIES_LIST], + $this->values[self::COUNTRIES_LIST] + ); + } + + /** + * @inheritdoc + */ + public function getSummary() + { + $i18nOperator = Operators::getI18n( + $this->translator, $this->operators[self::COUNTRIES_LIST] + ); + + $cntryStrList = ''; + + $cntryIds = $this->values[self::COUNTRIES_LIST]; + + if (null !== $cntryList = CountryQuery::create()->findPks($cntryIds)) { + + /** @var Category $cntry */ + foreach($cntryList as $cntry) { + $cntryStrList .= $cntry->getTitle() . ', '; + } + + $cntryStrList = rtrim($cntryStrList, ', '); + } + + return $this->getSummaryLabel($cntryStrList, $i18nOperator); + } + + /** + * @inheritdoc + */ + protected function generateInputs() + { + return array( + self::COUNTRIES_LIST => array( + 'availableOperators' => $this->availableOperators[self::COUNTRIES_LIST], + 'value' => '', + 'selectedOperator' => Operators::IN + ) + ); + } + + /** + * @inheritdoc + */ + public function drawBackOfficeInputs() + { + return $this->facade->getParser()->render('coupon/condition-fragments/countries-condition.html', [ + 'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::COUNTRIES_LIST), + 'countries_field_name' => self::COUNTRIES_LIST, + 'values' => isset($this->values[self::COUNTRIES_LIST]) ? $this->values[self::COUNTRIES_LIST] : array(), + 'countryLabel' => $this->getFormLabel() + ] + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Implementation/MatchBillingCountries.php b/core/lib/Thelia/Condition/Implementation/MatchBillingCountries.php new file mode 100644 index 000000000..c4a525d5a --- /dev/null +++ b/core/lib/Thelia/Condition/Implementation/MatchBillingCountries.php @@ -0,0 +1,87 @@ + + * + */ +class MatchBillingCountries extends AbstractMatchCountries +{ + /** + * @inheritdoc + */ + public function getServiceId() + { + return 'thelia.condition.match_billing_countries'; + } + + /** + * @inheritdoc + */ + public function isMatching() + { + $billingAddress = $this->facade->getCustomer()->getDefaultAddress(); + + return $this->conditionValidator->variableOpComparison( + $billingAddress->getCountryId(), + $this->operators[self::COUNTRIES_LIST], + $this->values[self::COUNTRIES_LIST] + ); + } + + /** + * @inheritdoc + */ + public function getName() + { + return $this->translator->trans( + 'Billing country condition', + [], + 'condition' + ); + } + + /** + * @inheritdoc + */ + public function getToolTip() + { + $toolTip = $this->translator->trans( + 'The coupon applies to the selected delivery countries', + [], + 'condition' + ); + + return $toolTip; + } + + protected function getSummaryLabel($cntryStrList, $i18nOperator) { + + return $this->translator->trans( + 'Only if order billing country is %op% %countries_list%', [ + '%countries_list%' => $cntryStrList, + '%op%' => $i18nOperator + ], 'condition' + ); + } + + protected function getFormLabel() { + return $this->translator->trans( + 'Billing coutry is', [], 'condition' + ); + } +} diff --git a/core/lib/Thelia/Condition/Implementation/MatchDeliveryCountries.php b/core/lib/Thelia/Condition/Implementation/MatchDeliveryCountries.php new file mode 100644 index 000000000..3ca0195e4 --- /dev/null +++ b/core/lib/Thelia/Condition/Implementation/MatchDeliveryCountries.php @@ -0,0 +1,87 @@ + + * + */ +class MatchDeliveryCountries extends AbstractMatchCountries +{ + /** + * @inheritdoc + */ + public function getServiceId() + { + return 'thelia.condition.match_delivery_countries'; + } + + /** + * @inheritdoc + */ + public function isMatching() + { + $deliveryAddress = $this->facade->getDeliveryAddress(); + + return $this->conditionValidator->variableOpComparison( + $deliveryAddress->getCountryId(), + $this->operators[self::COUNTRIES_LIST], + $this->values[self::COUNTRIES_LIST] + ); + } + + /** + * @inheritdoc + */ + public function getName() + { + return $this->translator->trans( + 'Delivery country condition', + [], + 'condition' + ); + } + + /** + * @inheritdoc + */ + public function getToolTip() + { + $toolTip = $this->translator->trans( + 'The coupon applies to the selected delivery countries', + [], + 'condition' + ); + + return $toolTip; + } + + protected function getSummaryLabel($cntryStrList, $i18nOperator) { + + return $this->translator->trans( + 'Only if order shipping country is %op% %countries_list%', [ + '%countries_list%' => $cntryStrList, + '%op%' => $i18nOperator + ], 'condition' + ); + } + + protected function getFormLabel() { + return $this->translator->trans( + 'Delivery coutry is', [], 'condition' + ); + } +} diff --git a/templates/backOffice/default/coupon/condition-fragments/countries-condition.html b/templates/backOffice/default/coupon/condition-fragments/countries-condition.html index 1a03a5042..0e3fed699 100644 --- a/templates/backOffice/default/coupon/condition-fragments/countries-condition.html +++ b/templates/backOffice/default/coupon/condition-fragments/countries-condition.html @@ -1,5 +1,5 @@