1 | <?php |
||
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() |
|
160 | |||
161 | /** |
||
162 | * @param int $size |
||
163 | * |
||
164 | * @throws InvalidConfigException |
||
165 | * @return mixed |
||
166 | */ |
||
167 | 3 | public function getAvatarUrl($size = 200) |
|
171 | |||
172 | /** |
||
173 | * @return ProfileQuery |
||
174 | */ |
||
175 | 11 | public static function find() |
|
179 | } |
||
180 |