module shop client

This commit is contained in:
2024-10-29 14:53:52 +03:00
parent 8eae977357
commit 9d243b5b35
11 changed files with 844 additions and 3 deletions

View File

@ -0,0 +1,17 @@
<?php
namespace kernel\IGTabel\action_column;
use Itguild\Tables\ActionColumn\ActionColumn;
class InstallActionColumn extends ActionColumn
{
protected string $prefix = '/install/';
public function fetch()
{
$link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-warning'>Установить</a> ";
}
}

View File

@ -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,6 +67,7 @@ class ModuleController extends AdminController
'module_count' => $module_count,
'per_page' => $per_page,
]);
}
public function actionActivate(): void

View File

@ -0,0 +1,32 @@
<?php
namespace kernel\modules\module_shop_client;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
class ModuleShopClientModule extends Module
{
public MenuService $menuService;
public function __construct()
{
$this->menuService = new MenuService();
}
/**
* @throws \Exception
*/
public function init(): void
{
$this->menuService->createItem([
"label" => "Магазин модулей",
"url" => "/admin/module_shop_client",
"slug" => "module_shop_client",
]);
}
public function deactivate(): void
{
$this->menuService->removeItemBySlug("module_shop_client");
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace kernel\modules\module_shop_client\controllers;
use GuzzleHttp\Client;
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\modules\module_shop_client\services\ModuleShopClientService;
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();
}
public function actionIndex(int $page_number = 1): void
{
$per_page = 8;
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.POQZmGB7EZIayINtYhzu5r1rdgZhF9qPJpaQkl_g6pU';
$modules_info = $this->client->request('GET', 'http://igfs.loc/api/module_shop/gb_slug', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$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,
'page_number' => $page_number,
'module_count' => $module_count,
'per_page' => $per_page,
]);
}
public function actionView(int $id): void
{
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.POQZmGB7EZIayINtYhzu5r1rdgZhF9qPJpaQkl_g6pU';
$module_info = $this->client->request('GET', 'http://igfs.loc/api/module_shop/' . $id, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$module_info = json_decode($module_info->getBody()->getContents(), true);
$this->cgView->render("view.php", ['data' => $module_info]);
}
public function actionInstall(int $id): void
{
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.POQZmGB7EZIayINtYhzu5r1rdgZhF9qPJpaQkl_g6pU';
$module_info = $this->client->request('GET', 'http://igfs.loc/api/module_shop/' . $id, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$module_info = json_decode($module_info->getBody()->getContents(), true);
Debug::dd($module_info['path_to_archive']);
$this->moduleService->installModule($module_info['path_to_archive']);
}
}

View File

@ -0,0 +1,12 @@
{
"name": "Module shop client",
"version": "0.1",
"author": "ITGuild",
"slug": "module_shop_client",
"description": "Module shop client module",
"module_class": "kernel\\modules\\module_shop_client\\ModuleShopClientModule",
"module_class_file": "{KERNEL_MODULES}/module_shop_client/ModuleShopClientModule.php",
"routs": "routs/module_shop_client.php",
"migration_path": "migrations",
"dependence": "menu"
}

View File

@ -0,0 +1,29 @@
<?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/{id}', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionInstall']);
// 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\module_shop_client\controllers\ModuleShopClientController::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']);
});
});
});
//App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
// App::$collector->group(['before' => 'bearer'], function (CgRouteCollector $router){
// $router->rest("post", [\kernel\modules\post\controllers\PostRestController::class]);
// });
//});

View File

@ -0,0 +1,57 @@
<?php
namespace kernel\modules\module_shop_client\services;
use GuzzleHttp\Client;
use Itguild\Tables\ListJsonTable;
use kernel\services\ModuleService;
class ModuleShopClientService extends ModuleService
{
public static function getModuleShopClientInfo()
{
$client = new Client();
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.POQZmGB7EZIayINtYhzu5r1rdgZhF9qPJpaQkl_g6pU';
$res = $client->request('GET', 'http://igfs.loc/api/module_shop/gb_slug', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
return json_decode($res->getBody()->getContents());
}
/**
* @throws \Exception
*/
// public static function getModuleShopClientTable(): ListJsonTable
// {
// $modules_info = json_decode(ModuleShopClientService::getModuleShopClientInfo());
//
// $meta = [];
// $meta['columns'] = [
// "name" => "Название",
// "author" => "Автор",
// "version" => "Версия",
// "description" => "Описание",
// "installations" => 'Установки',
// "views" => 'Просмотры'
// ];
// $meta['params'] = ["class" => "table table-bordered"];
// $meta['perPage'] = 3;
// $meta['baseUrl'] = "/admin/module_shop_client";
// $meta['currentPage'] = 1;
// $meta['total'] = count($modules_info);
//
// $info_to_table['meta'] = $meta;
// $info_to_table['data'] = $modules_info;
//
// $table = new ListJsonTable(json_encode($info_to_table, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// $table->create();
// return $table;
// }
}

View File

@ -0,0 +1,53 @@
<?php
/**
* @var array $modules_info
* @var int $module_count
* @var int $page_number
* @var int $per_page
*/
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(function ($row, $url) use ($moduleService){
// $slug = $row['slug'];
// if ($moduleService->isActive($slug)){
// $label = "Деактивировать";
// $btn_type = "warning";
// $btn = "<a class='btn btn-$btn_type' href='$url/deactivate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
//
// }
// else {
// $label = "Активировать";
// $btn_type = "primary";
// $btn = "<a class='btn btn-$btn_type' href='$url/activate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
// }
//
// return $btn;
//});
$table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\InstallActionColumn::class);
$table->create();
$table->render();

View 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();