From 8a9f156235c7ccb05ec4819a94a248b59ff3a594 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Wed, 27 Nov 2024 14:01:21 +0300 Subject: [PATCH] crud tag entity, some fix tag --- app/modules/tag/TagModule.php | 16 ++- kernel/EntityRelation.php | 2 +- .../tag/controllers/TagController.php | 6 +- .../tag/controllers/TagEntityController.php | 128 +++++++++--------- .../2024_10_08_093710_create_tag_table.php | 2 - ...4_11_25_122506_create_tag_entity_table.php | 4 +- kernel/app_modules/tag/models/Tag.php | 4 +- kernel/app_modules/tag/models/TagEntity.php | 4 +- .../tag/models/forms/CreateTagForm.php | 2 - kernel/app_modules/tag/routs/tag.php | 10 ++ kernel/app_modules/tag/routs/tag_entity.php | 23 ---- .../app_modules/tag/services/TagService.php | 4 - .../app_modules/tag/views/{ => tag}/form.php | 8 -- .../app_modules/tag/views/{ => tag}/index.php | 0 .../app_modules/tag/views/{ => tag}/view.php | 0 .../tag/views/tag_entity/index.php | 12 +- .../app_modules/tag/views/tag_entity/view.php | 30 ++++ .../controllers/AdminConsoleController.php | 28 +++- kernel/modules/post/views/form.php | 3 + kernel/views/widgets/menu.php | 2 +- 20 files changed, 155 insertions(+), 133 deletions(-) delete mode 100755 kernel/app_modules/tag/routs/tag_entity.php rename kernel/app_modules/tag/views/{ => tag}/form.php (84%) rename kernel/app_modules/tag/views/{ => tag}/index.php (100%) rename kernel/app_modules/tag/views/{ => tag}/view.php (100%) create mode 100755 kernel/app_modules/tag/views/tag_entity/view.php diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index e088e01..50b7982 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -5,6 +5,7 @@ namespace app\modules\tag; use Illuminate\Database\Eloquent\Model; use itguild\forms\builders\TextInputBuilder; use kernel\app_modules\tag\models\Tag; +use kernel\app_modules\tag\models\TagEntity; use kernel\helpers\Debug; use kernel\helpers\Slug; use kernel\Module; @@ -30,7 +31,7 @@ class TagModule extends Module public function init(): void { $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag"); -// $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag_entity"); + $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag_entity"); $this->menuService->createItem([ "label" => "Тэги", @@ -51,17 +52,16 @@ class TagModule extends Module public function deactivate(): void { $this->menuService->removeItemBySlug("tag"); + $this->menuService->removeItemBySlug("tag_settings"); OptionService::removeOptionByKey("entity_tag_list"); } public function formInputs(string $entity, Model $model = null): void { - $tag = Tag::where("entity", $entity)->where("entity_id", $model->id)->first(); - $input = TextInputBuilder::build("tag", [ 'class' => 'form-control', 'placeholder' => 'Теги', - 'value' => $tag->label ?? "" + 'value' => Tag::find($model->id)->label ?? "" ]); $input->setLabel("Теги"); $input->create()->render(); @@ -70,12 +70,16 @@ class TagModule extends Module public function saveInputs(string $entity, Model $model, Request $request): void { $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(); + + $tagEntity = new TagEntity(); + $tagEntity->entity = $entity; + $tagEntity->entity_id = $model->id; + $tagEntity->tag_id = $tag->id; + $tagEntity->save(); } public function getInputs(string $entity, Model $model) diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index cd6139b..28a5c97 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -171,7 +171,7 @@ class EntityRelation { $moduleClass = $this->getAdditionalPropertyClassBySlug($slug); if ($moduleClass and method_exists($moduleClass, "saveInputs")) { - $moduleClass->saveInputs($entity, $model, $request); + $moduleClass->saveInputs($entity, $model, $request, ); } } diff --git a/kernel/app_modules/tag/controllers/TagController.php b/kernel/app_modules/tag/controllers/TagController.php index 11af195..748af2a 100755 --- a/kernel/app_modules/tag/controllers/TagController.php +++ b/kernel/app_modules/tag/controllers/TagController.php @@ -17,7 +17,7 @@ class TagController extends AdminController protected function init(): void { parent::init(); - $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/"; + $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag/"; $this->tagService = new TagService(); } @@ -33,7 +33,7 @@ class TagController extends AdminController if ($tagForm->validate()){ $tag = $this->tagService->create($tagForm); if ($tag){ - $this->redirect("/admin/tag/" . $tag->id); + $this->redirect("/admin/tag/view/" . $tag->id); } } $this->redirect("/admin/tag/create"); @@ -85,7 +85,7 @@ class TagController extends AdminController if ($tagForm->validate()) { $tag = $tagService->update($tagForm, $tag); if ($tag) { - $this->redirect("/admin/tag/" . $tag->id); + $this->redirect("/admin/tag/view/" . $tag->id); } } $this->redirect("/admin/tag/update/" . $id); diff --git a/kernel/app_modules/tag/controllers/TagEntityController.php b/kernel/app_modules/tag/controllers/TagEntityController.php index 1e727e7..41152ad 100755 --- a/kernel/app_modules/tag/controllers/TagEntityController.php +++ b/kernel/app_modules/tag/controllers/TagEntityController.php @@ -7,6 +7,7 @@ use JetBrains\PhpStorm\NoReturn; use kernel\AdminController; use kernel\app_modules\tag\models\forms\CreateTagForm; use kernel\app_modules\tag\models\Tag; +use kernel\app_modules\tag\models\TagEntity; use kernel\app_modules\tag\services\TagEntityService; use kernel\app_modules\tag\services\TagService; use kernel\helpers\Debug; @@ -22,23 +23,23 @@ class TagEntityController extends AdminController $this->tagEntityService = new TagEntityService(); } - public function actionCreate(): void - { - $this->cgView->render("form.php"); - } - - #[NoReturn] public function actionAdd(): void - { - $tagForm = new CreateTagForm(); - $tagForm->load($_REQUEST); - if ($tagForm->validate()){ - $tag = $this->tagEntityService->create($tagForm); - if ($tag){ - $this->redirect("/admin/tag/" . $tag->id); - } - } - $this->redirect("/admin/tag/create"); - } +// public function actionCreate(): void +// { +// $this->cgView->render("form.php"); +// } +// +// #[NoReturn] public function actionAdd(): void +// { +// $tagForm = new CreateTagForm(); +// $tagForm->load($_REQUEST); +// if ($tagForm->validate()){ +// $tag = $this->tagEntityService->create($tagForm); +// if ($tag){ +// $this->redirect("/admin/tag/" . $tag->id); +// } +// } +// $this->redirect("/admin/tag/create"); +// } public function actionIndex($page_number = 1): void { @@ -50,58 +51,53 @@ class TagEntityController extends AdminController */ public function actionView($id): void { - $tag = Tag::find($id); + $tagEntity = TagEntity::find($id); - if (!$tag){ - throw new Exception(message: "The tag not found"); + if (!$tagEntity){ + throw new Exception(message: "The tag entity not found"); } - $this->cgView->render("view.php", ['tag' => $tag]); + $this->cgView->render("view.php", ['tagEntity' => $tagEntity]); } - /** - * @throws Exception - */ - public function actionUpdate($id): void - { - $model = Tag::find($id); - if (!$model){ - throw new Exception(message: "The tag not found"); - } - - $this->cgView->render("form.php", ['model' => $model]); - } - - /** - * @throws Exception - */ - public function actionEdit($id): void - { - $tag = Tag::find($id); - if (!$tag){ - throw new Exception(message: "The tag not found"); - } - $tagForm = new CreateTagForm(); - $tagService = new TagService(); - $tagForm->load($_REQUEST); - if ($tagForm->validate()) { - $tag = $tagService->update($tagForm, $tag); - if ($tag) { - $this->redirect("/admin/tag/" . $tag->id); - } - } - $this->redirect("/admin/tag/update/" . $id); - } - - #[NoReturn] public function actionDelete($id): void - { - $post = Tag::find($id)->first(); - $post->delete(); - $this->redirect("/admin/tag/"); - } - - public function actionSettings(): void - { - echo "tag settings"; - } +// /** +// * @throws Exception +// */ +// public function actionUpdate($id): void +// { +// $model = Tag::find($id); +// if (!$model){ +// throw new Exception(message: "The tag not found"); +// } +// +// $this->cgView->render("form.php", ['model' => $model]); +// } +// +// /** +// * @throws Exception +// */ +// public function actionEdit($id): void +// { +// $tag = Tag::find($id); +// if (!$tag){ +// throw new Exception(message: "The tag not found"); +// } +// $tagForm = new CreateTagForm(); +// $tagService = new TagService(); +// $tagForm->load($_REQUEST); +// if ($tagForm->validate()) { +// $tag = $tagService->update($tagForm, $tag); +// if ($tag) { +// $this->redirect("/admin/tag/" . $tag->id); +// } +// } +// $this->redirect("/admin/tag/update/" . $id); +// } +// +// #[NoReturn] public function actionDelete($id): void +// { +// $post = Tag::find($id)->first(); +// $post->delete(); +// $this->redirect("/admin/tag/"); +// } } \ No newline at end of file diff --git a/kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php b/kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php index bbbf518..b037292 100755 --- a/kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php +++ b/kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php @@ -14,8 +14,6 @@ return new class extends Migration \kernel\App::$db->schema->create('tag', function (Blueprint $table) { $table->increments('id'); $table->string('label', 255)->nullable(false); - $table->string('entity', 255)->nullable(false); - $table->integer('entity_id', 255)->nullable(false); $table->string('slug', 255)->unique(); $table->integer('status')->default(1); $table->timestamps(); diff --git a/kernel/app_modules/tag/migrations/tag_entity/2024_11_25_122506_create_tag_entity_table.php b/kernel/app_modules/tag/migrations/tag_entity/2024_11_25_122506_create_tag_entity_table.php index 0688679..2006dfd 100644 --- a/kernel/app_modules/tag/migrations/tag_entity/2024_11_25_122506_create_tag_entity_table.php +++ b/kernel/app_modules/tag/migrations/tag_entity/2024_11_25_122506_create_tag_entity_table.php @@ -13,9 +13,9 @@ return new class extends Migration { \kernel\App::$db->schema->create('tag_entity', function (Blueprint $table) { $table->increments('id'); - $table->integer('tag_id', 255)->nullable(false); + $table->integer('tag_id')->nullable(false); $table->string('entity', 255)->nullable(false); - $table->integer('entity_id', 255)->nullable(false); + $table->integer('entity_id')->nullable(false); $table->timestamps(); }); } diff --git a/kernel/app_modules/tag/models/Tag.php b/kernel/app_modules/tag/models/Tag.php index 17fa05f..3ef42c2 100755 --- a/kernel/app_modules/tag/models/Tag.php +++ b/kernel/app_modules/tag/models/Tag.php @@ -19,14 +19,12 @@ class Tag extends Model protected $table = 'tag'; - protected $fillable = ['label', 'entity', 'entity_id', 'slug', 'status']; + protected $fillable = ['label', 'slug', 'status']; public static function labels(): array { return [ 'label' => 'Заголовок', - 'entity' => 'Сущность', - 'entity_id' => 'Идентификатор сущность', 'slug' => 'Slug', 'status' => 'Статус', ]; diff --git a/kernel/app_modules/tag/models/TagEntity.php b/kernel/app_modules/tag/models/TagEntity.php index 2fcaa0b..ff59857 100755 --- a/kernel/app_modules/tag/models/TagEntity.php +++ b/kernel/app_modules/tag/models/TagEntity.php @@ -14,12 +14,12 @@ class TagEntity extends Model { protected $table = 'tag_entity'; - protected $fillable = ['$tag_id', 'entity', 'entity_id']; + protected $fillable = ['tag_id', 'entity', 'entity_id']; public static function labels(): array { return [ - '$tag_id' => 'тег', + 'tag_id' => 'тег', 'entity' => 'Сущность', 'entity_id' => 'Идентификатор сущности', ]; diff --git a/kernel/app_modules/tag/models/forms/CreateTagForm.php b/kernel/app_modules/tag/models/forms/CreateTagForm.php index e657ff3..4a651ac 100755 --- a/kernel/app_modules/tag/models/forms/CreateTagForm.php +++ b/kernel/app_modules/tag/models/forms/CreateTagForm.php @@ -11,8 +11,6 @@ class CreateTagForm extends FormModel { return [ 'label' => 'required|min-str-len:5|max-str-len:30', - 'entity' => 'required|min-str-len:1|max-str-len:50', - 'entity_id' => 'required', 'slug' => '', 'status' => '' ]; diff --git a/kernel/app_modules/tag/routs/tag.php b/kernel/app_modules/tag/routs/tag.php index 91a4746..c17fa96 100755 --- a/kernel/app_modules/tag/routs/tag.php +++ b/kernel/app_modules/tag/routs/tag.php @@ -16,6 +16,16 @@ 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" => "tag_entity"], function (CGRouteCollector $router) { + App::$collector->get('/', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionIndex']); + App::$collector->get('/create', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionCreate']); + App::$collector->post("/", [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionAdd']); + App::$collector->get('view/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionView']); + App::$collector->any('/update/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionDelete']); + }); App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) { App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']); }); diff --git a/kernel/app_modules/tag/routs/tag_entity.php b/kernel/app_modules/tag/routs/tag_entity.php deleted file mode 100755 index 034e745..0000000 --- a/kernel/app_modules/tag/routs/tag_entity.php +++ /dev/null @@ -1,23 +0,0 @@ -group(["prefix" => "admin"], function (CgRouteCollector $router) { - App::$collector->group(["before" => "auth"], function (RouteCollector $router) { - App::$collector->group(["prefix" => "tag_entity"], function (CGRouteCollector $router) { - App::$collector->get('/', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionIndex']); - App::$collector->get('/page/{page_number}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionIndex']); - App::$collector->get('/create', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionCreate']); - App::$collector->post("/", [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionAdd']); - App::$collector->get('/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionView']); - App::$collector->any('/update/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionUpdate']); - App::$collector->any("/edit/{id}", [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionEdit']); - App::$collector->get('/delete/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionDelete']); - }); - App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) { - App::$collector->get('/tag_entity', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionSettings']); - }); - }); -}); \ No newline at end of file diff --git a/kernel/app_modules/tag/services/TagService.php b/kernel/app_modules/tag/services/TagService.php index c92edc4..a057059 100755 --- a/kernel/app_modules/tag/services/TagService.php +++ b/kernel/app_modules/tag/services/TagService.php @@ -13,8 +13,6 @@ class TagService { $model = new Tag(); $model->label = $form_model->getItem('label'); - $model->entity = $form_model->getItem('entity'); - $model->entity_id = $form_model->getItem('entity_id'); $model->status = $form_model->getItem('status'); $model->slug = Slug::createSlug($form_model->getItem('label'), Tag::class); if ($model->save()){ @@ -30,8 +28,6 @@ class TagService $tag->slug = Slug::createSlug($form_model->getItem('label'), Tag::class); } $tag->label = $form_model->getItem('label'); - $tag->entity = $form_model->getItem('entity'); - $tag->entity_id = $form_model->getItem('entity_id'); $tag->status = $form_model->getItem('status'); if ($tag->save()){ diff --git a/kernel/app_modules/tag/views/form.php b/kernel/app_modules/tag/views/tag/form.php similarity index 84% rename from kernel/app_modules/tag/views/form.php rename to kernel/app_modules/tag/views/tag/form.php index 3a37b96..eb408c7 100755 --- a/kernel/app_modules/tag/views/form.php +++ b/kernel/app_modules/tag/views/tag/form.php @@ -16,14 +16,6 @@ $form->field(class: \itguild\forms\inputs\TextInput::class, name: "label", param ->setLabel("Заголовок") ->render(); -$form->field(class: \itguild\forms\inputs\TextInput::class, name: "entity", params: [ - 'class' => "form-control", - 'placeholder' => 'Сущность', - 'value' => $model->entity ?? '' -]) - ->setLabel("Сущность") - ->render(); - $form->field(\itguild\forms\inputs\Select::class, 'status', [ 'class' => "form-control", 'value' => $model->status ?? '' diff --git a/kernel/app_modules/tag/views/index.php b/kernel/app_modules/tag/views/tag/index.php similarity index 100% rename from kernel/app_modules/tag/views/index.php rename to kernel/app_modules/tag/views/tag/index.php diff --git a/kernel/app_modules/tag/views/view.php b/kernel/app_modules/tag/views/tag/view.php similarity index 100% rename from kernel/app_modules/tag/views/view.php rename to kernel/app_modules/tag/views/tag/view.php diff --git a/kernel/app_modules/tag/views/tag_entity/index.php b/kernel/app_modules/tag/views/tag_entity/index.php index 6f59b57..663ee97 100755 --- a/kernel/app_modules/tag/views/tag_entity/index.php +++ b/kernel/app_modules/tag/views/tag_entity/index.php @@ -1,6 +1,5 @@ $page_number, 'perPage' => 8, 'params' => ["class" => "table table-bordered", "border" => "2"], - 'baseUrl' => "/admin/tag", + 'baseUrl' => "/admin/tag_entity", ])); $table->beforePrint(function () { return PrimaryBtn::create("Создать", "/admin/tag_entity/create")->fetch(); }); - $table->columns([ "tag_id" => [ "value" => function ($data) { @@ -35,6 +29,6 @@ $table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class); -\kernel\widgets\ModuleTabsWidget::create()->run(); +\kernel\widgets\TagTabsWidget::create()->run(); $table->create(); $table->render(); \ No newline at end of file diff --git a/kernel/app_modules/tag/views/tag_entity/view.php b/kernel/app_modules/tag/views/tag_entity/view.php new file mode 100755 index 0000000..1e44ccf --- /dev/null +++ b/kernel/app_modules/tag/views/tag_entity/view.php @@ -0,0 +1,30 @@ + ["class" => "table table-bordered", "border" => "2"], + 'baseUrl' => "/admin/tag", +])); +$table->beforePrint(function () use ($tagEntity) { + $btn = PrimaryBtn::create("Список", "/admin/tag_entity")->fetch(); + $btn .= SuccessBtn::create("Редактировать", "/admin/tag_entity/update/" . $tagEntity->id)->fetch(); + $btn .= DangerBtn::create("Удалить", "/admin/tag_entity/delete/" . $tagEntity->id)->fetch(); + return $btn; +}); +$table->rows([ + 'tag_id' => (function ($data) { + return \kernel\app_modules\tag\models\Tag::find($data)->label; + }) +]); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/kernel/console/controllers/AdminConsoleController.php b/kernel/console/controllers/AdminConsoleController.php index 4a2b288..d3b7d90 100644 --- a/kernel/console/controllers/AdminConsoleController.php +++ b/kernel/console/controllers/AdminConsoleController.php @@ -69,7 +69,7 @@ class AdminConsoleController extends ConsoleController $this->optionService->createFromParams( key: "active_modules", - value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\"]}", + value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\", \"post\", \"option\"]}", label: "Активные модули" ); $this->out->r("create option active_modules", "green"); @@ -108,6 +108,25 @@ class AdminConsoleController extends ConsoleController ]); $this->out->r("create item menu user", "green"); + $this->menuService->createItem([ + "label" => "Посты", + "url" => "#", + "slug" => "post", + ]); + $this->menuService->createItem([ + "label" => "Список", + "url" => "/admin/post", + "slug" => "post_list", + "parent_slug" => "post", + ]); + $this->menuService->createItem([ + "label" => "Создать", + "url" => "/admin/post/create", + "slug" => "post_create", + "parent_slug" => "post", + ]); + $this->out->r("create item menu post", "green"); + $this->menuService->createItem([ "label" => "Настройки", "url" => "#", @@ -131,6 +150,13 @@ class AdminConsoleController extends ConsoleController ]); $this->out->r("create item menu admin-themes", "green"); + $this->menuService->createItem([ + "label" => "Опции", + "url" => "/admin/option", + "slug" => "option" + ]); + $this->out->r("create item menu option", "green"); + $user = new CreateUserForm(); $user->load([ 'username' => 'admin', diff --git a/kernel/modules/post/views/form.php b/kernel/modules/post/views/form.php index b3713c6..568e88a 100644 --- a/kernel/modules/post/views/form.php +++ b/kernel/modules/post/views/form.php @@ -34,6 +34,9 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params ->render(); $entityRelations = new \kernel\EntityRelation(); +if (!isset($model)) { + $model = new Post(); +} $entityRelations->renderEntityAdditionalPropertyFormBySlug("post", $model); ?> diff --git a/kernel/views/widgets/menu.php b/kernel/views/widgets/menu.php index 124c239..fe5773b 100644 --- a/kernel/views/widgets/menu.php +++ b/kernel/views/widgets/menu.php @@ -18,7 +18,7 @@ -
  • "> +
  • "> label ?>