Commit 1d32a040 by semenov

1

parent 732d2e6c
......@@ -8,16 +8,57 @@
{
sx.createNamespace('classes.shop', sx);
sx.classes.shop._Cart = sx.classes.Component.extend({
/**
* Позиция корзины
*/
sx.classes.shop._Basket = sx.classes.Component.extend({
construct: function (product_id, quantity, opts)
{
var self = this;
opts = opts || {};
this.set('product_id', product_id);
this.set('quantity', quantity);
//this.parent.construct(opts);
this.applyParentMethod(sx.classes.Component, 'construct', [opts]); // TODO: make a workaround for magic parent calling
},
_init: function()
{},
{
},
_onDomReady: function()
{},
});
_onWindowReady: function()
{}
sx.classes.shop.Basket = sx.classes.shop._Basket.extend({});
/**
* Объект корзины
*/
sx.classes.shop._Cart = sx.classes.Component.extend({
_init: function()
{
this.ajax = sx.ajax.preparePostQuery(this.get('backend'));
},
addProduct: function(product_id, quantity)
{
this.ajax.setData({
'product_id' : Number(product_id),
'quantity' : Number(quantity),
});
this.ajax.execute();
},
update: function()
{},
});
sx.classes.shop.Cart = sx.classes.shop._Cart.extend({});
......
......@@ -12,14 +12,8 @@
_init: function()
{
this.Cart = new sx.classes.shop.Cart();
this.Cart = new sx.classes.shop.Cart(this.get('cart'));
},
_onDomReady: function()
{},
_onWindowReady: function()
{}
});
sx.classes.shop.App = sx.classes.shop._App.extend({});
......
<?php
/**
* @author Semenov Alexander <semenov@skeeks.com>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 02.04.2015
*/
namespace skeeks\modules\cms\shop\components;
use skeeks\cms\helpers\UrlHelper;
use skeeks\modules\cms\shop\models\ShopFuser;
use yii\helpers\ArrayHelper;
/**
* Class Shop
* @package skeeks\modules\cms\shop\components
*/
class Cart extends \skeeks\cms\base\Component
{
/**
* @var ShopFuser
*/
public $shopFuser = null;
public $sessionFuserName = 'sx_fuser';
public function init()
{
parent::init();
if (\Yii::$app->user->isGuest)
{
//Проверка сессии
if (\Yii::$app->getSession()->offsetExists($this->sessionFuserName))
{
$fuserId = \Yii::$app->getSession()->get($this->sessionFuserName);
$shopFuser = ShopFuser::find()->where(['id' => $fuserId])->one();
//Поиск юзера
if ($shopFuser)
{
$this->shopFuser = $shopFuser;
}
}
} else
{
$this->shopFuser = ShopFuser::fetchByUser(\Yii::$app->cms->getAuthUser());
}
}
/**
* @return array|\yii\db\ActiveQuery
*/
public function getShopBaskets()
{
if (!$this->shopFuser)
{
return [];
}
return $this->shopFuser->getShopBaskets();
}
/**
* Количество позиций в коризне
*
* @return int
*/
public function countBaskets()
{
return count($this->getShopBaskets());
}
public function cost()
{
$money = \Yii::$app->money->newMoney();
if (!$this->countBaskets())
{
return $money;
}
}
}
\ No newline at end of file
......@@ -6,6 +6,8 @@
* @date 02.04.2015
*/
namespace skeeks\modules\cms\shop\components\shop;
use skeeks\cms\helpers\UrlHelper;
use skeeks\modules\cms\shop\components\Cart;
use yii\helpers\ArrayHelper;
/**
......@@ -20,6 +22,16 @@ class Shop extends \skeeks\cms\base\Component
public $email = 'admin@skeeks.com';
/**
* @var Cart
*/
public $cart = null;
public function init()
{
parent::init();
$this->cart = new Cart();
}
/**
* Можно задать название и описание компонента
* @return array
*/
......@@ -31,7 +43,6 @@ class Shop extends \skeeks\cms\base\Component
];
}
public function rules()
{
return ArrayHelper::merge(parent::rules(), [
......@@ -45,4 +56,23 @@ class Shop extends \skeeks\cms\base\Component
'email' => 'Email отдела продаж',
]);
}
/**
* Опции для js.
*
* @return array
*/
public function getShopClientOptions()
{
return [
'cart' =>
[
'backend' => UrlHelper::construct('shop/basket/add-product')->toString()
]
];
}
}
\ No newline at end of file
<?php
/**
* @author Semenov Alexander <semenov@skeeks.com>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 03.04.2015
*/
namespace skeeks\modules\cms\shop\controllers;
use skeeks\cms\base\Controller;
use skeeks\cms\helpers\RequestResponse;
/**
* Class ShopController
* @package skeeks\modules\cms\shop\controllers
*/
class BasketController extends Controller
{
public function actionAddProduct()
{
$rr = new RequestResponse();
if ($rr->isRequestAjaxPost())
{
$product_id = \Yii::$app->request->get('product_id');
$quantity = \Yii::$app->request->get('quantity');
return (array) $rr;
} else
{
return $this->goBack();
}
}
}
\ No newline at end of file
<?php
/**
* @author Semenov Alexander <semenov@skeeks.com>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 03.04.2015
*/
namespace skeeks\modules\cms\shop\controllers;
use skeeks\cms\base\Controller;
/**
* Class ShopController
* @package skeeks\modules\cms\shop\controllers
*/
class ShopController extends Controller
{
public function actionRussiaAll()
{
$this->actionCounties()->actionRussiaRegion()->actionRussiaLocality();
$this->stdoutN("ready");
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ namespace skeeks\modules\cms\shop\models;
use skeeks\cms\models\behaviors\HasStatus;
use skeeks\cms\models\behaviors\HasStatusBoolean;
use skeeks\cms\models\Core;
use skeeks\modules\cms\catalog\models\Product;
use \Yii;
use yii\helpers\ArrayHelper;
......@@ -133,6 +134,23 @@ class ShopFuser extends Core
}
/**
* Стоимость этой позиции
* @return \skeeks\modules\cms\money\Money
*/
public function cost()
{
/**
* @var Product $product
*/
$product = $this->getProduct()->one();
$money = $product->cost();
$money = $money->multiply((int) $this->quantity);
return $money;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProduct()
......
......@@ -9,6 +9,7 @@ namespace skeeks\modules\cms\shop\models;
use skeeks\cms\models\behaviors\HasStatus;
use skeeks\cms\models\behaviors\HasStatusBoolean;
use skeeks\cms\models\Core;
use skeeks\cms\models\User;
use \Yii;
use yii\helpers\ArrayHelper;
......@@ -106,4 +107,42 @@ class ShopFuser extends Core
}
/**
* @param User $user
* @return static|null
*/
static public function fetchByUser(User $user)
{
return static::find()->where(['user_id' => $user->id])->one();
}
/**
* @param User $user
* @return static
*/
static public function getByUser(User $user)
{
$fuser = static::find()->where(['user_id' => $user->id])->one();
if (!$fuser)
{
$fuser = new static([
'user_id' => $user->id
]);
$fuser->save();
}
return $fuser;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getShopBaskets()
{
return $this->hasMany(ShopBasket::className(), ['fuser_id' => 'id']);
}
}
\ No newline at end of file
......@@ -7,21 +7,30 @@
*/
namespace skeeks\modules\cms\shop\widgets\cart;
use skeeks\cms\widgets\base\hasTemplate\WidgetHasTemplate;
use skeeks\modules\cms\shop\assets\ShopAsset;
use yii\helpers\Json;
/**
* Class Products
* @package skeeks\modules\cms\shop\widgets\cart
*/
class CartWidget extends WidgetHasTemplate
{
static public $isRegisteredAssets = false;
public $allowRegisterAsset = true;
public function rules()
{
return ArrayHelper::merge(parent::rules(), [
[['allowRegisterAsset'], 'integer']
]);
}
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'allowRegisterAsset' => 'Подключить стандартные скрипты'
]);
}
......@@ -34,7 +43,24 @@ class CartWidget extends WidgetHasTemplate
{
parent::bind();
if (static::$isRegisteredAssets === false && $this->allowRegisterAsset)
{
ShopAsset::register($this->getView());
$options = \Yii::$app->shop->getShopClientOptions();
$options = Json::encode($options);
$this->getView()->registerJs(<<<JS
(function(sx, $, _)
{
sx.Shop = new sx.classes.shop.App($options);
})(sx, sx.$, sx._);
JS
);
static::$isRegisteredAssets = true;
}
return $this;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment