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) |
26
|
|
|
{ |
27
|
|
|
$this->user = $user; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function rules() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
['email', 'filter', 'filter' => 'trim'], |
37
|
|
|
['email', 'required'], |
38
|
|
|
['email', 'string', 'max' => 255], |
39
|
|
|
['email', 'email'], |
40
|
|
|
['email', 'unique', |
41
|
|
|
'targetClass' => '\app\models\User', |
42
|
|
|
'message' => Yii::t('app.validators', 'This email address has already been taken') |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function attributeLabels() |
51
|
|
|
{ |
52
|
|
|
return (new User())->attributeLabels(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create manually UploadedFile instance by file path |
57
|
|
|
* |
58
|
|
|
* @param string $path file path |
59
|
|
|
* @return UploadedFile |
60
|
|
|
*/ |
61
|
|
|
private function makeUploadedFile($path) |
62
|
|
|
{ |
63
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'app'); |
64
|
|
|
file_put_contents($tmpFile, file_get_contents($path)); |
65
|
|
|
|
66
|
|
|
$uploadedFile = new UploadedFile(); |
67
|
|
|
$uploadedFile->name = pathinfo($path, PATHINFO_BASENAME); |
68
|
|
|
$uploadedFile->tempName = $tmpFile; |
69
|
|
|
$uploadedFile->type = FileHelper::getMimeType($tmpFile); |
70
|
|
|
$uploadedFile->size = filesize($tmpFile); |
71
|
|
|
$uploadedFile->error = 0; |
72
|
|
|
|
73
|
|
|
return $uploadedFile; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Save photo |
78
|
|
|
* |
79
|
|
|
* @param \app\models\UserProfile $profile |
80
|
|
|
* @param string $photo |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
private function savePhoto($profile, $photo) |
84
|
|
|
{ |
85
|
|
|
$file = $this->makeUploadedFile($photo); |
86
|
|
|
$model = new DynamicModel(compact('file')); |
87
|
|
|
$model->addRule('file', 'image', $profile->fileRules('photo', true))->validate(); |
|
|
|
|
88
|
|
|
if (!$model->hasErrors()) { |
89
|
|
|
$profile->createFile('photo', $file->tempName, $model->file->name); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Signs user up |
95
|
|
|
* |
96
|
|
|
* @param bool $checkExistEmail |
97
|
|
|
* @return \app\models\User |
98
|
|
|
*/ |
99
|
|
|
public function signup($checkExistEmail = true) |
100
|
|
|
{ |
101
|
|
|
if ($this->validate($checkExistEmail ? null : [])) { |
102
|
|
|
$this->user->email = $this->email; |
103
|
|
|
|
104
|
|
|
$profile = $this->user->profile; |
|
|
|
|
105
|
|
|
if ($profile->isNewRecord && !empty($profile->photo)) { |
106
|
|
|
$this->savePhoto($profile, $profile->photo); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->user->save()) { |
110
|
|
|
if ($this->user->authorize(true)) { |
111
|
|
|
return $this->user; |
112
|
|
|
} |
113
|
|
|
} // @codeCoverageIgnore |
114
|
|
|
} // @codeCoverageIgnore |
115
|
|
|
|
116
|
|
|
return false; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sends an email with a link, for confirm the email |
121
|
|
|
* |
122
|
|
|
* @return boolean |
123
|
|
|
*/ |
124
|
|
|
public function sendEmail() |
125
|
|
|
{ |
126
|
|
|
if (!User::isTokenValid($this->user->email_confirm_token)) { |
127
|
|
|
$this->user->generateEmailConfirmToken(); |
128
|
|
|
$this->user->updateAttributes([ |
129
|
|
|
'email_confirm_token' => $this->user->email_confirm_token, |
130
|
|
|
'date_confirm' => $this->user->date_confirm, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return Yii::$app->notify->sendMessage( |
135
|
|
|
$this->email, |
136
|
|
|
Yii::t('app', 'Activate Your Account'), |
137
|
|
|
'emailConfirmToken', |
138
|
|
|
['user' => $this->user] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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: