Profile.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace thread\modules\user\models;
use Yii;
//
use thread\app\base\models\ActiveRecord;
use thread\modules\user\User as ParentModule;
use thread\modules\user\models\User as UserModel;
/**
* Class Profile
*
* @package thread\modules\user\models
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class Profile extends ActiveRecord
{
/**
* @var
*/
public static $commonQuery = query\ProfileQuery::class;
/**
* @return string
*/
public static function getDb()
{
return ParentModule::getDb();
}
/**
* @return string
*/
public static function tableName()
{
return '{{%user_profile}}';
}
/**
* @return array
*/
public function rules()
{
return [
[['user_id'], 'unique'],
['user_id', 'exist', 'targetClass' => UserModel::className(), 'targetAttribute' => 'id'],
[['user_id', 'created_at', 'updated_at'], 'integer'],
[['first_name', 'last_name', 'avatar', 'preferred_language'], 'string', 'max' => 255],
[['phone', 'company', 'website'], 'string', 'max' => 50],
];
}
/**
* @return array
*/
public function scenarios()
{
return [
'backend' => ['first_name', 'last_name', 'avatar', 'preferred_language', 'phone', 'company', 'website'],
'frontend' => ['first_name', 'last_name', 'avatar', 'preferred_language', 'phone', 'company', 'website'],
'ownEdit' => ['first_name', 'last_name', 'avatar', 'preferred_language', 'phone', 'company', 'website'],
'basicCreate' => ['user_id', 'first_name', 'last_name', 'company', 'website'],
];
}
/**
* @return array
*/
public function attributeLabels()
{
return [
'first_name' => Yii::t('user', 'First name'),
'last_name' => Yii::t('user', 'Last name'),
'avatar' => Yii::t('user', 'Avatar'),
'preferred_language' => Yii::t('user', 'Preferred language'),
'created_at' => Yii::t('app', 'Create time'),
'updated_at' => Yii::t('app', 'Update time'),
'phone' => Yii::t('app', 'Phone'),
'website' => Yii::t('user', 'Website'),
'company' => Yii::t('user', 'Company'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(UserModel::className(), ['id' => 'user_id']);
}
/**
* @return null|string
*/
public function getAvatarImage()
{
/**
* @var $module \thread\modules\user\User
*/
$module = Yii::$app->getModule('user');
$path = $module->getAvatarUploadPath($this->user);
$url = $module->getAvatarUploadUrl($this->user);
$image = null;
if (!empty($this->avatar) && is_file($path . '/' . $this->avatar)) {
$image = $url . '/' . $this->avatar;
}
return $image;
}
/**
* @param $user_id
* @return mixed
*/
public static function findByUserId($user_id)
{
return self::find()->user_id($user_id)->one();
}
/**
* @return string
*/
public function getFullName()
{
return $this->first_name . ' ' . $this->last_name;
}
}