diff --git a/core/lib/Thelia/Condition/ConditionCollection.php b/core/lib/Thelia/Condition/ConditionCollection.php index 7b70df07c..d820edfe1 100644 --- a/core/lib/Thelia/Condition/ConditionCollection.php +++ b/core/lib/Thelia/Condition/ConditionCollection.php @@ -27,7 +27,7 @@ use Thelia\Condition\Implementation\ConditionInterface; class ConditionCollection implements Iterator, Countable, ArrayAccess { /** @var array Array of ConditionInterface */ - protected $conditions = array(); + protected $conditions = []; /** * (PHP 5 >= 5.0.0) @@ -180,7 +180,7 @@ class ConditionCollection implements Iterator, Countable, ArrayAccess */ public function __toString() { - $arrayToSerialize = array(); + $arrayToSerialize = []; /** @var ConditionInterface $condition */ foreach ($this as $condition) { $arrayToSerialize[] = $condition->getSerializableCondition(); diff --git a/core/lib/Thelia/Condition/ConditionFactory.php b/core/lib/Thelia/Condition/ConditionFactory.php index 60e2643b9..8fd9647b6 100644 --- a/core/lib/Thelia/Condition/ConditionFactory.php +++ b/core/lib/Thelia/Condition/ConditionFactory.php @@ -61,7 +61,7 @@ class ConditionFactory ); $collection[] = $conditionNone; } - $serializableConditions = array(); + $serializableConditions = []; /** @var $condition ConditionInterface */ foreach ($collection as $condition) { $serializableConditions[] = $condition->getSerializableCondition(); diff --git a/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php b/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php index 5a1f5c71d..5ddf4f6e4 100644 --- a/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php +++ b/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php @@ -18,6 +18,7 @@ use Thelia\Condition\SerializableCondition; use Thelia\Core\Translation\Translator; use Thelia\Coupon\FacadeInterface; use Thelia\Exception\InvalidConditionValueException; +use Thelia\Model\Base\CurrencyQuery; use Thelia\Model\Currency; use Thelia\Type\FloatType; @@ -35,10 +36,10 @@ abstract class ConditionAbstract implements ConditionInterface protected $serviceId = null; /** @var array Available Operators (Operators::CONST) */ - protected $availableOperators = array(); + protected $availableOperators = []; /** @var array Parameters validating parameters against */ - protected $validators = array(); + protected $validators = []; /** @var FacadeInterface Provide necessary value from Thelia */ protected $facade = null; @@ -47,10 +48,10 @@ abstract class ConditionAbstract implements ConditionInterface protected $translator = null; /** @var array Operators set by Admin in BackOffice */ - protected $operators = array(); + protected $operators = []; /** @var array Values set by Admin in BackOffice */ - protected $values = array(); + protected $values = []; /** @var ConditionEvaluator Conditions validator */ protected $conditionValidator = null; @@ -64,6 +65,7 @@ abstract class ConditionAbstract implements ConditionInterface { $this->facade = $facade; $this->translator = $facade->getTranslator(); + $this->parser = $facade->getParser(); $this->conditionValidator = $facade->getConditionEvaluator(); } @@ -86,9 +88,9 @@ abstract class ConditionAbstract implements ConditionInterface { $this->validators = $this->generateInputs(); - $translatedInputs = array(); + $translatedInputs = []; foreach ($this->validators as $key => $validator) { - $translatedOperators = array(); + $translatedOperators = []; foreach ($validator['availableOperators'] as $availableOperators) { $translatedOperators[$availableOperators] = Operators::getI18n( $this->translator, @@ -99,7 +101,7 @@ abstract class ConditionAbstract implements ConditionInterface $validator['availableOperators'] = $translatedOperators; $translatedInputs[$key] = $validator; } - $validators = array(); + $validators = []; $validators['inputs'] = $translatedInputs; $validators['setOperators'] = $this->operators; $validators['setValues'] = $this->values; @@ -216,27 +218,21 @@ abstract class ConditionAbstract implements ConditionInterface */ protected function drawBackOfficeInputOperators($inputKey) { - $selectHtml = ''; - $optionHtml = ''; - $inputs = $this->getValidators(); - if (isset($inputs['inputs'][$inputKey])) { - $operators = $inputs['inputs'][$inputKey]['availableOperators']; - foreach ($operators as $key => $operator) { - $selected = ''; - if (isset($this->operators) && isset($this->operators[$inputKey]) && $this->operators[$inputKey] == $key) { - $selected = ' selected="selected"'; - } - $optionHtml .= ''; - } + $html = ''; - $selectHtml .= ' - - '; + $inputs = $this->getValidators(); + + if (isset($inputs['inputs'][$inputKey])) { + + $html = $this->facade->getParser()->render('coupon/condition-fragments/condition-selector.html', [ + 'operators' => $inputs['inputs'][$inputKey]['availableOperators'], + 'value' => isset($this->operators[$inputKey]) ? $this->operators[$inputKey] : '', + 'inputKey' => $inputKey + ] + ); } - return $selectHtml; + return $html; } /** @@ -251,26 +247,19 @@ abstract class ConditionAbstract implements ConditionInterface protected function drawBackOfficeBaseInputsText($label, $inputKey) { $operatorSelectHtml = $this->drawBackOfficeInputOperators($inputKey); + $currentValue = ''; if (isset($this->values) && isset($this->values[$inputKey])) { $currentValue = $this->values[$inputKey]; } - $html = ' -
- -
-
- ' . $operatorSelectHtml . ' -
-
- -
-
-
- '; - - return $html; + return $this->facade->getParser()->render('coupon/conditions-fragments/base-input-text.html', [ + 'label' => $label, + 'inputKey' => $inputKey, + 'currentValue' => $currentValue, + 'operatorSelectHtml' => $operatorSelectHtml + ] + ); } /** @@ -285,23 +274,39 @@ abstract class ConditionAbstract implements ConditionInterface */ protected function drawBackOfficeInputQuantityValues($inputKey, $max = 10, $min = 0) { - $selectHtml = ''; - $optionHtml = ''; - for ($i = $min; $i <= $max; $i++) { - $selected = ''; - if (isset($this->values) && isset($this->values[$inputKey]) && $this->values[$inputKey] == $i) { - $selected = ' selected="selected"'; - } - $optionHtml .= ''; - } - - $selectHtml .= ' - - '; - - return $selectHtml; + return $this->facade->getParser()->render('coupon/condition-fragments/quantity-selector.html', [ + 'min' => $min, + 'max' => $max, + 'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '', + 'inputKey' => $inputKey + ] + ); } -} + /** + * Draw the currency input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @param string $inputKey Input key (ex: self::INPUT1) + * + * @return string HTML string + */ + protected function drawBackOfficeCurrencyInput($inputKey) + { + $currencies = CurrencyQuery::create()->find(); + + $cleanedCurrencies = []; + + /** @var Currency $currency */ + foreach ($currencies as $currency) { + $cleanedCurrencies[$currency->getCode()] = $currency->getSymbol(); + } + + return $this->facade->getParser()->render('coupon/condition-fragments/currency-selector.html', [ + 'currencies' => $cleanedCurrencies, + 'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '', + 'inputKey' => $inputKey + ] + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php b/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php index a5a3e4d1d..5049470a2 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php @@ -25,7 +25,7 @@ class MatchForEveryone extends ConditionAbstract protected $serviceId = 'thelia.condition.match_for_everyone'; /** @var array Available Operators (Operators::CONST) */ - protected $availableOperators = array(); + protected $availableOperators = []; /** * Check validators relevancy and store them @@ -51,8 +51,8 @@ class MatchForEveryone extends ConditionAbstract */ protected function setValidators() { - $this->operators = array(); - $this->values = array(); + $this->operators = []; + $this->values = []; return $this; } @@ -75,8 +75,8 @@ class MatchForEveryone extends ConditionAbstract public function getName() { return $this->translator->trans( - 'Everybody can use it (no condition)', - array(), + 'Unconditional usage', + [], 'condition' ); } @@ -90,8 +90,8 @@ class MatchForEveryone extends ConditionAbstract public function getToolTip() { $toolTip = $this->translator->trans( - 'Will return always true', - array(), + 'This condition is always true', + [], 'condition' ); @@ -107,8 +107,8 @@ class MatchForEveryone extends ConditionAbstract public function getSummary() { $toolTip = $this->translator->trans( - 'Will return always true', - array(), + 'Unconditionnal usage', + [], 'condition' ); @@ -122,7 +122,7 @@ class MatchForEveryone extends ConditionAbstract */ protected function generateInputs() { - return array(); + return []; } /** diff --git a/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php b/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php index 5c26ee75b..6b4b48f15 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php @@ -153,7 +153,7 @@ class MatchForTotalAmount extends ConditionAbstract { return $this->translator->trans( 'By cart total amount', - array(), + [], 'condition' ); } @@ -168,7 +168,7 @@ class MatchForTotalAmount extends ConditionAbstract { $toolTip = $this->translator->trans( 'Check the total Cart amount in the given currency', - array(), + [], 'condition' ); @@ -208,7 +208,7 @@ class MatchForTotalAmount extends ConditionAbstract protected function generateInputs() { $currencies = CurrencyQuery::create()->find(); - $cleanedCurrencies = array(); + $cleanedCurrencies = []; /** @var Currency $currency */ foreach ($currencies as $currency) { $cleanedCurrencies[$currency->getCode()] = $currency->getSymbol(); @@ -240,7 +240,7 @@ class MatchForTotalAmount extends ConditionAbstract { $labelPrice = $this->facade ->getTranslator() - ->trans('Price', array(), 'condition'); + ->trans('Cart total amount is', [], 'condition'); $html = $this->drawBackOfficeBaseInputsText($labelPrice, self::INPUT1); @@ -258,66 +258,17 @@ class MatchForTotalAmount extends ConditionAbstract */ protected function drawBackOfficeBaseInputsText($label, $inputKey) { - $operatorSelectHtml = $this->drawBackOfficeInputOperators(self::INPUT1); - $currencySelectHtml = $this->drawBackOfficeCurrencyInput(self::INPUT2); - $selectedAmount = ''; - if (isset($this->values) && isset($this->values[$inputKey])) { - $selectedAmount = $this->values[$inputKey]; - } + return $this->facade->getParser()->render('coupon/condition-fragments/cart-total-amount-condition.html', [ + 'label' => $label, + 'inputKey' => $inputKey, + 'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '', - $html = ' - -
-
- ' . $operatorSelectHtml . ' -
-
- -
-
- - ' . $currencySelectHtml . ' -
-
- '; + 'field_1_name' => self::INPUT1, + 'field_2_name' => self::INPUT2, - return $html; + 'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::INPUT1), + 'currencySelectHtml' => $this->drawBackOfficeCurrencyInput(self::INPUT2), + ] + ); } - - /** - * Draw the currency input displayed in the BackOffice - * allowing Admin to set its Coupon Conditions - * - * @param string $inputKey Input key (ex: self::INPUT1) - * - * @return string HTML string - */ - protected function drawBackOfficeCurrencyInput($inputKey) - { - $optionHtml = ''; - - $currencies = CurrencyQuery::create()->find(); - $cleanedCurrencies = array(); - /** @var Currency $currency */ - foreach ($currencies as $currency) { - $cleanedCurrencies[$currency->getCode()] = $currency->getSymbol(); - } - - foreach ($cleanedCurrencies as $key => $cleanedCurrency) { - $selected = ''; - if (isset($this->values) && isset($this->values[$inputKey]) && $this->values[$inputKey] == $key) { - $selected = ' selected="selected"'; - } - $optionHtml .= ''; - } - - $selectHtml = ' - - '; - - return $selectHtml; - } - -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php b/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php index dec3fff29..0e7c9d832 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php @@ -127,8 +127,8 @@ class MatchForXArticles extends ConditionAbstract public function getName() { return $this->translator->trans( - 'By number of articles in cart', - array(), + 'Cart item count condition', + [], 'condition' ); } @@ -142,8 +142,8 @@ class MatchForXArticles extends ConditionAbstract public function getToolTip() { $toolTip = $this->translator->trans( - 'Check the amount of product in the Cart', - array(), + 'The cart item count should match the condition', + [], 'condition' ); @@ -163,7 +163,7 @@ class MatchForXArticles extends ConditionAbstract ); $toolTip = $this->translator->trans( - 'If cart products quantity is %operator% %quantity%', + 'If cart item count is %operator% %quantity%', array( '%operator%' => $i18nOperator, '%quantity%' => $this->values[self::INPUT1] @@ -200,7 +200,7 @@ class MatchForXArticles extends ConditionAbstract { $labelQuantity = $this->facade ->getTranslator() - ->trans('Quantity', array(), 'condition'); + ->trans('Cart item count is', [], 'condition'); $html = $this->drawBackOfficeBaseInputsText($labelQuantity, self::INPUT1); @@ -218,24 +218,11 @@ class MatchForXArticles extends ConditionAbstract */ protected function drawBackOfficeBaseInputsText($label, $inputKey) { - $operatorSelectHtml = $this->drawBackOfficeInputOperators($inputKey); - $quantitySelectHtml = $this->drawBackOfficeInputQuantityValues($inputKey, 20, 1); - - $html = ' -
- -
-
- ' . $operatorSelectHtml . ' -
-
- ' . $quantitySelectHtml . ' -
-
-
- '; - - return $html; + return $this->facade->getParser()->render('coupon/condition-fragments/cart-item-count-condition.html', [ + 'label' => $label, + 'operatorSelectHtml' => $this->drawBackOfficeInputOperators($inputKey), + 'quantitySelectHtml' => $this->drawBackOfficeInputQuantityValues($inputKey, 20, 1) + ] + ); } - -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Operators.php b/core/lib/Thelia/Condition/Operators.php index 5d1801839..7d2ae1485 100644 --- a/core/lib/Thelia/Condition/Operators.php +++ b/core/lib/Thelia/Condition/Operators.php @@ -54,57 +54,57 @@ abstract class Operators switch ($operator) { case self::INFERIOR: $ret = $translator->trans( - 'inferior to', - array(), + 'Less than', + [], 'condition' ); break; case self::INFERIOR_OR_EQUAL: $ret = $translator->trans( - 'inferior or equal to', - array(), + 'Less than or equals', + [], 'condition' ); break; case self::EQUAL: $ret = $translator->trans( - 'equal to', - array(), + 'Equals to', + [], 'condition' ); break; case self::SUPERIOR_OR_EQUAL: $ret = $translator->trans( - 'superior or equal to', - array(), + 'Greater than or equals', + [], 'condition' ); break; case self::SUPERIOR: $ret = $translator->trans( - 'superior to', - array(), + 'Greater than', + [], 'condition' ); break; case self::DIFFERENT: $ret = $translator->trans( - 'different from', - array(), + 'Not equals', + [], 'condition' ); break; case self::IN: $ret = $translator->trans( - 'in', - array(), + 'In', + [], 'condition' ); break; case self::OUT: $ret = $translator->trans( - 'not in', - array(), + 'Not in', + [], 'condition' ); break; @@ -113,4 +113,4 @@ abstract class Operators return $ret; } -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Condition/SerializableCondition.php b/core/lib/Thelia/Condition/SerializableCondition.php index 0e921388f..8d8cf4a7d 100644 --- a/core/lib/Thelia/Condition/SerializableCondition.php +++ b/core/lib/Thelia/Condition/SerializableCondition.php @@ -25,9 +25,9 @@ class SerializableCondition public $conditionServiceId = null; /** @var array Operators set by Admin for this Condition */ - public $operators = array(); + public $operators = []; /** @var array Values set by Admin for this Condition */ - public $values = array(); + public $values = []; } diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 82c3a97d5..6ecd418e8 100644 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -292,30 +292,42 @@ class CouponController extends BaseAdminController */ public function getConditionEmptyInputAjaxAction($conditionId) { - $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); + if (null !== $response = $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW)) + return $response; $this->checkXmlHttpRequest(); - /** @var ConditionFactory $conditionFactory */ - $conditionFactory = $this->container->get('thelia.condition.factory'); - $inputs = $conditionFactory->getInputsFromServiceId($conditionId); - if (!$this->container->has($conditionId)) { - return false; + if (! empty($conditionId)) { + + /** @var ConditionFactory $conditionFactory */ + $conditionFactory = $this->container->get('thelia.condition.factory'); + $inputs = $conditionFactory->getInputsFromServiceId($conditionId); + + if (!$this->container->has($conditionId)) { + return false; + } + + if ($inputs === null) { + return $this->pageNotFound(); + } + + /** @var ConditionInterface $condition */ + $condition = $this->container->get($conditionId); + + $html = $condition->drawBackOfficeInputs(); + $serviceId = $condition->getServiceId(); } - - /** @var ConditionInterface $condition */ - $condition = $this->container->get($conditionId); - - if ($inputs === null) { - return $this->pageNotFound(); + else { + $html = ''; + $serviceId = ''; } return $this->render( 'coupon/condition-input-ajax', array( - 'inputsDrawn' => $condition->drawBackOfficeInputs(), - 'conditionServiceId' => $condition->getServiceId(), - 'conditionIndex' => -1, + 'inputsDrawn' => $html, + 'conditionServiceId' => $serviceId, + 'conditionIndex' => '', ) ); }