feat: edit/create form

This commit is contained in:
b1ek 2024-08-20 15:53:49 +10:00
parent 8efdde5f8c
commit 674dc7dd6a
Signed by: blek
GPG Key ID: 14546221E3595D0C
6 changed files with 174 additions and 2 deletions

View File

@ -3,10 +3,13 @@
namespace app\controllers;
use app\models\Parameter;
use app\models\ParameterForm;
use app\models\ParameterSearch;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\web\Request;
use yii\web\UploadedFile;
class ListController extends \yii\web\Controller
{
@ -24,4 +27,38 @@ class ListController extends \yii\web\Controller
);
}
public function actionEdit(Request $request)
{
$id = $request->getQueryParam('id');
$parameter = Parameter::findOne($id);
if ($id !== null && $parameter !== null) {
throw new NotFoundHttpException();
}
$model = new ParameterForm();
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,
]
);
}
}

View File

@ -3,6 +3,8 @@
namespace app\models;
use Yii;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
/**
* This is the model class for table "image".
@ -21,6 +23,30 @@ class Image extends \yii\db\ActiveRecord
return 'image';
}
public static function fromUploadedFile(?UploadedFile $file): ?Image
{
if ($file === null) {
return null;
}
$hash = hash_file('sha256', $file->tempName);
$existing = Image::find()->where([ 'sha256' => $hash ])->one();
if ($existing) {
return $existing;
}
copy($file->tempName, env('UPLOADS_PATH') . '/' . $hash);
$new = new Image([
'sha256' => $hash,
'original_name' => $file->name,
'mime' => FileHelper::getMimeType($file->tempName)
]);
$new->save(true);
return $new;
}
/**
* {@inheritdoc}
*/

View File

@ -54,7 +54,7 @@ class Parameter extends \yii\db\ActiveRecord
{
return [
[['title', 'type'], 'required'],
[['type'], 'integer'],
[['type'], 'integer', 'min' => 1, 'max' => 2],
[['title'], 'string', 'max' => 255],
];
}

77
models/ParameterForm.php Normal file
View File

@ -0,0 +1,77 @@
<?php
namespace app\models;
use Exception;
use yii\base\Model;
use yii\web\UploadedFile;
class ParameterForm extends Model
{
public $title;
public $type;
public ?UploadedFile $icon = null;
public ?UploadedFile $iconGray = null;
public function rules()
{
return [
[['title'],'string'],
[['type'], 'integer', 'min' => 1, 'max' => 2],
[['icon', 'iconGray'], 'file', 'mimeTypes' => 'image/*', 'maxSize' => 1024 * 1024, 'maxFiles' => 1],
];
}
public function load($data, $formName = null)
{
return $this->setAttributes([
'title' => $data['ParameterForm']['title'],
'type' => $data['ParameterForm']['type'],
'icon' => UploadedFile::getInstance($this, 'icon'),
'iconGray' => UploadedFile::getInstance($this, 'iconGray'),
]);
}
public function create(): int
{
$param = new Parameter();
if (!$this->title && !$this->type) {
throw new Exception('title and type are required');
}
$this->edit($param);
return $param->id;
}
public function edit(Parameter $param): void
{
if ($this->title) {
$param->title = $this->title;
}
if ($this->type) {
$param->type = (int) $this->type;
}
if ($param->type == 2) {
if ($this->icon) {
$icon = Image::fromUploadedFile($this->icon);
$param->icon = $icon->id;
}
if ($this->iconGray) {
$iconGray = Image::fromUploadedFile($this->iconGray);
$param->iconGray = $iconGray->id;
}
$param->setAttributes([
'icon' => $param->icon,
'iconGray' => $param->iconGray,
]);
}
$param->setAttributes([
'title' => $param->title,
'type' => $param->type,
]);
$param->save();
}
}

32
views/list/edit.php Normal file
View File

@ -0,0 +1,32 @@
<?php
use yii\bootstrap5\ActiveForm;
use yii\bootstrap5\Html;
/** @var yii\web\View $this */
$this->title = $id ? "#$id parameter edit" : 'New parameter';
?>
<h1 class="display-4"><?= $this->title ?></h1>
<?php $form = ActiveForm::begin([
'options' => [
'enctype' => 'multipart/form-data',
]
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'type') ?>
<div class="card card-body mb-3 pb-0">
<p class="small">
Note: upload images only if type is 2. They will be ignored if type is 1
</p>
<?= $form->field($model, 'icon')->fileInput() ?>
<?= $form->field($model, 'iconGray')->fileInput() ?>
</div>
<?= Html::submitButton('Save', [ 'class' => 'btn btn-l btn-success w-100' ]) ?>
<?php ActiveForm::end(); ?>

View File

@ -8,7 +8,6 @@ use yii\grid\GridView;
$this->title = 'params list';
?>
<h1>Parameters</h1>
@ -17,6 +16,7 @@ $this->title = 'params list';
'method' => 'get',
]); ?>
<?= Html::a('Create new', '/list/edit', [ 'class' => 'btn btn-l btn-success w-100 mb-1' ]) ?>
<?= Html::submitButton('Run search', [ 'class' => 'btn btn-l btn-success w-100' ]) ?>
<p class="small">Set your search query in the grid view</p>