From 567ab8544dfcddab036ae603b1e7731411250c8a Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 2 Dec 2024 14:15:01 +0300 Subject: [PATCH] tag settings --- app/modules/tag/TagModule.php | 1 + kernel/EntityRelation.php | 28 +++++++++++++++---- .../tag/controllers/TagController.php | 22 +++++++-------- .../tag/views/tag/settingsForm.php | 7 ++--- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index 2ae2e9f..36d07e8 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -65,6 +65,7 @@ class TagModule extends Module if (isset($model->id)) { $value = TagEntityService::getTagsByEntity($entity, $model->id); } + $input = SelectBuilder::build("tag[]", [ 'class' => 'form-control', 'placeholder' => 'Теги', diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index 682e584..6b2fc42 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -25,7 +25,7 @@ class EntityRelation $activeModules = $moduleService->getActiveModules(); foreach ($activeModules as $module) { if (isset($module['type']) and $module['type'] === "entity") { - $list[$module['slug']] = $module['name']; + $list[$module['slug']] = $module['slug']; } } @@ -45,7 +45,7 @@ class EntityRelation return $list; } - public function getEntitiesRelations(): array|bool + public static function getEntitiesRelations(): array|bool { $entity_relations = OptionService::getItem("entity_relations"); if ($entity_relations) { @@ -61,8 +61,13 @@ class EntityRelation if ($entity_relations_info) { $entity_relations = json_decode($entity_relations_info->value, true); if ($entity_relations[$entity]) { - if ($entity_relations[$entity][$property]) { - unset($entity_relations[$entity][$property]); + $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; @@ -129,7 +134,7 @@ class EntityRelation if (isset($entity_relations[$entity])) { $entity_relations[$entity][] = $property; } else { - $entity_relations[$entity] = $property; + $entity_relations[$entity][] = $property; } $entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE); $entity_relations_info->save(); @@ -231,4 +236,17 @@ class EntityRelation } } } + + 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; + } } \ No newline at end of file diff --git a/kernel/app_modules/tag/controllers/TagController.php b/kernel/app_modules/tag/controllers/TagController.php index e3cde3e..dfe95ad 100755 --- a/kernel/app_modules/tag/controllers/TagController.php +++ b/kernel/app_modules/tag/controllers/TagController.php @@ -111,32 +111,32 @@ class TagController extends AdminController $request = new Request(); $entities = $request->post('entity'); - $entityRelationsModel = Option::where("key", "entity_relations")->first(); - $entityRelations = json_decode($entityRelationsModel->value, true); - - - - Debug::prn($entities); - Debug::prn($entityRelations); - + $entityRelations = EntityRelation::getEntitiesRelations(); if (isset($entities)) { + foreach ($entities as $entity) { + if (!isset($entityRelations[$entity])) { + EntityRelation::addEntityRelation($entity, 'tag'); + } + } foreach ($entityRelations as $entity => $property) { if (in_array($entity, $entities)) { if (!in_array('tag', $property)) { EntityRelation::addEntityRelation($entity, 'tag'); } } else { - EntityRelation::removePropertyFromEntityRelations($entity, array_search('tag', $property)); + if (in_array('tag', $property)) { + EntityRelation::removePropertyFromEntityRelations($entity, 'tag'); + } } } } else { foreach ($entityRelations as $entity => $property) { - EntityRelation::removeEntityRelation($entity); + EntityRelation::removePropertyFromEntityRelations($entity, 'tag'); } } -// $this->redirect("/admin/settings/tag"); + $this->redirect("/admin/settings/tag", 302); } } \ No newline at end of file diff --git a/kernel/app_modules/tag/views/tag/settingsForm.php b/kernel/app_modules/tag/views/tag/settingsForm.php index b7f2ffb..6c3d209 100644 --- a/kernel/app_modules/tag/views/tag/settingsForm.php +++ b/kernel/app_modules/tag/views/tag/settingsForm.php @@ -1,14 +1,13 @@ beginForm("/admin/settings/tag/update"); +//\kernel\helpers\Debug::dd($value); + $form->field(\itguild\forms\inputs\Select::class, "entity[]", [ 'class' => "form-control", - 'value' => $model->entity ?? '', + 'value' => \kernel\EntityRelation::getEntityByProperty('tag') ?? '', 'multiple' => "multiple", ])