From f4b0f006aa4d53c23f17d43100d92caa4c07d6c9 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 18 Nov 2024 15:55:43 +0300 Subject: [PATCH] update, delete module from shop on local page --- app/modules/tag/manifest.json | 2 +- kernel/controllers/ModuleController.php | 19 +++++++++---- .../ModuleShopClientController.php | 23 +++++++++------ .../module_shop_client/views/index.php | 9 +++--- kernel/services/ModuleService.php | 25 ++++++++++++----- kernel/views/module/index.php | 28 +++++++++++++++---- 6 files changed, 74 insertions(+), 32 deletions(-) diff --git a/app/modules/tag/manifest.json b/app/modules/tag/manifest.json index 87665ad..39df526 100644 --- a/app/modules/tag/manifest.json +++ b/app/modules/tag/manifest.json @@ -1,6 +1,6 @@ { "name": "Tags", - "version": "0.1", + "version": "0.12", "author": "ITGuild", "slug": "tag", "description": "Tags module", diff --git a/kernel/controllers/ModuleController.php b/kernel/controllers/ModuleController.php index 45522ce..fab7bd3 100644 --- a/kernel/controllers/ModuleController.php +++ b/kernel/controllers/ModuleController.php @@ -109,13 +109,20 @@ class ModuleController extends AdminController public function actionUpdate(): void { $request = new Request(); - $active_res = $this->moduleService->updateModule($request->get("slug")); - if (!$active_res){ - Session::start(); - Session::set("error", implode(";", $this->moduleService->getErrors())); - $this->redirect("/admin", 302); + $slug = $request->get('slug'); + if ($this->moduleService->isKernelModule($slug)) { + $active_res = $this->moduleService->updateModule($request->get("slug")); + if (!$active_res){ + Session::start(); + Session::set("error", implode(";", $this->moduleService->getErrors())); + $this->redirect("/admin", 302); + } + $mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug')); + } else { + $this->redirect("/admin/module_shop_client/update/?slug=" . $slug, 302); } - $mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug')); + + $this->cgView->render("view.php", ['data' => $mod_info]); } diff --git a/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php b/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php index 38015d7..6e7846b 100644 --- a/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php +++ b/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php @@ -75,17 +75,24 @@ class ModuleShopClientController extends AdminController #[NoReturn] public function actionUpdate(): void { $request = new Request(); - $id = $request->get("id"); - $module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id); + $slug = $request->get("slug"); + $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); - $module_info = json_decode($module_info->getBody()->getContents(), true); - Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules"); - $this->moduleService->updateModule('/resources/tmp/modules/' . basename($module_info['path_to_archive'])); + $modules_info = json_decode($modules_info->getBody()->getContents(), true); + foreach ($modules_info as $module) { + if ($module['slug'] === $slug) { + $path = $module['path_to_archive']; + } + } + if (isset($path)) { + Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $path, RESOURCES_DIR . "/tmp/modules"); + $this->moduleService->updateModule('/resources/tmp/modules/' . basename($path)); + Flash::setMessage("success", "Модуль успешно обновлен."); + } else { + Flash::setMessage("error", "Ошибка обновления модуля."); + } - Flash::setMessage("success", "Модуль успешно обновлен."); $this->redirect('/admin/module_shop_client', 302); - - } #[NoReturn] public function actionDelete(): void diff --git a/kernel/modules/module_shop_client/views/index.php b/kernel/modules/module_shop_client/views/index.php index 5e44e0a..ab0199c 100644 --- a/kernel/modules/module_shop_client/views/index.php +++ b/kernel/modules/module_shop_client/views/index.php @@ -41,7 +41,7 @@ $table->addAction(function ($row, $url) use ($moduleService){ } else { $label = "Установить"; - $btn_type = "warning"; + $btn_type = "success"; $btn = "$label"; } @@ -50,16 +50,15 @@ $table->addAction(function ($row, $url) use ($moduleService){ $table->addAction(function ($row, $url) use ($moduleService){ $slug = $row['slug']; - $id = $row['id']; if ($moduleService->isInstall($slug)){ if (!$moduleService->isLastVersion($slug)) { $label = "Обновить"; - $btn_type = "danger"; - return "$label"; + $btn_type = "info"; + return "$label"; } } - return false; + return false; }); diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index f8aad07..cc45d1a 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -7,6 +7,7 @@ use GuzzleHttp\Client; use kernel\helpers\Debug; use kernel\helpers\Files; use kernel\helpers\Manifest; +use kernel\helpers\RESTClient; use kernel\models\Option; use ZipArchive; @@ -438,13 +439,8 @@ class ModuleService public function isLastVersion(string $slug): bool { - $client = new Client(); - $token = $_ENV['MODULE_SHOP_TOKEN']; - $modules_info = $client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug', [ - 'headers' => [ - 'Authorization' => 'Bearer ' . $token, - ] - ]); + $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); + $modules_info = json_decode($modules_info->getBody()->getContents(), true); $mod_info = $this->getModuleInfoBySlug($slug); foreach ($modules_info as $mod) { @@ -456,4 +452,19 @@ class ModuleService } + public function isKernelModule(string $slug): bool + { + $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); + + $modules_info = json_decode($modules_info->getBody()->getContents(), true); + $mod_info = $this->getModuleInfoBySlug($slug); + foreach ($modules_info as $mod) { + if ($mod['slug'] === $mod_info['slug']) { + return false; + } + } + + return true; + } + } \ No newline at end of file diff --git a/kernel/views/module/index.php b/kernel/views/module/index.php index 8bf0051..3cbf641 100644 --- a/kernel/views/module/index.php +++ b/kernel/views/module/index.php @@ -35,7 +35,7 @@ $table->addAction(function ($row, $url) use ($moduleService) { } else { $label = "Активировать"; - $btn_type = "primary"; + $btn_type = "success"; $btn = "$label"; } @@ -47,10 +47,28 @@ $table->addAction(function ($row, $url) use ($moduleService) { return "Просмотр"; }); -//$table->addAction(function ($row, $url) use ($moduleService) { -// $slug = $row['slug']; -// return "Обновить"; -//}); +$table->addAction(function ($row, $url) use ($moduleService){ + $slug = $row['slug']; + if (!$moduleService->isKernelModule($slug)){ + if (!$moduleService->isLastVersion($slug)) { + $label = "Обновить"; + $btn_type = "info"; + return "$label"; + } + } + + return false; +}); + +$table->addAction(function ($row, $url) use ($moduleService){ + $slug = $row['slug']; + if (!$moduleService->isKernelModule($slug)){ + $label = "Удалить"; + $btn_type = "danger"; + return "$label"; + } + return false; +}); $table->create();