From 9af44ca95f6ea590e0db7bdcf85e8a349386edf5 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 16 Oct 2024 17:54:33 +0300 Subject: [PATCH] module shop, flash message --- .../controllers/ModuleShopController.php | 39 +++++++++++++++++ .../controllers/ModuleShopRestController.php | 16 +++++++ .../models/forms/CreateModuleShopForm.php | 3 +- app/modules/module_shop/routs/ms.php | 2 +- app/modules/module_shop/views/form.php | 43 +------------------ app/modules/module_shop/views/index.php | 1 - app/modules/tag/controllers/TagController.php | 5 +++ app/modules/tag/manifest.json | 3 +- kernel/Flash.php | 29 +++++++++++++ kernel/FormModel.php | 17 +++++++- kernel/admin_themes/default/layout/main.php | 15 ++++--- resources/default/js/main.js | 18 ++++++-- 12 files changed, 137 insertions(+), 54 deletions(-) create mode 100644 app/modules/module_shop/controllers/ModuleShopRestController.php create mode 100644 kernel/Flash.php diff --git a/app/modules/module_shop/controllers/ModuleShopController.php b/app/modules/module_shop/controllers/ModuleShopController.php index 54505d0..1f64435 100644 --- a/app/modules/module_shop/controllers/ModuleShopController.php +++ b/app/modules/module_shop/controllers/ModuleShopController.php @@ -5,11 +5,16 @@ namespace app\modules\module_shop\controllers; use app\modules\module_shop\models\forms\CreateModuleShopForm; use app\modules\module_shop\models\ModuleShop; use app\modules\module_shop\services\ModuleShopService; +use Cassandra\Date; use JetBrains\PhpStorm\NoReturn; use kernel\AdminController; use kernel\app_modules\tag\services\TagService; +use kernel\FileUpload; +use kernel\Flash; +use kernel\helpers\Debug; use kernel\services\ModuleService; use mysql_xdevapi\Exception; +use ZipArchive; class ModuleShopController extends AdminController { @@ -27,16 +32,48 @@ class ModuleShopController extends AdminController $this->cgView->render("form.php"); } + /** + * @throws \Exception + */ #[NoReturn] public function actionAdd(): void { $moduleShopForm = new CreateModuleShopForm(); $moduleShopForm->load($_REQUEST); + + if (isset($_FILES['path_to_archive']) && $_FILES['path_to_archive']['error'] === UPLOAD_ERR_OK) { + $file = new FileUpload($_FILES['path_to_archive'], ['zip', 'rar', 'itguild']); + $file->upload(); + $moduleShopForm->setItem('path_to_archive', $file->getUploadFile()); + } + + $tmpThemeDir = md5(time()); + $zip = new ZipArchive; + $res = $zip->open(ROOT_DIR . $moduleShopForm->getItem('path_to_archive')); + if ($res === TRUE) { + $tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpThemeDir . "/"; + $zip->extractTo($tmpModuleShopDirFull); + $zip->close(); + + if (file_exists($tmpModuleShopDirFull . "/app/manifest.json")){ + $moduleInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "app"); + $moduleShopForm->load($moduleInfo); + } + else { + throw new \Exception("Manifest.json file not found"); + } + } + else { + throw new \Exception("zip not found"); + } + if ($moduleShopForm->validate()) { $module = $this->moduleShopService->create($moduleShopForm); if ($module) { + Flash::setMessage("success", "Модуль " . $moduleShopForm->getItem("name") . " добавлен."); $this->redirect("/admin/module_shop/" . $module->id); } } + Flash::setMessage("error", "Ошибка добавления модуля:
" . $moduleShopForm->getErrorsStr()); $this->redirect("/admin/module_shop/create"); } @@ -90,8 +127,10 @@ class ModuleShopController extends AdminController if (!$module) { throw new Exception("The module not found"); } + $name = $module->name; $module->delete(); + Flash::setMessage("success", "Модуль " . $name . " удален."); $this->redirect("/admin/module_shop"); } diff --git a/app/modules/module_shop/controllers/ModuleShopRestController.php b/app/modules/module_shop/controllers/ModuleShopRestController.php new file mode 100644 index 0000000..b53a7ba --- /dev/null +++ b/app/modules/module_shop/controllers/ModuleShopRestController.php @@ -0,0 +1,16 @@ +model = new ModuleShop(); + } + +} \ No newline at end of file diff --git a/app/modules/module_shop/models/forms/CreateModuleShopForm.php b/app/modules/module_shop/models/forms/CreateModuleShopForm.php index 43d295b..2271820 100644 --- a/app/modules/module_shop/models/forms/CreateModuleShopForm.php +++ b/app/modules/module_shop/models/forms/CreateModuleShopForm.php @@ -9,11 +9,12 @@ class CreateModuleShopForm extends FormModel public function rules(): array { return [ - 'name' => 'required|min-str-len:5', + 'name' => 'required|min-str-len:3', 'version' => 'required', 'description' => 'required|min-str-len:10', 'author' => 'required', 'status' => 'required', + 'slug' => 'required', 'path_to_archive' => 'required', 'dependence' => '', ]; diff --git a/app/modules/module_shop/routs/ms.php b/app/modules/module_shop/routs/ms.php index e9adcab..a2c1be9 100644 --- a/app/modules/module_shop/routs/ms.php +++ b/app/modules/module_shop/routs/ms.php @@ -18,5 +18,5 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router }); App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ - $router->rest("module_shop", [\app\modules\module_shop\controllers\ModuleShopController::class]); + $router->rest("module_shop", [\app\modules\module_shop\controllers\ModuleShopRestController::class]); }); \ No newline at end of file diff --git a/app/modules/module_shop/views/form.php b/app/modules/module_shop/views/form.php index 701fc97..fad7789 100644 --- a/app/modules/module_shop/views/form.php +++ b/app/modules/module_shop/views/form.php @@ -6,50 +6,11 @@ use app\modules\module_shop\models\ModuleShop; $form = new \itguild\forms\ActiveForm(); -$form->beginForm(isset($model) ? "/admin/module_shop/edit/" . $model->id : "/admin/module_shop"); +$form->beginForm(isset($model) ? "/admin/module_shop/edit/" . $model->id : "/admin/module_shop", enctype: 'multipart/form-data'); -$form->field(\itguild\forms\inputs\TextInput::class, 'name', [ - 'class' => "form-control", - 'placeholder' => 'Название', - 'value' => $model->name ?? '' -]) - ->setLabel("Название") - ->render(); -$form->field(\itguild\forms\inputs\TextInput::class, 'author', [ - 'class' => "form-control", - 'placeholder' => 'Автор', - 'value' => $model->author ?? '' -]) - ->setLabel("Автор") - ->render(); -$form->field(class: \itguild\forms\inputs\TextInput::class, name: "version", params: [ - 'class' => "form-control", - 'placeholder' => 'Версия', - 'value' => $model->version ?? '' -]) - ->setLabel("Версия") - ->render(); - -$form->field(class: \itguild\forms\inputs\TextArea::class, name: "description", params: [ - 'class' => "form-control", - 'placeholder' => 'Описание', - 'rows' => '10', - 'value' => $model->description ?? '' -]) - ->setLabel("Описание") - ->render(); - -$form->field(class: \itguild\forms\inputs\TextInput::class, name: "dependence", params: [ - 'class' => "form-control", - 'placeholder' => 'Зависимости', - 'value' => $model->dependence ?? '' -]) - ->setLabel("Зависимости") - ->render(); - -$form->field(class: \itguild\forms\inputs\TextInput::class, name: "path_to_archive", params: [ +$form->field(class: \itguild\forms\inputs\File::class, name: "path_to_archive", params: [ 'class' => "form-control", 'placeholder' => 'Путь к файлу модуля', 'value' => $model->path_to_archive ?? '' diff --git a/app/modules/module_shop/views/index.php b/app/modules/module_shop/views/index.php index 86e64ff..b00b819 100644 --- a/app/modules/module_shop/views/index.php +++ b/app/modules/module_shop/views/index.php @@ -32,6 +32,5 @@ $table->beforePrint(function () { $table->addAction(\app\modules\module_shop\table\columns\ModuleShopViewActionColumn::class); $table->addAction(\app\modules\module_shop\table\columns\ModuleShopEditActionColumn::class); $table->addAction(\app\modules\module_shop\table\columns\ModuleShopDeleteActionColumn::class); -$table->addAction(\app\modules\module_shop\table\columns\ModuleShopPackActionColumn::class); $table->create(); $table->render(); \ No newline at end of file diff --git a/app/modules/tag/controllers/TagController.php b/app/modules/tag/controllers/TagController.php index 4598872..e0212a2 100644 --- a/app/modules/tag/controllers/TagController.php +++ b/app/modules/tag/controllers/TagController.php @@ -5,4 +5,9 @@ namespace app\modules\tag\controllers; class TagController extends \kernel\app_modules\tag\controllers\TagController { + public function actionByEntity() + { + // TODO + } + } \ No newline at end of file diff --git a/app/modules/tag/manifest.json b/app/modules/tag/manifest.json index 456f81c..87665ad 100644 --- a/app/modules/tag/manifest.json +++ b/app/modules/tag/manifest.json @@ -7,5 +7,6 @@ "app_module_path": "{APP}/modules/{slug}", "module_class": "app\\modules\\tag\\TagModule", "module_class_file": "{APP}/modules/tag/TagModule.php", - "routs": "routs/tag.php" + "routs": "routs/tag.php", + "dependence": "menu" } diff --git a/kernel/Flash.php b/kernel/Flash.php new file mode 100644 index 0000000..83efc3f --- /dev/null +++ b/kernel/Flash.php @@ -0,0 +1,29 @@ +data[$name])){ + if (isset($this->data[$name])) { return $this->data[$name]; } @@ -64,4 +64,19 @@ class FormModel return $this->validator->getProcessedErrors(); } + public function getErrorsStr(): string + { + $errorsArr = $this->getErrors(); + $str = ''; + if ($errorsArr['errors']) { + foreach ($errorsArr['errors'] as $key => $errorArr) { + foreach ($errorsArr['errors'][$key] as $error){ + $str .= "$error
"; + } + } + } + + return $str; + } + } \ No newline at end of file diff --git a/kernel/admin_themes/default/layout/main.php b/kernel/admin_themes/default/layout/main.php index e8f69b8..704683f 100644 --- a/kernel/admin_themes/default/layout/main.php +++ b/kernel/admin_themes/default/layout/main.php @@ -70,13 +70,18 @@ - -
- - - + +
+ +
+ +
+ + +
+
diff --git a/resources/default/js/main.js b/resources/default/js/main.js index 4b4e790..84484e9 100644 --- a/resources/default/js/main.js +++ b/resources/default/js/main.js @@ -16,8 +16,20 @@ $('#sidebar').toggleClass('active'); }); - $("#closeAlertBtn").click(function () { - $("#mainAlert").alert("close"); - }); + // $(".closeAlertBtn").click(function () { + // console.log($(this).parentNode) + // $(this).parentElement.alert("close"); + // }); })(jQuery); + +window.addEventListener("load", function () { + let closeBtn = document.querySelectorAll(".closeAlertBtn"); + closeBtn.forEach((item) => { + item.addEventListener("click", closeAlert); + }); +}); + +function closeAlert() { + this.parentNode.setAttribute("style", "display: none;") +}