SiteController.php 3.38 KB
Newer Older
1
<?php
2
namespace frontend\controllers;
3

4 5 6 7 8
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
9
use yii\base\InvalidParamException;
10
use yii\web\BadRequestHttpException;
11 12
use yii\web\Controller;
use Yii;
13

14 15 16
/**
 * Site controller
 */
17 18
class SiteController extends Controller
{
19 20 21
	/**
	 * @inheritdoc
	 */
22 23
	public function behaviors()
	{
24 25
		return [
			'access' => [
26
				'class' => \yii\web\AccessControl::className(),
27
				'only' => ['logout', 'signup'],
28 29
				'rules' => [
					[
30
						'actions' => ['signup'],
31
						'allow' => true,
32 33 34 35
						'roles' => ['?'],
					],
					[
						'actions' => ['logout'],
36
						'allow' => true,
37 38 39 40 41
						'roles' => ['@'],
					],
				],
			],
		];
42 43
	}

44 45 46
	/**
	 * @inheritdoc
	 */
47 48
	public function actions()
	{
49 50
		return [
			'error' => [
51
				'class' => 'yii\web\ErrorAction',
52 53
			],
			'captcha' => [
Qiang Xue committed
54
				'class' => 'yii\captcha\CaptchaAction',
55
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
56 57
			],
		];
58 59 60 61
	}

	public function actionIndex()
	{
62
		return $this->render('index');
63 64 65 66
	}

	public function actionLogin()
	{
67
		if (!\Yii::$app->user->isGuest) {
68
			return $this->goHome();
69 70
		}

71
		$model = new LoginForm();
72
		if ($model->load(Yii::$app->request->post()) && $model->login()) {
73
			return $this->goBack();
74
		} else {
75
			return $this->render('login', [
76
				'model' => $model,
77
			]);
78 79 80 81 82
		}
	}

	public function actionLogout()
	{
83
		Yii::$app->user->logout();
Qiang Xue committed
84
		return $this->goHome();
85 86 87 88
	}

	public function actionContact()
	{
89
		$model = new ContactForm();
90 91 92 93 94 95
		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
			if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
				Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
			} else {
				Yii::$app->session->setFlash('error', 'There was an error sending email.');
			}
96
			return $this->refresh();
97
		} else {
98
			return $this->render('contact', [
99
				'model' => $model,
100
			]);
101 102 103 104 105
		}
	}

	public function actionAbout()
	{
106
		return $this->render('about');
107
	}
108 109 110

	public function actionSignup()
	{
111 112 113 114 115 116 117
		$model = new SignupForm();
		if ($model->load(Yii::$app->request->post())) {
			$user = $model->signup();
			if ($user) {
				if (Yii::$app->getUser()->login($user)) {
					return $this->goHome();
				}
118 119 120
			}
		}

121
		return $this->render('signup', [
122
			'model' => $model,
123
		]);
124
	}
125

126
	public function actionRequestPasswordReset()
127
	{
128
		$model = new PasswordResetRequestForm();
129
		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
130 131 132 133 134 135
			if ($model->sendEmail()) {
				Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
				return $this->goHome();
			} else {
				Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
			}
136
		}
137

138
		return $this->render('requestPasswordResetToken', [
139
			'model' => $model,
140
		]);
141
	}
142

143 144
	public function actionResetPassword($token)
	{
145 146 147 148
		try {
			$model = new ResetPasswordForm($token);
		} catch (InvalidParamException $e) {
			throw new BadRequestHttpException($e->getMessage());
149
		}
150

151
		if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
152
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
Qiang Xue committed
153
			return $this->goHome();
154 155
		}

156
		return $this->render('resetPassword', [
157
			'model' => $model,
158
		]);
159
	}
160
}