From 964355e60d9645952215910246767143322e2eba Mon Sep 17 00:00:00 2001 From: gmorel Date: Thu, 12 Sep 2013 21:34:50 +0200 Subject: [PATCH] Working : Coupon - update loop --- .../Thelia/Constraint/ConstraintFactory.php | 12 ++ core/lib/Thelia/Core/Template/Loop/Coupon.php | 28 ++++- templates/admin/default/assets/js/coupon.js | 9 +- templates/admin/default/coupon-read.html | 49 ++++++-- templates/admin/default/coupon-update.html | 10 +- templates/admin/default/coupon/form.html | 1 + .../casperjs/exe/31_coupons_rule.js | 110 ++++++++++++++++++ 7 files changed, 206 insertions(+), 13 deletions(-) diff --git a/core/lib/Thelia/Constraint/ConstraintFactory.php b/core/lib/Thelia/Constraint/ConstraintFactory.php index e96509172..e13d1d2aa 100644 --- a/core/lib/Thelia/Constraint/ConstraintFactory.php +++ b/core/lib/Thelia/Constraint/ConstraintFactory.php @@ -25,6 +25,7 @@ namespace Thelia\Constraint; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Thelia\Constraint\Rule\AvailableForEveryoneManager; use Thelia\Constraint\Rule\AvailableForTotalAmountManager; use Thelia\Constraint\Rule\CouponRuleInterface; use Thelia\Constraint\Rule\SerializableRule; @@ -74,11 +75,22 @@ class ConstraintFactory */ public function serializeCouponRuleCollection(CouponRuleCollection $collection) { + if ($collection->isEmpty()) { + /** @var CouponRuleInterface $ruleNoCondition */ + $ruleNoCondition = $this->container->get( + 'thelia.constraint.rule.available_for_everyone' + ); + $collection->add($ruleNoCondition); + } $serializableRules = array(); $rules = $collection->getRules(); if ($rules !== null) { /** @var $rule CouponRuleInterface */ foreach ($rules as $rule) { + // Remove all rule if the "no condition" rule is found +// if ($rule->getServiceId() == 'thelia.constraint.rule.available_for_everyone') { +// return base64_encode(json_encode(array($rule->getSerializableRule()))); +// } $serializableRules[] = $rule->getSerializableRule(); } } diff --git a/core/lib/Thelia/Core/Template/Loop/Coupon.php b/core/lib/Thelia/Core/Template/Loop/Coupon.php index 2fb49e8b9..9cef73e37 100755 --- a/core/lib/Thelia/Core/Template/Loop/Coupon.php +++ b/core/lib/Thelia/Core/Template/Loop/Coupon.php @@ -34,6 +34,7 @@ use Thelia\Core\Template\Element\LoopResultRow; use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\Argument; +use Thelia\Coupon\Type\CouponInterface; use Thelia\Model\CouponQuery; use Thelia\Model\Coupon as MCoupon; use Thelia\Type; @@ -98,6 +99,27 @@ class Coupon extends BaseI18nLoop $coupon->getSerializedRules() ); + /** @var CouponInterface $couponManager */ + $couponManager = $this->container->get($coupon->getType()); + $couponManager->set( + $this->container->get('thelia.adapter'), + $coupon->getCode(), + $coupon->getTitle(), + $coupon->getShortDescription(), + $coupon->getDescription(), + $coupon->getAmount(), + $coupon->getIsCumulative(), + $coupon->getIsRemovingPostage(), + $coupon->getIsAvailableOnSpecialOffers(), + $coupon->getIsEnabled(), + $coupon->getMaxUsage(), + $coupon->getExpirationDate() + ); + + $now = time(); + $datediff = $coupon->getExpirationDate()->getTimestamp() - $now; + $daysLeftBeforeExpiration = floor($datediff/(60*60*24)); + $cleanedRules = array(); /** @var CouponRuleInterface $rule */ foreach ($rules->getRules() as $key => $rule) { @@ -114,9 +136,13 @@ class Coupon extends BaseI18nLoop ->set("USAGE_LEFT", $coupon->getMaxUsage()) ->set("IS_CUMULATIVE", $coupon->getIsCumulative()) ->set("IS_REMOVING_POSTAGE", $coupon->getIsRemovingPostage()) + ->set("IS_AVAILABLE_ON_SPECIAL_OFFERS", $coupon->getIsAvailableOnSpecialOffers()) ->set("IS_ENABLED", $coupon->getIsEnabled()) ->set("AMOUNT", $coupon->getAmount()) - ->set("APPLICATION_CONDITIONS", $cleanedRules); + ->set("APPLICATION_CONDITIONS", $cleanedRules) + ->set("TOOLTIP", $couponManager->getToolTip()) + ->set("DAY_LEFT_BEFORE_EXPIRATION", $daysLeftBeforeExpiration) + ->set("SERVICE_ID", $couponManager->getServiceId()); $loopResult->addRow($loopResultRow); } diff --git a/templates/admin/default/assets/js/coupon.js b/templates/admin/default/assets/js/coupon.js index 05c0f6cd2..d07217061 100644 --- a/templates/admin/default/assets/js/coupon.js +++ b/templates/admin/default/assets/js/coupon.js @@ -78,7 +78,14 @@ $(function($){ // Save rules on click couponManager.onClickSaveRule = function() { $('#constraint-save-btn').on('click', function () { - couponManager.createOrUpdateRuleAjax(); + if($('#category-rule').val() == 'thelia.constraint.rule.available_for_everyone') { + // @todo translate + modal + var r= confirm("Do you really want to set this coupon available to everyone ?"); + if (r == true) { + couponManager.createOrUpdateRuleAjax(); + } + } + }); }; couponManager.onClickSaveRule(); diff --git a/templates/admin/default/coupon-read.html b/templates/admin/default/coupon-read.html index d0a86ac72..985bf5499 100755 --- a/templates/admin/default/coupon-read.html +++ b/templates/admin/default/coupon-read.html @@ -16,7 +16,6 @@

{intl l='Coupon : '}{$CODE}

-
@@ -31,9 +30,27 @@ {intl l='Title'} {$TITLE} + + + {if $IS_ENABLED} + + {intl l="Is enabled"} + + {else} + + {intl l="Is disabled"} + + {/if} + + + + + {$TOOLTIP} + + {intl l='Expiration date'} - {$EXPIRATION_DATE} + {$EXPIRATION_DATE} ({$DAY_LEFT_BEFORE_EXPIRATION} {intl l="days left"}) {intl l='Usage left'} @@ -43,7 +60,7 @@ {$USAGE_LEFT} {else} - + 0 {/if} @@ -62,7 +79,7 @@ {intl l="May be cumulative"} {else} - + {intl l="Can't be cumulative"} {/if} @@ -71,13 +88,26 @@ {if $IS_REMOVING_POSTAGE} - - {intl l="Will remove postage"} - + + {intl l="Will remove postage"} + {else} - {intl l="Won't remove postage"} - + {intl l="Won't remove postage"} + + {/if} + + + + + {if $IS_AVAILABLE_ON_SPECIAL_OFFERS} + + {intl l="Will be available on special offers"} + + {else} + + {intl l="Won't be available on special offers"} + {/if} @@ -102,7 +132,6 @@ {intl l='Actions'} {intl l='Edit'} - {intl l='Enabled'} diff --git a/templates/admin/default/coupon-update.html b/templates/admin/default/coupon-update.html index aa838f2ad..e1621c3f8 100755 --- a/templates/admin/default/coupon-update.html +++ b/templates/admin/default/coupon-update.html @@ -112,13 +112,21 @@ }).done(function(data) { $('#constraint-add-operators-values').html(data); couponManager.ruleToSave.serviceId = ruleId; - + if (ruleId == -1) { + // Placeholder can't be saved + $('#constraint-save-btn').hide(); + } else { + $('#constraint-save-btn').show(); + } return callBack(); }); }; // Rules which will be saved couponManager.rulesToSave = couponManager.initRules(); + + $('#constraint-save-btn').hide(); + }); {/block} diff --git a/templates/admin/default/coupon/form.html b/templates/admin/default/coupon/form.html index 917a5ff19..85b9438ef 100644 --- a/templates/admin/default/coupon/form.html +++ b/templates/admin/default/coupon/form.html @@ -202,6 +202,7 @@