1
|
|
|
<?php |
2
|
|
|
namespace site\models; |
3
|
|
|
|
4
|
|
|
use yii\base\Model; |
5
|
|
|
use Yii; |
6
|
|
|
use \common\interfaces\UserInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Signup form |
10
|
|
|
*/ |
11
|
|
|
class SignupForm extends Model |
12
|
|
|
{ |
13
|
|
|
public $email; |
14
|
|
|
public $password; |
15
|
|
|
public $timezone = "America/Los_Angeles"; // default |
16
|
|
|
public $captcha; |
17
|
|
|
public $send_email = false; |
18
|
|
|
public $email_category = 4; // default to "Speeding Up" |
19
|
|
|
public $partner_email1; |
20
|
|
|
public $partner_email2; |
21
|
|
|
public $partner_email3; |
22
|
|
|
|
23
|
|
|
private $user; |
24
|
|
|
private $categories; |
25
|
|
|
|
26
|
|
|
public function __construct(UserInterface $user, $config = []) { |
27
|
|
|
$this->user = $user; |
28
|
|
|
$this->categories = \common\models\Category::getCategories(); |
29
|
|
|
parent::__construct($config); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function rules() { |
36
|
|
|
return [ |
37
|
|
|
//['email_category', 'filter', 'filter' => 'intval', 'skipOnEmpty' => true], |
38
|
|
|
['email', 'filter', 'filter' => 'trim'], |
39
|
|
|
['email', 'filter', 'filter' => 'strtolower'], |
40
|
|
|
['email', 'required'], |
41
|
|
|
['email', 'email'], |
42
|
|
|
|
43
|
|
|
['password', 'required'], |
44
|
|
|
['password', 'string', 'min' => 8], |
45
|
|
|
|
46
|
|
|
['timezone', 'required'], |
47
|
|
|
['timezone', 'string', 'min' => 2, 'max' => 255], |
48
|
|
|
['timezone', 'in', 'range' => \DateTimeZone::listIdentifiers()], |
49
|
|
|
|
50
|
|
|
// captcha needs to be entered correctly |
51
|
|
|
['captcha', 'captcha', 'caseSensitive' => false, 'skipOnEmpty' => !!YII_ENV_TEST], |
52
|
|
|
|
53
|
|
|
['send_email', 'boolean'], |
54
|
|
|
['email_category', 'integer'], |
55
|
|
|
['email_category', 'in', 'range'=>array_keys($this->categories)], |
56
|
|
|
[['partner_email1', 'partner_email2', 'partner_email3'], 'email'], |
57
|
|
|
[ |
58
|
|
|
['partner_email1', 'email_category'], |
59
|
|
|
'required', |
60
|
|
|
'when' => function($model) { |
61
|
|
|
return $model->send_email; |
62
|
|
|
}, |
63
|
|
|
'message' => "If you've elected to send email reports, at least one partner email must be set.", |
64
|
|
|
"whenClient" => 'function(attribute, value) { |
65
|
|
|
return $("#signupform-send_email").is("checked"); |
66
|
|
|
}' |
67
|
|
|
] |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @codeCoverageIgnore |
73
|
|
|
*/ |
74
|
|
|
public function attributeLabels() { |
75
|
|
|
return [ |
76
|
|
|
'partner_email1' => "Partner Email #1", |
77
|
|
|
'partner_email2' => "Partner Email #2", |
78
|
|
|
'partner_email3' => "Partner Email #3", |
79
|
|
|
//'send_email' => 'Send an email when I complete a check-in' |
80
|
|
|
'send_email' => 'Automatically send an email when I select behaviors at or above a specific category', |
81
|
|
|
'email_category' => 'The email threshold category is', |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Signs user up. |
87
|
|
|
* |
88
|
|
|
* @return User|null the saved model or null if saving fails |
|
|
|
|
89
|
|
|
*/ |
90
|
|
|
public function signup() { |
91
|
|
|
$user = $this->user->findByEmail($this->email); |
92
|
|
|
if(!$user) { |
93
|
|
|
// this is a brand new user |
94
|
|
|
$this->user = $this->setFields($this->user); |
95
|
|
|
$this->user->save(); |
96
|
|
|
|
97
|
|
|
$this->user->sendSignupNotificationEmail(); |
|
|
|
|
98
|
|
|
$this->user->sendVerifyEmail(); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this->user; |
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
/* |
103
|
|
|
* this is a user that for whatever reason is trying to sign up again |
104
|
|
|
* with the same email address. |
105
|
|
|
*/ |
106
|
|
|
if(!$user->isTokenConfirmed()) { |
107
|
|
|
/* |
108
|
|
|
* they've never verified their account. We don't care if their |
109
|
|
|
* verification token is current or expired. We're resetting their |
110
|
|
|
* account and resending their verification email. |
111
|
|
|
*/ |
112
|
|
|
$this->setFields($user); |
113
|
|
|
$user->save(); |
114
|
|
|
$user->sendVerifyEmail(); |
115
|
|
|
return $user; |
116
|
|
|
} else { |
117
|
|
|
/* |
118
|
|
|
* they've already confirmed their account and are a full user, so skip |
119
|
|
|
* all this |
120
|
|
|
*/ |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setFields($user) { |
127
|
|
|
$user->send_email = $this->send_email; |
128
|
|
|
$user->email = $this->email; |
129
|
|
|
$user->setPassword($this->password); |
130
|
|
|
$user->timezone = $this->timezone; |
131
|
|
|
$user->generateAuthKey(); |
132
|
|
|
$user->generateVerifyEmailToken(); |
133
|
|
|
|
134
|
|
|
if($user->send_email) { |
135
|
|
|
$user->send_email = true; |
136
|
|
|
$user->email_category = $this->email_category; |
137
|
|
|
$user->partner_email1 = $this->partner_email1; |
138
|
|
|
$user->partner_email2 = $this->partner_email2; |
139
|
|
|
$user->partner_email3 = $this->partner_email3; |
140
|
|
|
} else { |
141
|
|
|
$user->send_email = false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $user; |
145
|
|
|
} |
146
|
|
|
} |