Blame view

frontend/controllers/AccountController.php 6.95 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;
a337a53b   Alexey Boroda   -Cabinet started
11
      use yii\web\Controller;
6628fe31   Alexey Boroda   -User's order pag...
12
13
      use yii\web\ForbiddenHttpException;
      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
23
24
      
      /**
       * Class AccountController
       *
       * @package frontend\controllers
       */
      class AccountController extends Controller
      {
          public function actionIndex()
          {
6628fe31   Alexey Boroda   -User's order pag...
25
              $user = Customer::findOne(\Yii::$app->user->identity->getId());
a337a53b   Alexey Boroda   -Cabinet started
26
27
28
29
              
              $orders = $user->getOrders()
                             ->with(
                                 [
6628fe31   Alexey Boroda   -User's order pag...
30
                                     'label.lang',
a337a53b   Alexey Boroda   -Cabinet started
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
                                     'orderProducts',
                                 ]
                             )
                             ->orderBy(
                                 [
                                     'id' => SORT_DESC,
                                 ]
                             )
                             ->all();
              
              return $this->render(
                  'index',
                  [
                      'orders' => $orders,
                  ]
              );
          }
6628fe31   Alexey Boroda   -User's order pag...
48
49
50
51
52
53
54
      
          public function actionOrder($id)
          {
              /**
               * @var Order $order
               */
              $order = Order::find()
fa8dafba   Alexey Boroda   -User's password ...
55
                            ->with('orderProducts.variant.product.lang')
6628fe31   Alexey Boroda   -User's order pag...
56
57
58
59
60
61
                            ->where(
                                [
                                    'id' => $id,
                                ]
                            )
                            ->one();
fa8dafba   Alexey Boroda   -User's password ...
62
      
6628fe31   Alexey Boroda   -User's order pag...
63
64
65
66
67
              if (empty($order)) {
                  throw new NotFoundHttpException(\Yii::t('app', 'Order not found'));
              } elseif ($order->user_id !== \Yii::$app->user->identity->getId()) {
                  throw new ForbiddenHttpException();
              }
fa8dafba   Alexey Boroda   -User's password ...
68
69
70
71
72
73
74
75
76
77
78
      
              return $this->render(
                  'order',
                  [
                      'order' => $order,
                  ]
              );
          }
      
          public function actionAccount()
          {
db63ba79   Alexey Boroda   -User's data form...
79
              $user = \Yii::$app->user->identity;
fa8dafba   Alexey Boroda   -User's password ...
80
81
82
              return $this->render(
                  'account',
                  [
db63ba79   Alexey Boroda   -User's data form...
83
                      'userModel'     => $user,
fa8dafba   Alexey Boroda   -User's password ...
84
85
86
87
88
                      'passwordModel' => new PasswordForm(),
                  ]
              );
          }
      
ac6e2c67   Alexey Boroda   -Cabinet ready
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
          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,
                  ]
              );
          }
          
fa8dafba   Alexey Boroda   -User's password ...
126
127
128
129
130
131
132
          public function actionChangePassword()
          {
              /**
               * @var Customer $modeluser
               */
              $model = new PasswordForm();
              $modeluser = \Yii::$app->user->identity;
db63ba79   Alexey Boroda   -User's data form...
133
      
fa8dafba   Alexey Boroda   -User's password ...
134
135
136
137
138
139
140
141
142
              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...
143
                                  'userModel'     => $modeluser,
fa8dafba   Alexey Boroda   -User's password ...
144
145
146
147
148
149
150
151
                                  'passwordModel' => $model,
                              ]
                          );
                      }
                  } else {
                      return $this->render(
                          'account',
                          [
db63ba79   Alexey Boroda   -User's data form...
152
                              'userModel'     => $modeluser,
fa8dafba   Alexey Boroda   -User's password ...
153
154
155
156
                              'passwordModel' => $model,
                          ]
                      );
                  }
fa8dafba   Alexey Boroda   -User's password ...
157
              }
db63ba79   Alexey Boroda   -User's data form...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
              return $this->render(
                  'account',
                  [
                      'userModel'     => $modeluser,
                      'passwordModel' => $model,
                  ]
              );
      
          }
      
          public function actionChangeData()
          {
              /**
               * @var Customer $model
               */
              $model = \Yii::$app->user->identity;
ac6e2c67   Alexey Boroda   -Cabinet ready
174
      
db63ba79   Alexey Boroda   -User's data form...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
              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...
189
          }
ac6e2c67   Alexey Boroda   -Cabinet ready
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
      
          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' => 'Ошибка',
                  ];
              }
          }
a337a53b   Alexey Boroda   -Cabinet started
221
      }