1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\models\forms; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Exception; |
7
|
|
|
use app\models\entity\UserProfile; |
8
|
|
|
|
9
|
|
|
class UserProfileForm extends \yii\base\Model |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
public $user_id; |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $full_name; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $photo; |
23
|
|
|
/** |
24
|
|
|
* @var \app\models\entity\UserProfile |
25
|
|
|
*/ |
26
|
|
|
private $model; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates a form model given a user profile |
30
|
|
|
* |
31
|
|
|
* @param UserProfile $model |
32
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
33
|
|
|
*/ |
34
|
|
|
public function __construct(UserProfile $model, $config = []) |
35
|
1 |
|
{ |
36
|
|
|
$this->model = $model; |
37
|
|
|
|
38
|
1 |
|
$this->user_id = $model->user_id; |
39
|
|
|
$this->full_name = $model->full_name; |
40
|
|
|
$this->photo = $model->photo; |
41
|
|
|
|
42
|
|
|
parent::__construct($config); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array The validation rules |
48
|
|
|
*/ |
49
|
1 |
|
public function rules() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
1 |
|
[['photo'], 'safe'], |
53
|
1 |
|
|
54
|
1 |
|
['full_name', 'string', 'max' => 40], |
55
|
1 |
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function attributeLabels() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
1 |
|
'user_id' => Yii::t('app', 'User'), |
65
|
|
|
'full_name' => Yii::t('app', 'Name'), |
66
|
1 |
|
'photo' => Yii::t('app', 'Photo'), |
67
|
|
|
]; |
68
|
1 |
|
} |
69
|
1 |
|
|
70
|
1 |
|
/** |
71
|
1 |
|
* Get model |
72
|
1 |
|
* |
73
|
|
|
* @return UserProfile |
74
|
|
|
*/ |
75
|
|
|
public function model(): UserProfile |
76
|
|
|
{ |
77
|
|
|
return $this->model; |
78
|
|
|
} |
79
|
1 |
|
|
80
|
|
|
/** |
81
|
1 |
|
* Save profile |
82
|
|
|
* |
83
|
|
|
* @throws Exception |
84
|
|
|
* @return UserProfile |
85
|
1 |
|
*/ |
86
|
|
|
public function save(): UserProfile |
87
|
|
|
{ |
88
|
|
|
$model = $this->model(); |
89
|
|
|
|
90
|
|
|
$model->user_id = $this->user_id; |
91
|
|
|
$model->full_name = $this->full_name; |
92
|
|
|
$model->photo = $this->photo; |
93
|
|
|
|
94
|
|
|
if (!$model->save()) { |
95
|
|
|
throw new Exception(Yii::t('app', 'An error occurred while saving profile')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $model; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|