1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Model; |
13
|
|
|
|
14
|
|
|
use Da\User\Helper\GravatarHelper; |
15
|
|
|
use Da\User\Query\ProfileQuery; |
16
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
17
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
18
|
|
|
use Da\User\Validator\TimeZoneValidator; |
19
|
|
|
use DateTime; |
20
|
|
|
use DateTimeZone; |
21
|
|
|
use Exception; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\db\ActiveRecord; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @property int $user_id |
27
|
|
|
* @property string $name |
28
|
|
|
* @property string $public_email |
29
|
|
|
* @property string $gravatar_email |
30
|
|
|
* @property string $gravatar_id |
31
|
|
|
* @property string $location |
32
|
|
|
* @property string $website |
33
|
|
|
* @property string $bio |
34
|
|
|
* @property string $timezone |
35
|
|
|
* @property User $user |
36
|
|
|
*/ |
37
|
|
|
class Profile extends ActiveRecord |
38
|
|
|
{ |
39
|
|
|
use ModuleAwareTrait; |
40
|
|
|
use ContainerAwareTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
3 |
|
public function beforeSave($insert) |
46
|
|
|
{ |
47
|
3 |
|
if ($this->isAttributeChanged('gravatar_email')) { |
48
|
|
|
$this->setAttribute( |
49
|
|
|
'gravatar_id', |
50
|
|
|
$this->make(GravatarHelper::class)->buildId(trim($this->getAttribute('gravatar_email'))) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
return parent::beforeSave($insert); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
4 |
|
public static function tableName() |
61
|
|
|
{ |
62
|
4 |
|
return '{{%profile}}'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function rules() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'bioString' => ['bio', 'string'], |
72
|
|
|
'timeZoneValidation' => [ |
73
|
|
|
'timezone', |
74
|
|
|
function ($attribute) { |
75
|
|
|
if ($this->make(TimeZoneValidator::class, [$this->{$attribute}])->validate() === false) { |
76
|
|
|
$this->addError($attribute, Yii::t('usuario', 'Time zone is not valid')); |
77
|
|
|
} |
78
|
|
|
}, |
79
|
|
|
], |
80
|
|
|
'publicEmailPattern' => ['public_email', 'email'], |
81
|
|
|
'gravatarEmailPattern' => ['gravatar_email', 'email'], |
82
|
|
|
'websiteUrl' => ['website', 'url'], |
83
|
|
|
'nameLength' => ['name', 'string', 'max' => 255], |
84
|
|
|
'publicEmailLength' => ['public_email', 'string', 'max' => 255], |
85
|
|
|
'gravatarEmailLength' => ['gravatar_email', 'string', 'max' => 255], |
86
|
|
|
'locationLength' => ['location', 'string', 'max' => 255], |
87
|
|
|
'websiteLength' => ['website', 'string', 'max' => 255], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function attributeLabels() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
'name' => Yii::t('usuario', 'Name'), |
98
|
|
|
'public_email' => Yii::t('usuario', 'Email (public)'), |
99
|
|
|
'gravatar_email' => Yii::t('usuario', 'Gravatar email'), |
100
|
|
|
'location' => Yii::t('usuario', 'Location'), |
101
|
|
|
'website' => Yii::t('usuario', 'Website'), |
102
|
|
|
'bio' => Yii::t('usuario', 'Bio'), |
103
|
|
|
'timezone' => Yii::t('usuario', 'Time zone'), |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the User's timezone. |
109
|
|
|
* |
110
|
|
|
* @return DateTimeZone |
111
|
|
|
*/ |
112
|
|
|
public function getTimeZone() |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
return new DateTimeZone($this->timezone); |
116
|
|
|
} catch (Exception $e) { |
117
|
|
|
return new DateTimeZone(Yii::$app->getTimeZone()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the User's timezone. |
123
|
|
|
* |
124
|
|
|
* @param DateTimeZone $timezone |
125
|
|
|
*/ |
126
|
|
|
public function setTimeZone(DateTimeZone $timezone) |
127
|
|
|
{ |
128
|
|
|
$this->setAttribute('timezone', $timezone->getName()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get User's local time. |
133
|
|
|
* |
134
|
|
|
* @param DateTime|null $dateTime |
135
|
|
|
* |
136
|
|
|
* @return DateTime |
137
|
|
|
*/ |
138
|
|
|
public function getLocalTimeZone(DateTime $dateTime = null) |
139
|
|
|
{ |
140
|
|
|
return $dateTime === null ? new DateTime() : $dateTime->setTimezone($this->getTimeZone()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return \yii\db\ActiveQuery |
145
|
|
|
*/ |
146
|
3 |
|
public function getUser() |
147
|
|
|
{ |
148
|
3 |
|
return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param int $size |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function getAvatarUrl($size = 200) |
157
|
|
|
{ |
158
|
|
|
return $this->make(GravatarHelper::class)->getUrl($this->gravatar_id, $size); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return ProfileQuery |
163
|
|
|
*/ |
164
|
3 |
|
public static function find() |
165
|
|
|
{ |
166
|
3 |
|
return new ProfileQuery(static::class); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|