From e606a6f8cea0a892f98cb60b53c4049b89586e91 Mon Sep 17 00:00:00 2001 From: gmorel Date: Sun, 5 Jan 2014 00:00:15 +0100 Subject: [PATCH 1/4] Coupon : Condition module refactor Less crappy unmaintainable javascript More logic in extendable php --- core/lib/Thelia/Action/Coupon.php | 2 +- .../Thelia/Condition/ConditionCollection.php | 150 +++++++- .../Thelia/Condition/ConditionEvaluator.php | 2 +- .../lib/Thelia/Condition/ConditionFactory.php | 32 +- .../Implementation/ConditionAbstract.php | 97 +++++ .../Implementation/ConditionInterface.php | 17 + .../Implementation/MatchForEveryone.php | 31 ++ .../Implementation/MatchForTotalAmount.php | 114 +++++- .../Implementation/MatchForXArticles.php | 70 +++- .../Thelia/Config/Resources/routing/admin.xml | 26 +- .../Controller/Admin/CouponController.php | 353 ++++++++++++++---- core/lib/Thelia/Core/Template/Loop/Coupon.php | 8 +- core/lib/Thelia/Coupon/CouponFactory.php | 4 +- core/lib/Thelia/Coupon/Type/RemoveXAmount.php | 1 - .../Condition/ConditionCollectionTest.php | 17 +- .../Condition/ConditionEvaluatorTest.php | 8 +- .../Tests/Condition/ConditionFactoryTest.php | 14 +- .../MatchForTotalAmountTest.php | 10 +- .../Implementation/MatchForXArticlesTest.php | 2 - .../Thelia/Tests/Coupon/CouponFactoryTest.php | 16 +- .../Thelia/Tests/Coupon/CouponManagerTest.php | 16 +- .../Tests/Coupon/Type/RemoveXAmountTest.php | 4 +- .../Tests/Coupon/Type/RemoveXPercentTest.php | 4 +- install/faker.php | 8 +- .../backOffice/default/assets/js/coupon.js | 344 ++++++++++------- .../backOffice/default/coupon-create.html | 1 + templates/backOffice/default/coupon-read.html | 2 +- .../backOffice/default/coupon-update.html | 93 +---- .../default/coupon/condition-input-ajax.html | 105 +----- .../backOffice/default/coupon/conditions.html | 7 +- templates/backOffice/default/coupon/form.html | 140 ++----- 31 files changed, 1086 insertions(+), 612 deletions(-) diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index 1bb8fd654..198285cef 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -156,7 +156,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface /** @var ConditionFactory $conditionFactory */ $conditionFactory = $this->container->get('thelia.condition.factory'); $couponRuleCollection = new ConditionCollection(); - $couponRuleCollection->add($noConditionRule); + $couponRuleCollection[] = $noConditionRule; $defaultSerializedRule = $conditionFactory->serializeConditionCollection( $couponRuleCollection ); diff --git a/core/lib/Thelia/Condition/ConditionCollection.php b/core/lib/Thelia/Condition/ConditionCollection.php index c7ec67caf..be457bded 100644 --- a/core/lib/Thelia/Condition/ConditionCollection.php +++ b/core/lib/Thelia/Condition/ConditionCollection.php @@ -23,6 +23,9 @@ namespace Thelia\Condition; +use ArrayAccess; +use Countable; +use Iterator; use Symfony\Component\DependencyInjection\ContainerInterface; use Thelia\Condition\Implementation\ConditionInterface; @@ -33,43 +36,153 @@ use Thelia\Condition\Implementation\ConditionInterface; * @author Guillaume MOREL * */ -class ConditionCollection +class ConditionCollection implements Iterator, Countable, ArrayAccess { /** @var array Array of ConditionInterface */ protected $conditions = array(); /** - * Get Conditions + * (PHP 5 >= 5.0.0) + * Return the current element + * @link http://php.net/manual/en/iterator.current.php * - * @return array Array of ConditionInterface + * @return mixed Can return any type. */ - public function getConditions() + public function current() { - return $this->conditions; + $var = current($this->conditions); + + return $var; } /** - * Add a ConditionInterface to the Collection + * (PHP 5 >= 5.0.0) + * Move forward to next element + * @link http://php.net/manual/en/iterator.next.php * - * @param ConditionInterface $condition Condition - * - * @return $this + * @return void Any returned value is ignored. */ - public function add(ConditionInterface $condition) + public function next() { - $this->conditions[] = $condition; - - return $this; + next($this->conditions); } /** - * Check if there is at least one condition in the collection + * (PHP 5 >= 5.0.0) + * Return the key of the current element + * @link http://php.net/manual/en/iterator.key.php * - * @return bool + * @return mixed scalar on success, or null on failure. */ - public function isEmpty() + public function key() { - return (empty($this->conditions)); + $var = key($this->conditions); + + return $var; + } + + /** + * (PHP 5 >= 5.0.0) + * Checks if current position is valid + * @link http://php.net/manual/en/iterator.valid.php + * + * @return boolean The return value will be casted to boolean and then evaluated. + * Returns true on success or false on failure. + */ + public function valid() + { + $key = key($this->conditions); + $var = ($key !== null && $key !== false); + + return $var; + } + + /** + * (PHP 5 >= 5.0.0) + * Rewind the Iterator to the first element + * @link http://php.net/manual/en/iterator.rewind.php + * + * @return void Any returned value is ignored. + */ + public function rewind() + { + reset($this->conditions); + } + + /** + * (PHP 5 >= 5.1.0) + * Count elements of an object + * @link http://php.net/manual/en/countable.count.php + * + * @return int The custom count as an integer. + * The return value is cast to an integer. + */ + public function count() + { + return count($this->conditions); + } + + /** + * (PHP 5 >= 5.0.0) + * Whether a offset exists + * @link http://php.net/manual/en/arrayaccess.offsetexists.php + * @param mixed $offset + * An offset to check for. + * + * @return boolean true on success or false on failure. + * The return value will be casted to boolean if non-boolean was returned. + */ + public function offsetExists($offset) + { + return isset($this->conditions[$offset]); + } + + /** + * (PHP 5 >= 5.0.0) + * Offset to retrieve + * @link http://php.net/manual/en/arrayaccess.offsetget.php + * @param mixed $offset + * The offset to retrieve. + * + * @return mixed Can return all value types. + */ + public function offsetGet($offset) + { + return isset($this->conditions[$offset]) ? $this->conditions[$offset] : null; + } + + /** + * (PHP 5 >= 5.0.0) + * Offset to set + * @link http://php.net/manual/en/arrayaccess.offsetset.php + * @param mixed $offset + * The offset to assign the value to. + * @param mixed $value + * The value to set. + * + * @return void + */ + public function offsetSet($offset, $value) + { + if (is_null($offset)) { + $this->conditions[] = $value; + } else { + $this->conditions[$offset] = $value; + } + } + + /** + * (PHP 5 >= 5.0.0) + * Offset to unset + * @link http://php.net/manual/en/arrayaccess.offsetunset.php + * @param mixed $offset + * The offset to unset. + * + * @return void + */ + public function offsetUnset($offset) + { + unset($this->conditions[$offset]); } /** @@ -81,12 +194,11 @@ class ConditionCollection { $arrayToSerialize = array(); /** @var ConditionInterface $condition */ - foreach ($this->getConditions() as $condition) { + foreach ($this as $condition) { $arrayToSerialize[] = $condition->getSerializableCondition(); } return json_encode($arrayToSerialize); } - } \ No newline at end of file diff --git a/core/lib/Thelia/Condition/ConditionEvaluator.php b/core/lib/Thelia/Condition/ConditionEvaluator.php index 992e9ca0d..901a9161a 100644 --- a/core/lib/Thelia/Condition/ConditionEvaluator.php +++ b/core/lib/Thelia/Condition/ConditionEvaluator.php @@ -49,7 +49,7 @@ class ConditionEvaluator { $isMatching = true; /** @var ConditionInterface $condition */ - foreach ($conditions->getConditions() as $condition) { + foreach ($conditions as $condition) { if (!$condition->isMatching()) { $isMatching = false; } diff --git a/core/lib/Thelia/Condition/ConditionFactory.php b/core/lib/Thelia/Condition/ConditionFactory.php index f7440634c..431641659 100644 --- a/core/lib/Thelia/Condition/ConditionFactory.php +++ b/core/lib/Thelia/Condition/ConditionFactory.php @@ -67,20 +67,17 @@ class ConditionFactory */ public function serializeConditionCollection(ConditionCollection $collection) { - if ($collection->isEmpty()) { + if ($collection->count() == 0) { /** @var ConditionInterface $conditionNone */ $conditionNone = $this->container->get( 'thelia.condition.match_for_everyone' ); - $collection->add($conditionNone); + $collection[] = $conditionNone; } $serializableConditions = array(); - $conditions = $collection->getConditions(); - if ($conditions !== null) { - /** @var $condition ConditionInterface */ - foreach ($conditions as $condition) { - $serializableConditions[] = $condition->getSerializableCondition(); - } + /** @var $condition ConditionInterface */ + foreach ($collection as $condition) { + $serializableConditions[] = $condition->getSerializableCondition(); } return base64_encode(json_encode($serializableConditions)); @@ -109,7 +106,7 @@ class ConditionFactory (array) $condition->operators, (array) $condition->values ); - $collection->add(clone $conditionManager); + $collection[] = clone $conditionManager; } } } @@ -138,7 +135,7 @@ class ConditionFactory $condition = $this->container->get($conditionServiceId); $condition->setValidatorsFromForm($operators, $values); - return $condition; + return clone $condition; } /** @@ -148,7 +145,7 @@ class ConditionFactory * * @return array Ready to be drawn condition inputs */ - public function getInputs($conditionServiceId) + public function getInputsFromServiceId($conditionServiceId) { if (!$this->container->has($conditionServiceId)) { return false; @@ -157,6 +154,19 @@ class ConditionFactory /** @var ConditionInterface $condition */ $condition = $this->container->get($conditionServiceId); + return $this->getInputsFromConditionInterface($condition); + } + + /** + * Get Condition inputs from serviceId + * + * @param ConditionInterface $condition ConditionManager + * + * @return array Ready to be drawn condition inputs + */ + public function getInputsFromConditionInterface(ConditionInterface $condition) + { + return $condition->getValidators(); } } \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php b/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php index b12dc2bd4..ab0b404b3 100644 --- a/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php +++ b/core/lib/Thelia/Condition/Implementation/ConditionAbstract.php @@ -219,4 +219,101 @@ abstract class ConditionAbstract implements ConditionInterface return true; } + /** + * Draw the operator 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 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 .= ''; + } + + $selectHtml .= ' + + '; + } + + return $selectHtml; + } + + /** + * Draw the base input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @param string $label I18n input label + * @param string $inputKey Input key (ex: self::INPUT1) + * + * @return string HTML string + */ + 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; + } + + /** + * Draw the quantity input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @param string $inputKey Input key (ex: self::INPUT1) + * @param int $max Maximum selectable + * @param int $min Minimum selectable + * + * @return string HTML string + */ + 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; + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Implementation/ConditionInterface.php b/core/lib/Thelia/Condition/Implementation/ConditionInterface.php index 8992c6c82..a123d2c10 100644 --- a/core/lib/Thelia/Condition/Implementation/ConditionInterface.php +++ b/core/lib/Thelia/Condition/Implementation/ConditionInterface.php @@ -85,11 +85,20 @@ interface ConditionInterface /** * Get I18n tooltip + * Explain in detail what the Condition checks * * @return string */ public function getToolTip(); + /** + * Get I18n summary + * Explain briefly the condition with given values + * + * @return string + */ + public function getSummary(); + /** * Return all validators * @@ -104,4 +113,12 @@ interface ConditionInterface */ public function getSerializableCondition(); + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @return string HTML string + */ + public function drawBackOfficeInputs(); + } diff --git a/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php b/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php index 5438bdd91..e3452a830 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForEveryone.php @@ -97,6 +97,7 @@ class MatchForEveryone extends ConditionAbstract /** * Get I18n tooltip + * Explain in detail what the Condition checks * * @return string */ @@ -111,6 +112,23 @@ class MatchForEveryone extends ConditionAbstract return $toolTip; } + /** + * Get I18n summary + * Explain briefly the condition with given values + * + * @return string + */ + public function getSummary() + { + $toolTip = $this->translator->trans( + 'Will return always true', + array(), + 'condition' + ); + + return $toolTip; + } + /** * Generate inputs ready to be drawn * @@ -121,4 +139,17 @@ class MatchForEveryone extends ConditionAbstract return array(); } + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @return string HTML string + */ + public function drawBackOfficeInputs() + { + // No input + + return ''; + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php b/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php index e4f8f8e74..8753a4100 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php @@ -167,7 +167,7 @@ class MatchForTotalAmount extends ConditionAbstract public function getName() { return $this->translator->trans( - 'Cart total amount', + 'By cart total amount', array(), 'condition' ); @@ -175,10 +175,28 @@ class MatchForTotalAmount extends ConditionAbstract /** * Get I18n tooltip + * Explain in detail what the Condition checks * * @return string */ public function getToolTip() + { + $toolTip = $this->translator->trans( + 'Check the total Cart amount in the given currency', + array(), + 'condition' + ); + + return $toolTip; + } + + /** + * Get I18n summary + * Explain briefly the condition with given values + * + * @return string + */ + public function getSummary() { $i18nOperator = Operators::getI18n( $this->translator, $this->operators[self::INPUT1] @@ -227,8 +245,6 @@ class MatchForTotalAmount extends ConditionAbstract 'title' => $name1, 'availableOperators' => $this->availableOperators[self::INPUT1], 'availableValues' => '', - 'type' => 'text', - 'class' => 'form-control', 'value' => '', 'selectedOperator' => '' ), @@ -236,12 +252,100 @@ class MatchForTotalAmount extends ConditionAbstract 'title' => $name2, 'availableOperators' => $this->availableOperators[self::INPUT2], 'availableValues' => $cleanedCurrencies, - 'type' => 'select', - 'class' => 'form-control', 'value' => '', 'selectedOperator' => Operators::EQUAL ) ); } + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @return string HTML string + */ + public function drawBackOfficeInputs() + { + $labelPrice = $this->facade + ->getTranslator() + ->trans('Price', array(), 'condition'); + + $html = $this->drawBackOfficeBaseInputsText($labelPrice, self::INPUT1); + + return $html; + } + + /** + * Draw the base input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @param string $label I18n input label + * @param string $inputKey Input key (ex: self::INPUT1) + * + * @return string HTML string + */ + 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]; + } + + $html = ' + +
+
+ ' . $operatorSelectHtml . ' +
+
+ +
+
+ + ' . $currencySelectHtml . ' +
+
+ '; + + return $html; + } + + /** + * 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 c99ed67c9..01e64afc4 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php @@ -140,7 +140,7 @@ class MatchForXArticles extends ConditionAbstract public function getName() { return $this->translator->trans( - 'Number of articles in cart', + 'By number of articles in cart', array(), 'condition' ); @@ -148,10 +148,28 @@ class MatchForXArticles extends ConditionAbstract /** * Get I18n tooltip + * Explain in detail what the Condition checks * * @return string */ public function getToolTip() + { + $toolTip = $this->translator->trans( + 'Check the amount of product in the Cart', + array(), + 'condition' + ); + + return $toolTip; + } + + /** + * Get I18n summary + * Explain briefly the condition with given values + * + * @return string + */ + public function getSummary() { $i18nOperator = Operators::getI18n( $this->translator, $this->operators[self::INPUT1] @@ -186,11 +204,57 @@ class MatchForXArticles extends ConditionAbstract self::INPUT1 => array( 'title' => $name1, 'availableOperators' => $this->availableOperators[self::INPUT1], - 'type' => 'text', - 'class' => 'form-control', 'value' => '', 'selectedOperator' => '' ) ); } + + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @return string HTML string + */ + public function drawBackOfficeInputs() + { + $labelQuantity = $this->facade + ->getTranslator() + ->trans('Quantity', array(), 'condition'); + + $html = $this->drawBackOfficeBaseInputsText($labelQuantity, self::INPUT1); + + return $html; + } + + /** + * Draw the base input displayed in the BackOffice + * allowing Admin to set its Coupon Conditions + * + * @param string $label I18n input label + * @param string $inputKey Input key (ex: self::INPUT1) + * + * @return string HTML string + */ + protected function drawBackOfficeBaseInputsText($label, $inputKey) + { + $operatorSelectHtml = $this->drawBackOfficeInputOperators($inputKey); + $quantitySelectHtml = $this->drawBackOfficeInputQuantityValues($inputKey, 20, 1); + + $html = ' +
+ +
+
+ ' . $operatorSelectHtml . ' +
+
+ ' . $quantitySelectHtml . ' +
+
+
+ '; + return $html; + } + } diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 613694ab7..c0e54528a 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -499,17 +499,31 @@ Thelia\Controller\Admin\CouponController::readAction \d+ - - Thelia\Controller\Admin\CouponController::getBackOfficeInputsAction + + Thelia\Controller\Admin\CouponController::getBackOfficeInputsAjaxAction .* - - Thelia\Controller\Admin\CouponController::getConditionInputAction + + Thelia\Controller\Admin\CouponController::getBackOfficeConditionSummariesAjaxAction + \d+ + + + Thelia\Controller\Admin\CouponController::getConditionEmptyInputAjaxAction .* - - Thelia\Controller\Admin\CouponController::updateConditionsAction + + Thelia\Controller\Admin\CouponController::getConditionToUpdateInputAjaxAction \d+ + \d+ + + + Thelia\Controller\Admin\CouponController::saveConditionsAction + \d+ + + + Thelia\Controller\Admin\CouponController::deleteConditionsAction + \d+ + \d+ Thelia\Controller\Admin\CouponController::consumeAction diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index a46411aca..078adb626 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -25,6 +25,7 @@ namespace Thelia\Controller\Admin; use Symfony\Component\Form\Form; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Router; use Thelia\Condition\ConditionFactory; use Thelia\Condition\Implementation\ConditionInterface; @@ -235,18 +236,6 @@ class CouponController extends BaseAdminController 'locale' => $coupon->getLocale(), ); - $args['conditionsObject'] = array(); - - /** @var ConditionInterface $condition */ - foreach ($conditions->getConditions() as $condition) { - $args['conditionsObject'][] = array( - 'serviceId' => $condition->getServiceId(), - 'name' => $condition->getName(), - 'tooltip' => $condition->getToolTip(), - 'validators' => $condition->getValidators() - ); - } - $args['conditions'] = $this->cleanConditionForTemplate($conditions); // Setup the object form @@ -259,19 +248,40 @@ class CouponController extends BaseAdminController $args['availableCoupons'] = $this->getAvailableCoupons(); $args['couponInputsHtml'] = $couponManager->drawBackOfficeInputs(); $args['urlAjaxAdminCouponDrawInputs'] = $this->getRoute( - 'admin.coupon.draw.inputs', + 'admin.coupon.draw.inputs.ajax', array('couponServiceId' => 'couponServiceId'), Router::ABSOLUTE_URL ); $args['availableConditions'] = $this->getAvailableConditions(); - $args['urlAjaxGetConditionInput'] = $this->getRoute( - 'admin.coupon.condition.input', + $args['urlAjaxGetConditionInputFromServiceId'] = $this->getRoute( + 'admin.coupon.draw.condition.read.inputs.ajax', array('conditionId' => 'conditionId'), Router::ABSOLUTE_URL ); + $args['urlAjaxGetConditionInputFromConditionInterface'] = $this->getRoute( + 'admin.coupon.draw.condition.update.inputs.ajax', + array( + 'couponId' => $couponId, + 'conditionIndex' => 8888888 + ), + Router::ABSOLUTE_URL + ); - $args['urlAjaxUpdateConditions'] = $this->getRoute( - 'admin.coupon.condition.update', + $args['urlAjaxSaveConditions'] = $this->getRoute( + 'admin.coupon.condition.save', + array('couponId' => $couponId), + Router::ABSOLUTE_URL + ); + $args['urlAjaxDeleteConditions'] = $this->getRoute( + 'admin.coupon.condition.delete', + array( + 'couponId' => $couponId, + 'conditionIndex' => 8888888 + ), + Router::ABSOLUTE_URL + ); + $args['urlAjaxGetConditionSummaries'] = $this->getRoute( + 'admin.coupon.draw.condition.summaries.ajax', array('couponId' => $couponId), Router::ABSOLUTE_URL ); @@ -288,7 +298,7 @@ class CouponController extends BaseAdminController * * @return \Thelia\Core\HttpFoundation\Response */ - public function getConditionInputAction($conditionId) + public function getConditionEmptyInputAjaxAction($conditionId) { $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); @@ -296,7 +306,14 @@ class CouponController extends BaseAdminController /** @var ConditionFactory $conditionFactory */ $conditionFactory = $this->container->get('thelia.condition.factory'); - $inputs = $conditionFactory->getInputs($conditionId); + $inputs = $conditionFactory->getInputsFromServiceId($conditionId); + if (!$this->container->has($conditionId)) { + return false; + } + + /** @var ConditionInterface $condition */ + $condition = $this->container->get($conditionId); + if ($inputs === null) { return $this->pageNotFound(); @@ -305,8 +322,64 @@ class CouponController extends BaseAdminController return $this->render( 'coupon/condition-input-ajax', array( - 'conditionId' => $conditionId, - 'inputs' => $inputs + 'inputsDrawn' => $condition->drawBackOfficeInputs(), + 'conditionServiceId' => $condition->getServiceId(), + 'conditionIndex' => -1, + ) + ); + } + + /** + * Manage Coupons read display + * + * @param int $couponId Coupon id being updated + * @param int $conditionIndex Coupon Condition position in the collection + * + * @return \Thelia\Core\HttpFoundation\Response + */ + public function getConditionToUpdateInputAjaxAction($couponId, $conditionIndex) + { + $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); + + $this->checkXmlHttpRequest(); + + $search = CouponQuery::create(); + /** @var Coupon $coupon */ + $coupon = $search->findOneById($couponId); + if (!$coupon) { + return $this->pageNotFound(); + } + + /** @var CouponFactory $couponFactory */ + $couponFactory = $this->container->get('thelia.coupon.factory'); + $couponManager = $couponFactory->buildCouponFromModel($coupon); + + if (!$couponManager instanceof CouponInterface) { + return $this->pageNotFound(); + } + + $conditions = $couponManager->getConditions(); + if (!isset($conditions[$conditionIndex])) { + return $this->pageNotFound(); + } + /** @var ConditionInterface $condition */ + $condition = $conditions[$conditionIndex]; + + /** @var ConditionFactory $conditionFactory */ + $conditionFactory = $this->container->get('thelia.condition.factory'); + $inputs = $conditionFactory->getInputsFromConditionInterface($condition); + + if ($inputs === null) { + return $this->pageNotFound(); + } + + return $this->render( + 'coupon/condition-input-ajax', + array( + 'inputsDrawn' => $condition->drawBackOfficeInputs(), + 'conditionServiceId' => $condition->getServiceId(), + 'conditionIndex' => $conditionIndex, + ) ); } @@ -318,7 +391,7 @@ class CouponController extends BaseAdminController * * @return \Thelia\Core\HttpFoundation\Response */ - public function updateConditionsAction($couponId) + public function saveConditionsAction($couponId) { $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); @@ -327,70 +400,72 @@ class CouponController extends BaseAdminController $search = CouponQuery::create(); /** @var Coupon $coupon */ $coupon = $search->findOneById($couponId); - if (!$coupon) { return $this->pageNotFound(); } - $conditions = new ConditionCollection(); + $conditionToSave = $this->buildConditionFromRequest(); - /** @var ConditionFactory $conditionFactory */ - $conditionFactory = $this->container->get('thelia.condition.factory'); - $conditionsReceived = json_decode($this->getRequest()->get('conditions')); - foreach ($conditionsReceived as $conditionReceived) { - $condition = $conditionFactory->build( - $conditionReceived->serviceId, - (array) $conditionReceived->operators, - (array) $conditionReceived->values - ); - $conditions->add(clone $condition); + /** @var CouponFactory $couponFactory */ + $couponFactory = $this->container->get('thelia.coupon.factory'); + $couponManager = $couponFactory->buildCouponFromModel($coupon); + + if (!$couponManager instanceof CouponInterface) { + return $this->pageNotFound(); } - $couponEvent = new CouponCreateOrUpdateEvent( - $coupon->getCode(), - $coupon->getType(), - $coupon->getTitle(), - $coupon->getEffects(), - $coupon->getShortDescription(), - $coupon->getDescription(), - $coupon->getIsEnabled(), - $coupon->getExpirationDate(), - $coupon->getIsAvailableOnSpecialOffers(), - $coupon->getIsCumulative(), - $coupon->getIsRemovingPostage(), - $coupon->getMaxUsage(), - $coupon->getLocale() - ); - $couponEvent->setCouponModel($coupon); - $couponEvent->setConditions($conditions); + $conditions = $couponManager->getConditions(); + $conditionIndex = $this->getRequest()->request->get('conditionIndex'); + if ($conditionIndex >= 0) { + // Update mode + $conditions[$conditionIndex] = $conditionToSave; + } else { + // Insert mode + $conditions[] = $conditionToSave; + } + $couponManager->setConditions($conditions); - $eventToDispatch = TheliaEvents::COUPON_CONDITION_UPDATE; - // Dispatch Event to the Action - $this->dispatch( - $eventToDispatch, - $couponEvent - ); + $this->manageConditionUpdate($coupon, $conditions); - $this->adminLogAppend( - AdminResources::COUPON, AccessManager::UPDATE, - sprintf( - 'Coupon %s (ID %s) conditions updated', - $couponEvent->getCouponModel()->getTitle(), - $couponEvent->getCouponModel()->getType() - ) - ); + return new Response(); + } - $cleanedConditions = $this->cleanConditionForTemplate($conditions); + /** + * Manage Coupons condition deleteion + * + * @param int $couponId Coupon id + * @param int $conditionIndex Coupon condition index in the collection + * + * @return \Thelia\Core\HttpFoundation\Response + */ + public function deleteConditionsAction($couponId, $conditionIndex) + { + $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); - return $this->render( - 'coupon/conditions', - array( - 'couponId' => $couponId, - 'conditions' => $cleanedConditions, - 'urlEdit' => $couponId, - 'urlDelete' => $couponId - ) - ); + $this->checkXmlHttpRequest(); + + $search = CouponQuery::create(); + /** @var Coupon $coupon */ + $coupon = $search->findOneById($couponId); + if (!$coupon) { + return $this->pageNotFound(); + } + + /** @var CouponFactory $couponFactory */ + $couponFactory = $this->container->get('thelia.coupon.factory'); + $couponManager = $couponFactory->buildCouponFromModel($coupon); + + if (!$couponManager instanceof CouponInterface) { + return $this->pageNotFound(); + } + + $conditions = $couponManager->getConditions(); + unset($conditions[$conditionIndex]); + $couponManager->setConditions($conditions); + + $this->manageConditionUpdate($coupon, $conditions); + + return new Response(); } /** @@ -577,8 +652,16 @@ class CouponController extends BaseAdminController { $cleanedConditions = array(); /** @var $condition ConditionInterface */ - foreach ($conditions->getConditions() as $condition) { - $cleanedConditions[] = $condition->getToolTip(); + foreach ($conditions as $index => $condition) { + $temp = array( + 'serviceId' => $condition->getServiceId(), + 'index' => $index, + 'name' => $condition->getName(), + 'toolTip' => $condition->getToolTip(), + 'summary' => $condition->getSummary(), + 'validators' => $condition->getValidators() + ); + $cleanedConditions[] = $temp; } return $cleanedConditions; @@ -592,20 +675,57 @@ class CouponController extends BaseAdminController * * @return ResponseRest */ - public function getBackOfficeInputsAction($couponServiceId) + public function getBackOfficeInputsAjaxAction($couponServiceId) { - /** @var CouponInterface $coupon */ - $coupon = $this->container->get($couponServiceId); + $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); + $this->checkXmlHttpRequest(); - if (!$coupon instanceof CouponInterface) { + /** @var CouponInterface $coupon */ + $couponManager = $this->container->get($couponServiceId); + + if (!$couponManager instanceof CouponInterface) { $this->pageNotFound(); } - $response = new ResponseRest($coupon->drawBackOfficeInputs()); + $response = new ResponseRest($couponManager->drawBackOfficeInputs()); return $response; } + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon effect + * + * @param int $couponId Coupon id + * + * @return ResponseRest + */ + public function getBackOfficeConditionSummariesAjaxAction($couponId) + { + $this->checkAuth(AdminResources::COUPON, array(), AccessManager::VIEW); + $this->checkXmlHttpRequest(); + + /** @var Coupon $coupon */ + $coupon = CouponQuery::create()->findPk($couponId); + if (null === $coupon) { + return $this->pageNotFound(); + + } + + /** @var CouponFactory $couponFactory */ + $couponFactory = $this->container->get('thelia.coupon.factory'); + $couponManager = $couponFactory->buildCouponFromModel($coupon); + + if (!$couponManager instanceof CouponInterface) { + return $this->pageNotFound(); + } + + $args = array(); + $args['conditions'] = $this->cleanConditionForTemplate($couponManager->getConditions()); + return $this->render('coupon/conditions', $args); + + } + /** * Add percentage logic if found in the Coupon post data * @@ -677,4 +797,73 @@ class CouponController extends BaseAdminController return $couponEvent; } + /** + * Build ConditionInterface from request + * + * @return ConditionInterface + */ + protected function buildConditionFromRequest() + { + $request = $this->getRequest(); + $post = $request->request->getIterator(); + $serviceId = $request->request->get('categoryCondition'); + $operators = array(); + $values = array(); + foreach ($post as $key => $input) { + if (isset($input['operator']) && isset($input['value'])) { + $operators[$key] = $input['operator']; + $values[$key] = $input['value']; + } + } + + /** @var ConditionFactory $conditionFactory */ + $conditionFactory = $this->container->get('thelia.condition.factory'); + $conditionToSave = $conditionFactory->build($serviceId, $operators, $values); + + return $conditionToSave; + } + + /** + * Manage how a Condition is updated + * + * @param Coupon $coupon Coupon Model + * @param ConditionCollection $conditions Condition collection + */ + protected function manageConditionUpdate(Coupon $coupon, ConditionCollection $conditions) + { + $couponEvent = new CouponCreateOrUpdateEvent( + $coupon->getCode(), + $coupon->getType(), + $coupon->getTitle(), + $coupon->getEffects(), + $coupon->getShortDescription(), + $coupon->getDescription(), + $coupon->getIsEnabled(), + $coupon->getExpirationDate(), + $coupon->getIsAvailableOnSpecialOffers(), + $coupon->getIsCumulative(), + $coupon->getIsRemovingPostage(), + $coupon->getMaxUsage(), + $coupon->getLocale() + ); + $couponEvent->setCouponModel($coupon); + $couponEvent->setConditions($conditions); + + $eventToDispatch = TheliaEvents::COUPON_CONDITION_UPDATE; + // Dispatch Event to the Action + $this->dispatch( + $eventToDispatch, + $couponEvent + ); + + $this->adminLogAppend( + AdminResources::COUPON, AccessManager::UPDATE, + sprintf( + 'Coupon %s (ID %s) conditions updated', + $couponEvent->getCouponModel()->getTitle(), + $couponEvent->getCouponModel()->getType() + ) + ); + } + } diff --git a/core/lib/Thelia/Core/Template/Loop/Coupon.php b/core/lib/Thelia/Core/Template/Loop/Coupon.php index 85a221a29..3efa480e0 100755 --- a/core/lib/Thelia/Core/Template/Loop/Coupon.php +++ b/core/lib/Thelia/Core/Template/Loop/Coupon.php @@ -122,8 +122,12 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface $cleanedConditions = array(); /** @var ConditionInterface $condition */ - foreach ($conditions->getConditions() as $condition) { - $cleanedConditions[] = $condition->getToolTip(); + foreach ($conditions as $condition) { + $temp = array( + 'toolTip' => $condition->getToolTip(), + 'summary' => $condition->getSummary() + ); + $cleanedConditions[] = $temp; } $loopResultRow->set("ID", $coupon->getId()) ->set("IS_TRANSLATED", $coupon->getVirtualColumn('IS_TRANSLATED')) diff --git a/core/lib/Thelia/Coupon/CouponFactory.php b/core/lib/Thelia/Coupon/CouponFactory.php index b157922a2..e34ee9263 100644 --- a/core/lib/Thelia/Coupon/CouponFactory.php +++ b/core/lib/Thelia/Coupon/CouponFactory.php @@ -79,7 +79,7 @@ class CouponFactory /** @var CouponInterface $couponInterface */ $couponInterface = $this->buildCouponFromModel($couponModel); - if ($couponInterface && $couponInterface->getConditions()->isEmpty()) { + if ($couponInterface && $couponInterface->getConditions()->count() == 0) { throw new InvalidConditionException( get_class($couponInterface) ); @@ -129,7 +129,7 @@ class CouponFactory $couponManager->setConditions($conditions); - return $couponManager; + return clone $couponManager; } diff --git a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php index 5de809904..3158fd2b2 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php @@ -23,7 +23,6 @@ namespace Thelia\Coupon\Type; -use Thelia\Coupon\FacadeInterface; use Thelia\Coupon\Type\CouponAbstract; /** diff --git a/core/lib/Thelia/Tests/Condition/ConditionCollectionTest.php b/core/lib/Thelia/Tests/Condition/ConditionCollectionTest.php index 298204c26..664349915 100644 --- a/core/lib/Thelia/Tests/Condition/ConditionCollectionTest.php +++ b/core/lib/Thelia/Tests/Condition/ConditionCollectionTest.php @@ -105,8 +105,7 @@ class ConditionCollectionTest extends \PHPUnit_Framework_TestCase } /** - * @covers Thelia\Condition\ConditionCollection::getConditions - * @covers Thelia\Condition\ConditionCollection::add + * */ public function testGetConditions() { @@ -123,23 +122,23 @@ class ConditionCollectionTest extends \PHPUnit_Framework_TestCase $condition1->setValidatorsFromForm($operators, $values); $collection = new ConditionCollection(); - $collection->add($condition1); + $collection[] = $condition1; $expected = $condition1; - $actual = $collection->getConditions()[0]; + $actual = $collection[0]; $this->assertEquals($expected, $actual); - $this->assertFalse($collection->isEmpty()); + $this->assertFalse($collection->count() == 0); } /** - * @covers Thelia\Condition\ConditionCollection::isEmpty + * @covers Thelia\Condition\ConditionCollection::count */ public function testIsEmpty() { $collection = new ConditionCollection(); - $this->assertTrue($collection->isEmpty()); + $this->assertTrue($collection->count() == 0); } /** @@ -170,8 +169,8 @@ class ConditionCollectionTest extends \PHPUnit_Framework_TestCase $condition2->setValidatorsFromForm($operators2, $values2); $collection = new ConditionCollection(); - $collection->add($condition1); - $collection->add($condition2); + $collection[] = $condition1; + $collection[] = $condition2; $expected = '[{"conditionServiceId":"thelia.condition.match_for_total_amount","operators":{"price":">","currency":"=="},"values":{"price":400,"currency":"EUR"}},{"conditionServiceId":"thelia.condition.match_for_total_amount","operators":{"price":"<","currency":"=="},"values":{"price":600,"currency":"EUR"}}]'; $actual = $collection->__toString();; diff --git a/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php b/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php index 1ecd047d5..37dce5120 100644 --- a/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php +++ b/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php @@ -157,8 +157,8 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(true)); $collection = new ConditionCollection(); - $collection->add($stubConditionTrue1); - $collection->add($stubConditionTrue2); + $collection[] = $stubConditionTrue1; + $collection[] = $stubConditionTrue2; $conditionEvaluator = new ConditionEvaluator(); $actual = $conditionEvaluator->isMatching($collection); @@ -188,8 +188,8 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(false)); $collection = new ConditionCollection(); - $collection->add($stubConditionTrue); - $collection->add($stubConditionFalse); + $collection[] = $stubConditionTrue; + $collection[] = $stubConditionFalse; $conditionEvaluator = new ConditionEvaluator(); $actual = $conditionEvaluator->isMatching($collection); diff --git a/core/lib/Thelia/Tests/Condition/ConditionFactoryTest.php b/core/lib/Thelia/Tests/Condition/ConditionFactoryTest.php index 1894e8e29..3152700e6 100644 --- a/core/lib/Thelia/Tests/Condition/ConditionFactoryTest.php +++ b/core/lib/Thelia/Tests/Condition/ConditionFactoryTest.php @@ -239,8 +239,8 @@ class ConditionFactoryTest extends \PHPUnit_Framework_TestCase $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $conditionFactory = new ConditionFactory($stubContainer); @@ -307,12 +307,12 @@ class ConditionFactoryTest extends \PHPUnit_Framework_TestCase $condition1->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); + $conditions[] = $condition1; $conditionFactory = new ConditionFactory($stubContainer); $expected = $condition1->getValidators(); - $actual = $conditionFactory->getInputs('thelia.condition.match_for_x_articles'); + $actual = $conditionFactory->getInputsFromServiceId('thelia.condition.match_for_x_articles'); $this->assertEquals($expected, $actual); @@ -372,12 +372,12 @@ class ConditionFactoryTest extends \PHPUnit_Framework_TestCase $condition1->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); + $conditions[] = $condition1; $conditionFactory = new ConditionFactory($stubContainer); $expected = false; - $actual = $conditionFactory->getInputs('thelia.condition.unknown'); + $actual = $conditionFactory->getInputsFromServiceId('thelia.condition.unknown'); $this->assertEquals($expected, $actual); @@ -435,7 +435,7 @@ class ConditionFactoryTest extends \PHPUnit_Framework_TestCase $conditionNone = new MatchForEveryone($stubFacade); $expectedCollection = new ConditionCollection(); - $expectedCollection->add($conditionNone); + $expectedCollection[] = $conditionNone; $expected = $conditionFactory->serializeConditionCollection($expectedCollection); $actual = $conditionFactory->serializeConditionCollection($conditions); diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php index fa69e46b6..4967ba91b 100644 --- a/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php @@ -659,7 +659,7 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase $conditionFactory = new ConditionFactory($stubContainer); $collection = new ConditionCollection(); - $collection->add($condition1); + $collection[] = $condition1; $serialized = $conditionFactory->serializeConditionCollection($collection); $conditionFactory->unserializeConditionCollection($serialized); @@ -726,7 +726,7 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase $conditionFactory = new ConditionFactory($stubContainer); $collection = new ConditionCollection(); - $collection->add($condition1); + $collection[] = $condition1; $serialized = $conditionFactory->serializeConditionCollection($collection); $conditionFactory->unserializeConditionCollection($serialized); @@ -793,7 +793,7 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase $conditionFactory = new ConditionFactory($stubContainer); $collection = new ConditionCollection(); - $collection->add($condition1); + $collection[] = $condition1; $serialized = $conditionFactory->serializeConditionCollection($collection); $conditionFactory->unserializeConditionCollection($serialized); @@ -927,8 +927,6 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase '>' => 'Price' ), 'availableValues' => '', - 'type' => 'text', - 'class' => 'form-control', 'value' => '', 'selectedOperator' => '' ), @@ -940,8 +938,6 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase 'USD' => '$', 'GBP' => '£', ), - 'type' => 'select', - 'class' => 'form-control', 'value' => '', 'selectedOperator' => Operators::EQUAL ) diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php index 4f9f96a4b..9911bfa33 100644 --- a/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php @@ -743,8 +743,6 @@ class MatchForXArticlesTest extends \PHPUnit_Framework_TestCase '>=' => 'Price', '>' => 'Price' ), - 'type' => 'text', - 'class' => 'form-control', 'value' => '', 'selectedOperator' => '' ) diff --git a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php index 648962e01..c96fb8b0f 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php @@ -146,8 +146,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $serializedConditions = $conditionFactory->serializeConditionCollection($conditions); $coupon1->setSerializedConditions($serializedConditions); @@ -210,8 +210,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory') ->disableOriginalConstructor() ->getMock(); @@ -312,8 +312,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory') ->disableOriginalConstructor() ->getMock(); @@ -439,8 +439,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory') ->disableOriginalConstructor() ->getMock(); diff --git a/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php b/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php index 70d15e29a..258fad35e 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php @@ -108,8 +108,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $serializedConditions = $conditionFactory->serializeConditionCollection($conditions); $coupon1->setSerializedConditions($serializedConditions); @@ -372,8 +372,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $stubFacade->expects($this->any()) ->method('getCurrentCoupons') @@ -428,8 +428,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $coupon->setConditions($conditions); $stubFacade->expects($this->any()) @@ -495,8 +495,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $coupon->setConditions($conditions); $stubFacade->expects($this->any()) diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php index 0a1d589b4..ae997d5cb 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php @@ -150,8 +150,8 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $coupon->setConditions($conditions); $this->assertEquals('XMAS', $coupon->getCode()); diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php index ac893ae26..0c3a8b2be 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php @@ -140,8 +140,8 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; $coupon->setConditions($conditions); $this->assertEquals('XMAS', $coupon->getCode()); diff --git a/install/faker.php b/install/faker.php index 3fd749ee1..ce48914b5 100755 --- a/install/faker.php +++ b/install/faker.php @@ -696,8 +696,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $condition2->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); + $conditions[] = $condition1; + $conditions[] = $condition2; /** @var ConditionFactory $conditionFactory */ $conditionFactory = $container->get('thelia.condition.factory'); @@ -743,7 +743,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua ); $condition1->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); + $conditions[] = $condition1; /** @var ConditionFactory $conditionFactory */ $conditionFactory = $container->get('thelia.condition.factory'); @@ -786,7 +786,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $values = array(); $condition1->setValidatorsFromForm($operators, $values); $conditions = new ConditionCollection(); - $conditions->add($condition1); + $conditions[] = $condition1; /** @var ConditionFactory $constraintCondition */ $constraintCondition = $container->get('thelia.condition.factory'); diff --git a/templates/backOffice/default/assets/js/coupon.js b/templates/backOffice/default/assets/js/coupon.js index 5cb00acbe..51cf41058 100755 --- a/templates/backOffice/default/assets/js/coupon.js +++ b/templates/backOffice/default/assets/js/coupon.js @@ -2,124 +2,126 @@ $(function($){ // Manage how coupon and conditions are saved $.couponManager = {}; - // Condition to be saved - $.couponManager.conditionToSave = {}; - $.couponManager.conditionToSave.serviceId = false; - $.couponManager.conditionToSave.operators = {}; - $.couponManager.conditionToSave.values = {}; - // Conditions payload to save - $.couponManager.conditionsToSave = []; - // Condition being updated id - $.couponManager.conditionToUpdateId = false; - // Clean array from deleteValue (undefined) keys - Array.prototype.clean = function(deleteValue) { - for (var i = 0; i < this.length; i++) { - if (this[i] == deleteValue) { - this.splice(i, 1); - i--; - } - } - return this; - }; + // Condition being updated category id + $.couponManager.conditionToUpdateServiceId = -1; + // Condition being updated index + $.couponManager.conditionToUpdateIndex = false; - // Remove 1 Condition then Save Conditions AJAX - $.couponManager.removeConditionAjax = function(id) { - // Delete condition in temporary array - delete $.couponManager.conditionsToSave[id]; - $.couponManager.conditionsToSave.clean(undefined); + // AJAX urls + $.couponManager.urlAjaxSaveConditions = ''; + $.couponManager.urlAjaxDeleteConditions = ''; + $.couponManager.urlAjaxGetConditionSummaries = ''; + $.couponManager.urlAjaxAdminCouponDrawInputs = ''; + $.couponManager.urlAjaxGetConditionInputFromServiceId = ''; + $.couponManager.urlAjaxGetConditionInputFromConditionInterface = ''; - // Save - $.couponManager.saveConditionAjax(); - }; - - // Add 1 Condition / or update the temporary Conditions array then Save Conditions via AJAX - $.couponManager.createOrUpdateConditionAjax = function() { - var id = $.couponManager.conditionToUpdateId; - // If create - if(!id) { - $.couponManager.conditionsToSave.push($.couponManager.conditionToSave); - } else { // else update - $.couponManager.conditionsToSave[id] = $.couponManager.conditionToSave; - // reset edit mode to off - $.couponManager.conditionToUpdateId = false; - } - - // Save - $.couponManager.saveConditionAjax(); - }; - - // Set condition inputs to allow editing - $.couponManager.updateConditionSelectAjax = function(id) { - $.couponManager.conditionToUpdateId = id; - $.couponManager.conditionToSave = $.couponManager.conditionsToSave[id]; - - // Set the condition selector - $("#category-condition option").filter(function() { - return $(this).val() == $.couponManager.conditionToSave.serviceId; - }).prop('selected', true); - - // Force condition input refresh - $.couponManager.loadConditionInputs($.couponManager.conditionToSave.serviceId, function() { - $.couponManager.fillInConditionInputs(); - }); - }; - - // Fill in condition inputs - $.couponManager.fillInConditionInputs = function() { - var operatorId = null; - var valueId = null; - var idName = null; - - var id = $.couponManager.conditionToUpdateId; - if(id) { - $.couponManager.conditionToSave = $.couponManager.conditionsToSave[id]; - } - - for (idName in $.couponManager.conditionToSave.operators) { - // Setting idName operator select - operatorId = idName + '-operator'; - $('#' + operatorId).val($.couponManager.conditionToSave.operators[idName]); - - // Setting idName value input - valueId = idName + '-value'; - $('#' + valueId).val($.couponManager.conditionToSave.values[idName]); - } - }; - - // Save conditions on click - $.couponManager.onClickSaveCondition = function() { - $('#condition-save-btn').on('click', function () { - if($('#category-condition').val() == 'thelia.condition.match_for_everyone') { -// // @todo translate message + put it in modal - var r = confirm("Do you really want to set this coupon available to everyone ?"); - if (r == true) { - $.couponManager.createOrUpdateConditionAjax(); - } - } else { - $.couponManager.createOrUpdateConditionAjax(); - } - - }); - }; - $.couponManager.onClickSaveCondition(); + // I18n messages + $.couponManager.intlPleaseRetry = ''; + $.couponManager.intlPleaseSelectAnotherCondition = ''; + $.couponManager.intlDoYouReallyWantToSetCouponAvailableForEveryOne = ''; + // ***************************************** + // ****************** Delete *************** + // ***************************************** // Remove condition on click $.couponManager.onClickDeleteCondition = function() { $('.condition-delete-btn').on('click', function (e) { e.preventDefault(); var $this = $(this); - $.couponManager.removeConditionAjax($this.attr('data-int')); + var index = $this.attr('data-conditionIndex'); + $.couponManager.conditionToUpdateServiceId = -1; + $.couponManager.conditionToUpdateIndex = false; + $.couponManager.removeConditionAjax(index); }); }; $.couponManager.onClickDeleteCondition(); + // Remove 1 Condition + $.couponManager.removeConditionAjax = function(index) { + var url = $.couponManager.urlAjaxDeleteConditions; + url = url.replace('8888888', index); + + $('#condition-list').html('
'); + $.ajax({ + url: url, + statusCode: { + 404: function() { + $('#condition-list').html($.couponManager.intlPleaseSelectAnotherCondition); + }, + 500: function() { + $('#condition-list').html($.couponManager.intlPleaseSelectAnotherCondition); + } + } + }).done(function(data) { + // Reload condition summaries ajax + $.couponManager.displayConditionsSummary(); + }); + }; + + // ***************************************** + // ****************** Save ***************** + // ***************************************** + + // Save conditions on click + $.couponManager.onClickSaveCondition = function() { + $('#condition-save-btn').on('click', function () { + if ($('#category-condition').val() == 'thelia.condition.match_for_everyone') { + var r = confirm($.couponManager.intlDoYouReallyWantToSetCouponAvailableForEveryOne); + if (r == true) { + $.couponManager.saveConditionAjax(); + } + } else { + $.couponManager.saveConditionAjax(); + } + }); + }; + $.couponManager.onClickSaveCondition(); + + // Save Conditions AJAX + $.couponManager.saveConditionAjax = function() { + var $form = $("#condition-form"); + var data = $form.serialize(); + var url = $form.attr('action'); + url = url.replace('8888888', $.couponManager.conditionToUpdateIndex); + $('#condition-add-operators-values').html('
'); + + $.post( + url, + data + ).done(function() { + $.couponManager.displayConditionsSummary(); + $('#condition-add-operators-values').html(''); + // Set the condition selector to default + $("#category-condition option").filter(function() { + return $(this).val() == '-1'; + }).prop('selected', true); + }).fail(function() { + $('#condition-add-operators-values').html( + $.couponManager.intlPleaseRetry + ); + }).always(function() { + // Reload condition summaries ajax + $.couponManager.displayConditionsSummary(); + }); + }; + + // ***************************************** + // ****************** Update**************** + // ***************************************** + // Update condition on click $.couponManager.onClickUpdateCondition = function() { $('.condition-update-btn').on('click', function (e) { e.preventDefault(); var $this = $(this); - $.couponManager.updateConditionSelectAjax($this.attr('data-int')); + $.couponManager.conditionToUpdateServiceId = $this.attr('data-serviceId'); + $.couponManager.conditionToUpdateIndex = $this.attr('data-conditionIndex'); + + $.couponManager.updateConditionSelectFromConditionInterfaceAjax( + $.couponManager.conditionToUpdateIndex, + $.couponManager.conditionToUpdateServiceId + ); // Hide row being updated $this.parent().parent().remove(); @@ -127,33 +129,82 @@ $(function($){ }; $.couponManager.onClickUpdateCondition(); - $.couponManager.displayEfffect = function(optionSelected) { - var mainDiv = $('#coupon-type'); - mainDiv.find('.typeToolTip').html(optionSelected.attr('data-description')); + // Reload condition inputs when changing Condition type + $.couponManager.onConditionChange = function() { + $('#category-condition').on('change', function () { + var $this = $(this); + // Only if add mode + if (false != $.couponManager.conditionToUpdateIndex) { + // Reload condition summaries ajax + $.couponManager.displayConditionsSummary(); + } + $.couponManager.conditionToUpdateServiceId = $this.val(); + $.couponManager.conditionToUpdateIndex = false; + $.couponManager.loadConditionInputsFromServiceId($this.val()); + }); + }; + $.couponManager.onConditionChange(); - var inputsDiv = mainDiv.find('.inputs'); - inputsDiv.html('
'); - var url = $.couponManager.urlAjaxAdminCouponDrawInputs; - console.log(url); - url = url.replace('couponServiceId', optionSelected.val()); - console.log(url); + // Set condition inputs in order to allow editing + $.couponManager.updateConditionSelectFromConditionInterfaceAjax = function(conditionIndex, serviceId) { + // Force condition input refresh + $.couponManager.loadConditionInputsFromConditionInterface(conditionIndex); + + // Set the condition selector + $("#category-condition option").filter(function() { + return $(this).val() == serviceId; + }).prop('selected', true); + }; + + // Reload condition inputs AJAX + $.couponManager.doAjaxloadConditionInputs = function(url) { + $('#condition-add-operators-values').html('
'); $.ajax({ - type: "GET", url: url, - data: '', statusCode: { 404: function() { - inputsDiv.html($.couponManager.intlPleaseRetry); + $('#condition-add-operators-values').html( + $.couponManager.intlPleaseSelectAnotherCondition + ); }, 500: function() { - inputsDiv.html($.couponManager.intlPleaseRetry); + $('#condition-add-operators-values').html( + $.couponManager.intlPleaseSelectAnotherCondition + ); } } }).done(function(data) { - inputsDiv.html(data); + $('#condition-add-operators-values').html(data); + if ($.couponManager.conditionToUpdateServiceId == -1) { + // Placeholder can't be saved + $('#condition-save-btn').hide(); + } else { + $('#condition-save-btn').show(); + } }); }; + // Reload condition inputs from Condition ServiceId + $.couponManager.loadConditionInputsFromServiceId = function(conditionServiceId) { + var url = $.couponManager.urlAjaxGetConditionInputFromServiceId; + url = url.replace('conditionId', conditionServiceId); + + return $.couponManager.doAjaxloadConditionInputs(url); + }; + // Reload condition inputs from Condition index + $.couponManager.loadConditionInputsFromConditionInterface = function(conditionIndex) { + var url = $.couponManager.urlAjaxGetConditionInputFromConditionInterface; + url = url.replace('8888888', conditionIndex); + + return $.couponManager.doAjaxloadConditionInputs(url); + }; + + + + + // *********************************************** + // *************** Manager Coupon **************** + // *********************************************** // Reload effect inputs when changing effect $.couponManager.onEffectChange = function() { var mainDiv = $('#coupon-type'); @@ -168,28 +219,65 @@ $(function($){ }; $.couponManager.onEffectChange(); - // Reload condition inputs when changing effect - $.couponManager.onConditionChange = function() { - $('#category-condition').on('change', function () { - $.couponManager.loadConditionInputs($(this).val(), function() {}); + $.couponManager.displayEfffect = function(optionSelected) { + var mainDiv = $('#coupon-type'); + mainDiv.find('.typeToolTip').html(optionSelected.attr('data-description')); + + var inputsDiv = mainDiv.find('.inputs'); + inputsDiv.html('
'); + var url = $.couponManager.urlAjaxAdminCouponDrawInputs; + url = url.replace('couponServiceId', optionSelected.val()); + $.ajax({ + type: "GET", + url: url, + data: '', + statusCode: { + 404: function() { + inputsDiv.html($.couponManager.intlPleaseRetry); + }, + 500: function() { + inputsDiv.html($.couponManager.intlPleaseRetry); + } + } + }).done(function(data) { + inputsDiv.html(data); }); }; - $.couponManager.onConditionChange(); - // Fill in ready to be saved condition array - // var onInputsChange = function() - // In AJAX response + $.couponManager.displayConditionsSummary = function() { + var mainDiv = $('#condition-list'); + mainDiv.html('
'); + var url = $.couponManager.urlAjaxGetConditionSummaries; + $.ajax({ + type: "GET", + url: url, + data: '', + statusCode: { + 404: function() { + mainDiv.html($.couponManager.intlPleaseRetry); + }, + 500: function() { + mainDiv.html($.couponManager.intlPleaseRetry); + } + } + }).done(function(data) { + mainDiv.html(data); + $.couponManager.onClickUpdateCondition(); + $.couponManager.onClickDeleteCondition(); + }); + }; // Set max usage to unlimited or not $.couponManager.onUsageUnlimitedChange = function() { var $isUnlimited = $('#is-unlimited'); - if ($('#max-usage').val() == -1) { + $maxUsage = $('#max-usage'); + if ($maxUsage.val() == -1) { $isUnlimited.prop('checked', true); - $('#max-usage').hide(); + $maxUsage.hide(); $('#max-usage-label').hide(); } else { $isUnlimited.prop('checked', false); - $('#max-usage').show(); + $maxUsage.show(); $('#max-usage-label').show(); } @@ -205,8 +293,4 @@ $(function($){ }); }; $.couponManager.onUsageUnlimitedChange(); - -}); - - - +}); \ No newline at end of file diff --git a/templates/backOffice/default/coupon-create.html b/templates/backOffice/default/coupon-create.html index 2eaf30990..aa6b7d353 100755 --- a/templates/backOffice/default/coupon-create.html +++ b/templates/backOffice/default/coupon-create.html @@ -64,6 +64,7 @@ // Url alowing to get coupon inputs $.couponManager.urlAjaxAdminCouponDrawInputs = "{$urlAjaxAdminCouponDrawInputs}"; $.couponManager.intlPleaseRetry = '{intl l='Please retry'}'; + $.couponManager.intlDoYouReallyWantToSetCouponAvailableForEveryOne = '{intl l='Do you really want to set this coupon available to everyone ?'}'; }); diff --git a/templates/backOffice/default/coupon-read.html b/templates/backOffice/default/coupon-read.html index a4f67c9f2..ff95084ac 100755 --- a/templates/backOffice/default/coupon-read.html +++ b/templates/backOffice/default/coupon-read.html @@ -53,7 +53,7 @@ - {$TOOLTIP} + {$SUMMARY} diff --git a/templates/backOffice/default/coupon-update.html b/templates/backOffice/default/coupon-update.html index 457d27d67..a5a21f698 100755 --- a/templates/backOffice/default/coupon-update.html +++ b/templates/backOffice/default/coupon-update.html @@ -36,8 +36,6 @@ {/javascripts} - {javascripts file='assets/js/coupon.js'} {/javascripts} @@ -62,94 +60,19 @@ }); $(function($){ -// miniBrowser(0, '/test_to_remove/datas_coupon_edit.json'); // Url alowing to get coupon inputs - $.couponManager.urlAjaxAdminCouponDrawInputs = "{$urlAjaxAdminCouponDrawInputs}"; + $.couponManager.urlAjaxSaveConditions = '{$urlAjaxSaveConditions}'; + $.couponManager.urlAjaxDeleteConditions = '{$urlAjaxDeleteConditions}'; + $.couponManager.urlAjaxGetConditionSummaries = '{$urlAjaxGetConditionSummaries}'; + $.couponManager.urlAjaxAdminCouponDrawInputs = '{$urlAjaxAdminCouponDrawInputs}'; + $.couponManager.urlAjaxGetConditionInputFromServiceId = '{$urlAjaxGetConditionInputFromServiceId}'; + $.couponManager.urlAjaxGetConditionInputFromConditionInterface = '{$urlAjaxGetConditionInputFromConditionInterface}'; $.couponManager.intlPleaseRetry = '{intl l='Please retry'}'; - - // Init Conditions - $.couponManager.initConditions = function() { - var conditions = []; - {foreach from=$conditionsObject key=k item=condition} - // Init condition - var condition = {}; - condition['serviceId'] = '{$condition.serviceId nofilter}'; - condition['operators'] = {}; - condition['values'] = {}; - - {foreach from=$condition.validators.setOperators key=input item=operator} - condition['operators']['{$input nofilter}'] = '{$operator nofilter}'; - condition['values']['{$input nofilter}'] = '{$condition.validators.setValues[$input] nofilter}'; - {/foreach} - - // Add condition - conditions.push(condition); - {/foreach} - - return conditions; - }; - - // Save Conditions AJAX - $.couponManager.saveConditionAjax = function() { - $('#condition-add-operators-values').html('
'); - var $url = '{$urlAjaxUpdateConditions}'; - $.ajax({ - type: "POST", - url: $url, - data: {literal}{{/literal}conditions:JSON.stringify($.couponManager.conditionsToSave){literal}}{/literal}, - statusCode: { - 404: function() { - $('#condition-add-operators-values').html( - '{intl l='Please retry'}' - ); - } - } - }).done(function(data) { - $('#condition-list').html(data); - $('#condition-add-operators-values').html(''); - // Set the condition selector - $("#category-condition option").filter(function() { - return $(this).val() == 'thelia.condition.match_for_everyone'; - }).prop('selected', true); - - $.couponManager.onClickUpdateCondition(); - $.couponManager.onClickDeleteCondition(); - }); - }; - - // Reload condition inputs - $.couponManager.loadConditionInputs = function(conditionId, callBack) { - $('#condition-add-operators-values').html('
'); - var url = "{$urlAjaxGetConditionInput}"; - url = url.replace('conditionId', conditionId) - $.ajax({ - url: url, - statusCode: { - 404: function() { - $('#condition-add-operators-values').html( - '{intl l='Please select another condition'}' - ); - } - } - }).done(function(data) { - $('#condition-add-operators-values').html(data); - $.couponManager.conditionToSave.serviceId = conditionId; - if (conditionId == -1) { - // Placeholder can't be saved - $('#condition-save-btn').hide(); - } else { - $('#condition-save-btn').show(); - } - return callBack(); - }); - }; - - // Conditions which will be saved - $.couponManager.conditionsToSave = $.couponManager.initConditions(); + $.couponManager.intlPleaseSelectAnotherCondition = '{intl l='Please select another condition'}'; + $.couponManager.intlDoYouReallyWantToSetCouponAvailableForEveryOne = '{intl l='Do you really want to set this coupon available to everyone ?'}'; $('#condition-save-btn').hide(); - }); {/block} diff --git a/templates/backOffice/default/coupon/condition-input-ajax.html b/templates/backOffice/default/coupon/condition-input-ajax.html index 57dd60f5a..bb2affe41 100755 --- a/templates/backOffice/default/coupon/condition-input-ajax.html +++ b/templates/backOffice/default/coupon/condition-input-ajax.html @@ -1,104 +1,7 @@ -{foreach from=$inputs.inputs key=name item=input} - -
-
- -
-
- {if $input.type == 'select'} - - {else} - - {**} - {/if} -
-
-{/foreach} - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {**} - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {*
*} - {*
*} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {*
Categories list
*} - {*
*} - {*
*} +{$inputsDrawn nofilter} + + \ No newline at end of file diff --git a/templates/backOffice/default/coupon/conditions.html b/templates/backOffice/default/coupon/conditions.html index 6e9906698..1ae577e42 100755 --- a/templates/backOffice/default/coupon/conditions.html +++ b/templates/backOffice/default/coupon/conditions.html @@ -1,17 +1,18 @@ +{* List all condition with their summary *} {foreach from=$conditions item=condition key=i name=conditionsForeach} {if !$smarty.foreach.conditionsForeach.first} {intl l='And'} {/if} - {$condition nofilter} + {$condition.summary nofilter} - + {intl l='Edit'} {if $conditions|count != 1} - + {intl l='Delete'} {/if} diff --git a/templates/backOffice/default/coupon/form.html b/templates/backOffice/default/coupon/form.html index 1fb0438a3..b44fa59e7 100755 --- a/templates/backOffice/default/coupon/form.html +++ b/templates/backOffice/default/coupon/form.html @@ -1,7 +1,8 @@ {$thelia_page_css_file = "assets/bootstrap-editable/css/bootstrap-editable.css"} {include file='includes/notifications.html' message=$general_error} -
-
+ +
+
{form_hidden_fields form=$form} @@ -122,17 +123,6 @@ {form_field form=$form field='amount'} {$couponInputsHtml nofilter} {/form_field} - {*
*} - {**} - {*form_field form=$form field='category'*} - {**} - {*if $error}{$message}{/if}*} - {*/form_field*} - {*
*}
@@ -160,30 +150,33 @@ -
- {if $noConditions} - {include file='includes/notifications.html' message={intl l='Please save your Coupon in oder to affect it some conditions'}} - {else} -
-
- - - - - - - - - - {include file='coupon/conditions.html' conditions=$conditions} - -
- {intl l='Conditions'} -
{intl l='Conditions'}{intl l='Actions'}
-
-
+ +
-
+{if $noConditions} + {include file='includes/notifications.html' message={intl l='Please save your Coupon in oder to affect it some conditions'}} +{else} +
+
+ + + + + + + + + + {include file='coupon/conditions.html' conditions=$conditions} + +
+ {intl l='Conditions'} +
{intl l='Conditions'}{intl l='Actions'}
+
+
+ +
+
-
- {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {**} - {**} - {*
*} - {*
*} - {**} - - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {**} - {**} - {*
*} - {*
*} - - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {**} - {*
*} - {*
*} - {*
*} - {*
*} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {**} - {*
Categories list
*} - {*
*} - {*
*} -
+
-
- {/if} - + +
+{/if} + From 716e26986f9197d86986ec0c13626858dbdb25ff Mon Sep 17 00:00:00 2001 From: gmorel Date: Sun, 5 Jan 2014 01:14:41 +0100 Subject: [PATCH 2/4] Coupon : Condition module cleaning --- .../Implementation/MatchForTotalAmount.php | 13 ------------- .../Condition/Implementation/MatchForXArticles.php | 7 ------- 2 files changed, 20 deletions(-) diff --git a/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php b/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php index 8753a4100..4ffe2ebd4 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForTotalAmount.php @@ -229,27 +229,14 @@ class MatchForTotalAmount extends ConditionAbstract $cleanedCurrencies[$currency->getCode()] = $currency->getSymbol(); } - $name1 = $this->translator->trans( - 'Price', - array(), - 'condition' - ); - $name2 = $this->translator->trans( - 'Currency', - array(), - 'condition' - ); - return array( self::INPUT1 => array( - 'title' => $name1, 'availableOperators' => $this->availableOperators[self::INPUT1], 'availableValues' => '', 'value' => '', 'selectedOperator' => '' ), self::INPUT2 => array( - 'title' => $name2, 'availableOperators' => $this->availableOperators[self::INPUT2], 'availableValues' => $cleanedCurrencies, 'value' => '', diff --git a/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php b/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php index 01e64afc4..2d2df9c14 100644 --- a/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php +++ b/core/lib/Thelia/Condition/Implementation/MatchForXArticles.php @@ -194,15 +194,8 @@ class MatchForXArticles extends ConditionAbstract */ protected function generateInputs() { - $name1 = $this->translator->trans( - 'Quantity', - array(), - 'condition' - ); - return array( self::INPUT1 => array( - 'title' => $name1, 'availableOperators' => $this->availableOperators[self::INPUT1], 'value' => '', 'selectedOperator' => '' From db09e0b2243b744f62ed64cb501a9c01c89e047e Mon Sep 17 00:00:00 2001 From: gmorel Date: Sun, 5 Jan 2014 19:02:19 +0100 Subject: [PATCH 3/4] Coupon : Condition module fix unit tests --- .../Tests/Condition/Implementation/MatchForTotalAmountTest.php | 2 -- .../Tests/Condition/Implementation/MatchForXArticlesTest.php | 1 - 2 files changed, 3 deletions(-) diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php index 4967ba91b..9cb09fdd2 100644 --- a/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php @@ -918,7 +918,6 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase $validators = array( 'inputs' => array( MatchForTotalAmount::INPUT1 => array( - 'title' => 'Price', 'availableOperators' => array( '<' => 'Price', '<=' => 'Price', @@ -931,7 +930,6 @@ class MatchForTotalAmountTest extends \PHPUnit_Framework_TestCase 'selectedOperator' => '' ), MatchForTotalAmount::INPUT2 => array( - 'title' => 'Price', 'availableOperators' => array('==' => 'Price'), 'availableValues' => array( 'EUR' => '€', diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php index 9911bfa33..92aa69564 100644 --- a/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchForXArticlesTest.php @@ -735,7 +735,6 @@ class MatchForXArticlesTest extends \PHPUnit_Framework_TestCase $validators = array( 'inputs' => array( MatchForXArticles::INPUT1 => array( - 'title' => 'Price', 'availableOperators' => array( '<' => 'Price', '<=' => 'Price', From b8c2c2d703b01b2aec868e2abdde53c1fb7f8324 Mon Sep 17 00:00:00 2001 From: gmorel Date: Sun, 5 Jan 2014 20:17:30 +0100 Subject: [PATCH 4/4] Coupon : Condition module : add tooltip on condition select --- core/lib/Thelia/Controller/Admin/CouponController.php | 1 + templates/backOffice/default/assets/js/coupon.js | 4 ++++ templates/backOffice/default/coupon/form.html | 5 +++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 078adb626..275f9b5de 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -611,6 +611,7 @@ class CouponController extends BaseAdminController $condition = array(); $condition['serviceId'] = $availableCondition->getServiceId(); $condition['name'] = $availableCondition->getName(); + $condition['toolTip'] = $availableCondition->getToolTip(); $cleanedConditions[] = $condition; } diff --git a/templates/backOffice/default/assets/js/coupon.js b/templates/backOffice/default/assets/js/coupon.js index 51cf41058..0bf95b41a 100755 --- a/templates/backOffice/default/assets/js/coupon.js +++ b/templates/backOffice/default/assets/js/coupon.js @@ -133,6 +133,10 @@ $(function($){ $.couponManager.onConditionChange = function() { $('#category-condition').on('change', function () { var $this = $(this); + var mainDiv = $('#condition-add-type'); + var optionSelected = $('option:selected', this); + mainDiv.find('.typeToolTip').html(optionSelected.attr('data-description')); + // Only if add mode if (false != $.couponManager.conditionToUpdateIndex) { // Reload condition summaries ajax diff --git a/templates/backOffice/default/coupon/form.html b/templates/backOffice/default/coupon/form.html index b44fa59e7..be00100b1 100755 --- a/templates/backOffice/default/coupon/form.html +++ b/templates/backOffice/default/coupon/form.html @@ -195,11 +195,12 @@
+