module shop, flash message
This commit is contained in:
parent
209b1e3f29
commit
9af44ca95f
@ -5,11 +5,16 @@ namespace app\modules\module_shop\controllers;
|
|||||||
use app\modules\module_shop\models\forms\CreateModuleShopForm;
|
use app\modules\module_shop\models\forms\CreateModuleShopForm;
|
||||||
use app\modules\module_shop\models\ModuleShop;
|
use app\modules\module_shop\models\ModuleShop;
|
||||||
use app\modules\module_shop\services\ModuleShopService;
|
use app\modules\module_shop\services\ModuleShopService;
|
||||||
|
use Cassandra\Date;
|
||||||
use JetBrains\PhpStorm\NoReturn;
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
use kernel\AdminController;
|
use kernel\AdminController;
|
||||||
use kernel\app_modules\tag\services\TagService;
|
use kernel\app_modules\tag\services\TagService;
|
||||||
|
use kernel\FileUpload;
|
||||||
|
use kernel\Flash;
|
||||||
|
use kernel\helpers\Debug;
|
||||||
use kernel\services\ModuleService;
|
use kernel\services\ModuleService;
|
||||||
use mysql_xdevapi\Exception;
|
use mysql_xdevapi\Exception;
|
||||||
|
use ZipArchive;
|
||||||
|
|
||||||
class ModuleShopController extends AdminController
|
class ModuleShopController extends AdminController
|
||||||
{
|
{
|
||||||
@ -27,16 +32,48 @@ class ModuleShopController extends AdminController
|
|||||||
$this->cgView->render("form.php");
|
$this->cgView->render("form.php");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
#[NoReturn] public function actionAdd(): void
|
#[NoReturn] public function actionAdd(): void
|
||||||
{
|
{
|
||||||
$moduleShopForm = new CreateModuleShopForm();
|
$moduleShopForm = new CreateModuleShopForm();
|
||||||
$moduleShopForm->load($_REQUEST);
|
$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()) {
|
if ($moduleShopForm->validate()) {
|
||||||
$module = $this->moduleShopService->create($moduleShopForm);
|
$module = $this->moduleShopService->create($moduleShopForm);
|
||||||
if ($module) {
|
if ($module) {
|
||||||
|
Flash::setMessage("success", "Модуль " . $moduleShopForm->getItem("name") . " добавлен.");
|
||||||
$this->redirect("/admin/module_shop/" . $module->id);
|
$this->redirect("/admin/module_shop/" . $module->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Flash::setMessage("error", "Ошибка добавления модуля: <br>" . $moduleShopForm->getErrorsStr());
|
||||||
$this->redirect("/admin/module_shop/create");
|
$this->redirect("/admin/module_shop/create");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,8 +127,10 @@ class ModuleShopController extends AdminController
|
|||||||
if (!$module) {
|
if (!$module) {
|
||||||
throw new Exception("The module not found");
|
throw new Exception("The module not found");
|
||||||
}
|
}
|
||||||
|
$name = $module->name;
|
||||||
|
|
||||||
$module->delete();
|
$module->delete();
|
||||||
|
Flash::setMessage("success", "Модуль " . $name . " удален.");
|
||||||
$this->redirect("/admin/module_shop");
|
$this->redirect("/admin/module_shop");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,11 +9,12 @@ class CreateModuleShopForm extends FormModel
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'required|min-str-len:5',
|
'name' => 'required|min-str-len:3',
|
||||||
'version' => 'required',
|
'version' => 'required',
|
||||||
'description' => 'required|min-str-len:10',
|
'description' => 'required|min-str-len:10',
|
||||||
'author' => 'required',
|
'author' => 'required',
|
||||||
'status' => 'required',
|
'status' => 'required',
|
||||||
|
'slug' => 'required',
|
||||||
'path_to_archive' => 'required',
|
'path_to_archive' => 'required',
|
||||||
'dependence' => '',
|
'dependence' => '',
|
||||||
];
|
];
|
||||||
|
@ -18,5 +18,5 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
|
|||||||
});
|
});
|
||||||
|
|
||||||
App::$collector->group(["prefix" => "api"], 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]);
|
||||||
});
|
});
|
@ -6,50 +6,11 @@
|
|||||||
use app\modules\module_shop\models\ModuleShop;
|
use app\modules\module_shop\models\ModuleShop;
|
||||||
|
|
||||||
$form = new \itguild\forms\ActiveForm();
|
$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: [
|
$form->field(class: \itguild\forms\inputs\File::class, name: "path_to_archive", 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: [
|
|
||||||
'class' => "form-control",
|
'class' => "form-control",
|
||||||
'placeholder' => 'Путь к файлу модуля',
|
'placeholder' => 'Путь к файлу модуля',
|
||||||
'value' => $model->path_to_archive ?? ''
|
'value' => $model->path_to_archive ?? ''
|
||||||
|
@ -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\ModuleShopViewActionColumn::class);
|
||||||
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopEditActionColumn::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\ModuleShopDeleteActionColumn::class);
|
||||||
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopPackActionColumn::class);
|
|
||||||
$table->create();
|
$table->create();
|
||||||
$table->render();
|
$table->render();
|
@ -5,4 +5,9 @@ namespace app\modules\tag\controllers;
|
|||||||
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public function actionByEntity()
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -7,5 +7,6 @@
|
|||||||
"app_module_path": "{APP}/modules/{slug}",
|
"app_module_path": "{APP}/modules/{slug}",
|
||||||
"module_class": "app\\modules\\tag\\TagModule",
|
"module_class": "app\\modules\\tag\\TagModule",
|
||||||
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
||||||
"routs": "routs/tag.php"
|
"routs": "routs/tag.php",
|
||||||
|
"dependence": "menu"
|
||||||
}
|
}
|
||||||
|
29
kernel/Flash.php
Normal file
29
kernel/Flash.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -42,7 +42,7 @@ class FormModel
|
|||||||
|
|
||||||
public function getItem(string $name)
|
public function getItem(string $name)
|
||||||
{
|
{
|
||||||
if (isset($this->data[$name])){
|
if (isset($this->data[$name])) {
|
||||||
return $this->data[$name];
|
return $this->data[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,4 +64,19 @@ class FormModel
|
|||||||
return $this->validator->getProcessedErrors();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -70,13 +70,18 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
<?php if (\Josantonius\Session\Facades\Session::get("error", false)): ?>
|
<?php if (\kernel\Flash::hasMessage("error")): ?>
|
||||||
<div class="alert alert-danger alert-dismissible" id="mainAlert">
|
<div class="alert alert-danger alert-dismissible mainAlert">
|
||||||
<?= \Josantonius\Session\Facades\Session::get("error"); ?>
|
<?= \kernel\Flash::getMessage("error"); ?>
|
||||||
<?php \Josantonius\Session\Facades\Session::remove("error"); ?>
|
<button type="button" class="btn-close closeAlertBtn"></button>
|
||||||
<button type="button" class="btn-close" id="closeAlertBtn"></button>
|
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?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 ?>
|
<?= $content ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -16,8 +16,20 @@
|
|||||||
$('#sidebar').toggleClass('active');
|
$('#sidebar').toggleClass('active');
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#closeAlertBtn").click(function () {
|
// $(".closeAlertBtn").click(function () {
|
||||||
$("#mainAlert").alert("close");
|
// console.log($(this).parentNode)
|
||||||
});
|
// $(this).parentElement.alert("close");
|
||||||
|
// });
|
||||||
|
|
||||||
})(jQuery);
|
})(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;")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user