yii2-netdisk/controllers/SiteController.php

111 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2013-05-24 22:14:49 +08: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 22:14:49 +08:00
use Yii;
use yii\filters\AccessControl;
2013-05-24 22:14:49 +08:00
use yii\web\Controller;
use yii\filters\VerbFilter;
2024-04-21 16:42:04 +08:00
use yii\web\NotFoundHttpException;
use yii\web\Response;
2013-05-24 22:14:49 +08:00
class SiteController extends Controller
{
/**
2018-02-19 06:29:21 +08:00
* {@inheritdoc}
*/
public function behaviors(): array
2014-03-16 12:46:16 +08:00
{
return [
'access' => [
'class' => AccessControl::class,
2014-03-16 12:46:16 +08:00
'only' => ['logout'],
'rules' => [
[
2024-04-21 16:42:04 +08:00
'actions' => ['logout', 'get-announcement'],
2014-03-16 12:46:16 +08:00
'allow' => true,
'roles' => ['@'],
2024-04-21 16:42:04 +08:00
]
2014-03-16 12:46:16 +08:00
],
],
'verbs' => [
'class' => VerbFilter::class,
2014-03-16 12:46:16 +08:00
'actions' => [
'logout' => ['post'],
2024-04-21 16:42:04 +08:00
'get-announcement' => ['get'],
2014-03-16 12:46:16 +08:00
],
],
];
}
2024-04-21 16:42:04 +08:00
public function init(): void
{
parent::init();
if (Yii::$app->user->can('admin')) {
$this->layout = 'admin_main';
2024-04-21 16:42:04 +08:00
} elseif (Yii::$app->user->isGuest) {
$this->layout = 'guest_main';
} else {
$this->layout = 'main';
}
}
2024-04-21 16:42:04 +08:00
/**
2018-02-19 06:29:21 +08:00
* {@inheritdoc}
*/
public function actions(): array
2014-03-16 12:46:16 +08:00
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
2013-05-24 22:14:49 +08:00
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex(): string
2014-03-16 12:46:16 +08: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 12:46:16 +08:00
}
2013-05-24 22:14:49 +08: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
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]);
}
}
}