From cd6276503f40cd1be72f3a408fa60cc64c16b61a Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sun, 21 Apr 2024 16:42:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=85=AC=E5=91=8A=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20*=E5=90=8E=E7=AB=AF=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/AdminController.php | 120 +++++++++++++++++++++++++++++++- controllers/SiteController.php | 32 +++++++-- models/Announcements.php | 65 +++++++++++++++++ models/AnnouncementsSearch.php | 71 +++++++++++++++++++ 4 files changed, 283 insertions(+), 5 deletions(-) create mode 100644 models/Announcements.php create mode 100644 models/AnnouncementsSearch.php diff --git a/controllers/AdminController.php b/controllers/AdminController.php index eee4ed8..1e6cf01 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -2,6 +2,8 @@ namespace app\controllers; +use app\models\Announcements; +use app\models\AnnouncementsSearch; use app\models\CollectionSearch; use app\models\CollectionTasks; use app\models\CollectionUploadedSearch; @@ -23,6 +25,7 @@ use RuntimeException; use Throwable; use Yii; use yii\base\Exception; +use yii\db\StaleObjectException; use yii\filters\AccessControl; use yii\filters\VerbFilter; use yii\web\Controller; @@ -49,7 +52,8 @@ class AdminController extends Controller 'actions' => ['system', 'user', 'info', 'user-view', 'user-create', 'user-update', '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', - '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 ] ], @@ -82,6 +86,11 @@ class AdminController extends Controller 'ticket-view' => ['GET'], 'ticket-delete' => ['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请求,返回一个错误响应 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.'); + } } diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 5d2f18a..888221e 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -2,11 +2,14 @@ namespace app\controllers; +use app\models\Announcements; use app\models\EntryForm; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; +use yii\web\NotFoundHttpException; +use yii\web\Response; class SiteController extends Controller { @@ -21,32 +24,35 @@ class SiteController extends Controller 'only' => ['logout'], 'rules' => [ [ - 'actions' => ['logout'], + 'actions' => ['logout', 'get-announcement'], 'allow' => true, 'roles' => ['@'], - ], + ] ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'logout' => ['post'], + 'get-announcement' => ['get'], ], ], ]; } + public function init(): void { parent::init(); if (Yii::$app->user->can('admin')) { $this->layout = 'admin_main'; - }elseif (Yii::$app->user->isGuest) { + } elseif (Yii::$app->user->isGuest) { $this->layout = 'guest_main'; } else { $this->layout = 'main'; } } + /** * {@inheritdoc} */ @@ -70,9 +76,27 @@ class SiteController extends Controller */ public function actionIndex(): string { - return $this->render('index'); + if(Yii::$app->user->isGuest){ + 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 { diff --git a/models/Announcements.php b/models/Announcements.php new file mode 100644 index 0000000..ba4958e --- /dev/null +++ b/models/Announcements.php @@ -0,0 +1,65 @@ + 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(); + } +} diff --git a/models/AnnouncementsSearch.php b/models/AnnouncementsSearch.php new file mode 100644 index 0000000..ed809de --- /dev/null +++ b/models/AnnouncementsSearch.php @@ -0,0 +1,71 @@ + $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; + } +}