2013-05-24 10:14:49 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\controllers;
|
|
|
|
|
2024-04-21 16:42:04 +08:00
|
|
|
use app\models\Announcements;
|
2024-02-09 12:15:09 +08:00
|
|
|
use app\models\EntryForm;
|
2013-05-24 10:14:49 -04:00
|
|
|
use Yii;
|
2014-04-05 01:00:14 -04:00
|
|
|
use yii\filters\AccessControl;
|
2013-05-24 10:14:49 -04:00
|
|
|
use yii\web\Controller;
|
2014-04-05 01:00:14 -04:00
|
|
|
use yii\filters\VerbFilter;
|
2024-04-21 16:42:04 +08:00
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
use yii\web\Response;
|
2013-05-24 10:14:49 -04:00
|
|
|
|
|
|
|
class SiteController extends Controller
|
|
|
|
{
|
2016-06-19 16:56:22 +03:00
|
|
|
/**
|
2018-02-19 00:29:21 +02:00
|
|
|
* {@inheritdoc}
|
2016-06-19 16:56:22 +03:00
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function behaviors(): array
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'access' => [
|
2022-02-25 14:58:50 +00:00
|
|
|
'class' => AccessControl::class,
|
2014-03-16 10:46:16 +06:00
|
|
|
'only' => ['logout'],
|
|
|
|
'rules' => [
|
|
|
|
[
|
2024-04-21 16:42:04 +08:00
|
|
|
'actions' => ['logout', 'get-announcement'],
|
2014-03-16 10:46:16 +06:00
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['@'],
|
2024-04-21 16:42:04 +08:00
|
|
|
]
|
2014-03-16 10:46:16 +06:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'verbs' => [
|
2022-02-25 14:58:50 +00:00
|
|
|
'class' => VerbFilter::class,
|
2014-03-16 10:46:16 +06:00
|
|
|
'actions' => [
|
|
|
|
'logout' => ['post'],
|
2024-04-21 16:42:04 +08:00
|
|
|
'get-announcement' => ['get'],
|
2014-03-16 10:46:16 +06:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2024-04-21 16:42:04 +08:00
|
|
|
|
2024-03-21 14:00:10 +08:00
|
|
|
public function init(): void
|
|
|
|
{
|
|
|
|
parent::init();
|
2013-09-16 02:41:19 +04:00
|
|
|
|
2024-03-21 14:00:10 +08:00
|
|
|
if (Yii::$app->user->can('admin')) {
|
|
|
|
$this->layout = 'admin_main';
|
2024-04-21 16:42:04 +08:00
|
|
|
} elseif (Yii::$app->user->isGuest) {
|
2024-03-21 14:00:10 +08:00
|
|
|
$this->layout = 'guest_main';
|
|
|
|
} else {
|
|
|
|
$this->layout = 'main';
|
|
|
|
}
|
|
|
|
}
|
2024-04-21 16:42:04 +08:00
|
|
|
|
2016-06-19 16:56:22 +03:00
|
|
|
/**
|
2018-02-19 00:29:21 +02:00
|
|
|
* {@inheritdoc}
|
2016-06-19 16:56:22 +03:00
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function actions(): array
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'error' => [
|
|
|
|
'class' => 'yii\web\ErrorAction',
|
|
|
|
],
|
|
|
|
'captcha' => [
|
|
|
|
'class' => 'yii\captcha\CaptchaAction',
|
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2016-06-19 16:56:22 +03:00
|
|
|
/**
|
|
|
|
* Displays homepage.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function actionIndex(): string
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
2024-04-21 16:42:04 +08:00
|
|
|
if(Yii::$app->user->isGuest){
|
|
|
|
return $this->render('index');
|
|
|
|
}
|
|
|
|
//fetch latest 3 announcements
|
|
|
|
$latestAnnouncements = Announcements::fetchLatestAnnouncements();
|
|
|
|
return $this->render('index', [
|
|
|
|
'latestAnnouncements' => $latestAnnouncements
|
|
|
|
]);
|
2014-03-16 10:46:16 +06:00
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2024-04-21 16:42:04 +08:00
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetAnnouncement($id): ?Announcements
|
|
|
|
{
|
|
|
|
if (Yii::$app->request->isAjax) {
|
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
|
|
|
return Announcements::findOne($id);
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
2024-02-09 12:15:09 +08:00
|
|
|
|
2024-03-17 15:37:13 +08:00
|
|
|
public function actionEntry(): string
|
2024-02-09 12:15:09 +08:00
|
|
|
{
|
|
|
|
$model = new EntryForm();
|
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
|
|
|
|
return $this->render('entry-confirm', ['model' => $model]);
|
|
|
|
} else {
|
|
|
|
return $this->render('entry', ['model' => $model]);
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 03:14:28 +01:00
|
|
|
}
|