update full
This commit is contained in:
279
kernel/EntityRelation.php
Normal file
279
kernel/EntityRelation.php
Normal file
@ -0,0 +1,279 @@
|
||||
<?php
|
||||
|
||||
namespace kernel;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\models\Option;
|
||||
use kernel\modules\option\service\OptionService;
|
||||
use kernel\services\ModuleService;
|
||||
|
||||
class EntityRelation
|
||||
{
|
||||
protected ModuleService $moduleService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->moduleService = new ModuleService();
|
||||
}
|
||||
|
||||
|
||||
public static function getEntityList(): array
|
||||
{
|
||||
$list = [];
|
||||
$moduleService = new ModuleService();
|
||||
$activeModules = $moduleService->getActiveModules();
|
||||
foreach ($activeModules as $module) {
|
||||
if (isset($module['type']) and $module['type'] === "entity") {
|
||||
$list[$module['slug']] = $module['slug'];
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getAdditionalPropertyList(): array
|
||||
{
|
||||
$list = [];
|
||||
$activeModules = $this->moduleService->getActiveModules();
|
||||
foreach ($activeModules as $module) {
|
||||
if (isset($module['type']) and $module['type'] === "additional_property") {
|
||||
$list[] = $module['slug'];
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function getEntitiesRelations(): array|bool
|
||||
{
|
||||
$entity_relations = OptionService::getItem("entity_relations");
|
||||
if ($entity_relations) {
|
||||
return json_decode($entity_relations, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function removePropertyFromEntityRelations(string $entity, string $property): bool
|
||||
{
|
||||
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
||||
if ($entity_relations_info) {
|
||||
$entity_relations = json_decode($entity_relations_info->value, true);
|
||||
if ($entity_relations[$entity]) {
|
||||
$propertyKey = array_search($property, $entity_relations[$entity]);
|
||||
if ($entity_relations[$entity][$propertyKey] === $property) {
|
||||
unset($entity_relations[$entity][$propertyKey]);
|
||||
$entity_relations[$entity] = array_values($entity_relations[$entity]);
|
||||
if (empty($entity_relations[$entity])) {
|
||||
unset($entity_relations[$entity]);
|
||||
}
|
||||
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
||||
$entity_relations_info->save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function removePropertyRelation(string $property): bool
|
||||
{
|
||||
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
||||
if ($entity_relations_info) {
|
||||
$entity_relations = json_decode($entity_relations_info->value, true);
|
||||
foreach ($entity_relations as $entity => $entity_relation) {
|
||||
if (in_array($property, $entity_relation)) {
|
||||
$index = array_search($property, $entity_relation);
|
||||
unset($entity_relations[$entity][$index]);
|
||||
}
|
||||
}
|
||||
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
||||
$entity_relations_info->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function removeEntityRelation(string $entity): bool
|
||||
{
|
||||
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
||||
if ($entity_relations_info) {
|
||||
$entity_relations = json_decode($entity_relations_info->value, true);
|
||||
if (isset($entity_relations[$entity])) {
|
||||
unset($entity_relations[$entity]);
|
||||
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
||||
$entity_relations_info->save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getEntityRelationsBySlug(string $slug)
|
||||
{
|
||||
$entityRelations = $this->getEntitiesRelations();
|
||||
if ($entityRelations) {
|
||||
if (isset($entityRelations[$slug])) {
|
||||
return $entityRelations[$slug];
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function addEntityRelation(string $entity, string $property): bool
|
||||
{
|
||||
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
||||
if ($entity_relations_info) {
|
||||
$entity_relations = json_decode($entity_relations_info->value, true);
|
||||
if (isset($entity_relations[$entity])) {
|
||||
$entity_relations[$entity][] = $property;
|
||||
} else {
|
||||
$entity_relations[$entity][] = $property;
|
||||
}
|
||||
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
||||
$entity_relations_info->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getAdditionalPropertyClassBySlug(string $slug)
|
||||
{
|
||||
$module = $this->moduleService->getModuleInfoBySlug($slug);
|
||||
if (isset($module['module_class'])) {
|
||||
return new $module['module_class']();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function renderFormInputsBySlug(string $entity, string $slug, Model $model): void
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
||||
if ($moduleClass and method_exists($moduleClass, "formInputs")) {
|
||||
$moduleClass->formInputs($entity, $model);
|
||||
}
|
||||
}
|
||||
|
||||
public function renderEntityAdditionalPropertyFormBySlug(string $entity, Model $model = null): void
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($entity);
|
||||
if ($relations) {
|
||||
foreach ($relations as $relation) {
|
||||
$this->renderFormInputsBySlug($entity, $relation, $model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEntityRelationBySlug(string $slug, string $entity, Model $model, Request $request): void
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
||||
if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
|
||||
$moduleClass->saveInputs($entity, $model, $request);
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEntityRelation(string $entity, Model $model, Request $request): void
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($entity);
|
||||
if ($relations) {
|
||||
foreach ($relations as $relation) {
|
||||
$this->saveEntityRelationBySlug($relation, $entity, $model, $request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getEntityAdditionalProperty(string $entity, Model $model): array
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($entity);
|
||||
if ($relations) {
|
||||
$relationsArr = [];
|
||||
foreach ($relations as $relation) {
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($relation);
|
||||
if ($moduleClass and method_exists($moduleClass, "getItems")) {
|
||||
$relationsArr[$relation] = $moduleClass->getItems($entity, $model);
|
||||
}
|
||||
}
|
||||
|
||||
return $relationsArr;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getAdditionalPropertyByEntityId(string $entity, string $entity_id, string $additionalPropertySlug): string
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($additionalPropertySlug);
|
||||
if ($moduleClass and method_exists($moduleClass, "getItem")) {
|
||||
return $moduleClass->getItem($entity, $entity_id);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public function deleteEntityRelationBySlug(string $slug, string $entity, Model $model): void
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
||||
if ($moduleClass and method_exists($moduleClass, "deleteItems")) {
|
||||
$moduleClass->deleteItems($entity, $model);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteEntityRelation(string $entity, Model $model): void
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($entity);
|
||||
if ($relations) {
|
||||
foreach ($relations as $relation) {
|
||||
$this->deleteEntityRelationBySlug($relation, $entity, $model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getEntityByProperty(string $data): array
|
||||
{
|
||||
$entityRelations = self::getEntitiesRelations();
|
||||
$entities = [];
|
||||
foreach ($entityRelations as $entity => $property) {
|
||||
if (in_array($data, $property)) {
|
||||
$entities[] = $entity;
|
||||
}
|
||||
}
|
||||
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public static function configurationEntitiesByProperty(array|null $entities, string $property): void
|
||||
{
|
||||
$entityRelations = self::getEntitiesRelations();
|
||||
if (isset($entities)) {
|
||||
foreach ($entities as $entity) {
|
||||
if (!isset($entityRelations[$entity])) {
|
||||
EntityRelation::addEntityRelation($entity, $property);
|
||||
}
|
||||
}
|
||||
foreach ($entityRelations as $entity => $additionalProperty) {
|
||||
if (in_array($entity, $entities)) {
|
||||
if (!in_array($property, $additionalProperty)) {
|
||||
EntityRelation::addEntityRelation($entity, $property);
|
||||
}
|
||||
} else {
|
||||
if (in_array($property, $additionalProperty)) {
|
||||
EntityRelation::removePropertyFromEntityRelations($entity, $property);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($entityRelations as $entity => $additionalProperty) {
|
||||
EntityRelation::removePropertyFromEntityRelations($entity, $property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -59,6 +59,16 @@ class FormModel
|
||||
return false;
|
||||
}
|
||||
|
||||
public function validateForUpdate(): bool
|
||||
{
|
||||
$res = $this->validator->validate($this->data, $this->rulesForUpdate());
|
||||
if (!$res) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->validator->getProcessedErrors();
|
||||
|
@ -69,11 +69,18 @@ class AdminConsoleController extends ConsoleController
|
||||
|
||||
$this->optionService->createFromParams(
|
||||
key: "active_modules",
|
||||
value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\"]}",
|
||||
value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\", \"post\", \"option\"]}",
|
||||
label: "Активные модули"
|
||||
);
|
||||
$this->out->r("create option active_modules", "green");
|
||||
|
||||
$this->optionService->createFromParams(
|
||||
key: "entity_relations",
|
||||
value: "{}",
|
||||
label: "Свойства сущностей"
|
||||
);
|
||||
$this->out->r("create option entity_relations", "green");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Модули",
|
||||
"url" => "/admin",
|
||||
@ -101,6 +108,13 @@ class AdminConsoleController extends ConsoleController
|
||||
]);
|
||||
$this->out->r("create item menu user", "green");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Посты",
|
||||
"url" => "/admin/post",
|
||||
"slug" => "post",
|
||||
]);
|
||||
$this->out->r("create item menu post", "green");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Настройки",
|
||||
"url" => "#",
|
||||
@ -116,6 +130,21 @@ class AdminConsoleController extends ConsoleController
|
||||
]);
|
||||
$this->out->r("create item menu admin-themes", "green");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Меню",
|
||||
"url" => "/admin/settings/menu",
|
||||
"slug" => "menu",
|
||||
"parent_slug" => "settings"
|
||||
]);
|
||||
$this->out->r("create item menu menu", "green");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Опции",
|
||||
"url" => "/admin/option",
|
||||
"slug" => "option"
|
||||
]);
|
||||
$this->out->r("create item menu option", "green");
|
||||
|
||||
$user = new CreateUserForm();
|
||||
$user->load([
|
||||
'username' => 'admin',
|
||||
|
@ -6,6 +6,7 @@ use DirectoryIterator;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use Josantonius\Session\Facades\Session;
|
||||
use kernel\AdminController;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\models\Option;
|
||||
use kernel\modules\module_shop_client\services\ModuleShopClientService;
|
||||
|
30
kernel/helpers/Html.php
Normal file
30
kernel/helpers/Html.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\helpers;
|
||||
|
||||
class Html
|
||||
{
|
||||
|
||||
public static function img(string $src, array $params = [])
|
||||
{
|
||||
$paramsStr = self::createParams($params);
|
||||
return "<img src='$src' $paramsStr>";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function createParams(array $data = []): string
|
||||
{
|
||||
$paramsString = "";
|
||||
foreach($data as $key => $param){
|
||||
if(is_string($param)){
|
||||
$paramsString .= $key . "='" . $param . "'";
|
||||
}
|
||||
}
|
||||
|
||||
return $paramsString;
|
||||
}
|
||||
|
||||
}
|
@ -23,6 +23,9 @@ $table->columns([
|
||||
if ($data == 0) return null;
|
||||
return Menu::find($data)->label;
|
||||
}),
|
||||
'icon_file' => function ($data) {
|
||||
return $data ? "<img src='$data' width='150px'>" : "";
|
||||
}
|
||||
]);
|
||||
$table->beforePrint(function () {
|
||||
return IconBtnCreateWidget::create(['url' => '/admin/settings/menu/create'])->run();
|
||||
|
@ -26,7 +26,10 @@ $table->rows([
|
||||
'parent_id' => (function ($data) {
|
||||
if ($data == 0) return null;
|
||||
return Menu::find($data)->label;
|
||||
})
|
||||
}),
|
||||
'icon_file' => function ($data) {
|
||||
return $data ? "<img src='$data' width='300px'>" : "";
|
||||
}
|
||||
]);
|
||||
$table->create();
|
||||
$table->render();
|
@ -35,7 +35,7 @@ class OptionService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createFromParams(string $key, string $value, string $label): false|Option
|
||||
public static function createFromParams(string $key, string $value, string $label): false|Option
|
||||
{
|
||||
$model = new Option();
|
||||
$model->key = $key;
|
||||
@ -63,6 +63,17 @@ class OptionService
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function removeOptionByKey(string $key): bool
|
||||
{
|
||||
$option = Option::where("key", $key)->first();
|
||||
if (!$option){
|
||||
return false;
|
||||
}
|
||||
|
||||
$option->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
// public function createOptionArr(): array
|
||||
// {
|
||||
// foreach (Option::all()->toArray() as $option) {
|
||||
|
@ -6,9 +6,12 @@ namespace kernel\modules\post\controllers;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\post\models\forms\CreatePostForm;
|
||||
use kernel\modules\post\models\Post;
|
||||
use kernel\modules\post\service\PostService;
|
||||
use kernel\Request;
|
||||
|
||||
class PostController extends AdminController
|
||||
{
|
||||
@ -31,6 +34,10 @@ class PostController extends AdminController
|
||||
$postForm->load($_REQUEST);
|
||||
if ($postForm->validate()) {
|
||||
$post = $this->postService->create($postForm);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request());
|
||||
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/view/" . $post->id);
|
||||
}
|
||||
@ -86,6 +93,10 @@ class PostController extends AdminController
|
||||
$postForm->load($_REQUEST);
|
||||
if ($postForm->validate()) {
|
||||
$post = $this->postService->update($postForm, $post);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request());
|
||||
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/view/" . $post->id);
|
||||
}
|
||||
@ -102,6 +113,10 @@ class PostController extends AdminController
|
||||
if (!$post){
|
||||
throw new Exception(message: "The post not found");
|
||||
}
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->deleteEntityRelation(entity: "post", model: $post);
|
||||
|
||||
$post->delete();
|
||||
$this->redirect("/admin/post/");
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
"version": "0.2",
|
||||
"author": "ITGuild",
|
||||
"slug": "post",
|
||||
"type": "entity",
|
||||
"description": "Post module",
|
||||
"module_class": "kernel\\modules\\post\\PostModule",
|
||||
"module_class_file": "{KERNEL_MODULES}/post/PostModule.php",
|
||||
|
@ -6,7 +6,7 @@
|
||||
use kernel\modules\post\models\Post;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post");
|
||||
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post", 'multipart/form-data');
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'title', [
|
||||
'class' => "form-control",
|
||||
@ -33,6 +33,12 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params
|
||||
->setOptions(\kernel\modules\user\service\UserService::createUsernameArr())
|
||||
->render();
|
||||
|
||||
$entityRelations = new \kernel\EntityRelation();
|
||||
if (!isset($model)) {
|
||||
$model = new Post();
|
||||
}
|
||||
$entityRelations->renderEntityAdditionalPropertyFormBySlug("post", $model);
|
||||
|
||||
?>
|
||||
<div class="row">
|
||||
<div class="col-sm-2">
|
||||
|
@ -24,6 +24,16 @@ $table = new ListEloquentTable(new EloquentDataProvider(Post::class, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/post"
|
||||
]));
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityRelationsBySlug("post");
|
||||
|
||||
foreach ($additionals as $additional) {
|
||||
$table->addColumn($additional, $additional, function ($id) use ($entityRelation, $additional) {
|
||||
return $entityRelation->getAdditionalPropertyByEntityId("post", $id, $additional);
|
||||
});
|
||||
}
|
||||
|
||||
$table->columns([
|
||||
'created_at' => function ($data) {
|
||||
if (!$data){
|
||||
|
@ -25,15 +25,24 @@ $table->beforePrint(function () use ($content) {
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityAdditionalProperty("post", $content);
|
||||
|
||||
foreach ($additionals as $key => $additional) {
|
||||
$table->addRow($key, function () use ($additional) {
|
||||
return $additional;
|
||||
}, ['after' => 'user_id']);
|
||||
}
|
||||
|
||||
$table->rows([
|
||||
'created_at' => function ($data) {
|
||||
if (!$data){
|
||||
if (!$data) {
|
||||
return null;
|
||||
}
|
||||
return (new DateTimeImmutable($data))->format("d-m-Y");
|
||||
},
|
||||
'updated_at' => function ($data) {
|
||||
if (!$data){
|
||||
if (!$data) {
|
||||
return null;
|
||||
}
|
||||
return (new DateTimeImmutable($data))->format("d-m-Y");
|
||||
|
@ -7,6 +7,7 @@ use kernel\AdminController;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\secure\models\forms\LoginForm;
|
||||
use kernel\modules\secure\models\forms\RegisterForm;
|
||||
use kernel\modules\user\service\UserService;
|
||||
|
||||
class SecureController extends AdminController
|
||||
@ -16,7 +17,6 @@ class SecureController extends AdminController
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
// $this->cgView->viewPath = KERNEL_DIR . "/views/secure/";
|
||||
$this->cgView->viewPath = KERNEL_MODULES_DIR. "/secure/views/";
|
||||
$this->cgView->layout = "/login.php";
|
||||
$this->userService = new UserService();
|
||||
@ -61,4 +61,31 @@ class SecureController extends AdminController
|
||||
$this->redirect("/", code: 302);
|
||||
}
|
||||
|
||||
public function actionRegister()
|
||||
{
|
||||
$this->cgView->render('register.php');
|
||||
}
|
||||
|
||||
public function actionRegistration(): void
|
||||
{
|
||||
$regForm = new RegisterForm();
|
||||
$regForm->load($_REQUEST);
|
||||
|
||||
if ($this->userService->getByField('username', $regForm->getItem("username"))) {
|
||||
Flash::setMessage("error", "Username already exists.");
|
||||
$this->redirect("/admin/register", code: 302);
|
||||
}
|
||||
|
||||
if ($this->userService->getByField('email', $regForm->getItem("email"))) {
|
||||
Flash::setMessage("error", "Email already exists.");
|
||||
$this->redirect("/admin/register", code: 302);
|
||||
}
|
||||
|
||||
$user = $this->userService->create($regForm);
|
||||
if ($user){
|
||||
setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false);
|
||||
$this->redirect("/admin", code: 302);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
19
kernel/modules/secure/models/forms/RegisterForm.php
Normal file
19
kernel/modules/secure/models/forms/RegisterForm.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\secure\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class RegisterForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|min-str-len:5|max-str-len:50',
|
||||
'email' => 'required|email|max-str-len:50',
|
||||
'password' => 'required|min-str-len:6|max-str-len:50',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -14,6 +14,8 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
|
||||
App::$collector->get('/login', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogin']);
|
||||
App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']);
|
||||
App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']);
|
||||
App::$collector->get('/register', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegister']);
|
||||
App::$collector->post('/registration', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegistration']);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
|
||||
|
@ -24,6 +24,9 @@
|
||||
</div>
|
||||
<div class="col-lg-8">
|
||||
<div class="card-body py-5 px-md-5">
|
||||
<div class="row md-4 text-md-center">
|
||||
<h1>Авторизация</h1>
|
||||
</div>
|
||||
|
||||
<form action="/admin/auth" method="post">
|
||||
<!-- Email input -->
|
||||
@ -48,10 +51,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<div class="col-3">
|
||||
<!-- Simple link -->
|
||||
<a href="#!">Забыл пароль?</a>
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<!-- Simple link -->
|
||||
<a href="/admin/register">Регистрация</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Submit button -->
|
||||
|
71
kernel/modules/secure/views/register.php
Normal file
71
kernel/modules/secure/views/register.php
Normal file
@ -0,0 +1,71 @@
|
||||
<!-- Section: Design Block -->
|
||||
<section class=" text-center text-lg-start">
|
||||
<style>
|
||||
.rounded-t-5 {
|
||||
border-top-left-radius: 0.5rem;
|
||||
border-top-right-radius: 0.5rem;
|
||||
}
|
||||
|
||||
@media (min-width: 992px) {
|
||||
.rounded-tr-lg-0 {
|
||||
border-top-right-radius: 0;
|
||||
}
|
||||
|
||||
.rounded-bl-lg-5 {
|
||||
border-bottom-left-radius: 0.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<div class="card mb-3">
|
||||
<div class="row g-0 d-flex align-items-center">
|
||||
<div class="col-lg-4 d-none d-lg-flex">
|
||||
<img src="https://mdbootstrap.com/img/new/ecommerce/vertical/004.jpg" alt="Trendy Pants and Shoes"
|
||||
class="w-100 rounded-t-5 rounded-tr-lg-0 rounded-bl-lg-5" />
|
||||
</div>
|
||||
<div class="col-lg-8">
|
||||
<div class="card-body py-5 px-md-5">
|
||||
|
||||
<div class="row md-4 text-md-center">
|
||||
<h1>Регистрация</h1>
|
||||
</div>
|
||||
|
||||
<form action="/admin/registration" method="post">
|
||||
<!--Username input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="text" id="form2Example1" class="form-control" name="username" />
|
||||
<label class="form-label" for="form2Example1">Логин</label>
|
||||
</div>
|
||||
|
||||
<!-- Email input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="email" id="form2Example1" class="form-control" name="email" />
|
||||
<label class="form-label" for="form2Example1">Email</label>
|
||||
</div>
|
||||
|
||||
<!-- Password input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="password" id="form2Example2" class="form-control" name="password" />
|
||||
<label class="form-label" for="form2Example2">Пароль</label>
|
||||
</div>
|
||||
|
||||
<div class="row mb-4">
|
||||
<div class="col-4">
|
||||
<!-- Submit button -->
|
||||
<button type="submit" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block mb-4">Регистрация</button>
|
||||
</div>
|
||||
<div class="col-4">
|
||||
<!-- Simple link -->
|
||||
<a href="/admin/login">Войти в существующий аккаунт</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<!-- Section: Design Block -->
|
@ -5,9 +5,11 @@ namespace kernel\modules\user\controllers;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\modules\user\models\forms\CreateUserForm;
|
||||
use kernel\modules\user\models\User;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\Request;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
@ -35,6 +37,11 @@ class UserController extends AdminController
|
||||
$userForm->load($_REQUEST);
|
||||
if ($userForm->validate()){
|
||||
$user = $this->userService->create($userForm);
|
||||
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
|
||||
|
||||
if ($user){
|
||||
$this->redirect("/admin/user/view/" . $user->id);
|
||||
}
|
||||
@ -91,8 +98,12 @@ class UserController extends AdminController
|
||||
$userForm = new CreateUserForm();
|
||||
$userService = new UserService();
|
||||
$userForm->load($_REQUEST);
|
||||
if ($userForm->validate()){
|
||||
if ($userForm->validateForUpdate()){
|
||||
$user = $userService->update($userForm, $user);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
|
||||
|
||||
if ($user){
|
||||
$this->redirect("/admin/user/view/" . $user->id);
|
||||
}
|
||||
@ -100,9 +111,20 @@ class UserController extends AdminController
|
||||
$this->redirect("/admin/user/update/" . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
User::find($id)->delete();
|
||||
$user = User::find($id)->first();
|
||||
if (!$user){
|
||||
throw new Exception(message: "The user not found");
|
||||
}
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->deleteEntityRelation(entity: "user", model: $user);
|
||||
|
||||
$user->delete();
|
||||
$this->redirect("/admin/user/");
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "user",
|
||||
"type": "entity",
|
||||
"description": "User module",
|
||||
"module_class": "kernel\\modules\\user\\UserModule",
|
||||
"module_class_file": "{KERNEL_MODULES}/user/UserModule.php",
|
||||
|
@ -16,4 +16,13 @@ class CreateUserForm extends FormModel
|
||||
];
|
||||
}
|
||||
|
||||
public function rulesForUpdate(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|min-str-len:5|max-str-len:30',
|
||||
'password' => '',
|
||||
'email' => 'required|email'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace kernel\modules\user\service;
|
||||
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
class UserService
|
||||
@ -25,7 +26,9 @@ class UserService
|
||||
{
|
||||
$user->username = $form_model->getItem('username');
|
||||
$user->email = $form_model->getItem('email');
|
||||
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
|
||||
if ($form_model->getItem('password')) {
|
||||
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
|
||||
}
|
||||
if ($user->save()){
|
||||
return $user;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user");
|
||||
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user", enctype: 'multipart/form-data');
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [
|
||||
'class' => "form-control",
|
||||
@ -32,6 +32,11 @@ $form->field(class: \itguild\forms\inputs\TextInput::class, name: "email", param
|
||||
->setLabel("Email")
|
||||
->render();
|
||||
|
||||
$entityRelations = new \kernel\EntityRelation();
|
||||
if (!isset($model)) {
|
||||
$model = new User();
|
||||
}
|
||||
$entityRelations->renderEntityAdditionalPropertyFormBySlug("user", $model);
|
||||
?>
|
||||
<div class="row">
|
||||
<div class="col-sm-2">
|
||||
|
@ -24,6 +24,16 @@ $table = new ListEloquentTable(new EloquentDataProvider(User::class, [
|
||||
'baseUrl' => "/admin/user",
|
||||
'filters' => ['email'],
|
||||
]));
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityRelationsBySlug("user");
|
||||
|
||||
foreach ($additionals as $additional) {
|
||||
$table->addColumn($additional, $additional, function ($id) use ($entityRelation, $additional) {
|
||||
return $entityRelation->getAdditionalPropertyByEntityId("user", $id, $additional);
|
||||
});
|
||||
}
|
||||
|
||||
$table->columns([
|
||||
'username' => [
|
||||
"filter" => [
|
||||
|
@ -25,6 +25,15 @@ $table->beforePrint(function () use ($user) {
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityAdditionalProperty("user", $user);
|
||||
|
||||
foreach ($additionals as $key => $additional) {
|
||||
$table->addRow($key, function () use ($additional) {
|
||||
return $additional;
|
||||
}, ['after' => 'email']);
|
||||
}
|
||||
|
||||
$table->rows([
|
||||
'created_at' => function ($data) {
|
||||
if (!$data){
|
||||
|
@ -23,6 +23,7 @@ class MigrationService
|
||||
public function runAtPath(string $path = ROOT_DIR . '/migrations'): array
|
||||
{
|
||||
$path = getConst($path);
|
||||
// Debug::dd($path);
|
||||
|
||||
try {
|
||||
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
|
||||
@ -31,7 +32,7 @@ class MigrationService
|
||||
|
||||
return $m->run($path);
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception('Не удалось поднять играции');
|
||||
throw new \Exception('Не удалось поднять миграции');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,18 +3,22 @@
|
||||
namespace kernel\services;
|
||||
|
||||
use DirectoryIterator;
|
||||
use GuzzleHttp\Client;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\Manifest;
|
||||
use kernel\helpers\RESTClient;
|
||||
use kernel\models\Option;
|
||||
use MongoDB\Driver\Session;
|
||||
use ZipArchive;
|
||||
|
||||
class ModuleService
|
||||
{
|
||||
protected array $errors = [];
|
||||
|
||||
protected null|bool $serverAvailable = null;
|
||||
|
||||
/**
|
||||
* @param string $module
|
||||
* @return false|array|string
|
||||
@ -67,9 +71,9 @@ class ModuleService
|
||||
{
|
||||
$active_modules = Option::where("key", "active_modules")->first();
|
||||
if ($active_modules) {
|
||||
$path = json_decode($active_modules->value);
|
||||
foreach ($path->modules as $p) {
|
||||
if ($p === $slug) {
|
||||
$modules = json_decode($active_modules->value);
|
||||
foreach ($modules->modules as $mod) {
|
||||
if ($mod === $slug) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -109,7 +113,7 @@ class ModuleService
|
||||
if (isset($module_info['dependence'])) {
|
||||
$dependence_array = explode(',', $module_info['dependence']);
|
||||
foreach ($dependence_array as $depend) {
|
||||
if (!in_array($depend, $active_modules->modules)) {
|
||||
if (!in_array(trim($depend), $active_modules->modules)) {
|
||||
$this->addError("first activate the $depend module");
|
||||
return false;
|
||||
}
|
||||
@ -131,6 +135,10 @@ class ModuleService
|
||||
public function deactivateModule(string $module): bool
|
||||
{
|
||||
$active_modules_info = Option::where("key", "active_modules")->first();
|
||||
|
||||
EntityRelation::removeEntityRelation($module);
|
||||
EntityRelation::removePropertyRelation($module);
|
||||
|
||||
$active_modules = json_decode($active_modules_info->value);
|
||||
if (!in_array($module, $active_modules->modules)) {
|
||||
return true;
|
||||
@ -355,8 +363,6 @@ class ModuleService
|
||||
mkdir(RESOURCES_DIR . '/tmp/modules', 0777, true);
|
||||
}
|
||||
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.igm');
|
||||
|
||||
//$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -438,17 +444,19 @@ class ModuleService
|
||||
|
||||
public function isLastVersion(string $slug): bool
|
||||
{
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
if ($this->isServerAvailable()){
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
|
||||
return true;
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isKernelModule(string $slug): bool
|
||||
@ -463,6 +471,23 @@ class ModuleService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isShopModule(string $slug): bool
|
||||
{
|
||||
if ($this->isServerAvailable()){
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $mod_info['slug']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getKernelModules(): array
|
||||
{
|
||||
$modules_info = [];
|
||||
@ -474,4 +499,21 @@ class ModuleService
|
||||
return $modules_info;
|
||||
}
|
||||
|
||||
public function isServerAvailable(): bool
|
||||
{
|
||||
if (null !== $this->serverAvailable){
|
||||
return $this->serverAvailable;
|
||||
}
|
||||
|
||||
try {
|
||||
RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
$this->serverAvailable = true;
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->serverAvailable = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
* @var \kernel\services\ModuleService $moduleService
|
||||
*/
|
||||
|
||||
use kernel\Flash;
|
||||
use kernel\widgets\IconBtn\IconBtnActivateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeactivateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
@ -32,7 +33,6 @@ $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)) {
|
||||
@ -54,7 +54,7 @@ $table->addAction(function ($row, $url) use ($moduleService) {
|
||||
});
|
||||
|
||||
$table->addAction(function ($row, $url) use ($moduleService){
|
||||
if (!$moduleService->isKernelModule($row['slug'])){
|
||||
if ($moduleService->isShopModule($row['slug'])){
|
||||
if (!$moduleService->isLastVersion($row['slug'])) {
|
||||
$url = "$url/update/?slug=". $row['slug'];
|
||||
|
||||
@ -75,7 +75,7 @@ $table->addAction(function ($row) use ($moduleService){
|
||||
return false;
|
||||
});
|
||||
|
||||
if ($moduleService->isActive('module_shop_client')) {
|
||||
if ($moduleService->isActive('module_shop_client') && $moduleService->isServerAvailable()) {
|
||||
ModuleTabsWidget::create()->run();
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
</ul>
|
||||
</li>
|
||||
<?php else: ?>
|
||||
<li class="<?= $item->url === \kernel\Request::getUrlPath() || $item->url . "/module_shop_client" === \kernel\Request::getUrlPath() ? "active" : "" ?>">
|
||||
<li class="<?= $item->url === \kernel\Request::getUrlPath() ? "active" : "" ?>">
|
||||
<a href="<?= $item->url ?>"><?= $item->label ?></a>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
|
@ -13,6 +13,6 @@ class ModuleTabsWidget extends Widget
|
||||
'/admin' => 'Локальные',
|
||||
'/admin/module_shop_client' => 'Каталог'
|
||||
];
|
||||
$this->cgView->render('/module_tabs.php', ['tabs' => $tabs]);
|
||||
$this->cgView->render('/tabs.php', ['tabs' => $tabs]);
|
||||
}
|
||||
}
|
18
kernel/widgets/TagTabsWidget.php
Normal file
18
kernel/widgets/TagTabsWidget.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\widgets;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Widget;
|
||||
|
||||
class TagTabsWidget extends Widget
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$tabs = [
|
||||
'/admin/tag' => 'tag',
|
||||
'/admin/tag_entity' => 'tag entity'
|
||||
];
|
||||
$this->cgView->render('/tabs.php', ['tabs' => $tabs]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user