1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\main\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\helpers\Url; |
8
|
|
|
use modules\main\Module; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ContactForm |
12
|
|
|
* @package modules\main\models |
13
|
|
|
*/ |
14
|
|
|
class ContactForm extends Model |
15
|
|
|
{ |
16
|
|
|
public $name; |
17
|
|
|
public $email; |
18
|
|
|
public $subject; |
19
|
|
|
public $body; |
20
|
|
|
public $verifyCode; |
21
|
|
|
|
22
|
|
|
const SCENARIO_GUEST = 'guest'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return array the validation rules. |
26
|
|
|
*/ |
27
|
|
|
public function rules() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
// name, email, subject and body are required |
31
|
|
|
[['name', 'email', 'subject', 'body'], 'required'], |
32
|
|
|
// email has to be a valid email address |
33
|
|
|
['email', 'email'], |
34
|
|
|
// verifyCode needs to be entered correctly |
35
|
|
|
['verifyCode', 'required', 'on' => self::SCENARIO_GUEST], |
36
|
|
|
[ |
37
|
|
|
'verifyCode', |
38
|
|
|
'captcha', |
39
|
|
|
'captchaAction' => Url::to('/main/default/captcha'), |
40
|
|
|
'on' => self::SCENARIO_GUEST |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array customized attribute labels |
47
|
|
|
*/ |
48
|
|
|
public function attributeLabels() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
'name' => Module::t('module', 'Name'), |
52
|
|
|
'email' => Module::t('module', 'Email'), |
53
|
|
|
'subject' => Module::t('module', 'Subject'), |
54
|
|
|
'body' => Module::t('module', 'Body'), |
55
|
|
|
'verifyCode' => Module::t('module', 'Verification Code'), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sends an email to the specified email address using the information collected by this model. |
61
|
|
|
* @param string $email the target email address |
62
|
|
|
* @return bool whether the model passes validation |
63
|
|
|
*/ |
64
|
|
|
public function contact($email) |
65
|
|
|
{ |
66
|
|
|
if ($this->validate()) { |
67
|
|
|
Yii::$app->mailer->compose() |
68
|
|
|
->setTo($email) |
69
|
|
|
->setFrom([$this->email => $this->name]) |
70
|
|
|
->setSubject($this->subject) |
71
|
|
|
->setTextBody($this->body) |
72
|
|
|
->send(); |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|