new-support.webeffector.ru/backend/modules/schema/controllers/DefaultController.php

109 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2024-07-04 06:42:20 +03:00
<?php
namespace backend\modules\schema\controllers;
use Yii;
use yii\web\Response;
use yii\web\Controller;
use yii\filters\AccessControl;
use common\helpers\UserHelper;
use yii\web\NotFoundHttpException;
use common\components\schema\models\Schema;
use common\components\schema\models\search\SchemaSearch;
class DefaultController extends Controller
{
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'denyCallback' => function () {
return $this->goHome();
},
'rules' => [
[
'allow' => true,
'roles' => [UserHelper::ROLE_SUPERVISOR],
],
],
],
];
}
public function actionIndex(): string
{
$searchModel = new SchemaSearch();
$params = Yii::$app->request->queryParams;
$dataProvider = $searchModel->search($params);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
public function actionCreate()
{
$model = new Schema();
if ($model->load(Yii::$app->request->post())) {
$model->save();
} else {
if ($model->getErrors()) {
Yii::$app->session->setFlash('error', json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
}
}
return $this->render('create', [
'model' => $model,
]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', Yii::t('schema', 'Сохранено'));
return $this->redirect('index');
} else {
if ($model->getErrors()) {
Yii::$app->session->setFlash('error', json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
}
}
return $this->render('update', [
'model' => $model,
]);
}
public function actionDelete($id): Response
{
$model = $this->findModel($id);
$message = Yii::t('schema', 'Удалена');
if ($model->delete()) {
Yii::$app->session->setFlash('success', $message);
}
if ($model->getErrors()) {
Yii::$app->session->setFlash('error', json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
}
return $this->redirect(['index']);
}
protected function findModel($id)
{
$model = Schema::find()
->where(['id' => $id])
->one();
if ($model !== null) {
return $model;
} else {
throw new NotFoundHttpException(Yii::t('app/error', 'The requested page does not exist.'));
}
}
}