|
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\base\InvalidConfigException; |
|
24
|
|
|
use yii\base\InvalidParamException; |
|
25
|
|
|
use yii\db\ActiveRecord; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @property int $user_id |
|
29
|
|
|
* @property string $name |
|
30
|
|
|
* @property string $public_email |
|
31
|
|
|
* @property string $gravatar_email |
|
32
|
|
|
* @property string $gravatar_id |
|
33
|
|
|
* @property string $location |
|
34
|
|
|
* @property string $website |
|
35
|
|
|
* @property string $bio |
|
36
|
|
|
* @property string $timezone |
|
37
|
|
|
* @property User $user |
|
38
|
|
|
*/ |
|
39
|
|
|
class Profile extends ActiveRecord |
|
40
|
|
|
{ |
|
41
|
|
|
use ModuleAwareTrait; |
|
42
|
|
|
use ContainerAwareTrait; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
* |
|
47
|
|
|
* @throws InvalidParamException |
|
48
|
|
|
* @throws InvalidConfigException |
|
49
|
|
|
*/ |
|
50
|
7 |
|
public function beforeSave($insert) |
|
51
|
|
|
{ |
|
52
|
7 |
|
if ($this->isAttributeChanged('gravatar_email')) { |
|
53
|
|
|
$this->setAttribute( |
|
54
|
|
|
'gravatar_id', |
|
55
|
|
|
$this->make(GravatarHelper::class)->buildId(trim($this->getAttribute('gravatar_email'))) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
7 |
|
return parent::beforeSave($insert); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
11 |
|
public static function tableName() |
|
66
|
|
|
{ |
|
67
|
11 |
|
return '{{%profile}}'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidConfigException |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function rules() |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
1 |
|
'bioString' => ['bio', 'string'], |
|
79
|
|
|
'timeZoneValidation' => [ |
|
80
|
1 |
|
'timezone', |
|
81
|
|
|
function ($attribute) { |
|
82
|
|
|
if ($this->make(TimeZoneValidator::class, [$this->{$attribute}])->validate() === false) { |
|
83
|
|
|
$this->addError($attribute, Yii::t('usuario', 'Time zone is not valid')); |
|
84
|
|
|
} |
|
85
|
1 |
|
}, |
|
86
|
|
|
], |
|
87
|
|
|
'publicEmailPattern' => ['public_email', 'email'], |
|
88
|
|
|
'gravatarEmailPattern' => ['gravatar_email', 'email'], |
|
89
|
|
|
'websiteUrl' => ['website', 'url'], |
|
90
|
|
|
'nameLength' => ['name', 'string', 'max' => 255], |
|
91
|
|
|
'publicEmailLength' => ['public_email', 'string', 'max' => 255], |
|
92
|
|
|
'gravatarEmailLength' => ['gravatar_email', 'string', 'max' => 255], |
|
93
|
|
|
'locationLength' => ['location', 'string', 'max' => 255], |
|
94
|
|
|
'websiteLength' => ['website', 'string', 'max' => 255], |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function attributeLabels() |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
1 |
|
'name' => Yii::t('usuario', 'Name'), |
|
105
|
1 |
|
'public_email' => Yii::t('usuario', 'Email (public)'), |
|
106
|
1 |
|
'gravatar_email' => Yii::t('usuario', 'Gravatar email'), |
|
107
|
1 |
|
'location' => Yii::t('usuario', 'Location'), |
|
108
|
1 |
|
'website' => Yii::t('usuario', 'Website'), |
|
109
|
1 |
|
'bio' => Yii::t('usuario', 'Bio'), |
|
110
|
1 |
|
'timezone' => Yii::t('usuario', 'Time zone'), |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the User's timezone. |
|
116
|
|
|
* |
|
117
|
|
|
* @return DateTimeZone |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getTimeZone() |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
|
|
return new DateTimeZone($this->timezone); |
|
123
|
|
|
} catch (Exception $e) { |
|
124
|
|
|
return new DateTimeZone(Yii::$app->getTimeZone()); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the User's timezone. |
|
130
|
|
|
* |
|
131
|
|
|
* @param DateTimeZone $timezone |
|
132
|
|
|
* |
|
133
|
|
|
* @throws InvalidParamException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setTimeZone(DateTimeZone $timezone) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->setAttribute('timezone', $timezone->getName()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get User's local time. |
|
142
|
|
|
* |
|
143
|
|
|
* @param DateTime|null $dateTime |
|
144
|
|
|
* |
|
145
|
|
|
* @return DateTime |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getLocalTimeZone(DateTime $dateTime = null) |
|
148
|
|
|
{ |
|
149
|
|
|
return $dateTime === null ? new DateTime() : $dateTime->setTimezone($this->getTimeZone()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @throws InvalidConfigException |
|
154
|
|
|
* @return \yii\db\ActiveQuery |
|
155
|
|
|
*/ |
|
156
|
7 |
|
public function getUser() |
|
157
|
|
|
{ |
|
158
|
7 |
|
return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param int $size |
|
163
|
|
|
* |
|
164
|
|
|
* @throws InvalidConfigException |
|
165
|
|
|
* @return mixed |
|
166
|
|
|
*/ |
|
167
|
3 |
|
public function getAvatarUrl($size = 200) |
|
168
|
|
|
{ |
|
169
|
3 |
|
return $this->make(GravatarHelper::class)->getUrl($this->gravatar_id, $size); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return ProfileQuery |
|
174
|
|
|
*/ |
|
175
|
11 |
|
public static function find() |
|
176
|
|
|
{ |
|
177
|
11 |
|
return new ProfileQuery(static::class); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|