Blame view

frontend/controllers/AccountController.php 8.23 KB
a337a53b   Alexey Boroda   -Cabinet started
1
2
3
4
5
  <?php
      
      namespace frontend\controllers;
      
      use artbox\order\models\Customer;
6628fe31   Alexey Boroda   -User's order pag...
6
      use artbox\order\models\Order;
fa8dafba   Alexey Boroda   -User's password ...
7
      use artbox\order\models\PasswordForm;
ac6e2c67   Alexey Boroda   -Cabinet ready
8
9
10
      use artbox\order\models\Wishlist;
      use yii\data\ActiveDataProvider;
      use yii\db\ActiveQuery;
aee73024   Yarik   Artbox great prep...
11
      use yii\filters\AccessControl;
a337a53b   Alexey Boroda   -Cabinet started
12
      use yii\web\Controller;
6628fe31   Alexey Boroda   -User's order pag...
13
      use yii\web\NotFoundHttpException;
ac6e2c67   Alexey Boroda   -Cabinet ready
14
      use yii\web\Response;
a337a53b   Alexey Boroda   -Cabinet started
15
16
17
18
19
20
21
22
      
      /**
       * Class AccountController
       *
       * @package frontend\controllers
       */
      class AccountController extends Controller
      {
aee73024   Yarik   Artbox great prep...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
                      ],
                  ],
              ];
          }
      
          /**
           * Account main page with orders
           *
           * @return string
           */
a337a53b   Alexey Boroda   -Cabinet started
46
47
          public function actionIndex()
          {
aee73024   Yarik   Artbox great prep...
48
49
50
51
              /**
               * @var Customer $user
               */
              $user = \Yii::$app->user->identity;
a337a53b   Alexey Boroda   -Cabinet started
52
53
54
55
              
              $orders = $user->getOrders()
                             ->with(
                                 [
6628fe31   Alexey Boroda   -User's order pag...
56
                                     'label.lang',
a337a53b   Alexey Boroda   -Cabinet started
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                                     'orderProducts',
                                 ]
                             )
                             ->orderBy(
                                 [
                                     'id' => SORT_DESC,
                                 ]
                             )
                             ->all();
              
              return $this->render(
                  'index',
                  [
                      'orders' => $orders,
                  ]
              );
          }
6628fe31   Alexey Boroda   -User's order pag...
74
      
aee73024   Yarik   Artbox great prep...
75
76
77
78
79
80
81
82
          /**
           * Account detail order page
           *
           * @param $id
           *
           * @return string
           * @throws \yii\web\NotFoundHttpException
           */
6628fe31   Alexey Boroda   -User's order pag...
83
84
85
          public function actionOrder($id)
          {
              /**
aee73024   Yarik   Artbox great prep...
86
87
88
89
               * @var Customer $user
               */
              $user = \Yii::$app->user->identity;
              /**
6628fe31   Alexey Boroda   -User's order pag...
90
91
               * @var Order $order
               */
aee73024   Yarik   Artbox great prep...
92
              $order = $user->getOrders()
fa8dafba   Alexey Boroda   -User's password ...
93
                            ->with('orderProducts.variant.product.lang')
6628fe31   Alexey Boroda   -User's order pag...
94
95
96
97
98
99
                            ->where(
                                [
                                    'id' => $id,
                                ]
                            )
                            ->one();
fa8dafba   Alexey Boroda   -User's password ...
100
      
6628fe31   Alexey Boroda   -User's order pag...
101
102
              if (empty($order)) {
                  throw new NotFoundHttpException(\Yii::t('app', 'Order not found'));
6628fe31   Alexey Boroda   -User's order pag...
103
              }
fa8dafba   Alexey Boroda   -User's password ...
104
105
106
107
108
109
110
111
112
      
              return $this->render(
                  'order',
                  [
                      'order' => $order,
                  ]
              );
          }
      
aee73024   Yarik   Artbox great prep...
113
114
115
116
117
          /**
           * Acount info page
           *
           * @return string
           */
fa8dafba   Alexey Boroda   -User's password ...
118
119
          public function actionAccount()
          {
db63ba79   Alexey Boroda   -User's data form...
120
              $user = \Yii::$app->user->identity;
fa8dafba   Alexey Boroda   -User's password ...
121
122
123
              return $this->render(
                  'account',
                  [
db63ba79   Alexey Boroda   -User's data form...
124
                      'userModel'     => $user,
fa8dafba   Alexey Boroda   -User's password ...
125
126
127
128
129
                      'passwordModel' => new PasswordForm(),
                  ]
              );
          }
      
aee73024   Yarik   Artbox great prep...
130
131
132
133
134
          /**
           * Account wishlist page
           *
           * @return string
           */
ac6e2c67   Alexey Boroda   -Cabinet ready
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
          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,
                      ],
                  ]
              );
aee73024   Yarik   Artbox great prep...
163
      
ac6e2c67   Alexey Boroda   -Cabinet ready
164
165
166
167
168
169
170
              return $this->render(
                  'wishlist',
                  [
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
aee73024   Yarik   Artbox great prep...
171
172
173
174
175
176
      
          /**
           * Account change password action
           *
           * @return string|\yii\web\Response
           */
fa8dafba   Alexey Boroda   -User's password ...
177
178
179
180
181
182
183
          public function actionChangePassword()
          {
              /**
               * @var Customer $modeluser
               */
              $model = new PasswordForm();
              $modeluser = \Yii::$app->user->identity;
db63ba79   Alexey Boroda   -User's data form...
184
      
fa8dafba   Alexey Boroda   -User's password ...
185
186
187
188
189
190
191
192
193
              if ($model->load(\Yii::$app->request->post())) {
                  if ($model->validate()) {
                      $modeluser->setPassword($model->newpass);
                      if ($modeluser->save()) {
                          return $this->redirect([ 'index' ]);
                      } else {
                          return $this->render(
                              'account',
                              [
db63ba79   Alexey Boroda   -User's data form...
194
                                  'userModel'     => $modeluser,
fa8dafba   Alexey Boroda   -User's password ...
195
196
197
198
199
200
201
202
                                  'passwordModel' => $model,
                              ]
                          );
                      }
                  } else {
                      return $this->render(
                          'account',
                          [
db63ba79   Alexey Boroda   -User's data form...
203
                              'userModel'     => $modeluser,
fa8dafba   Alexey Boroda   -User's password ...
204
205
206
207
                              'passwordModel' => $model,
                          ]
                      );
                  }
fa8dafba   Alexey Boroda   -User's password ...
208
              }
db63ba79   Alexey Boroda   -User's data form...
209
210
211
212
213
214
215
216
217
218
              return $this->render(
                  'account',
                  [
                      'userModel'     => $modeluser,
                      'passwordModel' => $model,
                  ]
              );
      
          }
      
aee73024   Yarik   Artbox great prep...
219
220
221
222
223
          /**
           * Account change data action
           *
           * @return string|\yii\web\Response
           */
db63ba79   Alexey Boroda   -User's data form...
224
225
226
227
228
229
          public function actionChangeData()
          {
              /**
               * @var Customer $model
               */
              $model = \Yii::$app->user->identity;
ac6e2c67   Alexey Boroda   -Cabinet ready
230
      
db63ba79   Alexey Boroda   -User's data form...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
              if ($model->load(\Yii::$app->request->post())) {
                  //                VarDumper::dump($model, 10, 1);die();
                  $model->markAttributeDirty('birthday');
                  if ($model->save()) {
                      return $this->redirect([ 'index' ]);
                  }
              }
              return $this->render(
                  'account',
                  [
                      'userModel'     => $model,
                      'passwordModel' => new PasswordForm(),
                  ]
              );
6628fe31   Alexey Boroda   -User's order pag...
245
          }
ac6e2c67   Alexey Boroda   -Cabinet ready
246
      
aee73024   Yarik   Artbox great prep...
247
248
249
250
251
          /**
           * Account delete from wishlist action
           *
           * @return array
           */
ac6e2c67   Alexey Boroda   -Cabinet ready
252
253
254
255
256
257
258
259
260
261
262
263
264
          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'),
aee73024   Yarik   Artbox great prep...
265
  
ac6e2c67   Alexey Boroda   -Cabinet ready
266
267
268
269
270
271
                                       ]
                                   )
                                   ->one();
                  if (!empty($model) && $model->delete()) {
                      return [
                          'success' => true,
aee73024   Yarik   Artbox great prep...
272
                          'message' => \Yii::t('app', 'Товар удален из избранного'),
ac6e2c67   Alexey Boroda   -Cabinet ready
273
274
                      ];
                  }
aee73024   Yarik   Artbox great prep...
275
      
ac6e2c67   Alexey Boroda   -Cabinet ready
276
277
                  return [
                      'success' => false,
aee73024   Yarik   Artbox great prep...
278
                      'message' => \Yii::t('app', 'Ошибка'),
ac6e2c67   Alexey Boroda   -Cabinet ready
279
280
                  ];
              }
aee73024   Yarik   Artbox great prep...
281
              return [];
ac6e2c67   Alexey Boroda   -Cabinet ready
282
          }
a337a53b   Alexey Boroda   -Cabinet started
283
      }