module shop, flash message

This commit is contained in:
Kavalar 2024-10-16 17:54:33 +03:00
parent 209b1e3f29
commit 9af44ca95f
12 changed files with 137 additions and 54 deletions

View File

@ -5,11 +5,16 @@ 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 Cassandra\Date;
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\services\ModuleService;
use mysql_xdevapi\Exception;
use ZipArchive;
class ModuleShopController extends AdminController
{
@ -27,16 +32,48 @@ class ModuleShopController extends AdminController
$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', 'itguild']);
$file->upload();
$moduleShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpThemeDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $moduleShopForm->getItem('path_to_archive'));
if ($res === TRUE) {
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpThemeDir . "/";
$zip->extractTo($tmpModuleShopDirFull);
$zip->close();
if (file_exists($tmpModuleShopDirFull . "/app/manifest.json")){
$moduleInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "app");
$moduleShopForm->load($moduleInfo);
}
else {
throw new \Exception("Manifest.json file not found");
}
}
else {
throw new \Exception("zip not found");
}
if ($moduleShopForm->validate()) {
$module = $this->moduleShopService->create($moduleShopForm);
if ($module) {
Flash::setMessage("success", "Модуль " . $moduleShopForm->getItem("name") . " добавлен.");
$this->redirect("/admin/module_shop/" . $module->id);
}
}
Flash::setMessage("error", "Ошибка добавления модуля: <br>" . $moduleShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/create");
}
@ -90,8 +127,10 @@ class ModuleShopController extends AdminController
if (!$module) {
throw new Exception("The module not found");
}
$name = $module->name;
$module->delete();
Flash::setMessage("success", "Модуль " . $name . " удален.");
$this->redirect("/admin/module_shop");
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\ModuleShop;
use kernel\RestController;
class ModuleShopRestController extends RestController
{
public function __construct()
{
$this->model = new ModuleShop();
}
}

View File

@ -9,11 +9,12 @@ class CreateModuleShopForm extends FormModel
public function rules(): array
{
return [
'name' => 'required|min-str-len:5',
'name' => 'required|min-str-len:3',
'version' => 'required',
'description' => 'required|min-str-len:10',
'author' => 'required',
'status' => 'required',
'slug' => 'required',
'path_to_archive' => 'required',
'dependence' => '',
];

View File

@ -18,5 +18,5 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
});
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
$router->rest("module_shop", [\app\modules\module_shop\controllers\ModuleShopController::class]);
$router->rest("module_shop", [\app\modules\module_shop\controllers\ModuleShopRestController::class]);
});

View File

@ -6,50 +6,11 @@
use app\modules\module_shop\models\ModuleShop;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/module_shop/edit/" . $model->id : "/admin/module_shop");
$form->beginForm(isset($model) ? "/admin/module_shop/edit/" . $model->id : "/admin/module_shop", enctype: 'multipart/form-data');
$form->field(\itguild\forms\inputs\TextInput::class, 'name', [
'class' => "form-control",
'placeholder' => 'Название',
'value' => $model->name ?? ''
])
->setLabel("Название")
->render();
$form->field(\itguild\forms\inputs\TextInput::class, 'author', [
'class' => "form-control",
'placeholder' => 'Автор',
'value' => $model->author ?? ''
])
->setLabel("Автор")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "version", params: [
'class' => "form-control",
'placeholder' => 'Версия',
'value' => $model->version ?? ''
])
->setLabel("Версия")
->render();
$form->field(class: \itguild\forms\inputs\TextArea::class, name: "description", params: [
'class' => "form-control",
'placeholder' => 'Описание',
'rows' => '10',
'value' => $model->description ?? ''
])
->setLabel("Описание")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "dependence", params: [
'class' => "form-control",
'placeholder' => 'Зависимости',
'value' => $model->dependence ?? ''
])
->setLabel("Зависимости")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "path_to_archive", params: [
$form->field(class: \itguild\forms\inputs\File::class, name: "path_to_archive", params: [
'class' => "form-control",
'placeholder' => 'Путь к файлу модуля',
'value' => $model->path_to_archive ?? ''

View File

@ -32,6 +32,5 @@ $table->beforePrint(function () {
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopViewActionColumn::class);
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopEditActionColumn::class);
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopDeleteActionColumn::class);
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopPackActionColumn::class);
$table->create();
$table->render();

View File

@ -5,4 +5,9 @@ namespace app\modules\tag\controllers;
class TagController extends \kernel\app_modules\tag\controllers\TagController
{
public function actionByEntity()
{
// TODO
}
}

View File

@ -7,5 +7,6 @@
"app_module_path": "{APP}/modules/{slug}",
"module_class": "app\\modules\\tag\\TagModule",
"module_class_file": "{APP}/modules/tag/TagModule.php",
"routs": "routs/tag.php"
"routs": "routs/tag.php",
"dependence": "menu"
}

29
kernel/Flash.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace kernel;
use Josantonius\Session\Facades\Session;
class Flash
{
public static function setMessage(string $type, string $msg): void
{
Session::start();
Session::set($type, $msg);
}
public static function getMessage(string $type): string
{
$msg = Session::get($type, false);
Session::remove($type);
return $msg;
}
public static function hasMessage(string $type): bool
{
return Session::has($type);
}
}

View File

@ -42,7 +42,7 @@ class FormModel
public function getItem(string $name)
{
if (isset($this->data[$name])){
if (isset($this->data[$name])) {
return $this->data[$name];
}
@ -64,4 +64,19 @@ class FormModel
return $this->validator->getProcessedErrors();
}
public function getErrorsStr(): string
{
$errorsArr = $this->getErrors();
$str = '';
if ($errorsArr['errors']) {
foreach ($errorsArr['errors'] as $key => $errorArr) {
foreach ($errorsArr['errors'][$key] as $error){
$str .= "$error <br>";
}
}
}
return $str;
}
}

View File

@ -70,13 +70,18 @@
</div>
</div>
</nav>
<?php if (\Josantonius\Session\Facades\Session::get("error", false)): ?>
<div class="alert alert-danger alert-dismissible" id="mainAlert">
<?= \Josantonius\Session\Facades\Session::get("error"); ?>
<?php \Josantonius\Session\Facades\Session::remove("error"); ?>
<button type="button" class="btn-close" id="closeAlertBtn"></button>
<?php if (\kernel\Flash::hasMessage("error")): ?>
<div class="alert alert-danger alert-dismissible mainAlert">
<?= \kernel\Flash::getMessage("error"); ?>
<button type="button" class="btn-close closeAlertBtn"></button>
</div>
<?php endif; ?>
<?php if (\kernel\Flash::hasMessage("success")): ?>
<div class="alert alert-success alert-dismissible">
<?= \kernel\Flash::getMessage("success"); ?>
<button type="button" class="btn-close closeAlertBtn" ></button>
</div>
<?php endif; ?>
<?= $content ?>
</div>
</div>

View File

@ -16,8 +16,20 @@
$('#sidebar').toggleClass('active');
});
$("#closeAlertBtn").click(function () {
$("#mainAlert").alert("close");
});
// $(".closeAlertBtn").click(function () {
// console.log($(this).parentNode)
// $(this).parentElement.alert("close");
// });
})(jQuery);
window.addEventListener("load", function () {
let closeBtn = document.querySelectorAll(".closeAlertBtn");
closeBtn.forEach((item) => {
item.addEventListener("click", closeAlert);
});
});
function closeAlert() {
this.parentNode.setAttribute("style", "display: none;")
}