diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index af1d2b4..e4ec198 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -61,10 +61,19 @@ class TagModule extends Module public function formInputs(string $entity, Model $model = null): void { + if (isset($model)) { + $tags= TagEntity::where("entity_id", $model->id)->get()->toArray(); + $value = []; + foreach ($tags as $tag) { + $val = Tag::where('id', $tag['tag_id'])->first()->toArray(); + $value[] = $val['label']; + } + + } $input = SelectBuilder::build("tag[]", [ 'class' => 'form-control', 'placeholder' => 'Теги', - 'value' => '', + 'value' => $value ?? '', 'multiple' => "multiple", 'options' => Tag::getTagLabelByEntity($entity) ]); @@ -97,4 +106,9 @@ class TagModule extends Module return substr($tagsStr, 0, -2); } + + public function deleteItems(string $entity, Model $model): void + { + TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); + } } \ No newline at end of file diff --git a/composer.lock b/composer.lock index 315e67a..2f5296e 100644 --- a/composer.lock +++ b/composer.lock @@ -1217,11 +1217,11 @@ }, { "name": "itguild/forms", - "version": "0.1.4", + "version": "0.1.5", "source": { "type": "git", "url": "https://git.itguild.info/ItGuild/forms_bundle.git", - "reference": "ddb17cc47360910b3875b88e10e14f91fcd875be" + "reference": "45e57367d3f9571fde2b82fa2fd8126469ba6db6" }, "require": { "itguild/php-cg-select-v2": "^0.1.0", @@ -1245,7 +1245,7 @@ "email": "apuc06@mail.ru" } ], - "time": "2024-09-10T08:41:16+00:00" + "time": "2024-11-28T10:18:58+00:00" }, { "name": "itguild/php-cg-select-v2", @@ -3205,5 +3205,5 @@ "ext-zip": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index b387a70..21a867b 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -203,4 +203,22 @@ class EntityRelation 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); + } + } + } } \ No newline at end of file diff --git a/kernel/modules/post/controllers/PostController.php b/kernel/modules/post/controllers/PostController.php index c4bcb23..eef11f3 100644 --- a/kernel/modules/post/controllers/PostController.php +++ b/kernel/modules/post/controllers/PostController.php @@ -93,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); } @@ -109,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/"); }