SiteController.php 3.17 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 68 69 70
		if (!\Yii::$app->user->isGuest) {
			$this->goHome();
		}

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 89
	}

	public function actionContact()
	{
		$model = new ContactForm;
90
		if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
91
			Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
92
			return $this->refresh();
93
		} else {
94
			return $this->render('contact', [
95
				'model' => $model,
96
			]);
97 98 99 100 101
		}
	}

	public function actionAbout()
	{
102
		return $this->render('about');
103
	}
104 105 106

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

117
		return $this->render('signup', [
118
			'model' => $model,
119
		]);
120
	}
121

122
	public function actionRequestPasswordReset()
123
	{
124
		$model = new PasswordResetRequestForm();
125 126 127 128 129 130 131
		if ($model->load(Yii::$app->request->post())) {
			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.');
			}
132
		}
133

134
		return $this->render('requestPasswordResetToken', [
135
			'model' => $model,
136
		]);
137
	}
138

139 140
	public function actionResetPassword($token)
	{
141 142 143 144
		try {
			$model = new ResetPasswordForm($token);
		} catch (InvalidParamException $e) {
			throw new BadRequestHttpException($e->getMessage());
145
		}
146

147
		if ($model->load($_POST) && $model->resetPassword()) {
148
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
Qiang Xue committed
149
			return $this->goHome();
150 151
		}

152
		return $this->render('resetPassword', [
153
			'model' => $model,
154
		]);
155
	}
156
}