1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models\forms; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\DynamicModel; |
7
|
|
|
use app\helpers\Upload; |
8
|
|
|
use app\models\User; |
9
|
|
|
|
10
|
|
|
class SignupProviderForm extends \yii\base\Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public $email; |
16
|
|
|
/** |
17
|
|
|
* @var \app\models\User |
18
|
|
|
*/ |
19
|
|
|
private $user = null; |
20
|
|
|
|
21
|
8 |
|
public function __construct($user) |
22
|
|
|
{ |
23
|
8 |
|
$this->user = $user; |
24
|
8 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
8 |
|
public function rules() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
8 |
|
['email', 'filter', 'filter' => 'trim'], |
33
|
|
|
['email', 'required'], |
34
|
|
|
['email', 'string', 'max' => 255], |
35
|
|
|
['email', 'email'], |
36
|
8 |
|
['email', 'unique', |
37
|
8 |
|
'targetClass' => '\app\models\User', |
38
|
8 |
|
'message' => Yii::t('app.validators', 'This email address has already been taken') |
39
|
|
|
], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
7 |
|
public function attributeLabels() |
47
|
|
|
{ |
48
|
7 |
|
return (new User())->attributeLabels(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Save photo |
53
|
|
|
* |
54
|
|
|
* @param \app\models\UserProfile $profile |
55
|
|
|
* @param string $photo |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
6 |
|
private function savePhoto($profile, $photo) |
59
|
|
|
{ |
60
|
6 |
|
$file = Upload::makeUploadedFile($photo); |
61
|
6 |
|
$model = new DynamicModel(compact('file')); |
62
|
6 |
|
$model->addRule('file', 'image', $profile->getFileRules('photo', true))->validate(); |
|
|
|
|
63
|
6 |
|
if (!$model->hasErrors()) { |
64
|
5 |
|
$file = $profile->createFile('photo', $file->tempName, 'photo', true); |
|
|
|
|
65
|
5 |
|
$profile->photo = $file->id; |
66
|
|
|
} |
67
|
6 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Signs user up |
71
|
|
|
* |
72
|
|
|
* @param bool $checkExistEmail |
73
|
|
|
* @return \app\models\User |
74
|
|
|
*/ |
75
|
8 |
|
public function signup($checkExistEmail = true) |
76
|
|
|
{ |
77
|
8 |
|
if ($this->validate($checkExistEmail ? null : [])) { |
78
|
6 |
|
$this->user->email = $this->email; |
79
|
|
|
|
80
|
6 |
|
$profile = $this->user->profile; |
|
|
|
|
81
|
6 |
|
if ($profile->isNewRecord && !empty($profile->photo)) { |
82
|
6 |
|
$this->savePhoto($profile, $profile->photo); |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
if ($this->user->save()) { |
86
|
6 |
|
if ($this->user->authorize(true)) { |
87
|
6 |
|
return $this->user; |
88
|
|
|
} |
89
|
|
|
} // @codeCoverageIgnore |
90
|
|
|
} // @codeCoverageIgnore |
91
|
|
|
|
92
|
2 |
|
return false; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sends an email with a link, for confirm the email |
97
|
|
|
* |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
5 |
|
public function sendEmail() |
101
|
|
|
{ |
102
|
5 |
|
if (!User::isTokenValid($this->user->email_confirm_token)) { |
103
|
5 |
|
$this->user->generateEmailConfirmToken(); |
104
|
5 |
|
$this->user->updateAttributes([ |
105
|
5 |
|
'email_confirm_token' => $this->user->email_confirm_token, |
106
|
5 |
|
'date_confirm' => $this->user->date_confirm, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
return Yii::$app->notify->sendMessage( |
111
|
5 |
|
$this->email, |
112
|
5 |
|
Yii::t('app', 'Activate Your Account'), |
113
|
5 |
|
'emailConfirmToken', |
114
|
5 |
|
['user' => $this->user] |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: