update, delete module from shop on local page

This commit is contained in:
Билай Станислав 2024-11-18 15:55:43 +03:00
parent faf0217b52
commit f4b0f006aa
6 changed files with 74 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "Tags", "name": "Tags",
"version": "0.1", "version": "0.12",
"author": "ITGuild", "author": "ITGuild",
"slug": "tag", "slug": "tag",
"description": "Tags module", "description": "Tags module",

View File

@ -109,13 +109,20 @@ class ModuleController extends AdminController
public function actionUpdate(): void public function actionUpdate(): void
{ {
$request = new Request(); $request = new Request();
$active_res = $this->moduleService->updateModule($request->get("slug")); $slug = $request->get('slug');
if (!$active_res){ if ($this->moduleService->isKernelModule($slug)) {
Session::start(); $active_res = $this->moduleService->updateModule($request->get("slug"));
Session::set("error", implode(";", $this->moduleService->getErrors())); if (!$active_res){
$this->redirect("/admin", 302); 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]); $this->cgView->render("view.php", ['data' => $mod_info]);
} }

View File

@ -75,17 +75,24 @@ class ModuleShopClientController extends AdminController
#[NoReturn] public function actionUpdate(): void #[NoReturn] public function actionUpdate(): void
{ {
$request = new Request(); $request = new Request();
$id = $request->get("id"); $slug = $request->get("slug");
$module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id); $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$module_info = json_decode($module_info->getBody()->getContents(), true); $modules_info = json_decode($modules_info->getBody()->getContents(), true);
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules"); foreach ($modules_info as $module) {
$this->moduleService->updateModule('/resources/tmp/modules/' . basename($module_info['path_to_archive'])); 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); $this->redirect('/admin/module_shop_client', 302);
} }
#[NoReturn] public function actionDelete(): void #[NoReturn] public function actionDelete(): void

View File

@ -41,7 +41,7 @@ $table->addAction(function ($row, $url) use ($moduleService){
} }
else { else {
$label = "Установить"; $label = "Установить";
$btn_type = "warning"; $btn_type = "success";
$btn = "<a class='btn btn-$btn_type' href='$url/install/?id=$id' style='margin: 3px; width: 150px;' >$label</a>"; $btn = "<a class='btn btn-$btn_type' href='$url/install/?id=$id' style='margin: 3px; width: 150px;' >$label</a>";
} }
@ -50,16 +50,15 @@ $table->addAction(function ($row, $url) use ($moduleService){
$table->addAction(function ($row, $url) use ($moduleService){ $table->addAction(function ($row, $url) use ($moduleService){
$slug = $row['slug']; $slug = $row['slug'];
$id = $row['id'];
if ($moduleService->isInstall($slug)){ if ($moduleService->isInstall($slug)){
if (!$moduleService->isLastVersion($slug)) { if (!$moduleService->isLastVersion($slug)) {
$label = "Обновить"; $label = "Обновить";
$btn_type = "danger"; $btn_type = "info";
return "<a class='btn btn-$btn_type' href='$url/update/?id=$id' style='margin: 3px; width: 150px;' >$label</a>"; return "<a class='btn btn-$btn_type' href='$url/update/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
} }
} }
return false;
return false;
}); });

View File

@ -7,6 +7,7 @@ use GuzzleHttp\Client;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\helpers\Files; use kernel\helpers\Files;
use kernel\helpers\Manifest; use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option; use kernel\models\Option;
use ZipArchive; use ZipArchive;
@ -438,13 +439,8 @@ class ModuleService
public function isLastVersion(string $slug): bool public function isLastVersion(string $slug): bool
{ {
$client = new Client(); $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$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 = json_decode($modules_info->getBody()->getContents(), true); $modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug); $mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) { 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;
}
} }

View File

@ -35,7 +35,7 @@ $table->addAction(function ($row, $url) use ($moduleService) {
} else { } else {
$label = "Активировать"; $label = "Активировать";
$btn_type = "primary"; $btn_type = "success";
$btn = "<a class='btn btn-$btn_type' href='$url/activate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>"; $btn = "<a class='btn btn-$btn_type' href='$url/activate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
} }
@ -47,10 +47,28 @@ $table->addAction(function ($row, $url) use ($moduleService) {
return "<a class='btn btn-primary' href='$url/view/?slug=$slug' style='margin: 3px; width: 150px;' >Просмотр</a>"; return "<a class='btn btn-primary' href='$url/view/?slug=$slug' style='margin: 3px; width: 150px;' >Просмотр</a>";
}); });
//$table->addAction(function ($row, $url) use ($moduleService) { $table->addAction(function ($row, $url) use ($moduleService){
// $slug = $row['slug']; $slug = $row['slug'];
// return "<a class='btn btn-primary' href='$url/update/?slug=$slug' style='margin: 3px; width: 150px;' >Обновить</a>"; if (!$moduleService->isKernelModule($slug)){
//}); if (!$moduleService->isLastVersion($slug)) {
$label = "Обновить";
$btn_type = "info";
return "<a class='btn btn-$btn_type' href='$url/update/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
}
}
return false;
});
$table->addAction(function ($row, $url) use ($moduleService){
$slug = $row['slug'];
if (!$moduleService->isKernelModule($slug)){
$label = "Удалить";
$btn_type = "danger";
return "<a class='btn btn-$btn_type' href='admin/module_shop_client/delete/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
}
return false;
});
$table->create(); $table->create();