Completed
Branch master (99b5bb)
by WILMER
02:19
created

SiteController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Cjt Terabyte LLC [yii2-extension].
5
 *
6
 * (c) Cjt Terabyte LLC [yii2-extension] <http://github.com/cjtterabytesoft>.
7
 * For the full copyright and license information, please view the LICENSE.md.
8
 * file that was distributed with this source code.
9
 *
10
 * @link http://www.cjtterabyte.com.
11
 * @author Wilmer Arámbula <[email protected]>.
12
 * @copyright (c) 2015 Cjt Terabyte LLC.
13
 * @Extension: [yii2-adminlte-basic].
14
 * @Controllers App [SiteController].
15
 * @since 1.0
16
 */
17
18
namespace cjtterabytesoft\adminlte\basic\controllers;
19
20
use Yii;
21
use yii\filters\AccessControl;
22
use yii\web\Controller;
23
use yii\filters\VerbFilter;
24
use cjtterabytesoft\adminlte\basic\models\LoginForm;
25
use cjtterabytesoft\adminlte\basic\models\ContactForm;
26
27
class SiteController extends Controller
28
{
29
    public function behaviors()
30
    {
31
        return [
32
            'access' => [
33
                'class' => AccessControl::className(),
34
                'only' => ['logout'],
35
                'rules' => [
36
                    [
37
                        'actions' => ['logout'],
38
                        'allow' => true,
39
                        'roles' => ['@'],
40
                    ],
41
                ],
42
            ],
43
            'verbs' => [
44
                'class' => VerbFilter::className(),
45
                'actions' => [
46
                    'logout' => ['post'],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    public function actions()
53
    {
54
        return [
55
            'error' => [
56
                'class' => 'yii\web\ErrorAction',
57
            ],
58
            'captcha' => [
59
                'class' => 'yii\captcha\CaptchaAction',
60
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
61
            ],
62
        ];
63
    }
64
65
    public function actionIndex()
66
    {
67
        return $this->render('index');
68
    }
69
70
    public function actionLogin()
71
    {
72
        if (!\Yii::$app->user->isGuest) {
73
            return $this->goHome();
74
        }
75
76
        $model = new LoginForm();
77
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
78
            return $this->goBack();
79
        }
80
        return $this->render('login', [
81
            'model' => $model,
82
        ]);
83
    }
84
85
    public function actionLogout()
86
    {
87
        Yii::$app->user->logout();
88
89
        return $this->goHome();
90
    }
91
92
    public function actionContact()
93
    {
94
        $model = new ContactForm();
95
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
96
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
97
                Yii::$app->session->setFlash('success', yii::t('adminlte', 'Thank you for contacting us. We will respond to you as soon as possible.'));
98
            } else {
99
                Yii::$app->session->setFlash('error', yii::t('adminlte', 'There was an error sending email.'));
100
            }
101
102
            return $this->refresh();
103
        } else {
104
            return $this->render('contact', [
105
                'model' => $model,
106
            ]);
107
        }
108
    }
109
110
    public function actionAbout()
111
    {
112
        return $this->render('about');
113
    }
114
}