kernel update

This commit is contained in:
2024-12-18 12:46:57 +03:00
parent 653e0bc983
commit 4ff9fa9ad3
56 changed files with 1023 additions and 84 deletions

View File

@ -30,8 +30,13 @@ class ModuleShopModule extends Module
]);
}
/**
* @throws \Exception
*/
public function deactivate(): void
{
$this->menuService->removeItemBySlug("module_shop");
$this->migrationService->rollbackAtPath("{APP}/modules/module_shop/migrations");
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\forms\CreateModuleShopForm;
use app\modules\module_shop\models\ModuleShop;
use app\modules\module_shop\services\ModuleShopService;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\FileUpload;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\services\ModuleService;
use ZipArchive;
class KernelShopController extends AdminController
{
protected ModuleShopService $moduleShopService;
protected Files $files;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/kernel/";
$this->moduleShopService = new ModuleShopService();
$this->files = new Files();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
/**
* @throws \Exception
*/
#[NoReturn] public function actionAdd(): void
{
$moduleShopForm = new CreateModuleShopForm();
$moduleShopForm->load($_REQUEST);
if (isset($_FILES['path_to_archive']) && $_FILES['path_to_archive']['error'] === UPLOAD_ERR_OK) {
$file = new FileUpload($_FILES['path_to_archive'], ['zip', 'rar', 'igk']);
$file->upload();
$moduleShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpKernelDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $moduleShopForm->getItem('path_to_archive'));
if ($res === TRUE) {
if (!is_dir(RESOURCES_DIR . '/tmp/ms/')) {
mkdir(RESOURCES_DIR . '/tmp/ms/');
}
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpKernelDir . "/";
$zip->extractTo($tmpModuleShopDirFull);
$zip->close();
if (file_exists($tmpModuleShopDirFull . "kernel/manifest.json")){
$kernelInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "kernel");
$moduleShopForm->load($kernelInfo);
}
else {
throw new \Exception("Manifest.json file not found");
}
$this->files->recursiveRemoveDir($tmpModuleShopDirFull);
}
else {
throw new \Exception("zip not found");
}
if ($moduleShopForm->validate()) {
$kernel = $this->moduleShopService->create($moduleShopForm);
if ($kernel) {
Flash::setMessage("success", "Ядро добавлено.");
$this->redirect("/admin/module_shop/view/" . $kernel->id);
}
}
Flash::setMessage("error", "Ошибка добавления ядра: <br>" . $moduleShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/kernel/create");
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\ModuleShop;
use JetBrains\PhpStorm\NoReturn;
use kernel\helpers\Debug;
use kernel\Request;
use kernel\RestController;
class KernelShopRestController extends RestController
{
public function __construct()
{
$this->model = new ModuleShop();
}
#[NoReturn] public function actionUpdate($id): void
{
$model = $this->model->where("id", $id)->first();
$model->installations++;
$model->save();
$this->renderApi($model->toArray());
}
}

View File

@ -11,18 +11,21 @@ use kernel\app_modules\tag\services\TagService;
use kernel\FileUpload;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\services\ModuleService;
use ZipArchive;
class ModuleShopController extends AdminController
{
protected ModuleShopService $moduleShopService;
protected Files $files;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/";
$this->moduleShopService = new ModuleShopService();
$this->files = new Files();
}
public function actionCreate(): void
@ -44,14 +47,14 @@ class ModuleShopController extends AdminController
$moduleShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpThemeDir = md5(time());
$tmpModuleDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $moduleShopForm->getItem('path_to_archive'));
if ($res === TRUE) {
if (!is_dir(RESOURCES_DIR . '/tmp/ms/')) {
mkdir(RESOURCES_DIR . '/tmp/ms/');
}
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpThemeDir . "/";
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleShopDirFull);
$zip->close();
@ -62,6 +65,8 @@ class ModuleShopController extends AdminController
else {
throw new \Exception("Manifest.json file not found");
}
$this->files->recursiveRemoveDir($tmpModuleShopDirFull);
}
else {
throw new \Exception("zip not found");
@ -71,11 +76,11 @@ class ModuleShopController extends AdminController
$module = $this->moduleShopService->create($moduleShopForm);
if ($module) {
Flash::setMessage("success", "Модуль " . $moduleShopForm->getItem("name") . " добавлен.");
$this->redirect("/admin/module_shop/" . $module->id);
$this->redirect("/admin/module_shop/view/" . $module->id);
}
}
Flash::setMessage("error", "Ошибка добавления модуля: <br>" . $moduleShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/create");
$this->redirect("/admin/module_shop/module/create");
}
public function actionIndex(int $page_number = 1): void
@ -101,6 +106,9 @@ class ModuleShopController extends AdminController
throw new \Exception("The module not found");
}
$name = $module->name;
$dir = $module->path_to_archive;
$this->files->recursiveRemoveDir(ROOT_DIR . dirname($dir, 2));
$module->delete();
Flash::setMessage("success", "Модуль " . $name . " удален.");

View File

@ -97,7 +97,7 @@ class ModuleShopRestController extends RestController
$this->renderApi($res);
}
public function actionInstall($id): void
#[NoReturn] public function actionInstall($id): void
{
$model = $this->model->where("id", $id)->first();
$model->installations++;

View File

@ -14,6 +14,7 @@ return new class extends Migration {
$table->increments('id');
$table->string("name", 255)->nullable(false);
$table->string("slug", 255)->nullable(false);
$table->string("type", 255)->nullable(false);
$table->text("description")->nullable(false);
$table->string("version", 255)->nullable(false);
$table->string("author", 255)->nullable(false);

View File

@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
* @property int $id
* @property string $name
* @property string $slug
* @property string $type
* @property string $version
* @property string $description
* @property string $author
@ -24,7 +25,7 @@ class ModuleShop extends Model
protected $table = "module_shop";
protected $fillable = ['name', 'slug', 'version', 'description', 'author', 'status', 'dependence', 'installations', 'views'];
protected $fillable = ['name', 'slug', 'type', 'version', 'description', 'author', 'status', 'dependence', 'installations', 'views'];
public static function labels(): array
{
@ -35,6 +36,7 @@ class ModuleShop extends Model
'author' => 'Автор',
'status' => 'Статус',
'slug' => 'Slug',
'type' => 'Тип',
'dependence' => 'Зависимости',
'installations' => 'Установки',
'views' => 'Просмотры',

View File

@ -8,14 +8,26 @@ class CreateModuleShopForm extends FormModel
{
public function rules(): array
{
// return [
// 'name' => 'required|min-str-len:3',
// 'version' => 'required',
// 'description' => 'required|min-str-len:10',
// 'author' => 'required',
// 'status' => 'required',
// 'slug' => 'required',
// 'type' => 'required',
// 'path_to_archive' => 'required',
// 'dependence' => '',
// ];
return [
'name' => 'required|min-str-len:3',
'version' => 'required',
'description' => 'required|min-str-len:10',
'author' => 'required',
'status' => 'required',
'slug' => 'required',
'path_to_archive' => 'required',
'name' => '',
'version' => '',
'description' => '',
'author' => '',
'status' => '',
'slug' => '',
'type' => '',
'path_to_archive' => '',
'dependence' => '',
];
}

View File

@ -6,24 +6,34 @@ use kernel\CgRouteCollector;
App::$collector->filter('bearer', [\kernel\modules\secure\middlewares\BearerAuthMiddleware::class, "handler"]);
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router){
App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router) {
App::$collector->get('/', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']);
App::$collector->get('/create', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionAdd']);
App::$collector->get('/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionView']);
App::$collector->get('/view/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionDelete']);
App::$collector->get('/pack/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionPack']);
App::$collector->group(["prefix" => "module"], function (CgRouteCollector $router) {
App::$collector->get('/create', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionAdd']);
});
App::$collector->group(["prefix" => "kernel"], function (CgRouteCollector $router) {
App::$collector->get('/create', [\app\modules\module_shop\controllers\KernelShopController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\module_shop\controllers\KernelShopController::class, 'actionAdd']);
});
});
});
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
App::$collector->group(['before' => 'bearer'], function (CgRouteCollector $router){
App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router){
App::$collector->get('/gb_slug', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionIndexGroupBySlug']);
App::$collector->get('/install/{id}', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionInstall']);
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router) {
App::$collector->group(['before' => 'bearer'], function (CgRouteCollector $router) {
App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router) {
App::$collector->get('/gb_slug', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionIndexGroupBySlug']);
App::$collector->get('/install/{id}', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionInstall']);
App::$collector->group(["prefix" => "kernel"], function (CgRouteCollector $router) {
App::$collector->get('/update/{id}', [\app\modules\module_shop\controllers\KernelShopRestController::class, 'actionUpdate']);
});
});
$router->rest("module_shop", [\app\modules\module_shop\controllers\ModuleShopRestController::class]);
});

View File

@ -23,6 +23,7 @@ class ModuleShopService extends ModuleService
$model->path_to_archive = $form_model->getItem("path_to_archive");
$model->dependence = $form_model->getItem("dependence");
$model->slug = $form_model->getItem("slug");
$model->type = $form_model->getItem("type");
if ($model->save()) {
return $model;
@ -41,6 +42,7 @@ class ModuleShopService extends ModuleService
$model->path_to_archive = $form_model->getItem("path_to_archive");
$model->dependence = $form_model->getItem("dependence");
$model->slug = $form_model->getItem("slug");
$model->type = $form_model->getItem("type");
if ($model->save()) {
return $model;

View File

@ -6,7 +6,7 @@
use app\modules\module_shop\models\ModuleShop;
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/admin/module_shop", enctype: 'multipart/form-data');
$form->beginForm("/admin/module_shop/module", enctype: 'multipart/form-data');

View File

@ -24,8 +24,9 @@ $table->columns([
}
]);
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/module_shop/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
$btn = PrimaryBtn::create("Добавить модуль", "/admin/module_shop/module/create")->fetch();
$btn .= PrimaryBtn::create("Добавить ядро", "/admin/module_shop/kernel/create")->fetch();
return $btn;
});
$table->addAction(ViewActionColumn::class);
$table->addAction(DeleteActionColumn::class);

View File

@ -0,0 +1,53 @@
<?php
/**
* @var ModuleShop $model
*/
use app\modules\module_shop\models\ModuleShop;
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/admin/module_shop/kernel", enctype: 'multipart/form-data');
$form->field(class: \itguild\forms\inputs\File::class, name: "path_to_archive", params: [
'class' => "form-control",
'placeholder' => 'Путь к файлу модуля',
'value' => $model->path_to_archive ?? ''
])
->setLabel("Путь к файлу модуля")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [
'class' => "form-control",
'value' => $model->status ?? ''
])
->setLabel("Статус")
->setOptions(ModuleShop::getStatus())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();