diff --git a/controllers/ListController.php b/controllers/ListController.php index e66892c..64d8f4a 100644 --- a/controllers/ListController.php +++ b/controllers/ListController.php @@ -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, + ] + ); + } } diff --git a/models/Image.php b/models/Image.php index c5c3b23..85381ce 100644 --- a/models/Image.php +++ b/models/Image.php @@ -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} */ diff --git a/models/Parameter.php b/models/Parameter.php index c7c52e3..1cd17a5 100644 --- a/models/Parameter.php +++ b/models/Parameter.php @@ -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], ]; } diff --git a/models/ParameterForm.php b/models/ParameterForm.php new file mode 100644 index 0000000..683e8a9 --- /dev/null +++ b/models/ParameterForm.php @@ -0,0 +1,77 @@ + 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(); + } +} \ No newline at end of file diff --git a/views/list/edit.php b/views/list/edit.php new file mode 100644 index 0000000..d8ecfbd --- /dev/null +++ b/views/list/edit.php @@ -0,0 +1,32 @@ +title = $id ? "#$id parameter edit" : 'New parameter'; + +?> + +

title ?>

+ + [ + 'enctype' => 'multipart/form-data', + ] +]); ?> + + field($model, 'title') ?> + field($model, 'type') ?> + +
+

+ Note: upload images only if type is 2. They will be ignored if type is 1 +

+ field($model, 'icon')->fileInput() ?> + field($model, 'iconGray')->fileInput() ?> +
+ + 'btn btn-l btn-success w-100' ]) ?> + + \ No newline at end of file diff --git a/views/list/index.php b/views/list/index.php index bd246e1..2bb890e 100644 --- a/views/list/index.php +++ b/views/list/index.php @@ -8,7 +8,6 @@ use yii\grid\GridView; $this->title = 'params list'; - ?>

Parameters

@@ -17,6 +16,7 @@ $this->title = 'params list'; 'method' => 'get', ]); ?> + 'btn btn-l btn-success w-100 mb-1' ]) ?> 'btn btn-l btn-success w-100' ]) ?>

Set your search query in the grid view