tag settings

This commit is contained in:
Билай Станислав 2024-12-02 14:15:01 +03:00
parent b981ff0c44
commit 567ab8544d
4 changed files with 38 additions and 20 deletions

View File

@ -65,6 +65,7 @@ class TagModule extends Module
if (isset($model->id)) { if (isset($model->id)) {
$value = TagEntityService::getTagsByEntity($entity, $model->id); $value = TagEntityService::getTagsByEntity($entity, $model->id);
} }
$input = SelectBuilder::build("tag[]", [ $input = SelectBuilder::build("tag[]", [
'class' => 'form-control', 'class' => 'form-control',
'placeholder' => 'Теги', 'placeholder' => 'Теги',

View File

@ -25,7 +25,7 @@ class EntityRelation
$activeModules = $moduleService->getActiveModules(); $activeModules = $moduleService->getActiveModules();
foreach ($activeModules as $module) { foreach ($activeModules as $module) {
if (isset($module['type']) and $module['type'] === "entity") { 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; return $list;
} }
public function getEntitiesRelations(): array|bool public static function getEntitiesRelations(): array|bool
{ {
$entity_relations = OptionService::getItem("entity_relations"); $entity_relations = OptionService::getItem("entity_relations");
if ($entity_relations) { if ($entity_relations) {
@ -61,8 +61,13 @@ class EntityRelation
if ($entity_relations_info) { if ($entity_relations_info) {
$entity_relations = json_decode($entity_relations_info->value, true); $entity_relations = json_decode($entity_relations_info->value, true);
if ($entity_relations[$entity]) { if ($entity_relations[$entity]) {
if ($entity_relations[$entity][$property]) { $propertyKey = array_search($property, $entity_relations[$entity]);
unset($entity_relations[$entity][$property]); 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->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
$entity_relations_info->save(); $entity_relations_info->save();
return true; return true;
@ -129,7 +134,7 @@ class EntityRelation
if (isset($entity_relations[$entity])) { if (isset($entity_relations[$entity])) {
$entity_relations[$entity][] = $property; $entity_relations[$entity][] = $property;
} else { } else {
$entity_relations[$entity] = $property; $entity_relations[$entity][] = $property;
} }
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE); $entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
$entity_relations_info->save(); $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;
}
} }

View File

@ -111,32 +111,32 @@ class TagController extends AdminController
$request = new Request(); $request = new Request();
$entities = $request->post('entity'); $entities = $request->post('entity');
$entityRelationsModel = Option::where("key", "entity_relations")->first(); $entityRelations = EntityRelation::getEntitiesRelations();
$entityRelations = json_decode($entityRelationsModel->value, true);
Debug::prn($entities);
Debug::prn($entityRelations);
if (isset($entities)) { if (isset($entities)) {
foreach ($entities as $entity) {
if (!isset($entityRelations[$entity])) {
EntityRelation::addEntityRelation($entity, 'tag');
}
}
foreach ($entityRelations as $entity => $property) { foreach ($entityRelations as $entity => $property) {
if (in_array($entity, $entities)) { if (in_array($entity, $entities)) {
if (!in_array('tag', $property)) { if (!in_array('tag', $property)) {
EntityRelation::addEntityRelation($entity, 'tag'); EntityRelation::addEntityRelation($entity, 'tag');
} }
} else { } else {
EntityRelation::removePropertyFromEntityRelations($entity, array_search('tag', $property)); if (in_array('tag', $property)) {
EntityRelation::removePropertyFromEntityRelations($entity, 'tag');
}
} }
} }
} else { } else {
foreach ($entityRelations as $entity => $property) { foreach ($entityRelations as $entity => $property) {
EntityRelation::removeEntityRelation($entity); EntityRelation::removePropertyFromEntityRelations($entity, 'tag');
} }
} }
// $this->redirect("/admin/settings/tag"); $this->redirect("/admin/settings/tag", 302);
} }
} }

View File

@ -1,14 +1,13 @@
<?php <?php
use itguild\forms\builders\SelectBuilder;
use kernel\EntityRelation;
$form = new \itguild\forms\ActiveForm(); $form = new \itguild\forms\ActiveForm();
$form->beginForm("/admin/settings/tag/update"); $form->beginForm("/admin/settings/tag/update");
//\kernel\helpers\Debug::dd($value);
$form->field(\itguild\forms\inputs\Select::class, "entity[]", [ $form->field(\itguild\forms\inputs\Select::class, "entity[]", [
'class' => "form-control", 'class' => "form-control",
'value' => $model->entity ?? '', 'value' => \kernel\EntityRelation::getEntityByProperty('tag') ?? '',
'multiple' => "multiple", 'multiple' => "multiple",
]) ])