Blame view

frontend/controllers/AccountsController.php 12.7 KB
6310a439   Administrator   firs page
1
  <?php
5f49082a   Yarik   test
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      namespace frontend\controllers;
  
      use common\models\CompanyInfo;
      use common\models\Fields;
      use common\models\Job;
      use common\models\Language;
      use common\models\Payment;
      use common\models\Specialization;
      use common\models\UserPayment;
      use common\models\UserSpecialization;
      use Yii;
      use common\models\User;
      use common\models\UserInfo;
  
      use yii\filters\AccessControl;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
6310a439   Administrator   firs page
19
  
5f49082a   Yarik   test
20
21
22
23
24
      /**
       * Site controller
       */
      class AccountsController extends Controller
      {
6e1510b2   Administrator   firs page
25
  
5f49082a   Yarik   test
26
          public $layout = 'admin';
6e1510b2   Administrator   firs page
27
  
5f49082a   Yarik   test
28
29
30
31
32
33
34
35
36
37
38
          public function behaviors()
          {
              return [
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              //'actions' => ['cabinet','change-password', 'bookmarks', 'projects'],
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
6310a439   Administrator   firs page
39
40
                      ],
                  ],
5f49082a   Yarik   test
41
42
              ];
          }
6310a439   Administrator   firs page
43
  
5f49082a   Yarik   test
44
45
          public function actionCabinet()
          {
6310a439   Administrator   firs page
46
  
5f49082a   Yarik   test
47
              $user = $this->findUser(Yii::$app->user->identity->id);
6310a439   Administrator   firs page
48
  
5f49082a   Yarik   test
49
              $langs = Language::getActiveLanguages();
6310a439   Administrator   firs page
50
  
5f49082a   Yarik   test
51
52
53
54
55
56
57
58
59
              if($user->load(Yii::$app->request->post()) && $user->save()) {
                  $user->userInfo->load(Yii::$app->request->post());
                  $user->userInfo->save();
                  Fields::saveFieldData(Yii::$app->request->post('Fields'), $user->id, $user::className(), 'ru');
                  return $this->render('cabinet', [
                      'user'      => $user,
                      'user_info' => $user->userInfo,
                      'langs'     => $langs,
                  ]);
2ff00662   Administrator   firs page
60
  
5f49082a   Yarik   test
61
              } else {
70abf3e7   Administrator   add field v.1
62
  
5f49082a   Yarik   test
63
64
65
66
67
                  return $this->render('cabinet', [
                      'user'      => $user,
                      'user_info' => $user->userInfo,
                      'langs'     => $langs,
                  ]);
6310a439   Administrator   firs page
68
  
5f49082a   Yarik   test
69
              }
6310a439   Administrator   firs page
70
  
70abf3e7   Administrator   add field v.1
71
          }
6310a439   Administrator   firs page
72
  
5f49082a   Yarik   test
73
74
75
76
          public function actionBookmarks()
          {
              return $this->render('bookmarks');
          }
6310a439   Administrator   firs page
77
  
5f49082a   Yarik   test
78
          public function actionGeneral()
51e0a262   Yarik   test
79
          {
5f49082a   Yarik   test
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
              $user_info = UserInfo::find()
                                   ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                   ->one();
              $company_info = CompanyInfo::find()
                                         ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                         ->one();
              $user = \Yii::$app->user->identity;
              if(empty( $user_info )) {
                  $user_info = new UserInfo([ 'user_id' => \Yii::$app->user->getId() ]);
              }
              if(empty( $company_info )) {
                  $company_info = new CompanyInfo([ 'user_id' => \Yii::$app->user->getId() ]);
              }
              $post = \Yii::$app->request->post();
              if(!empty( $post )) {
                  $user_info->load($post);
                  $company_info->load($post);
                  $user->load($post);
                  if($user_info->save() && $user->save() && $company_info->save()) {
                      \Yii::$app->session->setFlash('userinfoupdate', 'Информация успешно обновлена');
                  } else {
                      \Yii::$app->session->setFlash('userinfoupdate', 'Ошибка обновления. Проверьте форму');
                  }
              }
              return $this->render('general', [
                  'user_info'    => $user_info,
                  'user'         => $user,
                  'company_info' => $company_info,
              ]);
51e0a262   Yarik   test
109
          }
5f49082a   Yarik   test
110
111
  
          public function actionPortfolio()
51e0a262   Yarik   test
112
          {
5f49082a   Yarik   test
113
  
51e0a262   Yarik   test
114
          }
5f49082a   Yarik   test
115
116
117
118
119
  
          /**
           * $user User
           */
          public function actionSetting()
51e0a262   Yarik   test
120
          {
5f49082a   Yarik   test
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
              $user = \Yii::$app->user->identity;
              $post = \Yii::$app->request->post('User');
              if(!empty( $post )) {
                  if(empty( $post[ 'new_password' ] )) {
                      $user->addError('new_password', 'Введите новый пароль');
                  } else {
                      $user->new_password = $post[ 'new_password' ];
                  }
                  if(empty( $post[ 'old_password' ] )) {
                      $user->addError('old_password', 'Введите новый пароль');
                  } else {
                      $user->old_password = $post[ 'old_password' ];
                  }
                  if(empty( $post[ 'password_reply' ] ) || $post[ 'password_reply' ] !== $post[ 'new_password' ]) {
                      $user->addError('password_reply', 'Неправильный повтор пароля');
                  } else {
                      $user->password_reply = $post[ 'password_reply' ];
                  }
                  if(!$user->hasErrors()) {
                      if($user->validatePassword($user->old_password)) {
                          $user->setPassword($user->new_password);
                          $user->generateAuthKey();
                          if($user->save()) {
                              \Yii::$app->session->setFlash('passwordupdate', 'Пароль успешно обновлен');
                          }
                      } else {
                          $user->addError('old_password', 'Неправильный старый пароль');
                      }
                  }
51e0a262   Yarik   test
150
              }
5f49082a   Yarik   test
151
              return $this->render('setting', [ 'user' => $user ]);
51e0a262   Yarik   test
152
          }
51e0a262   Yarik   test
153
  
5f49082a   Yarik   test
154
155
156
157
158
159
160
          public function actionContacts()
          {
              $user_info = UserInfo::find()
                                   ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                   ->one();
              if(empty( $user_info )) {
                  $user_info = new UserInfo([ 'user_id' => \Yii::$app->user->getId() ]);
51e0a262   Yarik   test
161
              }
5f49082a   Yarik   test
162
163
164
165
              if(!empty( \Yii::$app->request->post() )) {
                  Fields::saveFieldData(Yii::$app->request->post('Fields'), \Yii::$app->user->identity->id, User::className(), 'ru');
                  $user_info->load(\Yii::$app->request->post());
                  $user_info->save();
51e0a262   Yarik   test
166
              }
5f49082a   Yarik   test
167
168
169
170
171
172
173
174
175
176
              return $this->render('contacts', [ 'user_info' => $user_info ]);
          }
  
          public function actionService()
          {
              $user_info = UserInfo::find()
                                   ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                   ->one();
              if(empty( $user_info )) {
                  $user_info = new UserInfo([ 'user_id' => \Yii::$app->user->getId() ]);
51e0a262   Yarik   test
177
              }
5f49082a   Yarik   test
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
              $post = \Yii::$app->request->post();
              if(!empty( $post )) {
                  $user_info->load($post);
                  $user_info->save();
                  $user_specialization_values = [ ];
                  $user_payment_values = [ ];
                  UserSpecialization::deleteAll([ 'user_id' => \Yii::$app->user->getId() ]);
                  UserPayment::deleteAll([ 'user_id' => \Yii::$app->user->getId() ]);
                  foreach($post[ 'UserSpecialization' ][ 'specialization_id' ] as $index => $value) {
                      $user_specialization_values[] = (new UserSpecialization([ 'user_id'           => \Yii::$app->user->getId(),
                                                                                'specialization_id' => $value,
                      ]))->save();
                  }
                  foreach($post[ 'UserPayment' ][ 'payment_id' ] as $index => $value) {
                      $user_payment_values[] = (new UserPayment([ 'user_id'    => \Yii::$app->user->getId(),
                                                                  'payment_id' => $value,
                      ]))->save();
51e0a262   Yarik   test
195
196
                  }
              }
5f49082a   Yarik   test
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
              $user_specialization = UserSpecialization::find()
                                                       ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                                       ->select([ 'specialization_id' ])
                                                       ->column();
              $user_payment = UserPayment::find()
                                         ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                         ->select([ 'payment_id' ])
                                         ->column();
              $user_specialization_input = new UserSpecialization([ 'user_id' => \Yii::$app->user->getId() ]);
              $user_payment_input = new UserPayment([ 'user_id' => \Yii::$app->user->getId() ]);
              $specialization = Specialization::find()
                                              ->select([
                                                  'specialization_name',
                                                  'specialization_id',
                                              ])
                                              ->indexBy('specialization_id')
                                              ->asArray()
                                              ->column();
              $payment = Payment::find()
                                ->select([
                                    'name',
                                    'payment_id',
                                ])
                                ->indexBy('payment_id')
                                ->asArray()
                                ->column();
              return $this->render('service', [
                  'user_info'                 => $user_info,
                  'specialization'            => $specialization,
                  'user_specialization'       => $user_specialization,
                  'user_specialization_input' => $user_specialization_input,
                  'payment'                   => $payment,
                  'user_payment'              => $user_payment,
                  'user_payment_input'        => $user_payment_input,
              ]);
51e0a262   Yarik   test
232
          }
51e0a262   Yarik   test
233
  
5f49082a   Yarik   test
234
235
236
          public function actionAddSkills()
          {
              return $this->render('add-skills');
f6a2a0d5   Yarik   test
237
          }
51e0a262   Yarik   test
238
  
5f49082a   Yarik   test
239
240
241
242
243
244
245
246
247
248
249
250
          public function actionDescription()
          {
              $user_info = UserInfo::find()
                                   ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                   ->one();
              if(empty( $user_info )) {
                  $user_info = new UserInfo([ 'user_id' => \Yii::$app->user->getId() ]);
              }
              $post = \Yii::$app->request->post();
              if(!empty($post)) {
                  $user_info->load($post);
                  $user_info->save();
eacb83cc   Yarik   test
251
              }
5f49082a   Yarik   test
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
              return $this->render('description', [ 'user_info' => $user_info ]);
          }
  
          public function actionEmployment()
          {
              $post = \Yii::$app->request->post();
              if(!empty( $post )) {
                  $job = [ ];
                  for($i = 0; $i < count($post[ 'Job' ]); $i++) {
                      $job[ $i ] = new Job([
                          'user_id' => \Yii::$app->user->getId(),
                          'current' => 0,
                      ]);
                  }
                  if(Job::loadMultiple($job, $post)) {
                      $job[ 0 ]->current = 1;
                      if(Job::validateMultiple($job)) {
                          Job::deleteAll([ 'user_id' => \Yii::$app->user->getId() ]);
                          foreach($job as $onejob) {
                              $onejob->save(false);
                          }
eacb83cc   Yarik   test
273
274
                      }
                  }
5f49082a   Yarik   test
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
              } else {
                  $job = Job::find()
                            ->where([ 'user_id' => \Yii::$app->user->getId() ])
                            ->orderBy([ 'current' => SORT_DESC ])
                            ->all();
                  if(empty( $job )) {
                      $job[] = new Job([
                          'user_id' => \Yii::$app->user->getId(),
                          'current' => 0,
                      ]);
                  }
                  if(!$job[ 0 ]->current) {
                      array_unshift($job, new Job([
                          'user_id' => \Yii::$app->user->getId(),
                          'current' => 1,
                      ]));
                  }
eacb83cc   Yarik   test
292
              }
5f49082a   Yarik   test
293
              return $this->render('employment', [ 'job' => $job ]);
e9013fc3   Yarik   test
294
          }
51e0a262   Yarik   test
295
  
5f49082a   Yarik   test
296
297
298
299
          public function actionProjects()
          {
              return $this->render('projects');
          }
6310a439   Administrator   firs page
300
  
5f49082a   Yarik   test
301
302
          public function actionGallery()
          {
e9013fc3   Yarik   test
303
  
5f49082a   Yarik   test
304
          }
6310a439   Administrator   firs page
305
  
5f49082a   Yarik   test
306
307
308
          public function actionGetForm($lastindex)
          {
              return $this->renderAjax('_job_form', [ 'index' => $lastindex + 1 ]);
6310a439   Administrator   firs page
309
          }
6310a439   Administrator   firs page
310
  
5f49082a   Yarik   test
311
312
313
314
315
316
317
318
          /**
           * @param $id
           *
           * @return User
           * @throws NotFoundHttpException
           */
          protected function findUser($id)
          {
6310a439   Administrator   firs page
319
  
5f49082a   Yarik   test
320
321
322
323
324
325
              if($model = User::findOne([ "id" => $id ])) {
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
6310a439   Administrator   firs page
326
  
5f49082a   Yarik   test
327
      }