From 745707ee09ee9aa60c238c5d53ff68874e642a1f Mon Sep 17 00:00:00 2001 From: Kavalar Date: Mon, 25 Nov 2024 15:17:41 +0300 Subject: [PATCH] tags --- app/modules/tag/TagModule.php | 45 ++++++++++++++----- kernel/EntityRelation.php | 45 ++++++++++--------- .../tag/controllers/TagController.php | 5 +++ kernel/app_modules/tag/models/Tag.php | 5 +-- kernel/app_modules/tag/routs/tag.php | 3 ++ .../modules/option/service/OptionService.php | 13 +++++- .../post/controllers/PostController.php | 2 +- kernel/modules/post/views/form.php | 2 +- 8 files changed, 81 insertions(+), 39 deletions(-) diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index 2438479..6f48673 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -2,12 +2,14 @@ namespace app\modules\tag; +use Illuminate\Database\Eloquent\Model; use itguild\forms\builders\TextInputBuilder; use kernel\app_modules\tag\models\Tag; use kernel\helpers\Debug; use kernel\helpers\Slug; use kernel\Module; use kernel\modules\menu\service\MenuService; +use kernel\modules\option\service\OptionService; use kernel\Request; use kernel\services\MigrationService; @@ -34,29 +36,50 @@ class TagModule extends Module "url" => "/admin/tag", "slug" => "tag", ]); + + $this->menuService->createItem([ + "label" => "Тэги", + "url" => "/admin/settings/tag", + "slug" => "tag_settings", + "parent_slug" => "settings" + ]); + + OptionService::createFromParams("entity_tag_list", "{}", "Список тегов"); } public function deactivate(): void { $this->menuService->removeItemBySlug("tag"); + OptionService::removeOptionByKey("entity_tag_list"); } - public function formInputs(): void + public function formInputs(string $entity, Model $model = null): void { - $input = TextInputBuilder::build("tag", ['class' => 'form-control', 'placeholder' => 'Теги']); + $tag = Tag::where("entity", $entity)->where("entity_id", $model->id)->first(); + + $input = TextInputBuilder::build("tag", [ + 'class' => 'form-control', + 'placeholder' => 'Теги', + 'value' => $tag->label ?? "" + ]); $input->setLabel("Теги"); $input->create()->render(); } - public function saveInputs(string $entity, Request $request): void + public function saveInputs(string $entity, Model $model, Request $request): void { - $model = new Tag(); - $model->entity = $entity; - Debug::dd($request->post()); - $model->entity_id = 1; - $model->label = $request->post('tag'); - $model->status = Tag::ACTIVE_STATUS; - $model->slug = Slug::createSlug($request->post('tag'), $model); - $model->save(); + $tag = new Tag(); + $tag->entity = $entity; + $tag->entity_id = $model->id; + $tag->label = $request->post('tag'); + $tag->status = Tag::ACTIVE_STATUS; + $tag->slug = Slug::createSlug($request->post('tag'), $model); + $tag->save(); + } + + public function getInputs(string $entity, Model $model) + { + + Debug::dd($tag); } } \ No newline at end of file diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index 0b24c25..cd6139b 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -2,6 +2,7 @@ namespace kernel; +use Illuminate\Database\Eloquent\Model; use kernel\helpers\Debug; use kernel\models\Option; use kernel\modules\option\service\OptionService; @@ -148,38 +149,38 @@ class EntityRelation return false; } - public function renderFormInputsBySlug(string $slug): void + public function renderFormInputsBySlug(string $entity, string $slug, Model $model): void { $moduleClass = $this->getAdditionalPropertyClassBySlug($slug); if ($moduleClass and method_exists($moduleClass, "formInputs")) { - $moduleClass->formInputs(); + $moduleClass->formInputs($entity, $model); } } - public function renderEntityAdditionalPropertyFormBySlug(string $slug): void - { - $relations = $this->getEntityRelationsBySlug($slug); - if ($relations){ - foreach ($relations as $relation){ - $this->renderFormInputsBySlug($relation); - } - } - } - - public function saveEntityRelationBySlug(string $slug, string $entity, Request $request): void - { - $moduleClass = $this->getAdditionalPropertyClassBySlug($slug); - if ($moduleClass and method_exists($moduleClass, "saveInputs")) { - $moduleClass->saveInputs($entity, $request); - } - } - - public function saveEntityRelation(string $entity, Request $request): void + public function renderEntityAdditionalPropertyFormBySlug(string $entity, Model $model = null): void { $relations = $this->getEntityRelationsBySlug($entity); if ($relations){ foreach ($relations as $relation){ - $this->saveEntityRelationBySlug($relation, $entity, $request); + $this->renderFormInputsBySlug($entity, $relation, $model); + } + } + } + + public function saveEntityRelationBySlug(string $slug, string $entity, Model $model, Request $request): void + { + $moduleClass = $this->getAdditionalPropertyClassBySlug($slug); + if ($moduleClass and method_exists($moduleClass, "saveInputs")) { + $moduleClass->saveInputs($entity, $model, $request); + } + } + + public function saveEntityRelation(string $entity, Model $model, Request $request): void + { + $relations = $this->getEntityRelationsBySlug($entity); + if ($relations){ + foreach ($relations as $relation){ + $this->saveEntityRelationBySlug($relation, $entity, $model, $request); } } } diff --git a/kernel/app_modules/tag/controllers/TagController.php b/kernel/app_modules/tag/controllers/TagController.php index 2a72451..11af195 100755 --- a/kernel/app_modules/tag/controllers/TagController.php +++ b/kernel/app_modules/tag/controllers/TagController.php @@ -98,4 +98,9 @@ class TagController extends AdminController $this->redirect("/admin/tag/"); } + public function actionSettings(): void + { + echo "tag settings"; + } + } \ 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 81c891a..17fa05f 100755 --- a/kernel/app_modules/tag/models/Tag.php +++ b/kernel/app_modules/tag/models/Tag.php @@ -11,8 +11,6 @@ use Illuminate\Database\Eloquent\Model; * @property int $entity_id * @property string $slug * @property int $status - * @method static where(int[] $array) - * @method static find($id) */ class Tag extends Model { @@ -21,13 +19,14 @@ class Tag extends Model protected $table = 'tag'; - protected $fillable = ['label', 'entity', 'slug', 'status']; + protected $fillable = ['label', 'entity', 'entity_id', 'slug', 'status']; public static function labels(): array { return [ 'label' => 'Заголовок', 'entity' => 'Сущность', + 'entity_id' => 'Идентификатор сущность', 'slug' => 'Slug', 'status' => 'Статус', ]; diff --git a/kernel/app_modules/tag/routs/tag.php b/kernel/app_modules/tag/routs/tag.php index 6ec41fe..ed83dbe 100755 --- a/kernel/app_modules/tag/routs/tag.php +++ b/kernel/app_modules/tag/routs/tag.php @@ -15,4 +15,7 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router App::$collector->any("/edit/{id}", [\app\modules\tag\controllers\TagController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\app\modules\tag\controllers\TagController::class, 'actionDelete']); }); + App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router){ + App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']); + }); }); \ No newline at end of file diff --git a/kernel/modules/option/service/OptionService.php b/kernel/modules/option/service/OptionService.php index 139d0fb..15eb5a5 100644 --- a/kernel/modules/option/service/OptionService.php +++ b/kernel/modules/option/service/OptionService.php @@ -35,7 +35,7 @@ class OptionService return false; } - public function createFromParams(string $key, string $value, string $label): false|Option + public static function createFromParams(string $key, string $value, string $label): false|Option { $model = new Option(); $model->key = $key; @@ -63,6 +63,17 @@ class OptionService return false; } + public static function removeOptionByKey(string $key): bool + { + $option = Option::where("key", $key)->first(); + if (!$option){ + return false; + } + + $option->delete(); + return true; + } + // public function createOptionArr(): array // { // foreach (Option::all()->toArray() as $option) { diff --git a/kernel/modules/post/controllers/PostController.php b/kernel/modules/post/controllers/PostController.php index ed12c3b..c4bcb23 100644 --- a/kernel/modules/post/controllers/PostController.php +++ b/kernel/modules/post/controllers/PostController.php @@ -36,7 +36,7 @@ class PostController extends AdminController $post = $this->postService->create($postForm); $entityRelation = new EntityRelation(); - $entityRelation->saveEntityRelation("post", new Request()); + $entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request()); if ($post) { $this->redirect("/admin/post/view/" . $post->id); diff --git a/kernel/modules/post/views/form.php b/kernel/modules/post/views/form.php index 1d5f16a..b3713c6 100644 --- a/kernel/modules/post/views/form.php +++ b/kernel/modules/post/views/form.php @@ -34,7 +34,7 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params ->render(); $entityRelations = new \kernel\EntityRelation(); -$entityRelations->renderEntityAdditionalPropertyFormBySlug("post"); +$entityRelations->renderEntityAdditionalPropertyFormBySlug("post", $model); ?>