|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace site\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* ContactForm is the model behind the contact form. |
|
10
|
|
|
*/ |
|
11
|
|
|
class ContactForm extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
public $name; |
|
14
|
|
|
public $email; |
|
15
|
|
|
public $subject; |
|
16
|
|
|
public $body; |
|
17
|
|
|
public $verifyCode; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function rules() |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
// name, email, subject and body are required |
|
26
|
|
|
[['name', 'email', 'subject', 'body'], 'required'], |
|
27
|
|
|
// email has to be a valid email address |
|
28
|
|
|
['email', 'filter', 'filter' => 'trim'], |
|
29
|
|
|
['email', 'filter', 'filter' => 'strtolower'], |
|
30
|
|
|
['email', 'email'], |
|
31
|
|
|
// verifyCode needs to be entered correctly |
|
32
|
|
|
['verifyCode', 'captcha', 'when' => function() { |
|
33
|
|
|
return Yii::$app->user->isGuest; |
|
34
|
|
|
}, 'skipOnEmpty' => !!YII_ENV_TEST], |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
* @codeCoverageIgnore |
|
41
|
|
|
*/ |
|
42
|
|
|
public function attributeLabels() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
'verifyCode' => 'Verification Code', |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Sends an email to the specified email address using the information collected by this model. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $email the target email address |
|
53
|
|
|
* @return boolean whether the email was sent |
|
54
|
|
|
*/ |
|
55
|
|
|
public function sendEmail($email) |
|
56
|
|
|
{ |
|
57
|
|
|
return Yii::$app->mailer->compose() |
|
58
|
|
|
->setTo($email) |
|
59
|
|
|
->setFrom([$this->email => $this->name]) |
|
60
|
|
|
->setSubject("[FSA Contact] ".$this->subject) |
|
61
|
|
|
->setTextBody($this->body) |
|
62
|
|
|
->send(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|