BiFace_Server_Lite/controllers/SiteController.php
2021-09-23 11:38:22 +07:00

131 lines
3.2 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex() {
return $this->render('index');
}
/**
* Login action.
*
* @return Response|string
*/
public function actionLogin($lang = "vi-VI") {
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
Yii::$app->language = $lang;
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$lang = Yii::$app->request->post('lang');
$language = \app\models\common::allLanguage();
foreach ($language as $key => $value) {
if ($value['name'] === $lang)
Yii::$app->session->set("language", $value);
}
return $this->redirect(["/config"]);
}
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
* @return Response
*/
public function actionLogout() {
$lang = Yii::$app->session->get("language") ? Yii::$app->session->get("language")["name"] : Yii::$app->language;
Yii::$app->user->logout();
return $this->redirect(['/site/login', 'lang' => $lang]); //$this->goHome();
}
/**
* Displays contact page.
*
* @return Response|string
*/
public function actionContact() {
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
/**
* Displays about page.
*
* @return string
*/
public function actionAbout() {
return $this->render('about');
}
}