yii2-netdisk/controllers/TicketsController.php

184 lines
5.3 KiB
PHP
Raw Normal View History

2024-04-16 16:46:56 +08:00
<?php
namespace app\controllers;
use app\models\TicketReplies;
2024-04-16 16:46:56 +08:00
use app\models\Tickets;
use app\models\TicketsSearch;
use Yii;
use yii\db\StaleObjectException;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
/**
* TicketsController implements the CRUD actions for Tickets model.
*/
class TicketsController extends Controller
{
/**
* @inheritDoc
*/
public function behaviors(): array
{
return array_merge(
parent::behaviors(),
[
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view', 'create', 'update', 'delete'],
'roles' => ['user'], // only user can do these
]
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'index' => ['GET'],
'view' => ['GET'],
'create' => ['GET', 'POST'],
'update' => ['GET', 'POST'],
'delete' => ['POST'],
],
],
]
);
}
/**
* Lists all Tickets models.
*
* @return string
*/
public function actionIndex(): string
{
$searchModel = new TicketsSearch();
$dataProvider = $searchModel->search($this->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Tickets model.
* @param int $id 工单id
* @return string
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView(int $id): string
{
//fetch all replies for this ticket
$ticketReplies = $this->findTicketReplies($id);
//json
$json = json_encode($ticketReplies);
2024-04-16 16:46:56 +08:00
return $this->render('view', [
'model' => $this->findModel($id),
'ticketReplies' => $json,
2024-04-16 16:46:56 +08:00
]);
}
protected function findTicketReplies(int $ticketId): array
{
$ticketReplies = TicketReplies::find()
->where(['ticket_id' => $ticketId])
->orderBy(['created_at' => SORT_ASC])
->all();
$result = [];
foreach ($ticketReplies as $reply) {
$result[] = $reply->toArray();
}
return $result;
}
2024-04-16 16:46:56 +08:00
/**
* Creates a new Tickets model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|Response
*/
public function actionCreate(): Response|string
{
$model = new Tickets();
if ($this->request->isPost) {
if ($model->load($this->request->post())) {
// add properties that are not in the form
$model->user_id = Yii::$app->user->id;
$model->status = Tickets::STATUS_OPEN;
$model->ip = $this->request->userIP;
$model->created_at = date('Y-m-d H:i:s');
$model->updated_at = date('Y-m-d H:i:s');
if ($model->save()) {
Yii::$app->session->setFlash('success', '工单创建成功');
2024-04-16 16:46:56 +08:00
return $this->redirect(['view', 'id' => $model->id]);
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Tickets 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 actionUpdate(int $id): Response|string
{
$model = $this->findModel($id);
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Tickets 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 actionDelete(int $id): Response
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Tickets 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 Tickets the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel(int $id): Tickets
{
if (($model = Tickets::findOne(['id' => $id])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}