admin theme to ms

This commit is contained in:
Билай Станислав 2024-12-23 14:16:00 +03:00
parent 483fe35561
commit 219ca30608
11 changed files with 206 additions and 37 deletions

View File

@ -0,0 +1,84 @@
<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\forms\CreateAdminThemeShopForm;
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 ZipArchive;
class AdminThemeShopController extends AdminController
{
protected ModuleShopService $moduleShopService;
protected Files $files;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/admin_theme/";
$this->moduleShopService = new ModuleShopService();
$this->files = new Files();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
/**
* @throws \Exception
*/
#[NoReturn] public function actionAdd(): void
{
$adminThemeShopForm = new CreateAdminThemeShopForm();
$adminThemeShopForm->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', 'igt']);
$file->upload();
$adminThemeShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpThemeDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $adminThemeShopForm->getItem('path_to_archive'));
if ($res === TRUE) {
if (!is_dir(RESOURCES_DIR . '/tmp/ms/')) {
$oldMask = umask(0);
mkdir(RESOURCES_DIR . '/tmp/ms/', 0775, true);
umask($oldMask);
}
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpThemeDir . "/";
$zip->extractTo($tmpModuleShopDirFull);
$zip->close();
if (file_exists($tmpModuleShopDirFull . "meta/manifest.json")){
$themeInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "meta");
$adminThemeShopForm->load($themeInfo);
}
else {
throw new \Exception("Manifest.json file not found");
}
$this->files->recursiveRemoveDir($tmpModuleShopDirFull);
}
else {
throw new \Exception("zip not found");
}
if ($adminThemeShopForm->validate()) {
$theme = $this->moduleShopService->create($adminThemeShopForm);
if ($theme) {
Flash::setMessage("success", "Тема админ-панели добавлена.");
$this->redirect("/admin/module_shop/view/" . $theme->id);
}
}
Flash::setMessage("error", "Ошибка добавления темы админ-панели: <br>" . $adminThemeShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/admin_theme/create");
}
}

View File

@ -2,8 +2,7 @@
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\models\forms\CreateKernelShopForm;
use app\modules\module_shop\services\ModuleShopService;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
@ -11,7 +10,6 @@ use kernel\FileUpload;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\services\ModuleService;
use ZipArchive;
class KernelShopController extends AdminController
@ -37,18 +35,18 @@ class KernelShopController extends AdminController
*/
#[NoReturn] public function actionAdd(): void
{
$moduleShopForm = new CreateModuleShopForm();
$moduleShopForm->load($_REQUEST);
$kernelShopForm = new CreateKernelShopForm();
$kernelShopForm->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());
$kernelShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpKernelDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $moduleShopForm->getItem('path_to_archive'));
$res = $zip->open(ROOT_DIR . $kernelShopForm->getItem('path_to_archive'));
if ($res === TRUE) {
if (!is_dir(RESOURCES_DIR . '/tmp/ms/')) {
mkdir(RESOURCES_DIR . '/tmp/ms/');
@ -59,7 +57,7 @@ class KernelShopController extends AdminController
if (file_exists($tmpModuleShopDirFull . "kernel/manifest.json")){
$kernelInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "kernel");
$moduleShopForm->load($kernelInfo);
$kernelShopForm->load($kernelInfo);
}
else {
throw new \Exception("Manifest.json file not found");
@ -71,14 +69,14 @@ class KernelShopController extends AdminController
throw new \Exception("zip not found");
}
if ($moduleShopForm->validate()) {
$kernel = $this->moduleShopService->create($moduleShopForm);
if ($kernelShopForm->validate()) {
$kernel = $this->moduleShopService->create($kernelShopForm);
if ($kernel) {
Flash::setMessage("success", "Ядро добавлено.");
$this->redirect("/admin/module_shop/view/" . $kernel->id);
}
}
Flash::setMessage("error", "Ошибка добавления ядра: <br>" . $moduleShopForm->getErrorsStr());
Flash::setMessage("error", "Ошибка добавления ядра: <br>" . $kernelShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/kernel/create");
}
}

View File

@ -7,12 +7,10 @@ use app\modules\module_shop\models\ModuleShop;
use app\modules\module_shop\services\ModuleShopService;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
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

View File

@ -0,0 +1,22 @@
<?php
namespace app\modules\module_shop\models\forms;
use kernel\FormModel;
class CreateAdminThemeShopForm extends FormModel
{
public function rules(): array
{
return [
'name' => 'required|min-str-len:3',
'version' => 'required',
'description' => '',
'author' => 'required',
'status' => 'required',
'slug' => 'required',
'type' => 'required',
'path_to_archive' => 'required',
];
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\modules\module_shop\models\forms;
use kernel\FormModel;
class CreateKernelShopForm extends FormModel
{
public function rules(): array
{
return [
'name' => 'required',
'version' => 'required',
'author' => 'required',
'status' => 'required',
'slug' => 'required',
'type' => 'required',
'description' => '',
'path_to_archive' => 'required',
];
}
}

View File

@ -8,26 +8,15 @@ 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' => '',
'version' => '',
'description' => '',
'author' => '',
'status' => '',
'slug' => '',
'type' => '',
'path_to_archive' => '',
'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' => '',
];
}

View File

@ -23,6 +23,10 @@ App::$collector->group(["prefix" => "admin"], 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" => "admin_theme"], function (CgRouteCollector $router) {
App::$collector->get('/create', [\app\modules\module_shop\controllers\AdminThemeShopController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\module_shop\controllers\AdminThemeShopController::class, 'actionAdd']);
});
});
});

View File

@ -0,0 +1,51 @@
<?php
/**
* @var ModuleShop $model
*/
use app\modules\module_shop\models\ModuleShop;
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/admin/module_shop/admin_theme", 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();

View File

@ -24,8 +24,9 @@ $table->columns([
}
]);
$table->beforePrint(function () {
$btn = PrimaryBtn::create("Добавить модуль", "/admin/module_shop/module/create", width: '200px')->fetch();
$btn .= PrimaryBtn::create("Добавить ядро", "/admin/module_shop/kernel/create", '200px')->fetch();
$btn = PrimaryBtn::create("Добавить модуль", "/admin/module_shop/module/create", width: '250px')->fetch();
$btn .= PrimaryBtn::create("Добавить ядро", "/admin/module_shop/kernel/create", '250px')->fetch();
$btn .= PrimaryBtn::create("Добавить тему админ-панели", "/admin/module_shop/admin_theme/create", '250px')->fetch();
return $btn;
});
$table->addAction(ViewActionColumn::class);

View File

@ -12,10 +12,10 @@ $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' => 'Путь к файлу модуля',
'placeholder' => 'Путь к ядра',
'value' => $model->path_to_archive ?? ''
])
->setLabel("Путь к файлу модуля")
->setLabel("Путь к файлу ядра")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [

View File

@ -8,7 +8,7 @@ class PrimaryBtn
public function __construct(string $title, string $url, $width)
{
$this->btn = "<a class='btn btn-primary' href='$url' style='margin: 3px; width: '$width >$title</a>";
$this->btn = "<a class='btn btn-primary' href='$url' style='margin: 3px; width: $width;' >$title</a>";
}
public function fetch(): string