diff --git a/frontend/assets/AppAsset.php b/frontend/assets/AppAsset.php index f7e2144..6445782 100755 --- a/frontend/assets/AppAsset.php +++ b/frontend/assets/AppAsset.php @@ -30,6 +30,7 @@ 'js/script.js', ]; public $depends = [ + 'hiqdev\assets\pnotify\PNotifyAsset', 'yii\web\YiiAsset', 'rmrevin\yii\fontawesome\AssetBundle', 'yii\bootstrap\BootstrapPluginAsset', diff --git a/frontend/controllers/AccountController.php b/frontend/controllers/AccountController.php index 1a035e5..afba717 100644 --- a/frontend/controllers/AccountController.php +++ b/frontend/controllers/AccountController.php @@ -5,9 +5,13 @@ use artbox\order\models\Customer; use artbox\order\models\Order; use artbox\order\models\PasswordForm; + use artbox\order\models\Wishlist; + use yii\data\ActiveDataProvider; + use yii\db\ActiveQuery; use yii\web\Controller; use yii\web\ForbiddenHttpException; use yii\web\NotFoundHttpException; + use yii\web\Response; /** * Class AccountController @@ -82,6 +86,43 @@ ); } + public function actionWishlist() + { + /** + * @var Customer $user + */ + $user = \Yii::$app->user->identity; + $query = $user->getWishVariants() + ->with( + [ + 'lang', + 'product' => function (ActiveQuery $query) { + $query->with( + [ + 'lang', + 'image', + ] + ); + }, + ] + ); + $dataProvider = new ActiveDataProvider( + [ + 'query' => $query, + 'pagination' => [ + 'pageSize' => 8, + ], + ] + ); + + return $this->render( + 'wishlist', + [ + 'dataProvider' => $dataProvider, + ] + ); + } + public function actionChangePassword() { /** @@ -130,7 +171,7 @@ * @var Customer $model */ $model = \Yii::$app->user->identity; - + if ($model->load(\Yii::$app->request->post())) { // VarDumper::dump($model, 10, 1);die(); $model->markAttributeDirty('birthday'); @@ -146,4 +187,35 @@ ] ); } + + public function actionWishlistDelete() + { + \Yii::$app->response->format = Response::FORMAT_JSON; + if (\Yii::$app->request->isPost) { + $model = Wishlist::find() + ->where( + [ + 'user_id' => \Yii::$app->request->post('user'), + ] + ) + ->andWhere( + [ + 'variant_id' => \Yii::$app->request->post('variant'), + + ] + ) + ->one(); + if (!empty($model) && $model->delete()) { + return [ + 'success' => true, + 'message' => 'Товар удален из избранного', + ]; + } + + return [ + 'success' => false, + 'message' => 'Ошибка', + ]; + } + } } \ No newline at end of file diff --git a/frontend/controllers/ProductController.php b/frontend/controllers/ProductController.php index 4b38743..47397cf 100755 --- a/frontend/controllers/ProductController.php +++ b/frontend/controllers/ProductController.php @@ -3,10 +3,14 @@ use artbox\catalog\models\Product; use artbox\core\components\SeoComponent; + use artbox\order\models\Wishlist; use yii\db\ActiveQuery; + use yii\helpers\Html; + use yii\helpers\Url; use yii\web\Controller; use yii\web\NotFoundHttpException; use Yii; + use yii\web\Response; /** * Class ProductController @@ -77,7 +81,7 @@ ) ->where([ 'id' => $id ]) ->one(); - if (!empty( $model )) { + if (!empty($model)) { if ($model->lang->alias_id !== $seo->aliasId) { throw new NotFoundHttpException('Wrong language'); } @@ -86,4 +90,114 @@ throw new NotFoundHttpException('Model not found'); } } + + public function actionWishlistRm() + { + \Yii::$app->response->format = Response::FORMAT_JSON; + if (\Yii::$app->request->isPost) { + $model = Wishlist::find() + ->where( + [ + 'user_id' => \Yii::$app->request->post('user'), + ] + ) + ->andWhere( + [ + 'variant_id' => \Yii::$app->request->post('variant'), + + ] + ) + ->one(); + if (!empty($model) && $model->delete()) { + return [ + 'button' => Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart-o' ]), + [ + 'title' => 'Добавить в избранное', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + 'product' => \Yii::$app->request->post('product'), + 'variant' => \Yii::$app->request->post('variant'), + 'user' => \Yii::$app->request->post('user'), + 'url' => Url::to([ 'product/wishlist-add' ]), + ], + 'class' => 'wishlist-add btn btn-success pull-right', + ] + ), + 'message' => 'Товар убран из избранного', + ]; + } + + return [ + 'button' => Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart' ]), + [ + 'title' => 'Убрать из избранного', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + 'product' => \Yii::$app->request->post('product'), + 'variant' => \Yii::$app->request->post('variant'), + 'user' => \Yii::$app->request->post('user'), + 'url' => Url::to([ 'product/wishlist-rm' ]), + ], + 'class' => 'wishlist-rm btn btn-success pull-right', + ] + ), + 'message' => 'Товар не найден', + ]; + } + } + + public function actionWishlistAdd() + { + \Yii::$app->response->format = Response::FORMAT_JSON; + if (\Yii::$app->request->isPost) { + $model = new Wishlist(); + $model->user_id = \Yii::$app->request->post('user'); + $model->variant_id = \Yii::$app->request->post('variant'); + $model->product_id = \Yii::$app->request->post('product'); + + if ($model->save()) { + return [ + 'button' => Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart' ]), + [ + 'title' => 'Убрать из избранного', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + 'product' => \Yii::$app->request->post('product'), + 'variant' => \Yii::$app->request->post('variant'), + 'user' => \Yii::$app->request->post('user'), + 'url' => Url::to([ 'product/wishlist-rm' ]), + ], + 'class' => 'wishlist-rm btn btn-success pull-right', + ] + ), + 'message' => 'Товар добавлен в избранное', + ]; + } + + return [ + 'button' => Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart-o' ]), + [ + 'title' => 'Добавить в избранное', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + 'product' => \Yii::$app->request->post('product'), + 'variant' => \Yii::$app->request->post('variant'), + 'user' => \Yii::$app->request->post('user'), + 'url' => Url::to([ 'product/wishlist-add' ]), + ], + 'class' => 'wishlist-add btn btn-success pull-right', + ] + ), + 'message' => 'Товар добавить не вышло', + ]; + } + } } \ No newline at end of file diff --git a/frontend/views/account/_product.php b/frontend/views/account/_product.php new file mode 100644 index 0000000..548e6ac --- /dev/null +++ b/frontend/views/account/_product.php @@ -0,0 +1,97 @@ + + +
+
+ + +
+

product->lang->title ?>

+

Цена: price ?> грн.

+

+ canBuy()) { + echo Html::a( + Html::tag( + 'i', + '', + [ + 'class' => 'fa fa-shopping-cart', + ] + ) . \Yii::t('app', 'В корзину'), + '#', + [ + 'class' => 'btn btn-template-main add-to-basket', + 'data-id' => $model->id, + ] + ); + } else { + echo Html::a( + \Yii::t('app', 'Нет в наличии'), + '#', + [ + 'class' => 'btn btn-info disabled', + 'data-id' => $model->id, + ] + ); + } + ?> +

+
+ +
+ +
diff --git a/frontend/views/account/wishlist.php b/frontend/views/account/wishlist.php index 2a2ea0d..c30fd87 100644 --- a/frontend/views/account/wishlist.php +++ b/frontend/views/account/wishlist.php @@ -1,11 +1,16 @@ params[ 'breadcrumbs' ][] = \Yii::t('app', 'Мои пожелания'); ?>
@@ -19,221 +24,23 @@ _________________________________________________________ -->
+ 'wishlist-products', + ] + ); + echo ListView::widget( + [ + 'dataProvider' => $dataProvider, + 'itemView' => '_product', + 'layout' => '{items}{pager}', + ] + ); + Pjax::end(); + ?> - - -
-
-
- - - -
- - - - -
-
SALE
-
-
- - -
-
NEW
-
-
- -
- -
- -
-
-
- - - -
- - - -
- -
- -
-
-
- - - -
- - - -
- -
- -
-
-
- - - -
- - - - -
-
SALE
-
-
- - -
-
NEW
-
-
- -
- -
- -
-
-
- - - -
- - - - -
-
NEW
-
-
- -
- -
- -
-
-
- - - -
- - - - -
-
NEW
-
-
- -
- -
-
-
-
- - - -
- - - -
- -
diff --git a/frontend/views/product/view.php b/frontend/views/product/view.php index c7dc777..8584ba8 100755 --- a/frontend/views/product/view.php +++ b/frontend/views/product/view.php @@ -8,6 +8,7 @@ use yii\bootstrap\ActiveForm; use yii\bootstrap\Html; use yii\helpers\ArrayHelper; + use yii\helpers\Url; use yii\web\View; /** @@ -201,12 +202,51 @@ } ?> - - */ + if (\Yii::$app->user->isGuest) { + echo Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart-o' ]), + [ + 'title' => 'Добавить в избранное', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + ], + 'class' => 'btn btn-default pull-right disabled', + ] + ); + } elseif (in_array($variant->id, \Yii::$app->user->identity->wishlist)) { + echo Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart' ]), + [ + 'title' => 'Убрать из избранного', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + 'product' => $model->id, + 'variant' => $variant->id, + 'user' => \Yii::$app->user->identity->getId(), + 'url' => Url::to([ 'product/wishlist-rm' ]), + ], + 'class' => 'wishlist-rm btn btn-success pull-right', + ] + ); + } else { + echo Html::button( + Html::tag('i', '', [ 'class' => 'fa fa-heart-o' ]), + [ + 'title' => 'Добавить в избранное', + 'data' => [ + 'toggle' => 'tooltip', + 'placement' => 'top', + 'product' => $model->id, + 'variant' => $variant->id, + 'user' => \Yii::$app->user->identity->getId(), + 'url' => Url::to([ 'product/wishlist-add' ]), + ], + 'class' => 'wishlist-add btn btn-success pull-right', + ] + ); + } ?>


diff --git a/frontend/web/js/script.js b/frontend/web/js/script.js index 6d2e962..24122f6 100755 --- a/frontend/web/js/script.js +++ b/frontend/web/js/script.js @@ -171,6 +171,68 @@ $(function() { } }.bind(this)); + $(document) + .on( + 'click', '.wishlist-rm, .wishlist-add', function() { + var button = $(this); + $.ajax( + { + url: button.data('url'), + type: "POST", + data: { + 'variant': button.data('variant'), + 'product': button.data('product'), + 'user': button.data('user') + }, + success: function(data) { + button.replaceWith(data.button); + new PNotify( + { + title: 'Info', + text: data.message, + type: 'info', + styling: 'fontawesome' + } + ); + + } + } + ) + } + ); + + $(document) + .on( + 'click', '.wishlist-delete', function(e) { + e.preventDefault(); + var button = $(this); + $.ajax( + { + url: button.data('url'), + type: "POST", + data: { + 'product': button.data('product'), + 'variant': button.data('variant'), + 'user': button.data('user') + }, + success: function(data) { + if (data.success) { + $.pjax.reload({container: '#wishlist-products'}); + new PNotify( + { + title: 'Info', + text: data.message, + type: 'info', + styling: 'fontawesome' + } + ); + } + } + } + ); + } + ); + }); function showLoader(container) { $(container) -- libgit2 0.21.4