session errors

This commit is contained in:
2024-10-11 17:02:35 +03:00
parent 5b9386f970
commit 7cf3708e4d
9 changed files with 146 additions and 8174 deletions

View File

@ -3,6 +3,7 @@
* @var $content
* @var string $resources
*/
\Josantonius\Session\Facades\Session::start();
?>
<!doctype html>
<html lang="en">
@ -14,6 +15,7 @@
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700,800,900" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="<?= $resources ?>/css/bootstrap.min.css">
<link rel="stylesheet" href="<?= $resources ?>/css/style.css">
</head>
<body>
@ -68,7 +70,13 @@
</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>
</div>
<?php endif; ?>
<?= $content ?>
</div>
</div>

View File

@ -3,6 +3,7 @@
namespace kernel\controllers;
use DirectoryIterator;
use Josantonius\Session\Facades\Session;
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\models\Option;
@ -59,7 +60,12 @@ class ModuleController extends AdminController
public function actionActivate(): void
{
$request = new Request();
$this->moduleService->setActiveModule($request->get("slug"));
$active_res = $this->moduleService->setActiveModule($request->get("slug"));
if (!$active_res){
Session::start();
Session::set("error", implode(";", $this->moduleService->getErrors()));
$this->redirect("/admin", 302);
}
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
$this->cgView->render("view.php", ['data' => $mod_info]);
@ -68,7 +74,7 @@ class ModuleController extends AdminController
public function actionDeactivate(): void
{
$request = new Request();
$this->moduleService->unsetActiveModule($request->get("slug"));
$this->moduleService->deactivateModule($request->get("slug"));
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
$this->cgView->render("view.php", ['data' => $mod_info]);

View File

@ -11,6 +11,7 @@ use ZipArchive;
class ModuleService
{
protected array $errors = [];
public function getModuleInfo(string $module): false|array|string
{
@ -26,6 +27,16 @@ class ModuleService
return $info;
}
public function getErrors(): array
{
return $this->errors;
}
public function addError($msg): void
{
$this->errors[] = $msg;
}
public function getModuleInfoBySlug(string $slug): false|array|string
{
return $this->getModuleInfo($this->getModuleDir($slug));
@ -74,12 +85,16 @@ class ModuleService
$active_modules_info->save();
}
public function setActiveModule(string $module): void
/**
* @param string $module
* @return bool
*/
public function setActiveModule(string $module): bool
{
$active_modules_info = Option::where("key", "active_modules")->first();
$active_modules = json_decode($active_modules_info->value);
if (in_array($module, $active_modules->modules)) {
throw new \Exception("$module module is already activated");
return true;
}
$module_info = $this->getModuleInfoBySlug($module);
@ -87,7 +102,8 @@ class ModuleService
$dependence_array = explode(',', $module_info['dependence']);
foreach ($dependence_array as $depend) {
if (!in_array($depend, $active_modules->modules)) {
throw new \Exception("first activate the $depend module");
$this->addError("first activate the $depend module");
return false;
}
}
}
@ -96,17 +112,20 @@ class ModuleService
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
return true;
}
/**
* @throws \Exception
* @param string $module
* @return bool
*/
public function unsetActiveModule(string $module): void
public function deactivateModule(string $module): bool
{
$active_modules_info = Option::where("key", "active_modules")->first();
$active_modules = json_decode($active_modules_info->value);
if (!in_array($module, $active_modules->modules)) {
throw new \Exception("$module module is already activated");
return true;
}
$dependence_array = $this->getDependencies();
@ -121,7 +140,8 @@ class ModuleService
}
if ($str_for_exception !== '') {
throw new \Exception("You can not deactivate $module module. First deactivate modules: $str_for_exception");
$this->addError("You can not deactivate $module module. First deactivate modules: $str_for_exception");
return false;
}
unset($active_modules->modules[array_search($module, $active_modules->modules)]);
@ -130,6 +150,8 @@ class ModuleService
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
return true;
}
public function getModuleDir(string $slug)

View File

@ -25,6 +25,7 @@ $info_to_table['data'] = $modules_info;
$table = new \Itguild\Tables\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)){
@ -41,6 +42,7 @@ $table->addAction(function ($row, $url) use ($moduleService){
return $btn;
});
$table->addAction(function ($row, $url) use ($moduleService){
$slug = $row['slug'];
return "<a class='btn btn-primary' href='$url/view/?slug=$slug' style='margin: 3px; width: 150px;' >Просмотр</a>";