diff --git a/controllers/BasketController.php b/controllers/BasketController.php index 9af8129..b5b02ca 100755 --- a/controllers/BasketController.php +++ b/controllers/BasketController.php @@ -137,4 +137,41 @@ ); return $output; } + + + + public function actionAddBonus($bonus){ + $response = \Yii::$app->response; + $response->format = Response::FORMAT_JSON; + /** + * @var Basket $basket + */ + $basket = \Yii::$app->get('basket'); + + $basket->setBonus($bonus); + $result = [ + 'basket' => $basket->getData(), + 'modal' => $this->getModal($basket), + 'cart' => $this->getCart($basket), + 'small' => $this->actionRenderBasket() + ]; + return $result; + } + + public function actionDeleteBonus(){ + $response = \Yii::$app->response; + $response->format = Response::FORMAT_JSON; + /** + * @var Basket $basket + */ + $basket = \Yii::$app->get('basket'); + $basket->removeBonus(); + $result = [ + 'basket' => $basket->getData(), + 'modal' => $this->getModal($basket), + 'cart' => $this->getCart($basket), + 'small' => $this->actionRenderBasket() + ]; + return $result; + } } diff --git a/models/Basket.php b/models/Basket.php index b08f7a6..a388e08 100755 --- a/models/Basket.php +++ b/models/Basket.php @@ -316,5 +316,29 @@ { $this->setData([]); } + + /** + * @param int $bonuses + */ + public function setBonus(int $bonuses){ + $this->session->set('bonus', $bonuses); + } + + /** + * @return mixed|null + */ + public function getBonuses(){ + if ($this->session->has('bonus')){ + return $this->session->get('bonus'); + } + return null; + } + + /** + * + */ + public function removeBonus(){ + $this->session->remove('bonus'); + } } \ No newline at end of file diff --git a/web/js/artbox_basket.js b/web/js/artbox_basket.js index 2deaafe..757af4b 100755 --- a/web/js/artbox_basket.js +++ b/web/js/artbox_basket.js @@ -1,5 +1,5 @@ /// -var ArtboxBasket = (function () { +var ArtboxBasket = /** @class */ (function () { function ArtboxBasket(settings) { if (settings === void 0) { settings = {}; } this._defaults = { @@ -7,7 +7,8 @@ var ArtboxBasket = (function () { url: 'basket', initError: 'Basket cannot be init', modalSelector: '.basket_modal', - cartSelector: '.basket_wrapper' + cartSelector: '.basket_wrapper', + smallSelector: '.small_basket_page' }; this._settings = {}; this._settings = ArtboxBasket.mergeObjects(this._defaults, settings); @@ -46,6 +47,7 @@ var ArtboxBasket = (function () { configurable: true }); ArtboxBasket.prototype.init = function (update_modal, update_cart) { + this.deleteBonus(); $.get(this.getLanguagePath() + this._settings['url'], function (data) { this._items = data.basket; if (update_modal) { @@ -100,6 +102,10 @@ var ArtboxBasket = (function () { var cart = $(this._settings['cartSelector']); cart.html(cart_html); }; + ArtboxBasket.prototype.updateSmall = function (small_html) { + var cart = $(this._settings['smallSelector']); + cart.html(small_html); + }; ArtboxBasket.prototype.hideBasket = function () { $(this._settings['modalSelector']) .animate({ @@ -137,5 +143,23 @@ var ArtboxBasket = (function () { return '/'; } }; + ArtboxBasket.prototype.setBonus = function (bonus) { + return $.post(this.getLanguagePath() + this._settings['url'] + '/add-bonus?bonus=' + bonus, function (data) { + this._items = data.basket; + this.updateModal(data.modal, data.cart, true); + this.updateSmall(data.small); + }.bind(this), 'json').fail(function (xhr, status, error) { + console.error(error); + }); + }; + ArtboxBasket.prototype.deleteBonus = function () { + return $.post(this.getLanguagePath() + this._settings['url'] + '/delete-bonus', function (data) { + this._items = data.basket; + this.updateModal(data.modal, data.cart, true); + this.updateSmall(data.small); + }.bind(this), 'json').fail(function (xhr, status, error) { + console.error(error); + }); + }; return ArtboxBasket; }()); diff --git a/web/js/artbox_basket.ts b/web/js/artbox_basket.ts index 16f567e..328ac7b 100755 --- a/web/js/artbox_basket.ts +++ b/web/js/artbox_basket.ts @@ -7,7 +7,8 @@ class ArtboxBasket { url: 'basket', initError: 'Basket cannot be init', modalSelector: '.basket_modal', - cartSelector: '.basket_wrapper' + cartSelector: '.basket_wrapper', + smallSelector: '.small_basket_page' }; private _settings: Object = {}; @@ -41,6 +42,7 @@ class ArtboxBasket { } public init(update_modal, update_cart) { + this.deleteBonus(); $.get(this.getLanguagePath() + this._settings['url'], function (data) { this._items = data.basket; if (update_modal) { @@ -100,6 +102,11 @@ class ArtboxBasket { cart.html(cart_html); } + public updateSmall(small_html) { + let cart = $(this._settings['smallSelector']); + cart.html(small_html); + } + public hideBasket() { $(this._settings['modalSelector']) .animate( @@ -137,4 +144,24 @@ class ArtboxBasket { return '/'; } } + + public setBonus(bonus) { + return $.post(this.getLanguagePath() + this._settings['url'] + '/add-bonus?bonus=' + bonus, function (data) { + this._items = data.basket; + this.updateModal(data.modal, data.cart, true); + this.updateSmall(data.small); + }.bind(this), 'json').fail(function (xhr, status, error) { + console.error(error); + }); + } + + public deleteBonus(){ + return $.post(this.getLanguagePath() + this._settings['url'] + '/delete-bonus', function (data) { + this._items = data.basket; + this.updateModal(data.modal, data.cart, true); + this.updateSmall(data.small); + }.bind(this), 'json').fail(function (xhr, status, error) { + console.error(error); + }); + } } \ No newline at end of file -- libgit2 0.21.4