From 62eff81213ec8608a4cab516ffd92b2f0f6b8cd0 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 21 Nov 2024 13:00:21 +0300 Subject: [PATCH] entity relation save --- app/modules/tag/TagModule.php | 22 ++++++++ app/modules/tag/manifest.json | 1 + kernel/EntityRelation.php | 56 ++++++++++++++++++- .../post/controllers/PostController.php | 7 +++ kernel/services/ModuleService.php | 5 ++ 5 files changed, 90 insertions(+), 1 deletion(-) diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index b2f0ffd..5f6a05f 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -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(); + } } \ No newline at end of file diff --git a/app/modules/tag/manifest.json b/app/modules/tag/manifest.json index 3967dd6..b164b3e 100644 --- a/app/modules/tag/manifest.json +++ b/app/modules/tag/manifest.json @@ -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", diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index e08c202..0b24c25 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -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); + } + } + } } \ No newline at end of file diff --git a/kernel/modules/post/controllers/PostController.php b/kernel/modules/post/controllers/PostController.php index ccd0d44..ed12c3b 100644 --- a/kernel/modules/post/controllers/PostController.php +++ b/kernel/modules/post/controllers/PostController.php @@ -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); } diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index f064e17..05a9f93 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -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;