Completed
Push — master ( 342ffa...2ca792 )
by Andrii
05:25
created

ContactForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\models;
13
14
use Yii;
15
use yii\base\Model;
16
17
/**
18
 * ContactForm is the model behind the contact form.
19
 */
20
class ContactForm extends Model
21
{
22
    public $name;
23
    public $email;
24
    public $subject;
25
    public $body;
26
    public $verifyCode;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function rules()
32
    {
33
        return [
34
            // name, email, subject and body are required
35
            [['name', 'email', 'subject', 'body'], 'required'],
36
            // email has to be a valid email address
37
            ['email', 'email'],
38
            // verifyCode needs to be entered correctly
39
            ['verifyCode', 'captcha'],
40
        ];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function attributeLabels()
47
    {
48
        return [
49
            'verifyCode' => 'Verification Code',
50
        ];
51
    }
52
53
    /**
54
     * Sends an email to the specified email address using the information collected by this model.
55
     *
56
     * @param  string  $email the target email address
57
     * @return boolean whether the email was sent
58
     */
59
    public function sendEmail($email)
60
    {
61
        return Yii::$app->mailer->compose()
62
            ->setTo($email)
63
            ->setFrom([$this->email => $this->name])
64
            ->setSubject($this->subject)
65
            ->setTextBody($this->body)
66
            ->send();
67
    }
68
}
69