kernel update
This commit is contained in:
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\module_shop_client\controllers;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\RESTClient;
|
||||
use kernel\modules\module_shop_client\services\ModuleShopClientService;
|
||||
use kernel\Request;
|
||||
use kernel\services\ModuleService;
|
||||
|
||||
class ModuleShopClientController extends AdminController
|
||||
{
|
||||
|
||||
protected Client $client;
|
||||
protected ModuleService $moduleService;
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->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);
|
||||
}
|
||||
|
||||
}
|
10
kernel/modules/module_shop_client/manifest.json
Normal file
10
kernel/modules/module_shop_client/manifest.json
Normal file
@ -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"
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->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']);
|
||||
});
|
||||
});
|
||||
});
|
68
kernel/modules/module_shop_client/views/index.php
Normal file
68
kernel/modules/module_shop_client/views/index.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @var array $modules_info
|
||||
* @var int $module_count
|
||||
* @var int $page_number
|
||||
* @var int $per_page
|
||||
* @var \kernel\services\ModuleService $moduleService
|
||||
*/
|
||||
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
|
||||
$meta = [];
|
||||
$meta['columns'] = [
|
||||
"name" => "Название",
|
||||
"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 = "<a class='btn btn-$btn_type' href='$url/delete/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
|
||||
}
|
||||
else {
|
||||
$label = "Установить";
|
||||
$btn_type = "success";
|
||||
$btn = "<a class='btn btn-$btn_type' href='$url/install/?id=$id' style='margin: 3px; width: 150px;' >$label</a>";
|
||||
}
|
||||
|
||||
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 "<a class='btn btn-$btn_type' href='$url/update/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
$table->create();
|
||||
|
||||
\kernel\widgets\ModuleTabsWidget::create()->run();
|
||||
$table->render();
|
32
kernel/modules/module_shop_client/views/view.php
Normal file
32
kernel/modules/module_shop_client/views/view.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* @var array $data
|
||||
*/
|
||||
|
||||
use kernel\IGTabel\btn\DangerBtn;
|
||||
use kernel\IGTabel\btn\PrimaryBtn;
|
||||
use kernel\IGTabel\btn\SuccessBtn;
|
||||
|
||||
$table_info = [
|
||||
"meta" => [
|
||||
"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();
|
Reference in New Issue
Block a user