From e7a20d9b97716c33cfd8a9234f48e78b4ce139ac Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 28 Nov 2024 16:25:51 +0300 Subject: [PATCH] additionsl property at list --- app/modules/tag/TagModule.php | 22 +++++++++----- kernel/EntityRelation.php | 30 ++++++++++++------- .../tag/services/TagEntityService.php | 12 ++++++++ kernel/modules/post/views/index.php | 10 +++++++ 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index e4ec198..9969185 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -8,6 +8,7 @@ use itguild\forms\builders\TextInputBuilder; use itguild\forms\inputs\Select; use kernel\app_modules\tag\models\Tag; use kernel\app_modules\tag\models\TagEntity; +use kernel\app_modules\tag\services\TagEntityService; use kernel\helpers\Debug; use kernel\helpers\Slug; use kernel\Module; @@ -61,14 +62,8 @@ 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']; - } - + if (isset($model->id)) { + $value = TagEntityService::getTagsByEntity($entity, $model->id); } $input = SelectBuilder::build("tag[]", [ 'class' => 'form-control', @@ -107,6 +102,17 @@ class TagModule extends Module return substr($tagsStr, 0, -2); } + public function getItem(string $entity, string $entity_id): string + { + $tags = TagEntity::where("entity", $entity)->where("entity_id", $entity_id)->get(); + $tagsStr = ""; + foreach ($tags as $tag) { + $tagsStr .= $tag->tag->label . ", "; + } + + return substr($tagsStr, 0, -2); + } + public function deleteItems(string $entity, Model $model): void { TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index 21a867b..1e44fea 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -78,7 +78,7 @@ class EntityRelation $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){ + foreach ($entity_relations as $entity => $entity_relation) { if (in_array($property, $entity_relation)) { $index = array_search($property, $entity_relation); unset($entity_relations[$entity][$index]); @@ -113,7 +113,7 @@ class EntityRelation { $entityRelations = $this->getEntitiesRelations(); if ($entityRelations) { - if (isset($entityRelations[$slug])){ + if (isset($entityRelations[$slug])) { return $entityRelations[$slug]; } } @@ -161,8 +161,8 @@ class EntityRelation public function renderEntityAdditionalPropertyFormBySlug(string $entity, Model $model = null): void { $relations = $this->getEntityRelationsBySlug($entity); - if ($relations){ - foreach ($relations as $relation){ + if ($relations) { + foreach ($relations as $relation) { $this->renderFormInputsBySlug($entity, $relation, $model); } } @@ -179,8 +179,8 @@ class EntityRelation public function saveEntityRelation(string $entity, Model $model, Request $request): void { $relations = $this->getEntityRelationsBySlug($entity); - if ($relations){ - foreach ($relations as $relation){ + if ($relations) { + foreach ($relations as $relation) { $this->saveEntityRelationBySlug($relation, $entity, $model, $request); } } @@ -189,9 +189,9 @@ class EntityRelation public function getEntityAdditionalProperty(string $entity, Model $model): array { $relations = $this->getEntityRelationsBySlug($entity); - if ($relations){ + if ($relations) { $relationsArr = []; - foreach ($relations as $relation){ + foreach ($relations as $relation) { $moduleClass = $this->getAdditionalPropertyClassBySlug($relation); if ($moduleClass and method_exists($moduleClass, "getItems")) { $relationsArr[$relation] = $moduleClass->getItems($entity, $model); @@ -204,6 +204,16 @@ class EntityRelation return []; } + public function getAdditionalPropertyByEntityId(string $entity, string $entity_id, string $additionalPropertySlug): string + { + $moduleClass = $this->getAdditionalPropertyClassBySlug($additionalPropertySlug); + if ($moduleClass and method_exists($moduleClass, "getItem")) { + return $moduleClass->getItem($entity, $entity_id); + } + + return ""; + } + public function deleteEntityRelationBySlug(string $slug, string $entity, Model $model): void { $moduleClass = $this->getAdditionalPropertyClassBySlug($slug); @@ -215,8 +225,8 @@ class EntityRelation public function deleteEntityRelation(string $entity, Model $model): void { $relations = $this->getEntityRelationsBySlug($entity); - if ($relations){ - foreach ($relations as $relation){ + if ($relations) { + foreach ($relations as $relation) { $this->deleteEntityRelationBySlug($relation, $entity, $model); } } diff --git a/kernel/app_modules/tag/services/TagEntityService.php b/kernel/app_modules/tag/services/TagEntityService.php index ca16103..67e4bcc 100755 --- a/kernel/app_modules/tag/services/TagEntityService.php +++ b/kernel/app_modules/tag/services/TagEntityService.php @@ -5,6 +5,7 @@ namespace kernel\app_modules\tag\services; use kernel\app_modules\tag\models\Tag; use kernel\app_modules\tag\models\TagEntity; use kernel\FormModel; +use kernel\helpers\Debug; use kernel\helpers\Slug; class TagEntityService @@ -36,4 +37,15 @@ class TagEntityService return false; } + public static function getTagsByEntity(string $entity, int $entity_id): array + { + $tags= TagEntity::where("entity_id", $entity_id)->where("entity", $entity)->get(); + $value = []; + foreach ($tags as $tag) { + $value[$tag->id] = $tag->tag->label; + } + + return $value; + } + } \ No newline at end of file diff --git a/kernel/modules/post/views/index.php b/kernel/modules/post/views/index.php index 65134cd..d82ecb5 100644 --- a/kernel/modules/post/views/index.php +++ b/kernel/modules/post/views/index.php @@ -24,6 +24,16 @@ $table = new ListEloquentTable(new EloquentDataProvider(Post::class, [ 'params' => ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/post" ])); + +$entityRelation = new \kernel\EntityRelation(); +$additionals = $entityRelation->getEntityRelationsBySlug("post"); + +foreach ($additionals as $additional) { + $table->addColumn($additional, $additional, function ($id) use ($entityRelation, $additional) { + return $entityRelation->getAdditionalPropertyByEntityId("post", $id, $additional); + }); +} + $table->columns([ 'created_at' => function ($data) { if (!$data){