Blame view

common/models/User.php 24.3 KB
b0f143c3   Yarik   first commit
1
  <?php
b95371cf   Yarik   test
2
      namespace common\models;
b0f143c3   Yarik   first commit
3
  
4ed1f788   Yarik   test
4
5
      use common\modules\comment\models\Comment;
      use common\modules\comment\models\Rating;
b95371cf   Yarik   test
6
      use Yii;
4ed1f788   Yarik   test
7
      use yii\base\InvalidConfigException;
b95371cf   Yarik   test
8
9
      use yii\base\NotSupportedException;
      use yii\behaviors\TimestampBehavior;
e8236f44   Yarik   test
10
      use yii\db\ActiveQuery;
b95371cf   Yarik   test
11
      use yii\db\ActiveRecord;
be662d1f   Yarik   test
12
      use yii\helpers\Url;
e8236f44   Yarik   test
13
14
      use yii\rbac\ManagerInterface;
      use yii\rbac\Role;
b95371cf   Yarik   test
15
16
      use yii\web\IdentityInterface;
      use developeruz\db_rbac\interfaces\UserRbacInterface;
b0f143c3   Yarik   first commit
17
18
  
      /**
b95371cf   Yarik   test
19
       * User model
5077a0ec   Yarik   test
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
       * @property integer              $id
       * @property string               $username
       * @property string               $password_hash
       * @property string               $password_reset_token
       * @property string               $email
       * @property string               $auth_key
       * @property integer              $status
       * @property integer              $created_at
       * @property integer              $updated_at
       * @property string               $password write-only password
       * @property string               $type
       * @property UserInfo             $userInfo
       * @property string               $userName
       * @property array[]              $roles
       * @property CompanyInfo          $companyInfo
       * @property array[]              $phones
       * @property array[]              $site
       * @property string               $address
       * @property string               $liveTime
       * @property Payment[]            $payments
       * @property integer[]            $paymentInput
       * @property Specialization[]     $specializations
       * @property integer[]            $specializationInput
       * @property Blog[]               $blog
       * @property Job[]                $jobs
       * @property Job                  $currentJob
       * @property Portfolio[]          $portfolios
       * @property Project[]            $projects
       * @property Team[]               $teams
       * @property Vacancy[]            $vacancies
       * @property Gallery[]            $galleries
be662d1f   Yarik   test
51
       * @property UserInfo|CompanyInfo $owner
5077a0ec   Yarik   test
52
53
54
55
56
57
       * @property string               $name
       * @property Comment[]            $comments
       * @property Rating[]             $commentRating
       * @property int                  $ratingPG
       * @property string               $lastVisit
       * @property string               $link
b0f143c3   Yarik   first commit
58
       */
b95371cf   Yarik   test
59
      class User extends ActiveRecord implements IdentityInterface, UserRbacInterface
b0f143c3   Yarik   first commit
60
      {
4c9663e0   Yarik   test
61
  
b95371cf   Yarik   test
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
          const STATUS_DELETED = 0;
          const STATUS_ACTIVE = 10;
  
          public $profile;
  
          public $old_password;
  
          public $new_password;
  
          public $password_reply;
  
          /**
           * @inheritdoc
           */
          public static function tableName()
          {
              return '{{%user}}';
          }
  
b95371cf   Yarik   test
81
82
83
84
85
86
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
35b03e57   Administrator   add yii jquery
87
                  [
be662d1f   Yarik   test
88
                      'class' => 'common\behaviors\ShowImage',
35b03e57   Administrator   add yii jquery
89
                  ],
b95371cf   Yarik   test
90
91
92
93
                  TimestampBehavior::className(),
              ];
          }
  
b95371cf   Yarik   test
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
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      'status',
                      'default',
                      'value' => self::STATUS_ACTIVE,
                  ],
                  [
                      'status',
                      'in',
                      'range' => [
                          self::STATUS_ACTIVE,
                          self::STATUS_DELETED,
                      ],
                  ],
                  [
                      [
                          'username',
                          'lastname',
                          'firstname',
                          'middlename',
                      ],
                      'string',
                      'max' => 255,
                  ],
                  [
b95371cf   Yarik   test
124
125
126
                      [
                          'specializationInput',
                          'paymentInput',
4ed1f788   Yarik   test
127
                          'type',
b95371cf   Yarik   test
128
129
130
                      ],
                      'safe',
                  ],
658a5f37   Yarik   test
131
132
133
134
                  [
                      'type',
                      'default',
                      'value' => 1,
4ed1f788   Yarik   test
135
                  ],
38a6e1dd   Yarik   test
136
137
138
139
140
141
142
143
                  [
                      [
                          'specializationInput',
                          'paymentInput',
                      ],
                      'default',
                      'value' => [ ],
                  ],
b95371cf   Yarik   test
144
145
146
147
148
149
              ];
          }
  
          /**
           * @inheritdoc
           */
3e86d3d1   Yarik   test
150
151
152
153
          public function attributeLabels()
          {
              return [
                  'firstname' => Yii::t('app', 'Имя'),
4ed1f788   Yarik   test
154
155
                  'lastname'  => Yii::t('app', 'Фамилия'),
                  'email'     => Yii::t('app', 'Email'),
3e86d3d1   Yarik   test
156
157
158
159
160
161
              ];
          }
  
          /**
           * @inheritdoc
           */
b95371cf   Yarik   test
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
          public static function findIdentity($id)
          {
              if(Yii::$app->getSession()
                          ->has('user-' . $id)
              ) {
                  if(Yii::$app->getSession()
                              ->has('user-' . $id)
                  ) {
                      return new self(Yii::$app->getSession()
                                               ->get('user-' . $id));
                  } else {
                      return isset( self::$users[ $id ] ) ? new self(self::$users[ $id ]) : NULL;
                  }
              } else {
                  return static::findOne([
                      'id'     => $id,
                      'status' => self::STATUS_ACTIVE,
                  ]);
b0f143c3   Yarik   first commit
180
              }
b95371cf   Yarik   test
181
  
b0f143c3   Yarik   first commit
182
          }
b95371cf   Yarik   test
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  
          /**
           * @param \nodge\eauth\ServiceBase $service
           *
           * @return User
           * @throws ErrorException
           */
          public static function findByEAuth($service)
          {
              if(!$service->getIsAuthenticated()) {
                  throw new ErrorException('EAuth user should be authenticated before creating identity.');
              }
              $id = $service->getServiceName() . '-' . $service->getId();
              $attributes = [
                  'id'       => $id,
                  'username' => $service->getAttribute('name'),
                  'authKey'  => md5($id),
                  'profile'  => $service->getAttributes(),
              ];
              $attributes[ 'profile' ][ 'service' ] = $service->getServiceName();
              Yii::$app->getSession()
                       ->set('user-' . $id, $attributes);
              return new self($attributes);
b0f143c3   Yarik   first commit
206
          }
b0f143c3   Yarik   first commit
207
  
b95371cf   Yarik   test
208
          public $authKey;
b0f143c3   Yarik   first commit
209
  
b95371cf   Yarik   test
210
211
212
213
214
215
216
          /**
           * @inheritdoc
           */
          public static function findIdentityByAccessToken($token, $type = NULL)
          {
              throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
          }
b0f143c3   Yarik   first commit
217
  
b95371cf   Yarik   test
218
219
220
221
222
223
224
225
226
227
228
229
230
231
          /**
           * Finds user by username
           *
           * @param string $username
           *
           * @return static|null
           */
          public static function findByUsername($username)
          {
              return static::findOne([
                  'username' => $username,
                  'status'   => self::STATUS_ACTIVE,
              ]);
          }
b0f143c3   Yarik   first commit
232
  
b95371cf   Yarik   test
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
          /**
           * Finds user by password reset token
           *
           * @param string $token password reset token
           *
           * @return static|null
           */
          public static function findByPasswordResetToken($token)
          {
              if(!static::isPasswordResetTokenValid($token)) {
                  return NULL;
              }
  
              return static::findOne([
                  'password_reset_token' => $token,
                  'status'               => self::STATUS_ACTIVE,
              ]);
b0f143c3   Yarik   first commit
250
251
          }
  
b95371cf   Yarik   test
252
253
254
255
256
257
258
259
260
261
262
263
          /**
           * Finds out if password reset token is valid
           *
           * @param string $token password reset token
           *
           * @return boolean
           */
          public static function isPasswordResetTokenValid($token)
          {
              if(empty( $token )) {
                  return false;
              }
b0f143c3   Yarik   first commit
264
  
b95371cf   Yarik   test
265
266
267
              $timestamp = (int) substr($token, strrpos($token, '_') + 1);
              $expire = Yii::$app->params[ 'user.passwordResetTokenExpire' ];
              return $timestamp + $expire >= time();
b0f143c3   Yarik   first commit
268
269
          }
  
b95371cf   Yarik   test
270
271
272
273
274
275
276
          /**
           * @inheritdoc
           */
          public function getId()
          {
              return $this->getPrimaryKey();
          }
b0f143c3   Yarik   first commit
277
  
b95371cf   Yarik   test
278
279
280
281
282
283
284
          /**
           * @inheritdoc
           */
          public function getAuthKey()
          {
              return $this->auth_key;
          }
b0f143c3   Yarik   first commit
285
  
b95371cf   Yarik   test
286
287
288
289
290
291
292
          /**
           * @inheritdoc
           */
          public function validateAuthKey($authKey)
          {
              return $this->getAuthKey() === $authKey;
          }
b0f143c3   Yarik   first commit
293
  
b95371cf   Yarik   test
294
295
296
297
298
299
300
301
302
303
304
          /**
           * Validates password
           *
           * @param string $password password to validate
           *
           * @return boolean if password provided is valid for current user
           */
          public function validatePassword($password)
          {
              return Yii::$app->security->validatePassword($password, $this->password_hash);
          }
b0f143c3   Yarik   first commit
305
  
b95371cf   Yarik   test
306
307
308
309
310
311
312
313
314
          /**
           * Generates password hash from password and sets it to the model
           *
           * @param string $password
           */
          public function setPassword($password)
          {
              $this->password_hash = Yii::$app->security->generatePasswordHash($password);
          }
b0f143c3   Yarik   first commit
315
  
b95371cf   Yarik   test
316
317
318
319
320
321
322
          /**
           * Generates "remember me" authentication key
           */
          public function generateAuthKey()
          {
              $this->auth_key = Yii::$app->security->generateRandomString();
          }
b0f143c3   Yarik   first commit
323
  
b95371cf   Yarik   test
324
325
326
327
328
329
330
          /**
           * Generates new password reset token
           */
          public function generatePasswordResetToken()
          {
              $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
          }
b0f143c3   Yarik   first commit
331
  
b95371cf   Yarik   test
332
333
334
335
336
337
338
          /**
           * Removes password reset token
           */
          public function removePasswordResetToken()
          {
              $this->password_reset_token = NULL;
          }
b0f143c3   Yarik   first commit
339
  
e8236f44   Yarik   test
340
341
          /**
           * Returns name of the User
e8236f44   Yarik   test
342
343
           * @return string
           */
b95371cf   Yarik   test
344
345
346
347
          public function getUserName()
          {
              return $this->username;
          }
b0f143c3   Yarik   first commit
348
  
e8236f44   Yarik   test
349
350
          /**
           * Return array of all User's roles
e8236f44   Yarik   test
351
352
           * @return array
           */
b95371cf   Yarik   test
353
354
355
356
357
358
          public function getRoles()
          {
              $auth = \Yii::$app->authManager;
              $roles = $this->getRoleChildrenRecursive($auth->getRolesByUser($this->id), $auth);
              return $roles;
          }
b0f143c3   Yarik   first commit
359
  
e8236f44   Yarik   test
360
          /**
4c9663e0   Yarik   test
361
362
363
364
           * @param       Role[]           $roles User roles returned by
           *                                      [ManagerInterface]->[getRolesByUser($id)]
           * @param       ManagerInterface $auth  Auth manager
           * @param array                  $result
e8236f44   Yarik   test
365
366
367
           *
           * @return array
           */
b95371cf   Yarik   test
368
          protected function getRoleChildrenRecursive($roles, $auth, $result = [ ])
b0f143c3   Yarik   first commit
369
          {
b95371cf   Yarik   test
370
371
372
373
374
375
376
              if(is_array($roles) && !empty( $roles )) {
                  foreach($roles as $role => $item) {
                      if(!( $item instanceof \yii\rbac\Role )) {
                          continue;
                      }
                      $result[] = $role;
                      $result = self::getRoleChildrenRecursive($auth->getChildren($role), $auth, $result);
b0f143c3   Yarik   first commit
377
                  }
b95371cf   Yarik   test
378
379
380
                  return $result;
              } else {
                  return $result;
b0f143c3   Yarik   first commit
381
              }
b0f143c3   Yarik   first commit
382
          }
cd6bd007   Yarik   test
383
  
e8236f44   Yarik   test
384
385
          /**
           * Return UserInfo for this User
e8236f44   Yarik   test
386
387
           * @return \yii\db\ActiveQuery
           */
b95371cf   Yarik   test
388
389
          public function getUserInfo()
          {
4ed1f788   Yarik   test
390
391
              return $this->hasOne(UserInfo::className(), [ 'user_id' => 'id' ])
                          ->inverseOf('user');
b95371cf   Yarik   test
392
          }
51e0a262   Yarik   test
393
  
e8236f44   Yarik   test
394
395
          /**
           * Check if User is Performer
e8236f44   Yarik   test
396
           * <i>currently in development mode</i>
e8236f44   Yarik   test
397
398
           * @return bool
           */
b95371cf   Yarik   test
399
400
401
402
          public function getIsPerformer()
          {
              return true;
          }
033f9331   Administrator   09.02.16
403
  
e8236f44   Yarik   test
404
405
          /**
           * Return CompanyInfo for this User
e8236f44   Yarik   test
406
407
           * @return \yii\db\ActiveQuery
           */
47559a4b   Yarik   test
408
409
410
411
          public function getCompanyInfo()
          {
              return $this->hasOne(CompanyInfo::className(), [ 'user_id' => 'id' ]);
          }
658d13d4   Administrator   09.02.16
412
  
e8236f44   Yarik   test
413
414
          /**
           * Return array of User's phones
e8236f44   Yarik   test
415
416
           * @return array
           */
a02e2fdb   Yarik   test
417
418
419
          public function getPhones()
          {
              return Fields::getData($this->id, self::className(), 'phone');
f6ea8941   Administrator   09.02.16
420
421
          }
  
e8236f44   Yarik   test
422
423
          /**
           * Return array of User's site
e8236f44   Yarik   test
424
425
           * @return array
           */
a02e2fdb   Yarik   test
426
427
428
          public function getSite()
          {
              return Fields::getData($this->id, self::className(), 'site');
f6ea8941   Administrator   09.02.16
429
430
          }
  
e8236f44   Yarik   test
431
432
          /**
           * Return full address of User in view like:
e8236f44   Yarik   test
433
434
435
           * <code>{country}, {city}, {street}, {house}</code>
           * @return string
           */
a02e2fdb   Yarik   test
436
437
438
          public function getAddress()
          {
              return $this->userInfo->country . ', ' . $this->userInfo->city . ', ' . $this->companyInfo->street . ', ' . $this->companyInfo->house;
f6ea8941   Administrator   09.02.16
439
440
          }
  
e8236f44   Yarik   test
441
442
          /**
           * Return relative interval of time from User registration date until now.
e8236f44   Yarik   test
443
444
           * @return string
           */
47559a4b   Yarik   test
445
446
          public function getLiveTime()
          {
47559a4b   Yarik   test
447
448
              $now = new \DateTime('now');
              $date1 = new \DateTime(date('Y-m-d H:i:s', $this->created_at));
eb7e82fb   Administrator   29.02.16
449
450
              $result = explode(',', \Yii::$app->formatter->asDuration($date1->diff($now)));
  
4ed1f788   Yarik   test
451
              if($result >= 4) {
eb7e82fb   Administrator   29.02.16
452
453
454
                  array_splice($result, 2);
              }
  
4ed1f788   Yarik   test
455
              return implode(',', $result);
47559a4b   Yarik   test
456
          }
b95371cf   Yarik   test
457
  
e8236f44   Yarik   test
458
459
          /**
           * Check if User is Customer
e8236f44   Yarik   test
460
           * <i>currently in development</i>
e8236f44   Yarik   test
461
462
           * @return bool
           */
b95371cf   Yarik   test
463
464
465
466
467
          public function getIsCustomer()
          {
              return true;
          }
  
e8236f44   Yarik   test
468
469
          /**
           * Return array of payments types accepted by the user.
e8236f44   Yarik   test
470
471
           * @return ActiveQuery
           */
b95371cf   Yarik   test
472
473
474
475
476
477
          public function getPayments()
          {
              return $this->hasMany(Payment::className(), [ 'payment_id' => 'payment_id' ])
                          ->viaTable('user_payment', [ 'user_id' => 'id' ]);
          }
  
e8236f44   Yarik   test
478
479
          /**
           * Return array of Payment IDs, accepted by the user.
e8236f44   Yarik   test
480
481
           * @return integer[]
           */
b95371cf   Yarik   test
482
483
484
485
486
487
488
          public function getPaymentInput()
          {
              return $this->getPayments()
                          ->asArray()
                          ->column();
          }
  
e8236f44   Yarik   test
489
490
491
492
493
          /**
           * Setter which allow to set User's payment ID's for further saving to the DB.
           *
           * @param integer[] $value
           */
b95371cf   Yarik   test
494
495
496
497
498
          public function setPaymentInput($value)
          {
              $this->paymentInput = $value;
          }
  
e8236f44   Yarik   test
499
500
          /**
           * Return array of Specializations in which the User works.
e8236f44   Yarik   test
501
502
           * @return ActiveQuery
           */
b95371cf   Yarik   test
503
504
505
506
507
508
          public function getSpecializations()
          {
              return $this->hasMany(Specialization::className(), [ 'specialization_id' => 'specialization_id' ])
                          ->viaTable('user_specialization', [ 'user_id' => 'id' ]);
          }
  
e8236f44   Yarik   test
509
510
          /**
           * Return array of User's blogs
e8236f44   Yarik   test
511
512
           * @return ActiveQuery
           */
376a557b   Administrator   09.02.16
513
514
515
516
517
          public function getBlog()
          {
              return $this->hasMany(Blog::className(), [ 'user_id' => 'id' ]);
          }
  
e8236f44   Yarik   test
518
519
          /**
           * Return array of User's jobs.
e8236f44   Yarik   test
520
521
           * @return ActiveQuery
           */
f6ea8941   Administrator   09.02.16
522
523
          public function getJobs()
          {
38a6e1dd   Yarik   test
524
              return $this->hasMany(Job::className(), [ 'user_id' => 'id' ])->orderBy(['current' => SORT_DESC]);
f6ea8941   Administrator   09.02.16
525
526
          }
  
e8236f44   Yarik   test
527
528
          /**
           * Return ActiveRecord of current User's place of work.
e8236f44   Yarik   test
529
530
531
532
           * @return ActiveQuery
           */
          public function getCurrentJob()
          {
4c9663e0   Yarik   test
533
534
              return $this->hasOne(Job::className(), [ 'user_id' => 'id' ])
                          ->where([ 'current' => 1 ]);
e8236f44   Yarik   test
535
536
537
538
          }
  
          /**
           * Return array of User's specialization IDs
e8236f44   Yarik   test
539
540
           * @return integer[]
           */
b95371cf   Yarik   test
541
542
543
544
          public function getSpecializationInput()
          {
              return $this->getSpecializations()
                          ->asArray()
4c9663e0   Yarik   test
545
                          ->indexBy('specialization_id')
b95371cf   Yarik   test
546
547
548
                          ->column();
          }
  
e8236f44   Yarik   test
549
550
551
552
553
          /**
           * Setter which allow to set User's specializations for further saving to the DB.
           *
           * @param integer[] $value
           */
b95371cf   Yarik   test
554
555
556
557
          public function setSpecializationInput($value)
          {
              $this->specializationInput = $value;
          }
51e0a262   Yarik   test
558
  
e8236f44   Yarik   test
559
560
          /**
           * Return array of User's portfolios.
e8236f44   Yarik   test
561
562
           * @return ActiveQuery
           */
a02e2fdb   Yarik   test
563
564
565
566
567
          public function getPortfolios()
          {
              return $this->hasMany(Portfolio::className(), [ 'user_id' => 'id' ]);
          }
  
e8236f44   Yarik   test
568
569
          /**
           * Return array of User's projects.
e8236f44   Yarik   test
570
571
           * @return ActiveQuery
           */
a02e2fdb   Yarik   test
572
573
574
575
576
          public function getProjects()
          {
              return $this->hasMany(Project::className(), [ 'user_id' => 'id' ]);
          }
  
e8236f44   Yarik   test
577
578
          /**
           * Return array of company's Team members.
e8236f44   Yarik   test
579
580
           * @return ActiveQuery
           */
a02e2fdb   Yarik   test
581
582
583
584
585
          public function getTeams()
          {
              return $this->hasMany(Team::className(), [ 'user_id' => 'id' ]);
          }
  
e8236f44   Yarik   test
586
587
          /**
           * Return array of company's Vacancies.
e8236f44   Yarik   test
588
589
           * @return ActiveQuery
           */
a02e2fdb   Yarik   test
590
591
          public function getVacancies()
          {
4ed1f788   Yarik   test
592
593
              return $this->hasMany(Vacancy::className(), [ 'user_id' => 'id' ])
                          ->inverseOf('user');
a02e2fdb   Yarik   test
594
595
          }
  
be662d1f   Yarik   test
596
597
          /**
           * Return all user's galleries
be662d1f   Yarik   test
598
599
           * @return ActiveQuery
           */
2e35d6bd   Yarik   test
600
601
602
603
604
          public function getGalleries()
          {
              return $this->hasMany(Gallery::className(), [ 'user_id' => 'id' ]);
          }
  
be662d1f   Yarik   test
605
606
          /**
           * Return company info or user info according to user type
be662d1f   Yarik   test
607
608
           * @return ActiveQuery
           */
3dc20ff7   Administrator   24.02.16
609
610
          public function getOwner()
          {
4ed1f788   Yarik   test
611
              if($this->type == 2) {
3dc20ff7   Administrator   24.02.16
612
                  return $this->hasOne(CompanyInfo::className(), [ 'user_id' => 'id' ]);
4ed1f788   Yarik   test
613
614
              } else {
                  return $this->hasOne(UserInfo::className(), [ 'user_id' => 'id' ]);
3dc20ff7   Administrator   24.02.16
615
              }
3dc20ff7   Administrator   24.02.16
616
617
          }
  
be662d1f   Yarik   test
618
619
          /**
           * Return company name or user firstname and lastname according to user type
be662d1f   Yarik   test
620
621
           * @return string
           */
4ed1f788   Yarik   test
622
623
624
          public function getName()
          {
              if($this->type == 2) {
3ea37908   Administrator   01.03.16
625
                  return $this->companyInfo->name;
4ed1f788   Yarik   test
626
627
              } else {
                  return $this->firstname . ' ' . $this->lastname;
3ea37908   Administrator   01.03.16
628
              }
4ed1f788   Yarik   test
629
          }
3ea37908   Administrator   01.03.16
630
  
2fd40ee7   Yarik   test
631
          /**
be662d1f   Yarik   test
632
           * Return all comments to user
2fd40ee7   Yarik   test
633
634
           * @return ActiveQuery
           */
4ed1f788   Yarik   test
635
636
          public function getComments()
          {
2fd40ee7   Yarik   test
637
638
639
640
641
642
643
644
645
              return $this->hasMany(Comment::className(), [
                  'model_id' => 'id',
              ])
                          ->andWhere([
                              'comment.model' => $this->className(),
                          ]);
          }
  
          /**
be662d1f   Yarik   test
646
           * Return all ratings to user
2fd40ee7   Yarik   test
647
648
649
650
651
652
653
654
655
           * @return ActiveQuery
           */
          public function getCommentRating()
          {
              return $this->hasMany(Rating::className(), [ 'model_id' => 'comment_id' ])
                          ->via('comments')
                          ->andWhere([
                              'rating.model' => Comment::className(),
                          ]);
3dc20ff7   Administrator   24.02.16
656
657
          }
  
be662d1f   Yarik   test
658
659
660
          /**
           * Return user's rating<br>
           * <b>Available only for PostgreSQL</b>
be662d1f   Yarik   test
661
662
663
           * @return int
           * @throws InvalidConfigException
           */
4ed1f788   Yarik   test
664
665
666
667
668
          public function getRatingPG()
          {
              if(\Yii::$app->db->driverName != 'pgsql') {
                  throw new InvalidConfigException('This method is available only in PostgreSQL');
              }
2fd40ee7   Yarik   test
669
              return $this->getCommentRating()
5077a0ec   Yarik   test
670
                          ->select([ 'rating' => 'ROUND(SUM("rating"."value")/COUNT("rating"."rating_id")::numeric, 2)' ])
2fd40ee7   Yarik   test
671
672
673
674
675
676
677
                          ->andWhere([
                              'not',
                              [ 'rating.value' => NULL ],
                          ])
                          ->scalar();
          }
  
be662d1f   Yarik   test
678
679
          /**
           * Recalculate rating and write it to db.
be662d1f   Yarik   test
680
           * <b>Only for PostgreSQL</b>
be662d1f   Yarik   test
681
           * @see \common\models\User::getRatingPG()
be662d1f   Yarik   test
682
683
           * @throws InvalidConfigException
           */
2fd40ee7   Yarik   test
684
685
686
687
688
689
          public function updateRating()
          {
              if($rating = $this->getRatingPG()) {
                  $this->userInfo->rating = $rating;
                  $this->userInfo->save();
              }
4ed1f788   Yarik   test
690
          }
3dc20ff7   Administrator   24.02.16
691
  
be662d1f   Yarik   test
692
693
694
695
696
697
698
          public function getLastVisit()
          {
              return \Yii::$app->formatter->asRelativeTime($this->userInfo->date_visit);
          }
  
          /**
           * Return link to user personal page according to user type
be662d1f   Yarik   test
699
700
701
702
703
           * @return string
           */
          public function getLink($page = 'common')
          {
              if($this->type == 2) {
5077a0ec   Yarik   test
704
705
706
707
                  return Url::to([
                      'company/' . $page,
                      'company_id' => $this->id,
                  ]);
be662d1f   Yarik   test
708
              } else {
5077a0ec   Yarik   test
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
                  return Url::to([
                      'performer/' . $page,
                      'performer_id' => $this->id,
                  ]);
              }
          }
  
          public function getIsBookmarked($type = Bookmark::TYPE_PERFORMER)
          {
              if(!empty( \Yii::$app->user->identity )) {
                  if(!empty( $this->hasOne(Bookmark::className(), [ 'model_id' => 'id' ])
                                  ->andWhere([
                                      'model'   => $this->className(),
                                      'user_id' => \Yii::$app->user->getId(),
                                      'type'    => $type,
                                  ])
                                  ->one() )
                  ) {
                      return true;
                  } else {
                      return false;
                  }
              } else {
                  return false;
be662d1f   Yarik   test
733
734
735
              }
          }
  
3c618303   Yarik   test
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
          public function getBookmarks()
          {
              return $this->hasMany(Bookmark::className(), [ 'user_id' => 'id' ]);
          }
  
          public function getBookmarksPerformers()
          {
              return $this->hasMany(User::className(), [ 'id' => 'model_id' ])
                          ->viaTable('{{%bookmark}}', [ 'user_id' => 'id' ], function($query) {
                              /**
                               * @var ActiveQuery $query
                               */
                              $query->andWhere([
                                  'model' => User::className(),
                                  'type'  => Bookmark::TYPE_PERFORMER,
                              ]);
                          });
          }
  
          public function getBookmarksCustomers()
          {
              return $this->hasMany(User::className(), [ 'id' => 'model_id' ])
                          ->viaTable('{{%bookmark}}', [ 'user_id' => 'id' ], function($query) {
                              /**
                               * @var ActiveQuery $query
                               */
                              $query->andWhere([
                                  'model' => User::className(),
                                  'type'  => Bookmark::TYPE_CUSTOMER,
                              ]);
                          });
          }
  
          public function getBookmarksProjects()
          {
              return $this->hasMany(Project::className(), [ 'project_id' => 'model_id' ])
                          ->viaTable('{{%bookmark}}', [ 'user_id' => 'id' ], function($query) {
                              /**
                               * @var ActiveQuery $query
                               */
                              $query->andWhere([
                                  'model' => Project::className(),
                                  'type'  => Bookmark::TYPE_PROJECT,
                              ]);
                          });
          }
38a6e1dd   Yarik   test
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  
          public function getBookmarksVacancies()
          {
              return $this->hasMany(Vacancy::className(), [ 'vacancy_id' => 'model_id' ])
                          ->viaTable('{{%bookmark}}', [ 'user_id' => 'id' ], function($query) {
                              /**
                               * @var ActiveQuery $query
                               */
                              $query->andWhere([
                                  'model' => Vacancy::className(),
                                  'type'  => Bookmark::TYPE_VACANCY,
                              ]);
                          });
          }
  
          public function getChatCount()
          {
              return Chat::find()
                         ->select('COUNT(*)')
                         ->where([
                             'or',
                             [ 'from_user' => $this->id ],
                             [ 'to_user' => $this->id ],
                         ])
                         ->scalar();
          }
51e0a262   Yarik   test
808
      }