diff --git a/composer.json b/composer.json index c4a4b2f..00bbe7e 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "ext-zip": "*", "josantonius/session": "^2.0", "firebase/php-jwt": "^6.10", - "k-adam/env-editor": "^2.0" + "k-adam/env-editor": "^2.0", + "guzzlehttp/guzzle": "^7.9" }, "autoload": { "psr-4": { diff --git a/kernel/IGTabel/action_column/InstallActionColumn.php b/kernel/IGTabel/action_column/InstallActionColumn.php new file mode 100644 index 0000000..4e64b56 --- /dev/null +++ b/kernel/IGTabel/action_column/InstallActionColumn.php @@ -0,0 +1,17 @@ +baseUrl . $this->prefix . $this->id; + return " Установить "; + } +} \ No newline at end of file diff --git a/kernel/IGTabel/action_column/ViewActionColumn.php b/kernel/IGTabel/action_column/ViewActionColumn.php index bf17e93..c8a2b3e 100644 --- a/kernel/IGTabel/action_column/ViewActionColumn.php +++ b/kernel/IGTabel/action_column/ViewActionColumn.php @@ -3,10 +3,11 @@ namespace kernel\IGTabel\action_column; use Itguild\Tables\ActionColumn\ActionColumn; +use kernel\helpers\Debug; class ViewActionColumn extends ActionColumn { - protected string $prefix = '/'; + protected string $prefix = '/view/'; public function fetch(): string { diff --git a/kernel/controllers/ModuleController.php b/kernel/controllers/ModuleController.php index 323a537..fab7bd3 100644 --- a/kernel/controllers/ModuleController.php +++ b/kernel/controllers/ModuleController.php @@ -7,6 +7,7 @@ use Josantonius\Session\Facades\Session; use kernel\AdminController; use kernel\helpers\Debug; use kernel\models\Option; +use kernel\modules\module_shop_client\services\ModuleShopClientService; use kernel\modules\user\service\UserService; use kernel\Request; use kernel\services\ModuleService; @@ -52,6 +53,11 @@ class ModuleController extends AdminController $i++; } } +// foreach (ModuleShopClientService::getModuleShopClientInfo() as $module) { +// $module->id = $i++; +// $modules_info[] = $module; +// } + $module_count = count($modules_info); $modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page); $this->cgView->render("index.php", [ @@ -61,13 +67,14 @@ class ModuleController extends AdminController 'module_count' => $module_count, 'per_page' => $per_page, ]); + } public function actionActivate(): void { $request = new Request(); $active_res = $this->moduleService->setActiveModule($request->get("slug")); - if (!$active_res){ + if (!$active_res) { Session::start(); Session::set("error", implode(";", $this->moduleService->getErrors())); $this->redirect("/admin", 302); @@ -102,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/helpers/Files.php b/kernel/helpers/Files.php index d140f2c..e05a3a1 100644 --- a/kernel/helpers/Files.php +++ b/kernel/helpers/Files.php @@ -11,7 +11,7 @@ class Files { if (is_dir($d1)) { if (!file_exists($d2)){ - $_d2 = mkdir($d2, permissions: 0755, recursive: true); + $_d2 = mkdir($d2, permissions: 0774, recursive: true); if (!$_d2) { return; } @@ -74,6 +74,12 @@ class Files $zip->close(); } + public static function uploadByUrl(string $url, string $uploadDir = RESOURCES_DIR . "/upload"): void + { + $file_name = basename($url); + file_put_contents($uploadDir . '/' . $file_name, file_get_contents($url)); + } + private function recursiveAddFile(ZipArchive $zip, string $dir, string $folder = ''): void { $includes = new FilesystemIterator($dir); diff --git a/kernel/helpers/RESTClient.php b/kernel/helpers/RESTClient.php new file mode 100644 index 0000000..2706b01 --- /dev/null +++ b/kernel/helpers/RESTClient.php @@ -0,0 +1,21 @@ +request($method, $url, [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $_ENV['MODULE_SHOP_TOKEN'] + ] + ]); + } + +} \ No newline at end of file diff --git a/kernel/modules/menu/controllers/MenuController.php b/kernel/modules/menu/controllers/MenuController.php index 3d36d5d..30d777c 100644 --- a/kernel/modules/menu/controllers/MenuController.php +++ b/kernel/modules/menu/controllers/MenuController.php @@ -43,7 +43,7 @@ class MenuController extends AdminController if ($menuForm->validate()){ $menuItem = $this->menuService->create($menuForm); if ($menuItem){ - $this->redirect("/admin/settings/menu/" . $menuItem->id, code: 302); + $this->redirect("/admin/settings/menu/view/" . $menuItem->id, code: 302); } } $this->redirect("/admin/settings/menu/create", code: 302); @@ -107,7 +107,7 @@ class MenuController extends AdminController if ($menuForm->validate()){ $menuItem = $this->menuService->update($menuForm, $menuItem); if ($menuItem){ - $this->redirect("/admin/settings/menu/" . $menuItem->id, code: 302); + $this->redirect("/admin/settings/menu/view/" . $menuItem->id, code: 302); } } $this->redirect("/admin/settings/menu/update/" . $id, code: 302); diff --git a/kernel/modules/menu/routs/menu.php b/kernel/modules/menu/routs/menu.php index 94ea806..a552303 100644 --- a/kernel/modules/menu/routs/menu.php +++ b/kernel/modules/menu/routs/menu.php @@ -14,7 +14,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) App::$collector->get('/page/{page_number}', [\kernel\modules\menu\controllers\MenuController::class, 'actionIndex']); App::$collector->get('/create', [\kernel\modules\menu\controllers\MenuController::class, 'actionCreate']); App::$collector->post("/", [\kernel\modules\menu\controllers\MenuController::class, 'actionAdd']); - App::$collector->get('/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionView']); + App::$collector->get('/view/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionView']); App::$collector->any('/update/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionUpdate']); App::$collector->any("/edit/{id}", [\kernel\modules\menu\controllers\MenuController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionDelete']); diff --git a/kernel/modules/menu/service/MenuService.php b/kernel/modules/menu/service/MenuService.php index 0b32ff6..65476c4 100644 --- a/kernel/modules/menu/service/MenuService.php +++ b/kernel/modules/menu/service/MenuService.php @@ -105,9 +105,6 @@ class MenuService $child = self::getChild($id); if (!$child->isEmpty()){ foreach ($child as $item){ -// if ($item->url === \kernel\Request::getUrlPath()){ -// return true; -// } if (strripos(Request::getUrlPath(), $item->url) !== false) { return true; } diff --git a/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php b/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php new file mode 100644 index 0000000..6e7846b --- /dev/null +++ b/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php @@ -0,0 +1,109 @@ +cgView->viewPath = KERNEL_MODULES_DIR . "/module_shop_client/views/"; + + $this->client = new Client(); + $this->moduleService = new ModuleService(); + } + + /** + * @throws GuzzleException + */ + public function actionIndex(int $page_number = 1): void + { + $per_page = 8; + $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); + $modules_info = json_decode($modules_info->getBody()->getContents(), true); + $module_count = count($modules_info); + $modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page); + $this->cgView->render("index.php", [ + 'modules_info' => $modules_info, + 'moduleService' => $this->moduleService, + 'page_number' => $page_number, + 'module_count' => $module_count, + 'per_page' => $per_page, + ]); + } + + public function actionView(int $id): void + { + $module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id); + $module_info = json_decode($module_info->getBody()->getContents(), true); + $this->cgView->render("view.php", ['data' => $module_info]); + } + + /** + * @throws GuzzleException + */ + #[NoReturn] public function actionInstall(): void + { + $request = new Request(); + $id = $request->get("id"); + $module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id); + + $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->installModule('/resources/tmp/modules/' . basename($module_info['path_to_archive'])); + + Flash::setMessage("success", "Модуль успешно установлен."); + $this->redirect('/admin/module_shop_client', 302); + } + + #[NoReturn] public function actionUpdate(): void + { + $request = new Request(); + $slug = $request->get("slug"); + $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); + + $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", "Ошибка обновления модуля."); + } + + $this->redirect('/admin/module_shop_client', 302); + } + + #[NoReturn] public function actionDelete(): void + { + $request = new Request(); + $slug = $request->get("slug"); + $module_info = $this->moduleService->getModuleInfoBySlug($slug); + $this->moduleService->uninstallModule($module_info['app_module_path']); + + Flash::setMessage("success", "Модуль успешно удален."); + $this->redirect('/admin/module_shop_client', 302); + } + +} \ No newline at end of file diff --git a/kernel/modules/module_shop_client/manifest.json b/kernel/modules/module_shop_client/manifest.json new file mode 100644 index 0000000..cf19300 --- /dev/null +++ b/kernel/modules/module_shop_client/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Module shop client", + "version": "0.1", + "author": "ITGuild", + "slug": "module_shop_client", + "description": "Module shop client module", + "routs": "routs/module_shop_client.php", + "migration_path": "migrations", + "dependence": "menu" +} \ No newline at end of file diff --git a/kernel/modules/module_shop_client/routs/module_shop_client.php b/kernel/modules/module_shop_client/routs/module_shop_client.php new file mode 100644 index 0000000..416831c --- /dev/null +++ b/kernel/modules/module_shop_client/routs/module_shop_client.php @@ -0,0 +1,20 @@ +filter('bearer', [\kernel\modules\secure\middlewares\BearerAuthMiddleware::class, "handler"]); + +App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ + App::$collector->group(["before" => "auth"], function (RouteCollector $router) { + App::$collector->group(["prefix" => "module_shop_client"], function (RouteCollector $router) { + App::$collector->get('/', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionIndex']); + App::$collector->get('/install', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionInstall']); + App::$collector->get('/view/{id}', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionView']); + App::$collector->get('/delete', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionDelete']); + App::$collector->get('/update', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionUpdate']); + }); + }); +}); \ No newline at end of file diff --git a/kernel/modules/module_shop_client/views/index.php b/kernel/modules/module_shop_client/views/index.php new file mode 100644 index 0000000..ab0199c --- /dev/null +++ b/kernel/modules/module_shop_client/views/index.php @@ -0,0 +1,68 @@ + "Название", + "author" => "Автор", + "version" => "Версия", + "description" => "Описание", + "installations" => "Установки", + "views" => "Просмотры" +]; +$meta['params'] = ["class" => "table table-bordered"]; +$meta['perPage'] = $per_page; +$meta['baseUrl'] = "/admin/module_shop_client"; +$meta['currentPage'] = $page_number; +$meta['total'] = $module_count; + +$info_to_table['meta'] = $meta; +$info_to_table['data'] = $modules_info; + +$table = new ListJsonTable(json_encode($info_to_table, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); + +$table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class); + +$table->addAction(function ($row, $url) use ($moduleService){ + $slug = $row['slug']; + $id = $row['id']; + if ($moduleService->isInstall($slug)){ + $label = "Удалить"; + $btn_type = "danger"; + $btn = "$label"; + } + else { + $label = "Установить"; + $btn_type = "success"; + $btn = "$label"; + } + + return $btn; +}); + +$table->addAction(function ($row, $url) use ($moduleService){ + $slug = $row['slug']; + if ($moduleService->isInstall($slug)){ + if (!$moduleService->isLastVersion($slug)) { + $label = "Обновить"; + $btn_type = "info"; + return "$label"; + } + } + + return false; +}); + + +$table->create(); + +\kernel\widgets\ModuleTabsWidget::create()->run(); +$table->render(); diff --git a/kernel/modules/module_shop_client/views/view.php b/kernel/modules/module_shop_client/views/view.php new file mode 100644 index 0000000..05bcc51 --- /dev/null +++ b/kernel/modules/module_shop_client/views/view.php @@ -0,0 +1,32 @@ + [ + "rows" => [ + "name" => "Название", + "author" => "Автор", + "version" => "Версия", + "description" => "Описание", + "installations" => "Установки", + "views" => "Просмотры" + ], + "params" => ["class" => "table table-bordered"], + "baseUrl" => "/admin/module_shop_client", + ], + "data" => $data +]; +$table = new \Itguild\Tables\ViewJsonTable(json_encode($table_info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); + +$table->beforePrint(function () { + $btn = PrimaryBtn::create("Список", "/admin/module_shop_client")->fetch(); + return $btn; +}); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/kernel/modules/option/controllers/OptionController.php b/kernel/modules/option/controllers/OptionController.php index bc929df..7b1fe2e 100644 --- a/kernel/modules/option/controllers/OptionController.php +++ b/kernel/modules/option/controllers/OptionController.php @@ -88,7 +88,7 @@ class OptionController extends AdminController if ($optionForm->validate()) { $option = $this->optionService->update($optionForm, $option); if ($option) { - $this->redirect('/admin/option/' . $option->id); + $this->redirect('/admin/option/view/' . $option->id); } } diff --git a/kernel/modules/option/routs/option.php b/kernel/modules/option/routs/option.php index 639becf..9a8ee01 100644 --- a/kernel/modules/option/routs/option.php +++ b/kernel/modules/option/routs/option.php @@ -10,7 +10,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) App::$collector->get('/page/{page_number}', [\kernel\modules\option\controllers\OptionController::class, 'actionIndex']); App::$collector->get('/create', [\kernel\modules\option\controllers\OptionController::class, 'actionCreate']); App::$collector->post("/", [\kernel\modules\option\controllers\OptionController::class, 'actionAdd']); - App::$collector->get('/{id}', [\kernel\modules\option\controllers\OptionController::class, 'actionView']); + App::$collector->get('/view/{id}', [\kernel\modules\option\controllers\OptionController::class, 'actionView']); App::$collector->any('/update/{id}', [\kernel\modules\option\controllers\OptionController::class, 'actionUpdate']); App::$collector->any("/edit/{id}", [\kernel\modules\option\controllers\OptionController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\kernel\modules\option\controllers\OptionController::class, 'actionDelete']); diff --git a/kernel/modules/post/controllers/PostController.php b/kernel/modules/post/controllers/PostController.php index b65c628..ccd0d44 100644 --- a/kernel/modules/post/controllers/PostController.php +++ b/kernel/modules/post/controllers/PostController.php @@ -32,7 +32,7 @@ class PostController extends AdminController if ($postForm->validate()) { $post = $this->postService->create($postForm); if ($post) { - $this->redirect("/admin/post/" . $post->id); + $this->redirect("/admin/post/view/" . $post->id); } } $this->redirect("/admin/post/create"); @@ -87,7 +87,7 @@ class PostController extends AdminController if ($postForm->validate()) { $post = $this->postService->update($postForm, $post); if ($post) { - $this->redirect("/admin/post/" . $post->id); + $this->redirect("/admin/post/view/" . $post->id); } } $this->redirect("/admin/post/update/" . $id); diff --git a/kernel/modules/post/routs/post.php b/kernel/modules/post/routs/post.php index c52a2bb..e5be155 100644 --- a/kernel/modules/post/routs/post.php +++ b/kernel/modules/post/routs/post.php @@ -13,7 +13,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']); App::$collector->get('/create', [\kernel\modules\post\controllers\PostController::class, 'actionCreate']); App::$collector->post("/", [\kernel\modules\post\controllers\PostController::class, 'actionAdd']); - App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']); + App::$collector->get('/view/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']); App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']); App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionDelete']); diff --git a/kernel/modules/post/table/columns/PostDeleteActionColumn.php b/kernel/modules/post/table/columns/PostDeleteActionColumn.php deleted file mode 100644 index c04ee6b..0000000 --- a/kernel/modules/post/table/columns/PostDeleteActionColumn.php +++ /dev/null @@ -1,16 +0,0 @@ -baseUrl . $this->prefix . $this->id; - return " Удалить "; - } -} \ No newline at end of file diff --git a/kernel/modules/post/table/columns/PostEditActionColumn.php b/kernel/modules/post/table/columns/PostEditActionColumn.php deleted file mode 100644 index d2802bb..0000000 --- a/kernel/modules/post/table/columns/PostEditActionColumn.php +++ /dev/null @@ -1,16 +0,0 @@ -baseUrl . $this->prefix . $this->id; - return " Редактировать "; - } -} \ No newline at end of file diff --git a/kernel/modules/post/table/columns/PostViewActionColumn.php b/kernel/modules/post/table/columns/PostViewActionColumn.php deleted file mode 100644 index ecdec5d..0000000 --- a/kernel/modules/post/table/columns/PostViewActionColumn.php +++ /dev/null @@ -1,16 +0,0 @@ -baseUrl . $this->prefix . $this->id; - return " Просмотр "; - } -} \ No newline at end of file diff --git a/kernel/modules/user/controllers/UserController.php b/kernel/modules/user/controllers/UserController.php index 36c6700..47d439f 100644 --- a/kernel/modules/user/controllers/UserController.php +++ b/kernel/modules/user/controllers/UserController.php @@ -36,7 +36,7 @@ class UserController extends AdminController if ($userForm->validate()){ $user = $this->userService->create($userForm); if ($user){ - $this->redirect("/admin/user/" . $user->id); + $this->redirect("/admin/user/view/" . $user->id); } } $this->redirect("/admin/user/create"); @@ -94,7 +94,7 @@ class UserController extends AdminController if ($userForm->validate()){ $user = $userService->update($userForm, $user); if ($user){ - $this->redirect("/admin/user/" . $user->id); + $this->redirect("/admin/user/view/" . $user->id); } } $this->redirect("/admin/user/update/" . $id); diff --git a/kernel/modules/user/routs/user.php b/kernel/modules/user/routs/user.php index d7214fd..5572c92 100644 --- a/kernel/modules/user/routs/user.php +++ b/kernel/modules/user/routs/user.php @@ -13,7 +13,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->get('/page/{page_number}', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']); App::$collector->get('/create', [\kernel\modules\user\controllers\UserController::class, 'actionCreate']); App::$collector->post("/", [\kernel\modules\user\controllers\UserController::class, 'actionAdd']); - App::$collector->get('/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionView']); + App::$collector->get('/view/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionView']); App::$collector->any('/update/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionUpdate']); App::$collector->any("/edit/{id}", [\kernel\modules\user\controllers\UserController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionDelete']); diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index 646f449..cc45d1a 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -3,9 +3,11 @@ namespace kernel\services; use DirectoryIterator; +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; @@ -281,7 +283,7 @@ class ModuleService $tmpModuleDir = md5(time()); $res = $zip->open(ROOT_DIR . $path); if ($res === TRUE) { - $tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/"; + $tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/"; $zip->extractTo($tmpModuleDirFull); $zip->close(); } else { @@ -289,20 +291,20 @@ class ModuleService return false; } - if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) { + if (!file_exists($tmpModuleDirFull . "app/manifest.json")) { $this->addError('manifest.json not found'); return false; } - $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json")); + $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json")); $manifest = Manifest::getWithVars($manifestJson); $fileHelper = new Files(); - $fileHelper->copy_folder($tmpModuleDirFull . '/app', $manifest['app_module_path']); + $fileHelper->copy_folder($tmpModuleDirFull . 'app', $manifest['app_module_path']); if (isset($manifest['kernel_module_path'])) { - $fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']); + $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']); } else { - $fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']); + $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']); } $fileHelper->recursiveRemoveDir($tmpModuleDirFull); @@ -368,7 +370,7 @@ class ModuleService $tmpModuleDir = md5(time()); $res = $zip->open(ROOT_DIR . $path); if ($res === TRUE) { - $tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/"; + $tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/"; $zip->extractTo($tmpModuleDirFull); $zip->close(); } else { @@ -376,19 +378,21 @@ class ModuleService return false; } - if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) { + if (!file_exists($tmpModuleDirFull . "app/manifest.json")) { $this->addError('manifest.json not found'); return false; } - $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json")); + $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json")); $manifest = Manifest::getWithVars($manifestJson); $fileHelper = new Files(); + + $fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', $manifest['app_module_path'] . '/manifest.json'); if (isset($manifest['kernel_module_path'])) { - $fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']); + $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']); } else { - $fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']); + $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']); } $fileHelper->recursiveRemoveDir($tmpModuleDirFull); @@ -411,4 +415,56 @@ class ModuleService return $dependence_array; } + public function isInstall(string $slug): bool + { + $module_paths = Option::where("key", "module_paths")->first(); + $dirs = []; + if ($module_paths){ + $path = json_decode($module_paths->value); + foreach ($path->paths as $p){ + $dirs[] = getConst($p); + } + } + foreach ($dirs as $dir){ + foreach (new DirectoryIterator($dir) as $fileInfo) { + if($fileInfo->isDot()) continue; + if ($this->getModuleInfo($fileInfo->getPathname())['slug'] === $slug) { + return true; + }; + } + } + + return false; + } + + public function isLastVersion(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'] && $mod['version'] === $mod_info['version']) { + return true; + } + } + return false; + + } + + 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 6cdc2fd..3cbf641 100644 --- a/kernel/views/module/index.php +++ b/kernel/views/module/index.php @@ -26,32 +26,53 @@ $info_to_table['data'] = $modules_info; $table = new \Itguild\Tables\ListJsonTable(json_encode($info_to_table, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); -$table->addAction(function ($row, $url) use ($moduleService){ +$table->addAction(function ($row, $url) use ($moduleService) { $slug = $row['slug']; - if ($moduleService->isActive($slug)){ + if ($moduleService->isActive($slug)) { $label = "Деактивировать"; $btn_type = "warning"; $btn = "$label"; - } - else { + } else { $label = "Активировать"; - $btn_type = "primary"; + $btn_type = "success"; $btn = "$label"; } return $btn; }); -$table->addAction(function ($row, $url) use ($moduleService){ +$table->addAction(function ($row, $url) use ($moduleService) { $slug = $row['slug']; return "Просмотр"; }); $table->addAction(function ($row, $url) use ($moduleService){ $slug = $row['slug']; - return "Обновить"; + 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(); + +if ($moduleService->isActive('module_shop_client')) { + \kernel\widgets\ModuleTabsWidget::create()->run(); +} $table->render(); diff --git a/kernel/views/module/view.php b/kernel/views/module/view.php index 7fb4409..168fcf9 100644 --- a/kernel/views/module/view.php +++ b/kernel/views/module/view.php @@ -23,7 +23,7 @@ $table_info = [ $table = new \Itguild\Tables\ViewJsonTable(json_encode($table_info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); $table->beforePrint(function () { - $btn = PrimaryBtn::create("Список", "/admin/module")->fetch(); + $btn = PrimaryBtn::create("Список", "/admin")->fetch(); return $btn; }); $table->create(); diff --git a/kernel/widgets/ModuleTabsWidget.php b/kernel/widgets/ModuleTabsWidget.php new file mode 100644 index 0000000..94686f2 --- /dev/null +++ b/kernel/widgets/ModuleTabsWidget.php @@ -0,0 +1,18 @@ + 'Локальные', + '/admin/module_shop_client' => 'Каталог' + ]; + $this->cgView->render('/admin/module_tabs.php', ['tabs' => $tabs]); + } +} \ No newline at end of file