diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index f5adc68aa..613694ab7 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -499,6 +499,10 @@ Thelia\Controller\Admin\CouponController::readAction \d+ + + Thelia\Controller\Admin\CouponController::getBackOfficeInputsAction + .* + Thelia\Controller\Admin\CouponController::getConditionInputAction .* diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index b382dbb30..f1612a164 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -31,6 +31,7 @@ use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; +use Thelia\Coupon\CouponFactory; use Thelia\Coupon\CouponManager; use Thelia\Condition\ConditionCollection; use Thelia\Coupon\Type\CouponInterface; @@ -41,6 +42,7 @@ use Thelia\Model\Coupon; use Thelia\Model\CouponQuery; use Thelia\Model\Lang; use Thelia\Tools\I18n; +use Thelia\Tools\Rest\ResponseRest; /** * Created by JetBrains PhpStorm. @@ -152,6 +154,11 @@ class CouponController extends BaseAdminController $args['dateFormat'] = $this->getSession()->getLang()->getDateFormat(); $args['availableCoupons'] = $this->getAvailableCoupons(); + $args['urlAjaxAdminCouponDrawInputs'] = $this->getRoute( + 'admin.coupon.draw.inputs', + array('couponServiceId' => 'couponServiceId'), + Router::ABSOLUTE_URL + ); $args['formAction'] = 'admin/coupon/create'; return $this->render( @@ -181,6 +188,9 @@ class CouponController extends BaseAdminController return $this->pageNotFound(); } + /** @var CouponFactory $couponFactory */ + $couponFactory = $this->container->get('thelia.coupon.factory'); + $couponManager = $couponFactory->buildCouponFromModel($coupon); // Parameters given to the template $args = array(); @@ -233,6 +243,7 @@ class CouponController extends BaseAdminController 'serviceId' => $condition->getServiceId(), 'name' => $condition->getName(), 'tooltip' => $condition->getToolTip(), + 'tooltip' => $condition->getToolTip(), 'validators' => $condition->getValidators() ); } @@ -247,6 +258,12 @@ class CouponController extends BaseAdminController } $args['couponCode'] = $coupon->getCode(); $args['availableCoupons'] = $this->getAvailableCoupons(); + $args['couponInputsHtml'] = $couponManager->drawBackOfficeInputs(); + $args['urlAjaxAdminCouponDrawInputs'] = $this->getRoute( + 'admin.coupon.draw.inputs', + array('couponServiceId' => 'couponServiceId'), + Router::ABSOLUTE_URL + ); $args['availableConditions'] = $this->getAvailableConditions(); $args['urlAjaxGetConditionInput'] = $this->getRoute( 'admin.coupon.condition.input', @@ -461,6 +478,9 @@ class CouponController extends BaseAdminController $couponEvent = new CouponCreateOrUpdateEvent( $data['code'], $data['title'], $data['amount'], $data['type'], $data['shortDescription'], $data['description'], $data['isEnabled'], \DateTime::createFromFormat('Y-m-d', $data['expirationDate']), $data['isAvailableOnSpecialOffers'], $data['isCumulative'], $data['isRemovingPostage'], $data['maxUsage'], $data['locale'] ); + $couponQuery = new CouponQuery(); + $coupon = $couponQuery->findOneByCode($data['code']); + $couponEvent->setCouponModel($coupon); // Dispatch Event to the Action $this->dispatch( @@ -524,7 +544,6 @@ class CouponController extends BaseAdminController $condition = array(); $condition['serviceId'] = $availableCondition->getServiceId(); $condition['name'] = $availableCondition->getName(); - // $condition['toolTip'] = $availableCondition->getToolTip(); $cleanedConditions[] = $condition; } @@ -548,6 +567,7 @@ class CouponController extends BaseAdminController $condition['serviceId'] = $availableCoupon->getServiceId(); $condition['name'] = $availableCoupon->getName(); $condition['toolTip'] = $availableCoupon->getToolTip(); + $condition['inputName'] = $availableCoupon->getInputName(); $cleanedCoupons[] = $condition; } @@ -572,4 +592,26 @@ class CouponController extends BaseAdminController return $cleanedConditions; } + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon effect + * + * @param string $couponServiceId Coupon service id + * + * @return ResponseRest + */ + public function getBackOfficeInputsAction($couponServiceId) + { + /** @var CouponInterface $coupon */ + $coupon = $this->container->get($couponServiceId); + + if (!$coupon instanceof CouponInterface) { + $this->pageNotFound(); + } + + $response = new ResponseRest($coupon->drawBackOfficeInputs()); + + return $response; + } + } diff --git a/core/lib/Thelia/Coupon/FacadeInterface.php b/core/lib/Thelia/Coupon/FacadeInterface.php index 4c264430c..1d3aabc60 100755 --- a/core/lib/Thelia/Coupon/FacadeInterface.php +++ b/core/lib/Thelia/Coupon/FacadeInterface.php @@ -167,4 +167,11 @@ interface FacadeInterface */ public function getAvailableCurrencies(); + /** + * Return the event dispatcher, + * + * @return \Symfony\Component\EventDispatcher\EventDispatcher + */ + public function getDispatcher(); + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 98c258c27..34dcb337c 100755 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -307,4 +307,25 @@ abstract class CouponAbstract implements CouponInterface return $this->conditionEvaluator->isMatching($this->conditions); } + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon effect + * + * @return string HTML string + */ + public function drawBackOfficeInputs() + { + $label = $this->getInputName(); + $value = $this->amount; + + $html = ' +
+ + +
+ '; + + return $html; + } + } diff --git a/core/lib/Thelia/Coupon/Type/CouponInterface.php b/core/lib/Thelia/Coupon/Type/CouponInterface.php index 63c7e4267..6ebeca748 100755 --- a/core/lib/Thelia/Coupon/Type/CouponInterface.php +++ b/core/lib/Thelia/Coupon/Type/CouponInterface.php @@ -46,6 +46,13 @@ interface CouponInterface */ public function getName(); + /** + * Get I18n amount input name + * + * @return string + */ + public function getInputName(); + /** * Get I18n tooltip * @@ -215,4 +222,12 @@ interface CouponInterface */ public function isMatching(); + /** + * Draw the input displayed in the BackOffice + * allowing Admin to set its Coupon effect + * + * @return string HTML string + */ + public function drawBackOfficeInputs(); + } diff --git a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php index 1609ffc3e..db8e52ee0 100755 --- a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php @@ -100,7 +100,19 @@ class RemoveXAmount extends CouponAbstract { return $this->facade ->getTranslator() - ->trans('Remove X amount to total cart', array(), 'constraint'); + ->trans('Remove X amount to total cart', array(), 'coupon'); + } + + /** + * Get I18n amount input name + * + * @return string + */ + public function getInputName() + { + return $this->facade + ->getTranslator() + ->trans('Amount removed from the cart', array(), 'coupon'); } /** @@ -115,7 +127,7 @@ class RemoveXAmount extends CouponAbstract ->trans( 'This coupon will remove the entered amount to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.', array(), - 'constraint' + 'coupon' ); return $toolTip; diff --git a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php index 931ddad67..b8bc5babb 100755 --- a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php @@ -122,7 +122,19 @@ class RemoveXPercent extends CouponAbstract { return $this->facade ->getTranslator() - ->trans('Remove X percent to total cart', array(), 'constraint'); + ->trans('Remove X percent to total cart', array(), 'coupon'); + } + + /** + * Get I18n amount input name + * + * @return string + */ + public function getInputName() + { + return $this->facade + ->getTranslator() + ->trans('Percentage removed from the cart', array(), 'coupon'); } /** @@ -137,7 +149,7 @@ class RemoveXPercent extends CouponAbstract ->trans( 'This coupon will remove the entered percentage to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.', array(), - 'constraint' + 'coupon' ); return $toolTip; diff --git a/templates/backOffice/default/assets/js/coupon.js b/templates/backOffice/default/assets/js/coupon.js index 2c412765a..3f31feaf0 100755 --- a/templates/backOffice/default/assets/js/coupon.js +++ b/templates/backOffice/default/assets/js/coupon.js @@ -127,13 +127,42 @@ $(function($){ }; $.couponManager.onClickUpdateCondition(); + $.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); + }); + }; + // Reload effect inputs when changing effect $.couponManager.onEffectChange = function() { - var optionSelected = $("option:selected", this); - $('#effectToolTip').html(optionSelected.attr("data-description")); - $('#effect').on('change', function () { - var optionSelected = $("option:selected", this); - $('#effectToolTip').html(optionSelected.attr("data-description")); + var mainDiv = $('#coupon-type'); + var optionSelected = mainDiv.find('#type option:selected'); + mainDiv.find('.typeToolTip').html(optionSelected.attr('data-description')); + + mainDiv.find('#type').on('change', function () { + var optionSelected = $('option:selected', this); + $.couponManager.displayEfffect(optionSelected); + }); }; $.couponManager.onEffectChange(); diff --git a/templates/backOffice/default/coupon-create.html b/templates/backOffice/default/coupon-create.html index 691067be2..6d08d7197 100755 --- a/templates/backOffice/default/coupon-create.html +++ b/templates/backOffice/default/coupon-create.html @@ -59,6 +59,11 @@ filemanager_title:"{intl l='Files manager'}" , external_plugins: { "filemanager" : "{url file='/tinymce/plugins/filemanager/plugin.min.js'}"} }); + + + // Url alowing to get coupon inputs + $.couponManager.urlAjaxAdminCouponDrawInputs = "{$urlAjaxAdminCouponDrawInputs}"; + $.couponManager.intlPleaseRetry = '{intl l='Please retry'}'; {/block} diff --git a/templates/backOffice/default/coupon-update.html b/templates/backOffice/default/coupon-update.html index 68cfb6a90..457d27d67 100755 --- a/templates/backOffice/default/coupon-update.html +++ b/templates/backOffice/default/coupon-update.html @@ -36,6 +36,8 @@ {/javascripts} + {javascripts file='assets/js/coupon.js'} {/javascripts} @@ -62,6 +64,10 @@ $(function($){ // miniBrowser(0, '/test_to_remove/datas_coupon_edit.json'); + // Url alowing to get coupon inputs + $.couponManager.urlAjaxAdminCouponDrawInputs = "{$urlAjaxAdminCouponDrawInputs}"; + $.couponManager.intlPleaseRetry = '{intl l='Please retry'}'; + // Init Conditions $.couponManager.initConditions = function() { var conditions = []; diff --git a/templates/backOffice/default/coupon/form.html b/templates/backOffice/default/coupon/form.html index d2642657f..1fb0438a3 100755 --- a/templates/backOffice/default/coupon/form.html +++ b/templates/backOffice/default/coupon/form.html @@ -99,32 +99,28 @@
-
+
{form_field form=$form field='type'}
{if $error}{$message}{/if} - {$availableCoupons.0.toolTip} + {$availableCoupons.0.toolTip}
{/form_field}
-
+
{form_field form=$form field='amount'} -
- - - {if $error}{$message}{/if} -
+ {$couponInputsHtml nofilter} {/form_field} {*
*} {**} @@ -208,7 +204,7 @@