diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index 2350a8f..539f71a 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -3,7 +3,9 @@ namespace app\modules\tag; use Illuminate\Database\Eloquent\Model; +use itguild\forms\builders\SelectBuilder; 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\helpers\Debug; @@ -19,6 +21,7 @@ class TagModule extends Module public MenuService $menuService; public MigrationService $migrationService; + public function __construct() { $this->menuService = new MenuService(); @@ -58,10 +61,12 @@ class TagModule extends Module public function formInputs(string $entity, Model $model = null): void { - $input = TextInputBuilder::build("tag", [ + $input = SelectBuilder::build("tag[]", [ 'class' => 'form-control', 'placeholder' => 'Теги', - 'value' => Tag::find($model->id)->label ?? "" + 'value' => '', + 'multiple' => "multiple", + 'options' => Tag::getTagLabelByEntity($entity) ]); $input->setLabel("Теги"); $input->create()->render(); @@ -69,23 +74,21 @@ class TagModule extends Module public function saveInputs(string $entity, Model $model, Request $request): void { - $tag = new Tag(); - $tag->label = $request->post('tag'); - $tag->entity = $entity; - $tag->status = Tag::ACTIVE_STATUS; - $tag->slug = Slug::createSlug($request->post('tag'), $model); - $tag->save(); + TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); - $tagEntity = new TagEntity(); - $tagEntity->entity = $entity; - $tagEntity->entity_id = $model->id; - $tagEntity->tag_id = $tag->id; - $tagEntity->save(); + $tags = $request->post("tag"); + foreach ($tags as $tag) { + $tagEntity = new TagEntity(); + $tagEntity->entity = $entity; + $tagEntity->entity_id = $model->id; + $tagEntity->tag_id = $tag; + $tagEntity->save(); + } } - public function getInputs(string $entity, Model $model) - { -// Debug::dd($tag); + public function getItems(string $entity, Model $model): array + { + return TagEntity::where("entity", $entity)->where("entity_id", $model->id)->with("tag")->get()->toArray(); } } \ No newline at end of file diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index 933ff14..f126b84 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -185,4 +185,19 @@ class EntityRelation } } } + + public function getEntityAdditionalProperty(string $entity, Model $model): array + { + $relations = $this->getEntityRelationsBySlug($entity); + if ($relations){ + foreach ($relations as $relation){ + $moduleClass = $this->getAdditionalPropertyClassBySlug($relation); + if ($moduleClass and method_exists($moduleClass, "getItems")) { + return $moduleClass->getItems($entity, $model); + } + } + } + + return []; + } } \ No newline at end of file diff --git a/kernel/app_modules/tag/models/Tag.php b/kernel/app_modules/tag/models/Tag.php index e7f6eea..df4c8ee 100755 --- a/kernel/app_modules/tag/models/Tag.php +++ b/kernel/app_modules/tag/models/Tag.php @@ -42,4 +42,20 @@ class Tag extends Model ]; } + public static function getTagListByEntity(string $entity): array + { + return self::where("entity", $entity)->get()->toArray(); + } + + public static function getTagLabelByEntity(string $entity): array + { + $result = []; + $tags = self::getTagListByEntity($entity); + foreach ($tags as $tag){ + $result[$tag['id']] = $tag['label']; + } + + return $result; + } + } \ No newline at end of file diff --git a/kernel/app_modules/tag/models/TagEntity.php b/kernel/app_modules/tag/models/TagEntity.php index ff59857..e1a8956 100755 --- a/kernel/app_modules/tag/models/TagEntity.php +++ b/kernel/app_modules/tag/models/TagEntity.php @@ -24,4 +24,9 @@ class TagEntity extends Model 'entity_id' => 'Идентификатор сущности', ]; } + + public function tag(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Tag::class); + } } \ No newline at end of file diff --git a/kernel/modules/post/views/view.php b/kernel/modules/post/views/view.php index 36a61f6..9b129e0 100644 --- a/kernel/modules/post/views/view.php +++ b/kernel/modules/post/views/view.php @@ -25,6 +25,9 @@ $table->beforePrint(function () use ($content) { return $btn; }); +$entityRelation = new \kernel\EntityRelation(); +$additional = $entityRelation->getEntityAdditionalProperty("post", $content); + $table->rows([ 'created_at' => function ($data) { if (!$data){