Blame view

frontend/controllers/AccountsController.php 22 KB
6310a439   Administrator   firs page
1
  <?php
5f49082a   Yarik   test
2
3
      namespace frontend\controllers;
  
84ebac3d   Yarik   test
4
      use common\models\Blog;
5f49082a   Yarik   test
5
6
7
8
9
      use common\models\CompanyInfo;
      use common\models\Fields;
      use common\models\Job;
      use common\models\Language;
      use common\models\Payment;
7eb29439   Yarik   test
10
11
      use common\models\Portfolio;
      use common\models\PortfolioSpecialization;
84ebac3d   Yarik   test
12
      use common\models\Project;
5f49082a   Yarik   test
13
14
15
16
17
18
19
20
21
22
      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
23
  
5f49082a   Yarik   test
24
25
26
27
28
      /**
       * Site controller
       */
      class AccountsController extends Controller
      {
6e1510b2   Administrator   firs page
29
  
5f49082a   Yarik   test
30
          public $layout = 'admin';
6e1510b2   Administrator   firs page
31
  
5f49082a   Yarik   test
32
33
34
35
36
37
38
39
40
41
42
          public function behaviors()
          {
              return [
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              //'actions' => ['cabinet','change-password', 'bookmarks', 'projects'],
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
6310a439   Administrator   firs page
43
44
                      ],
                  ],
5f49082a   Yarik   test
45
46
              ];
          }
6310a439   Administrator   firs page
47
  
5f49082a   Yarik   test
48
49
          public function actionCabinet()
          {
6310a439   Administrator   firs page
50
  
5f49082a   Yarik   test
51
              $user = $this->findUser(Yii::$app->user->identity->id);
6310a439   Administrator   firs page
52
  
5f49082a   Yarik   test
53
              $langs = Language::getActiveLanguages();
6310a439   Administrator   firs page
54
  
5f49082a   Yarik   test
55
56
57
58
59
60
61
62
63
              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
64
  
5f49082a   Yarik   test
65
              } else {
70abf3e7   Administrator   add field v.1
66
  
5f49082a   Yarik   test
67
68
69
70
71
                  return $this->render('cabinet', [
                      'user'      => $user,
                      'user_info' => $user->userInfo,
                      'langs'     => $langs,
                  ]);
6310a439   Administrator   firs page
72
  
5f49082a   Yarik   test
73
              }
6310a439   Administrator   firs page
74
  
70abf3e7   Administrator   add field v.1
75
          }
6310a439   Administrator   firs page
76
  
5f49082a   Yarik   test
77
78
79
80
          public function actionBookmarks()
          {
              return $this->render('bookmarks');
          }
6310a439   Administrator   firs page
81
  
5f49082a   Yarik   test
82
          public function actionGeneral()
51e0a262   Yarik   test
83
          {
5f49082a   Yarik   test
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
109
110
111
112
              $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
113
          }
5f49082a   Yarik   test
114
  
84ebac3d   Yarik   test
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
          public function actionBlog()
          {
              return $this->render('blog');
          }
  
          public function actionBlogCreate()
          {
              $blog = new Blog();
              $post = \Yii::$app->request->post();
              if($blog->load($post) && $blog->save()) {
                  return $this->redirect('blog');
              } else {
                  return $this->render('_blog_form', [ 'blog' => $blog ]);
              }
          }
  
          public function actionBlogUpdate($id)
          {
              $blog = Blog::findOne($id);
              $post = \Yii::$app->request->post();
              if($blog->load($post) && $blog->save()) {
                  return $this->redirect('blog');
              } else {
                  return $this->render('_blog_form', [ 'blog' => $blog ]);
              }
          }
  
5f49082a   Yarik   test
142
          public function actionPortfolio()
51e0a262   Yarik   test
143
          {
7eb29439   Yarik   test
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
              return $this->render('portfolio');
          }
  
          public function actionPortfolioCreate()
          {
              $portfolio = new Portfolio();
              $specialization = Specialization::find()
                                              ->select([
                                                  'specialization_name',
                                                  'specialization_id',
                                              ])
                                              ->indexBy('specialization_id')
                                              ->asArray()
                                              ->column();
              $post = \Yii::$app->request->post();
84ebac3d   Yarik   test
159
              if(!empty( $post )) {
7eb29439   Yarik   test
160
161
162
163
164
165
166
167
                  $portfolio->load($post);
                  $portfolio->validate();
                  if(!$portfolio->hasErrors()) {
                      $portfolio->save();
                      $portfolio->unlinkAll('specializations', true);
                      foreach($portfolio->specializationInput as $one_specialization) {
                          $portfolio->link('specializations', Specialization::findOne($one_specialization));
                      }
84ebac3d   Yarik   test
168
                      return $this->redirect('portfolio');
7eb29439   Yarik   test
169
170
                  }
              }
84ebac3d   Yarik   test
171
172
173
174
              return $this->render('_portfolio_form', [
                  'portfolio'      => $portfolio,
                  'specialization' => $specialization,
              ]);
7eb29439   Yarik   test
175
          }
5f49082a   Yarik   test
176
  
7eb29439   Yarik   test
177
178
179
180
181
182
183
184
185
186
187
188
          public function actionPortfolioUpdate($id)
          {
              $portfolio = Portfolio::findOne($id);
              $specialization = Specialization::find()
                                              ->select([
                                                  'specialization_name',
                                                  'specialization_id',
                                              ])
                                              ->indexBy('specialization_id')
                                              ->asArray()
                                              ->column();
              $post = \Yii::$app->request->post();
84ebac3d   Yarik   test
189
              if(!empty( $post )) {
7eb29439   Yarik   test
190
191
192
193
194
195
196
197
                  $portfolio->load($post);
                  $portfolio->validate();
                  if(!$portfolio->hasErrors()) {
                      $portfolio->save();
                      $portfolio->unlinkAll('specializations', true);
                      foreach($portfolio->specializationInput as $one_specialization) {
                          $portfolio->link('specializations', Specialization::findOne($one_specialization));
                      }
84ebac3d   Yarik   test
198
                      return $this->redirect('portfolio');
7eb29439   Yarik   test
199
200
                  }
              }
84ebac3d   Yarik   test
201
202
203
204
              return $this->render('_portfolio_form', [
                  'portfolio'      => $portfolio,
                  'specialization' => $specialization,
              ]);
51e0a262   Yarik   test
205
          }
5f49082a   Yarik   test
206
207
208
209
210
  
          /**
           * $user User
           */
          public function actionSetting()
51e0a262   Yarik   test
211
          {
5f49082a   Yarik   test
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
              $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
241
              }
5f49082a   Yarik   test
242
              return $this->render('setting', [ 'user' => $user ]);
51e0a262   Yarik   test
243
          }
51e0a262   Yarik   test
244
  
5f49082a   Yarik   test
245
246
247
248
249
250
251
          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
252
              }
5f49082a   Yarik   test
253
254
255
256
              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
257
              }
5f49082a   Yarik   test
258
259
260
261
262
263
264
265
266
267
              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
268
              }
5f49082a   Yarik   test
269
270
271
272
273
274
275
276
277
              $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) {
7eb29439   Yarik   test
278
279
280
                      $user_specialization_values[] = (new UserSpecialization([
                          'user_id'           => \Yii::$app->user->getId(),
                          'specialization_id' => $value,
5f49082a   Yarik   test
281
282
283
                      ]))->save();
                  }
                  foreach($post[ 'UserPayment' ][ 'payment_id' ] as $index => $value) {
7eb29439   Yarik   test
284
285
286
                      $user_payment_values[] = (new UserPayment([
                          'user_id'    => \Yii::$app->user->getId(),
                          'payment_id' => $value,
5f49082a   Yarik   test
287
                      ]))->save();
51e0a262   Yarik   test
288
289
                  }
              }
5f49082a   Yarik   test
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
              $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
325
          }
51e0a262   Yarik   test
326
  
5f49082a   Yarik   test
327
328
          public function actionAddSkills()
          {
766bd93b   Yarik   test
329
              $user = \Yii::$app->user->identity;
7eb29439   Yarik   test
330
              if(!empty( \Yii::$app->request->post() )) {
766bd93b   Yarik   test
331
332
                  Fields::saveFieldData(Yii::$app->request->post('Fields'), \Yii::$app->user->identity->id, User::className(), 'ru');
              }
7eb29439   Yarik   test
333
              return $this->render('add-skills', [ 'user' => $user ]);
f6a2a0d5   Yarik   test
334
          }
51e0a262   Yarik   test
335
  
5f49082a   Yarik   test
336
337
338
339
340
341
342
343
344
          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();
7eb29439   Yarik   test
345
              if(!empty( $post )) {
5f49082a   Yarik   test
346
347
                  $user_info->load($post);
                  $user_info->save();
eacb83cc   Yarik   test
348
              }
5f49082a   Yarik   test
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
              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
370
371
                      }
                  }
5f49082a   Yarik   test
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
              } 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
389
              }
5f49082a   Yarik   test
390
              return $this->render('employment', [ 'job' => $job ]);
e9013fc3   Yarik   test
391
          }
51e0a262   Yarik   test
392
  
5f49082a   Yarik   test
393
394
395
396
          public function actionProjects()
          {
              return $this->render('projects');
          }
6310a439   Administrator   firs page
397
  
84ebac3d   Yarik   test
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
          public function actionProjectsCreate()
          {
              $project = new Project();
              $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();
aa182b5c   Yarik   test
417
418
419
420
421
422
423
424
425
              $projects = Project::find()
                                 ->select([
                                     'name',
                                     'project_id',
                                 ])
                                 ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                 ->indexBy('project_id')
                                 ->asArray()
                                 ->column();
84ebac3d   Yarik   test
426
              $post = \Yii::$app->request->post();
aa182b5c   Yarik   test
427
              if(!empty( $post )) {
84ebac3d   Yarik   test
428
429
430
431
432
433
                  $project->load($post);
                  $project->validate();
                  if(!$project->hasErrors()) {
                      $project->save();
                      $project->unlinkAll('specializations', true);
                      foreach($project->specializationInput as $one_specialization) {
aa182b5c   Yarik   test
434
                          $project->link('specializations', Specialization::findOne($one_specialization));
84ebac3d   Yarik   test
435
436
437
                      }
                      $project->unlinkAll('payments', true);
                      foreach($project->paymentInput as $one_payment) {
aa182b5c   Yarik   test
438
                          $project->link('payments', Payment::findOne($one_payment));
84ebac3d   Yarik   test
439
440
441
442
                      }
                      return $this->redirect('projects');
                  }
              }
aa182b5c   Yarik   test
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
              return $this->render('_projects_form', [
                  'project'        => $project,
                  'specialization' => $specialization,
                  'payment'        => $payment,
                  'projects'       => $projects,
              ]);
          }
  
          public function actionProjectsUpdate($id)
          {
              $project = Project::findOne($id);
              $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();
              $projects = Project::find()
                                 ->select([
                                     'name',
                                     'project_id',
                                 ])
                                 ->where([ 'user_id' => \Yii::$app->user->getId() ])
                                 ->andWhere([
                                     'not',
                                     [ 'project_id' => $project->project_id ],
                                 ])
                                 ->indexBy('project_id')
                                 ->asArray()
                                 ->column();
              $post = \Yii::$app->request->post();
              if(!empty( $post )) {
                  $project->load($post);
                  $project->validate();
                  if(!$project->hasErrors()) {
                      $project->save();
                      $project->unlinkAll('specializations', true);
                      foreach($project->specializationInput as $one_specialization) {
                          $project->link('specializations', Specialization::findOne($one_specialization));
                      }
                      $project->unlinkAll('payments', true);
                      foreach($project->paymentInput as $one_payment) {
                          $project->link('payments', Payment::findOne($one_payment));
                      }
                      return $this->render('_projects_form', [
                          'project'        => $project,
                          'specialization' => $specialization,
                          'payment'        => $payment,
                          'projects'       => $projects,
                      ]);
                      //return $this->redirect('projects');
                  }
              }
              return $this->render('_projects_form', [
                  'project'        => $project,
                  'specialization' => $specialization,
                  'payment'        => $payment,
                  'projects'       => $projects,
              ]);
84ebac3d   Yarik   test
512
513
          }
  
5f49082a   Yarik   test
514
515
          public function actionGallery()
          {
e9013fc3   Yarik   test
516
  
5f49082a   Yarik   test
517
          }
6310a439   Administrator   firs page
518
  
5f49082a   Yarik   test
519
520
521
          public function actionGetForm($lastindex)
          {
              return $this->renderAjax('_job_form', [ 'index' => $lastindex + 1 ]);
6310a439   Administrator   firs page
522
          }
6310a439   Administrator   firs page
523
  
5f49082a   Yarik   test
524
525
526
527
528
529
530
531
          /**
           * @param $id
           *
           * @return User
           * @throws NotFoundHttpException
           */
          protected function findUser($id)
          {
6310a439   Administrator   firs page
532
  
5f49082a   Yarik   test
533
534
535
536
537
538
              if($model = User::findOne([ "id" => $id ])) {
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
6310a439   Administrator   firs page
539
  
5f49082a   Yarik   test
540
      }