SiteController.php 3.52 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
use yii\web\VerbFilter;
14

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

51 52 53
	/**
	 * @inheritdoc
	 */
54 55
	public function actions()
	{
56 57
		return [
			'error' => [
58
				'class' => 'yii\web\ErrorAction',
59 60
			],
			'captcha' => [
Qiang Xue committed
61
				'class' => 'yii\captcha\CaptchaAction',
62
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
63 64
			],
		];
65 66 67 68
	}

	public function actionIndex()
	{
69
		return $this->render('index');
70 71 72 73
	}

	public function actionLogin()
	{
74
		if (!\Yii::$app->user->isGuest) {
75
			return $this->goHome();
76 77
		}

78
		$model = new LoginForm();
79
		if ($model->load(Yii::$app->request->post()) && $model->login()) {
80
			return $this->goBack();
81
		} else {
82
			return $this->render('login', [
83
				'model' => $model,
84
			]);
85 86 87 88 89
		}
	}

	public function actionLogout()
	{
90
		Yii::$app->user->logout();
Qiang Xue committed
91
		return $this->goHome();
92 93 94 95
	}

	public function actionContact()
	{
96
		$model = new ContactForm();
97 98 99 100 101 102
		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.');
			}
103
			return $this->refresh();
104
		} else {
105
			return $this->render('contact', [
106
				'model' => $model,
107
			]);
108 109 110 111 112
		}
	}

	public function actionAbout()
	{
113
		return $this->render('about');
114
	}
115 116 117

	public function actionSignup()
	{
118 119 120 121 122 123 124
		$model = new SignupForm();
		if ($model->load(Yii::$app->request->post())) {
			$user = $model->signup();
			if ($user) {
				if (Yii::$app->getUser()->login($user)) {
					return $this->goHome();
				}
125 126 127
			}
		}

128
		return $this->render('signup', [
129
			'model' => $model,
130
		]);
131
	}
132

133
	public function actionRequestPasswordReset()
134
	{
135
		$model = new PasswordResetRequestForm();
136
		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
137 138 139 140 141 142
			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.');
			}
143
		}
144

145
		return $this->render('requestPasswordResetToken', [
146
			'model' => $model,
147
		]);
148
	}
149

150 151
	public function actionResetPassword($token)
	{
152 153 154 155
		try {
			$model = new ResetPasswordForm($token);
		} catch (InvalidParamException $e) {
			throw new BadRequestHttpException($e->getMessage());
156
		}
157

158
		if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
159
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
Qiang Xue committed
160
			return $this->goHome();
161 162
		}

163
		return $this->render('resetPassword', [
164
			'model' => $model,
165
		]);
166
	}
167
}