1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models\forms; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\DynamicModel; |
7
|
|
|
use yii\helpers\FileHelper; |
8
|
|
|
use yii\web\UploadedFile; |
9
|
|
|
use app\models\User; |
10
|
|
|
|
11
|
|
|
class SignupProviderForm extends \yii\base\Model |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $email; |
17
|
|
|
/** |
18
|
|
|
* @var \app\models\User |
19
|
|
|
*/ |
20
|
|
|
private $user = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param User $user |
24
|
|
|
*/ |
25
|
|
|
public function __construct($user, $email) |
26
|
|
|
{ |
27
|
|
|
$this->user = $user; |
28
|
|
|
$this->email = $email; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function rules() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
['email', 'filter', 'filter' => 'trim'], |
38
|
|
|
['email', 'required'], |
39
|
|
|
['email', 'string', 'max' => 255], |
40
|
|
|
['email', 'email'], |
41
|
|
|
['email', 'unique', |
42
|
|
|
'targetClass' => '\app\models\User', |
43
|
|
|
'message' => Yii::t('app.validators', 'This email address has already been taken') |
44
|
|
|
], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function attributeLabels() |
52
|
|
|
{ |
53
|
|
|
return (new User())->attributeLabels(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create manually UploadedFile instance by file path |
58
|
|
|
* |
59
|
|
|
* @param string $path file path |
60
|
|
|
* @return UploadedFile |
61
|
|
|
*/ |
62
|
|
|
private function makeUploadedFile($path) |
63
|
|
|
{ |
64
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'app'); |
65
|
|
|
file_put_contents($tmpFile, file_get_contents($path)); |
66
|
|
|
|
67
|
|
|
$uploadedFile = new UploadedFile(); |
68
|
|
|
$uploadedFile->name = pathinfo($path, PATHINFO_BASENAME); |
69
|
|
|
$uploadedFile->tempName = $tmpFile; |
70
|
|
|
$uploadedFile->type = FileHelper::getMimeType($tmpFile); |
71
|
|
|
$uploadedFile->size = filesize($tmpFile); |
72
|
|
|
$uploadedFile->error = 0; |
73
|
|
|
|
74
|
|
|
return $uploadedFile; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Save photo |
79
|
|
|
* |
80
|
|
|
* @param \app\models\UserProfile $profile |
81
|
|
|
* @param string $photo |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
private function savePhoto($profile, $photo) |
85
|
|
|
{ |
86
|
|
|
$file = $this->makeUploadedFile($photo); |
87
|
|
|
$model = new DynamicModel(compact('file')); |
88
|
|
|
$model->addRule('file', 'image', $profile->fileRules('photo', true))->validate(); |
|
|
|
|
89
|
|
|
if (!$model->hasErrors()) { |
90
|
|
|
$profile->createFile('photo', $file->tempName, $model->file->name); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Save user |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function saveUser() |
100
|
|
|
{ |
101
|
|
|
$this->user->email = $this->email; |
102
|
|
|
|
103
|
|
|
$profile = $this->user->profile; |
|
|
|
|
104
|
|
|
if ($profile->isNewRecord && !empty($profile->photo)) { |
105
|
|
|
$this->savePhoto($profile, $profile->photo); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($this->user->save()) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->addErrors($this->user->getErrors()); |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Login |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function login() |
122
|
|
|
{ |
123
|
|
|
return $this->user->authorize(true); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Signs user up |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function signup() |
132
|
|
|
{ |
133
|
|
|
if ($this->validate()) { |
134
|
|
|
return $this->saveUser(); |
135
|
|
|
} // @codeCoverageIgnore |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sends an email with a link, for confirm the email |
142
|
|
|
* |
143
|
|
|
* @return boolean |
144
|
|
|
*/ |
145
|
|
|
public function sendEmail() |
146
|
|
|
{ |
147
|
|
|
if (!User::isTokenValid($this->user->email_confirm_token)) { |
148
|
|
|
$this->user->generateEmailConfirmToken(); |
149
|
|
|
$this->user->updateAttributes([ |
150
|
|
|
'email_confirm_token' => $this->user->email_confirm_token, |
151
|
|
|
'date_confirm' => $this->user->date_confirm, |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return Yii::$app->notify->sendMessage( |
156
|
|
|
$this->email, |
157
|
|
|
Yii::t('app', 'Activate Your Account'), |
158
|
|
|
'emailConfirmToken', |
159
|
|
|
['user' => $this->user] |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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: