banki.test/controllers/ListController.php

87 lines
2.2 KiB
PHP
Raw Normal View History

2024-08-20 06:27:17 +02:00
<?php
namespace app\controllers;
use app\models\Parameter;
2024-08-20 07:53:49 +02:00
use app\models\ParameterForm;
2024-08-20 06:27:17 +02:00
use app\models\ParameterSearch;
2024-08-20 07:53:49 +02:00
use yii\web\NotFoundHttpException;
2024-08-20 06:27:17 +02:00
use yii\web\Request;
class ListController extends \yii\web\Controller
{
public function actionIndex(Request $request)
{
$searchModel = new ParameterSearch();
$dataProvider = $searchModel->search($request->queryParams);
return $this->render(
'index',
[
'provider' => $dataProvider,
'searchModel' => $searchModel,
]
);
}
2024-08-20 07:53:49 +02:00
public function actionEdit(Request $request)
{
$id = $request->getQueryParam('id');
$parameter = Parameter::findOne($id);
2024-08-20 08:53:14 +02:00
if ($parameter === null && $id !== null) {
2024-08-20 07:53:49 +02:00
throw new NotFoundHttpException();
}
$model = new ParameterForm();
if ($parameter) {
$model->setAttributes([
'title' => $parameter->title,
'type' => $parameter->type,
]);
}
2024-08-20 07:53:49 +02:00
if ($request->isPost) {
$data = $request->post();
$model->load($data);
if ($model->validate()) {
if ($id) {
$model->edit($parameter);
} else {
$id = $model->create();
}
return $this->redirect('/list?ParameterSearch[id]=' . $id);
}
}
return $this->render(
'edit',
[
'id' => $id,
'model' => $model,
]
);
}
2024-08-20 08:53:14 +02:00
public function actionDelicon(Request $request)
{
$id = $request->getQueryParam('id');
$type = $request->getQueryParam('type');
if ($id === null || !($type == 'normal' || $type == 'gray')) {
return $this->redirect('/list');
}
$param = Parameter::find()->where([ 'id' => $id ])->one();
if ($param === null) {
return $this->redirect('/list');
}
2024-08-21 03:50:20 +02:00
$param->setAttribute($type == 'normal' ? 'icon_id' : 'icon_gray_id', null);
2024-08-20 08:53:14 +02:00
$param->save();
return $this->redirect('/list?ParameterSearch[id]='. $id);
}
2024-08-20 06:27:17 +02:00
}