Blame view

frontend/controllers/ProductController.php 8.56 KB
e9f8a5ec   Yarik   Detail page
1
2
3
4
5
  <?php
      namespace frontend\controllers;
      
      use artbox\catalog\models\Product;
      use artbox\core\components\SeoComponent;
ac6e2c67   Alexey Boroda   -Cabinet ready
6
      use artbox\order\models\Wishlist;
e9f8a5ec   Yarik   Detail page
7
      use yii\db\ActiveQuery;
ac6e2c67   Alexey Boroda   -Cabinet ready
8
9
      use yii\helpers\Html;
      use yii\helpers\Url;
e9f8a5ec   Yarik   Detail page
10
11
12
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      use Yii;
ac6e2c67   Alexey Boroda   -Cabinet ready
13
      use yii\web\Response;
e9f8a5ec   Yarik   Detail page
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
      
      /**
       * Class ProductController
       *
       * @package frontend\controllers
       */
      class ProductController extends Controller
      {
          /**
           * Show product by ID
           *
           * @param int $id
           *
           * @return string
           */
          public function actionView($id)
          {
              $model = $this->findModel($id);
              /**
               * @var SeoComponent $seo
               */
              $seo = Yii::$app->get('seo');
              $seo->setModel($model->lang);
              $variant = $model->variants[ 0 ];
              $groups = $variant->getSortedGroups();
              $similar = $model->getSimilarProducts(8);
              
              return $this->render(
                  'view',
                  [
                      'model'   => $model,
                      'variant' => $variant,
                      'groups'  => $groups,
                      'similar' => $similar,
                  ]
              );
          }
          
          /**
           * Find product by ID
           *
           * @param $id
           *
           * @return Product
           * @throws \yii\web\NotFoundHttpException
           */
          protected function findModel($id)
          {
              /**
               * @var SeoComponent $seo
               */
              $seo = \Yii::$app->get('seo');
              /**
               * @var Product $model
               */
              $model = Product::findWithFilters()
4b0990fd   Yarik   Image fixes
70
                              ->with('lang', 'images', 'image')
e9f8a5ec   Yarik   Detail page
71
72
73
74
75
76
77
78
79
80
81
82
83
                              ->with(
                                  [
                                      'category' => function ($query) {
                                          /**
                                           * @var ActiveQuery $query
                                           */
                                          $query->with('lang')
                                                ->with('parent.lang');
                                      },
                                  ]
                              )
                              ->where([ 'id' => $id ])
                              ->one();
ac6e2c67   Alexey Boroda   -Cabinet ready
84
              if (!empty($model)) {
e9f8a5ec   Yarik   Detail page
85
                  if ($model->lang->alias_id !== $seo->aliasId) {
aee73024   Yarik   Artbox great prep...
86
                      throw new NotFoundHttpException(\Yii::t('app', 'Wrong language'));
e9f8a5ec   Yarik   Detail page
87
88
89
                  }
                  return $model;
              } else {
aee73024   Yarik   Artbox great prep...
90
                  throw new NotFoundHttpException(\Yii::t('app', 'Model not found'));
e9f8a5ec   Yarik   Detail page
91
92
              }
          }
ac6e2c67   Alexey Boroda   -Cabinet ready
93
      
aee73024   Yarik   Artbox great prep...
94
95
96
97
98
          /**
           * Remove product from wishlist
           *
           * @return array
           */
ac6e2c67   Alexey Boroda   -Cabinet ready
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
          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' ]),
                              [
aee73024   Yarik   Artbox great prep...
121
                                  'title' => \Yii::t('app', 'Добавить в избранное'),
ac6e2c67   Alexey Boroda   -Cabinet ready
122
123
124
125
126
127
128
129
130
131
132
                                  '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',
                              ]
                          ),
aee73024   Yarik   Artbox great prep...
133
                          'message' => \Yii::t('app', 'Товар убран из избранного'),
ac6e2c67   Alexey Boroda   -Cabinet ready
134
135
136
137
138
139
140
                      ];
                  }
              
                  return [
                      'button'  => Html::button(
                          Html::tag('i', '', [ 'class' => 'fa fa-heart' ]),
                          [
aee73024   Yarik   Artbox great prep...
141
                              'title' => \Yii::t('app', 'Убрать из избранного'),
ac6e2c67   Alexey Boroda   -Cabinet ready
142
143
144
145
146
147
148
149
150
151
152
                              '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',
                          ]
                      ),
aee73024   Yarik   Artbox great prep...
153
                      'message' => \Yii::t('app', 'Товар не найден'),
ac6e2c67   Alexey Boroda   -Cabinet ready
154
155
                  ];
              }
aee73024   Yarik   Artbox great prep...
156
              return [];
ac6e2c67   Alexey Boroda   -Cabinet ready
157
158
          }
      
aee73024   Yarik   Artbox great prep...
159
160
161
162
163
          /**
           * Add product to wishlist
           *
           * @return array
           */
ac6e2c67   Alexey Boroda   -Cabinet ready
164
165
166
167
168
169
170
171
172
173
174
175
176
177
          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' ]),
                              [
aee73024   Yarik   Artbox great prep...
178
                                  'title' => \Yii::t('app', 'Убрать из избранного'),
ac6e2c67   Alexey Boroda   -Cabinet ready
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
                                  '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' ]),
                          [
aee73024   Yarik   Artbox great prep...
198
                              'title' => \Yii::t('app', 'Добавить в избранное'),
ac6e2c67   Alexey Boroda   -Cabinet ready
199
200
201
202
203
204
205
206
207
208
209
                              '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',
                          ]
                      ),
aee73024   Yarik   Artbox great prep...
210
                      'message' => \Yii::t('app', 'Товар добавить не вышло'),
ac6e2c67   Alexey Boroda   -Cabinet ready
211
212
                  ];
              }
aee73024   Yarik   Artbox great prep...
213
              return [];
ac6e2c67   Alexey Boroda   -Cabinet ready
214
          }
e9f8a5ec   Yarik   Detail page
215
      }