实现公告功能
*后端部分
This commit is contained in:
parent
ffffaeebf4
commit
cd6276503f
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace app\controllers;
|
namespace app\controllers;
|
||||||
|
|
||||||
|
use app\models\Announcements;
|
||||||
|
use app\models\AnnouncementsSearch;
|
||||||
use app\models\CollectionSearch;
|
use app\models\CollectionSearch;
|
||||||
use app\models\CollectionTasks;
|
use app\models\CollectionTasks;
|
||||||
use app\models\CollectionUploadedSearch;
|
use app\models\CollectionUploadedSearch;
|
||||||
@ -23,6 +25,7 @@ use RuntimeException;
|
|||||||
use Throwable;
|
use Throwable;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\base\Exception;
|
use yii\base\Exception;
|
||||||
|
use yii\db\StaleObjectException;
|
||||||
use yii\filters\AccessControl;
|
use yii\filters\AccessControl;
|
||||||
use yii\filters\VerbFilter;
|
use yii\filters\VerbFilter;
|
||||||
use yii\web\Controller;
|
use yii\web\Controller;
|
||||||
@ -49,7 +52,8 @@ class AdminController extends Controller
|
|||||||
'actions' => ['system', 'user', 'info', 'user-view', 'user-create', 'user-update',
|
'actions' => ['system', 'user', 'info', 'user-view', 'user-create', 'user-update',
|
||||||
'user-delete', 'user-totpoff', 'user-pwdreset', 'login-log', 'access-log', 'collection-up-log',
|
'user-delete', 'user-totpoff', 'user-pwdreset', 'login-log', 'access-log', 'collection-up-log',
|
||||||
'share-manage', 'share-manage-view', 'share-manage-delete', 'collection-manage', 'collection-manage-view',
|
'share-manage', 'share-manage-view', 'share-manage-delete', 'collection-manage', 'collection-manage-view',
|
||||||
'collection-manage-delete', 'notice-manage', 'sysinfo', 'get-sysinfo', 'ticket-manage', 'ticket-view', 'ticket-delete', 'ticket-reply'],
|
'collection-manage-delete', 'notice-manage', 'sysinfo', 'get-sysinfo', 'ticket-manage', 'ticket-view', 'ticket-delete',
|
||||||
|
'ticket-reply', 'announcements-manage', 'announcements-view', 'announcements-create', 'announcements-update', 'announcements-delete'],
|
||||||
'roles' => ['admin'], // only admin can do these
|
'roles' => ['admin'], // only admin can do these
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
@ -82,6 +86,11 @@ class AdminController extends Controller
|
|||||||
'ticket-view' => ['GET'],
|
'ticket-view' => ['GET'],
|
||||||
'ticket-delete' => ['POST'],
|
'ticket-delete' => ['POST'],
|
||||||
'ticket-reply' => ['POST'],
|
'ticket-reply' => ['POST'],
|
||||||
|
'announcements-manage' => ['GET'],
|
||||||
|
'announcements-view' => ['GET'],
|
||||||
|
'announcements-create' => ['GET', 'POST'],
|
||||||
|
'announcements-update' => ['GET', 'POST'],
|
||||||
|
'announcements-delete' => ['POST'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
@ -723,4 +732,113 @@ class AdminController extends Controller
|
|||||||
// 如果不是POST请求,返回一个错误响应
|
// 如果不是POST请求,返回一个错误响应
|
||||||
return $this->asJson(['status' => 'error', 'message' => 'Invalid request']);
|
return $this->asJson(['status' => 'error', 'message' => 'Invalid request']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists all Announcements models.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function actionAnnouncementsManage(): string
|
||||||
|
{
|
||||||
|
$searchModel = new AnnouncementsSearch();
|
||||||
|
$dataProvider = $searchModel->search($this->request->queryParams);
|
||||||
|
|
||||||
|
return $this->render('announcements_index', [
|
||||||
|
'searchModel' => $searchModel,
|
||||||
|
'dataProvider' => $dataProvider,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays a single Announcements model.
|
||||||
|
* @param int $id 公告ID
|
||||||
|
* @return string
|
||||||
|
* @throws NotFoundHttpException if the model cannot be found
|
||||||
|
*/
|
||||||
|
public function actionAnnouncementsView(int $id): string
|
||||||
|
{
|
||||||
|
return $this->render('announcements_view', [
|
||||||
|
'model' => $this->findAnnouncementsModel($id),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Announcements model.
|
||||||
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
||||||
|
* @return string|Response
|
||||||
|
*/
|
||||||
|
public function actionAnnouncementsCreate(): Response|string
|
||||||
|
{
|
||||||
|
$model = new Announcements();
|
||||||
|
|
||||||
|
if ($this->request->isPost) {
|
||||||
|
if ($model->load($this->request->post())) {
|
||||||
|
// set published_at to current time
|
||||||
|
$model->published_at = date('Y-m-d H:i:s');
|
||||||
|
if($model->save()){
|
||||||
|
Yii::$app->session->setFlash('success', '公告发布成功');
|
||||||
|
return $this->redirect(['announcements-view', 'id' => $model->id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$model->loadDefaultValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('announcements_create', [
|
||||||
|
'model' => $model,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates an existing Announcements model.
|
||||||
|
* If update is successful, the browser will be redirected to the 'view' page.
|
||||||
|
* @param int $id 公告ID
|
||||||
|
* @return string|Response
|
||||||
|
* @throws NotFoundHttpException if the model cannot be found
|
||||||
|
*/
|
||||||
|
public function actionAnnouncementsUpdate(int $id): Response|string
|
||||||
|
{
|
||||||
|
$model = $this->findAnnouncementsModel($id);
|
||||||
|
|
||||||
|
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
|
||||||
|
Yii::$app->session->setFlash('success', '公告修改成功');
|
||||||
|
return $this->redirect(['announcements-view', 'id' => $model->id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('announcements_update', [
|
||||||
|
'model' => $model,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes an existing Announcements model.
|
||||||
|
* If deletion is successful, the browser will be redirected to the 'index' page.
|
||||||
|
* @param int $id 公告ID
|
||||||
|
* @return Response
|
||||||
|
* @throws NotFoundHttpException if the model cannot be found
|
||||||
|
* @throws \Throwable
|
||||||
|
* @throws StaleObjectException
|
||||||
|
*/
|
||||||
|
public function actionAnnouncementsDelete(int $id): Response
|
||||||
|
{
|
||||||
|
$this->findAnnouncementsModel($id)->delete();
|
||||||
|
Yii::$app->session->setFlash('success', '公告删除成功');
|
||||||
|
return $this->redirect(['announcements-manage']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the Announcements model based on its primary key value.
|
||||||
|
* If the model is not found, a 404 HTTP exception will be thrown.
|
||||||
|
* @param int $id 公告ID
|
||||||
|
* @return Announcements the loaded model
|
||||||
|
* @throws NotFoundHttpException if the model cannot be found
|
||||||
|
*/
|
||||||
|
protected function findAnnouncementsModel(int $id): Announcements
|
||||||
|
{
|
||||||
|
if (($model = Announcements::findOne(['id' => $id])) !== null) {
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotFoundHttpException('The requested page does not exist.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
|
|
||||||
namespace app\controllers;
|
namespace app\controllers;
|
||||||
|
|
||||||
|
use app\models\Announcements;
|
||||||
use app\models\EntryForm;
|
use app\models\EntryForm;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\filters\AccessControl;
|
use yii\filters\AccessControl;
|
||||||
use yii\web\Controller;
|
use yii\web\Controller;
|
||||||
use yii\filters\VerbFilter;
|
use yii\filters\VerbFilter;
|
||||||
|
use yii\web\NotFoundHttpException;
|
||||||
|
use yii\web\Response;
|
||||||
|
|
||||||
class SiteController extends Controller
|
class SiteController extends Controller
|
||||||
{
|
{
|
||||||
@ -21,20 +24,22 @@ class SiteController extends Controller
|
|||||||
'only' => ['logout'],
|
'only' => ['logout'],
|
||||||
'rules' => [
|
'rules' => [
|
||||||
[
|
[
|
||||||
'actions' => ['logout'],
|
'actions' => ['logout', 'get-announcement'],
|
||||||
'allow' => true,
|
'allow' => true,
|
||||||
'roles' => ['@'],
|
'roles' => ['@'],
|
||||||
],
|
]
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'verbs' => [
|
'verbs' => [
|
||||||
'class' => VerbFilter::class,
|
'class' => VerbFilter::class,
|
||||||
'actions' => [
|
'actions' => [
|
||||||
'logout' => ['post'],
|
'logout' => ['post'],
|
||||||
|
'get-announcement' => ['get'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
@ -47,6 +52,7 @@ class SiteController extends Controller
|
|||||||
$this->layout = 'main';
|
$this->layout = 'main';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@ -70,9 +76,27 @@ class SiteController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function actionIndex(): string
|
public function actionIndex(): string
|
||||||
{
|
{
|
||||||
|
if(Yii::$app->user->isGuest){
|
||||||
return $this->render('index');
|
return $this->render('index');
|
||||||
}
|
}
|
||||||
|
//fetch latest 3 announcements
|
||||||
|
$latestAnnouncements = Announcements::fetchLatestAnnouncements();
|
||||||
|
return $this->render('index', [
|
||||||
|
'latestAnnouncements' => $latestAnnouncements
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
public function actionEntry(): string
|
public function actionEntry(): string
|
||||||
{
|
{
|
||||||
|
65
models/Announcements.php
Normal file
65
models/Announcements.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\models;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
|
use yii\db\ActiveRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the model class for table "announcements".
|
||||||
|
*
|
||||||
|
* @property int $id 公告ID
|
||||||
|
* @property string $title 标题
|
||||||
|
* @property string $content 内容
|
||||||
|
* @property string $published_at 发布时间
|
||||||
|
* @property string|null $updated_at 更新时间
|
||||||
|
*/
|
||||||
|
class Announcements extends ActiveRecord
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function tableName(): string
|
||||||
|
{
|
||||||
|
return 'announcements';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[['title', 'content', 'published_at'], 'required'],
|
||||||
|
[['content'], 'string'],
|
||||||
|
[['published_at', 'updated_at'], 'safe'],
|
||||||
|
[['title'], 'string', 'max' => 255],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function attributeLabels(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => '公告ID',
|
||||||
|
'title' => '标题',
|
||||||
|
'content' => '内容',
|
||||||
|
'published_at' => '发布时间',
|
||||||
|
'updated_at' => '更新时间',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* give me the latest 3 announcements
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function fetchLatestAnnouncements(): array
|
||||||
|
{
|
||||||
|
return self::find()
|
||||||
|
->orderBy(['updated_at' => SORT_DESC])
|
||||||
|
->limit(3)
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
}
|
71
models/AnnouncementsSearch.php
Normal file
71
models/AnnouncementsSearch.php
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\models;
|
||||||
|
|
||||||
|
use yii\base\Model;
|
||||||
|
use yii\data\ActiveDataProvider;
|
||||||
|
use app\models\Announcements;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AnnouncementsSearch represents the model behind the search form of `app\models\Announcements`.
|
||||||
|
*/
|
||||||
|
class AnnouncementsSearch extends Announcements
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[['id'], 'integer'],
|
||||||
|
[['title', 'content', 'published_at', 'updated_at'], 'safe'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function scenarios(): array
|
||||||
|
{
|
||||||
|
// bypass scenarios() implementation in the parent class
|
||||||
|
return Model::scenarios();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates data provider instance with search query applied
|
||||||
|
*
|
||||||
|
* @param array $params
|
||||||
|
*
|
||||||
|
* @return ActiveDataProvider
|
||||||
|
*/
|
||||||
|
public function search($params): ActiveDataProvider
|
||||||
|
{
|
||||||
|
$query = Announcements::find();
|
||||||
|
|
||||||
|
// add conditions that should always apply here
|
||||||
|
|
||||||
|
$dataProvider = new ActiveDataProvider([
|
||||||
|
'query' => $query,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->load($params);
|
||||||
|
|
||||||
|
if (!$this->validate()) {
|
||||||
|
// uncomment the following line if you do not want to return any records when validation fails
|
||||||
|
// $query->where('0=1');
|
||||||
|
return $dataProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
// grid filtering conditions
|
||||||
|
$query->andFilterWhere([
|
||||||
|
'id' => $this->id,
|
||||||
|
'published_at' => $this->published_at,
|
||||||
|
'updated_at' => $this->updated_at,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$query->andFilterWhere(['like', 'title', $this->title])
|
||||||
|
->andFilterWhere(['like', 'content', $this->content]);
|
||||||
|
|
||||||
|
return $dataProvider;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user