entity relation save

This commit is contained in:
Kavalar 2024-11-21 13:00:21 +03:00
parent 860ea1a82d
commit 62eff81213
5 changed files with 90 additions and 1 deletions

View File

@ -2,8 +2,13 @@
namespace app\modules\tag;
use itguild\forms\builders\TextInputBuilder;
use kernel\app_modules\tag\models\Tag;
use kernel\helpers\Debug;
use kernel\helpers\Slug;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\Request;
use kernel\services\MigrationService;
class TagModule extends Module
@ -35,4 +40,21 @@ class TagModule extends Module
{
$this->menuService->removeItemBySlug("tag");
}
public function formInputs(): void
{
$input = TextInputBuilder::build("tag", ['class' => 'form-control', 'placeholder' => 'Теги']);
$input->setLabel("Теги");
$input->create()->render();
}
public function saveInputs(string $entity, Request $request): void
{
$model = new Tag();
$model->entity = $entity;
$model->label = $request->post('tag');
$model->status = Tag::ACTIVE_STATUS;
$model->slug = Slug::createSlug($request->post('tag'), $model);
$model->save();
}
}

View File

@ -3,6 +3,7 @@
"version": "0.2",
"author": "ITGuild",
"slug": "tag",
"type": "additional_property",
"description": "Tags module",
"app_module_path": "{APP}/modules/{slug}",
"module_class": "app\\modules\\tag\\TagModule",

View File

@ -53,7 +53,7 @@ class EntityRelation
return false;
}
public function removeFromEntityRelations(string $entity, string $property): bool
public static function removePropertyFromEntityRelations(string $entity, string $property): bool
{
$entity_relations_info = Option::where("key", "entity_relations")->first();
if ($entity_relations_info) {
@ -71,6 +71,42 @@ class EntityRelation
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();
@ -129,4 +165,22 @@ class EntityRelation
}
}
}
public function saveEntityRelationBySlug(string $slug, string $entity, Request $request): void
{
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
$moduleClass->saveInputs($entity, $request);
}
}
public function saveEntityRelation(string $entity, Request $request): void
{
$relations = $this->getEntityRelationsBySlug($entity);
if ($relations){
foreach ($relations as $relation){
$this->saveEntityRelationBySlug($relation, $entity, $request);
}
}
}
}

View File

@ -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("post", new Request());
if ($post) {
$this->redirect("/admin/post/view/" . $post->id);
}

View File

@ -3,6 +3,7 @@
namespace kernel\services;
use DirectoryIterator;
use kernel\EntityRelation;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
@ -130,6 +131,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;