From 4920d3b08efb2948bf1b726692907e6b691847ae Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 25 Nov 2024 16:58:01 +0300 Subject: [PATCH] CRUD tag entity --- app/modules/tag/TagModule.php | 5 +- .../tag/controllers/TagEntityController.php | 107 ++++++++++++++++++ .../2024_10_08_093710_create_tag_table.php | 2 +- ...4_11_25_122506_create_tag_entity_table.php | 30 +++++ kernel/app_modules/tag/models/TagEntity.php | 27 +++++ .../tag/models/forms/CreateTagEntityForm.php | 19 ++++ kernel/app_modules/tag/routs/tag.php | 26 +++-- kernel/app_modules/tag/routs/tag_entity.php | 23 ++++ .../tag/services/TagEntityService.php | 39 +++++++ kernel/app_modules/tag/views/index.php | 3 + .../tag/views/tag_entity/index.php | 40 +++++++ kernel/services/MigrationService.php | 3 +- .../widgets/{module_tabs.php => tabs.php} | 0 kernel/widgets/ModuleTabsWidget.php | 2 +- kernel/widgets/TagTabsWidget.php | 18 +++ 15 files changed, 327 insertions(+), 17 deletions(-) create mode 100755 kernel/app_modules/tag/controllers/TagEntityController.php rename kernel/app_modules/tag/migrations/{ => tag}/2024_10_08_093710_create_tag_table.php (92%) create mode 100644 kernel/app_modules/tag/migrations/tag_entity/2024_11_25_122506_create_tag_entity_table.php create mode 100755 kernel/app_modules/tag/models/TagEntity.php create mode 100755 kernel/app_modules/tag/models/forms/CreateTagEntityForm.php create mode 100755 kernel/app_modules/tag/routs/tag_entity.php create mode 100755 kernel/app_modules/tag/services/TagEntityService.php create mode 100755 kernel/app_modules/tag/views/tag_entity/index.php rename kernel/views/widgets/{module_tabs.php => tabs.php} (100%) create mode 100644 kernel/widgets/TagTabsWidget.php diff --git a/app/modules/tag/TagModule.php b/app/modules/tag/TagModule.php index 6f48673..e088e01 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -29,7 +29,8 @@ class TagModule extends Module */ public function init(): void { - $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations"); + $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag"); +// $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag_entity"); $this->menuService->createItem([ "label" => "Тэги", @@ -80,6 +81,6 @@ class TagModule extends Module public function getInputs(string $entity, Model $model) { - Debug::dd($tag); +// Debug::dd($tag); } } \ No newline at end of file diff --git a/kernel/app_modules/tag/controllers/TagEntityController.php b/kernel/app_modules/tag/controllers/TagEntityController.php new file mode 100755 index 0000000..1e727e7 --- /dev/null +++ b/kernel/app_modules/tag/controllers/TagEntityController.php @@ -0,0 +1,107 @@ +cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag_entity/"; + $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 actionIndex($page_number = 1): void + { + $this->cgView->render("index.php", ['page_number' => $page_number]); + } + + /** + * @throws Exception + */ + public function actionView($id): void + { + $tag = Tag::find($id); + + if (!$tag){ + throw new Exception(message: "The tag not found"); + } + $this->cgView->render("view.php", ['tag' => $tag]); + } + + /** + * @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"; + } + +} \ No newline at end of file diff --git a/kernel/app_modules/tag/migrations/2024_10_08_093710_create_tag_table.php b/kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php similarity index 92% rename from kernel/app_modules/tag/migrations/2024_10_08_093710_create_tag_table.php rename to kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php index 84f49d9..bbbf518 100755 --- a/kernel/app_modules/tag/migrations/2024_10_08_093710_create_tag_table.php +++ b/kernel/app_modules/tag/migrations/tag/2024_10_08_093710_create_tag_table.php @@ -15,7 +15,7 @@ return new class extends Migration $table->increments('id'); $table->string('label', 255)->nullable(false); $table->string('entity', 255)->nullable(false); - $table->string('entity_id', 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 new file mode 100644 index 0000000..0688679 --- /dev/null +++ b/kernel/app_modules/tag/migrations/tag_entity/2024_11_25_122506_create_tag_entity_table.php @@ -0,0 +1,30 @@ +schema->create('tag_entity', function (Blueprint $table) { + $table->increments('id'); + $table->integer('tag_id', 255)->nullable(false); + $table->string('entity', 255)->nullable(false); + $table->integer('entity_id', 255)->nullable(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + \kernel\App::$db->schema->dropIfExists('tag_entity'); + } +}; diff --git a/kernel/app_modules/tag/models/TagEntity.php b/kernel/app_modules/tag/models/TagEntity.php new file mode 100755 index 0000000..2fcaa0b --- /dev/null +++ b/kernel/app_modules/tag/models/TagEntity.php @@ -0,0 +1,27 @@ + 'тег', + 'entity' => 'Сущность', + 'entity_id' => 'Идентификатор сущности', + ]; + } +} \ No newline at end of file diff --git a/kernel/app_modules/tag/models/forms/CreateTagEntityForm.php b/kernel/app_modules/tag/models/forms/CreateTagEntityForm.php new file mode 100755 index 0000000..52a029f --- /dev/null +++ b/kernel/app_modules/tag/models/forms/CreateTagEntityForm.php @@ -0,0 +1,19 @@ + 'required', + 'entity' => '', + 'entity_id' => '', + ]; + } + +} \ No newline at end of file diff --git a/kernel/app_modules/tag/routs/tag.php b/kernel/app_modules/tag/routs/tag.php index ed83dbe..91a4746 100755 --- a/kernel/app_modules/tag/routs/tag.php +++ b/kernel/app_modules/tag/routs/tag.php @@ -5,17 +5,19 @@ use kernel\CgRouteCollector; use Phroute\Phroute\RouteCollector; App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) { - App::$collector->group(["prefix" => "tag"], function (CGRouteCollector $router){ - App::$collector->get('/', [\app\modules\tag\controllers\TagController::class, 'actionIndex']); - App::$collector->get('/page/{page_number}', [\app\modules\tag\controllers\TagController::class, 'actionIndex']); - App::$collector->get('/create', [\app\modules\tag\controllers\TagController::class, 'actionCreate']); - App::$collector->post("/", [\app\modules\tag\controllers\TagController::class, 'actionAdd']); - App::$collector->get('/{id}', [\app\modules\tag\controllers\TagController::class, 'actionView']); - App::$collector->any('/update/{id}', [\app\modules\tag\controllers\TagController::class, 'actionUpdate']); - 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']); + App::$collector->group(["before" => "auth"], function (RouteCollector $router) { + App::$collector->group(["prefix" => "tag"], function (CGRouteCollector $router) { + App::$collector->get('/', [\app\modules\tag\controllers\TagController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\app\modules\tag\controllers\TagController::class, 'actionIndex']); + App::$collector->get('/create', [\app\modules\tag\controllers\TagController::class, 'actionCreate']); + App::$collector->post("/", [\app\modules\tag\controllers\TagController::class, 'actionAdd']); + App::$collector->get('/view/{id}', [\app\modules\tag\controllers\TagController::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\modules\tag\controllers\TagController::class, 'actionUpdate']); + 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/app_modules/tag/routs/tag_entity.php b/kernel/app_modules/tag/routs/tag_entity.php new file mode 100755 index 0000000..034e745 --- /dev/null +++ b/kernel/app_modules/tag/routs/tag_entity.php @@ -0,0 +1,23 @@ +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/TagEntityService.php b/kernel/app_modules/tag/services/TagEntityService.php new file mode 100755 index 0000000..ca16103 --- /dev/null +++ b/kernel/app_modules/tag/services/TagEntityService.php @@ -0,0 +1,39 @@ +tag_id = $form_model->getItem('tag_id'); + $model->entity = $form_model->getItem('entity'); + $model->entity_id = $form_model->getItem('entity_id'); + if ($model->save()){ + return $model; + } + + return false; + } + + public function update(FormModel $form_model, TagEntity $tag): false|TagEntity + { + $tag->tag_id = $form_model->getItem('tag_id'); + $tag->entity = $form_model->getItem('entity'); + $tag->entity_id = $form_model->getItem('entity_id'); + + if ($tag->save()){ + return $tag; + } + + return false; + } + +} \ No newline at end of file diff --git a/kernel/app_modules/tag/views/index.php b/kernel/app_modules/tag/views/index.php index 0d5503f..9d46593 100755 --- a/kernel/app_modules/tag/views/index.php +++ b/kernel/app_modules/tag/views/index.php @@ -32,6 +32,9 @@ $table->columns([ }] ]); +\kernel\widgets\TagTabsWidget::create()->run(); + + $table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class); diff --git a/kernel/app_modules/tag/views/tag_entity/index.php b/kernel/app_modules/tag/views/tag_entity/index.php new file mode 100755 index 0000000..6f59b57 --- /dev/null +++ b/kernel/app_modules/tag/views/tag_entity/index.php @@ -0,0 +1,40 @@ + $page_number, + 'perPage' => 8, + 'params' => ["class" => "table table-bordered", "border" => "2"], + 'baseUrl' => "/admin/tag", +])); + +$table->beforePrint(function () { + return PrimaryBtn::create("Создать", "/admin/tag_entity/create")->fetch(); +}); + +$table->columns([ + "tag_id" => [ + "value" => function ($data) { + return Tag::find($data)->label; + }] +]); + +$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(); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/kernel/services/MigrationService.php b/kernel/services/MigrationService.php index 8205fa7..aabee7b 100644 --- a/kernel/services/MigrationService.php +++ b/kernel/services/MigrationService.php @@ -23,6 +23,7 @@ class MigrationService public function runAtPath(string $path = ROOT_DIR . '/migrations'): array { $path = getConst($path); +// Debug::dd($path); try { $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); @@ -31,7 +32,7 @@ class MigrationService return $m->run($path); } catch (\Exception $e) { - throw new \Exception('Не удалось поднять играции'); + throw new \Exception('Не удалось поднять миграции'); } } diff --git a/kernel/views/widgets/module_tabs.php b/kernel/views/widgets/tabs.php similarity index 100% rename from kernel/views/widgets/module_tabs.php rename to kernel/views/widgets/tabs.php diff --git a/kernel/widgets/ModuleTabsWidget.php b/kernel/widgets/ModuleTabsWidget.php index ef41ec2..d740dcf 100644 --- a/kernel/widgets/ModuleTabsWidget.php +++ b/kernel/widgets/ModuleTabsWidget.php @@ -13,6 +13,6 @@ class ModuleTabsWidget extends Widget '/admin' => 'Локальные', '/admin/module_shop_client' => 'Каталог' ]; - $this->cgView->render('/module_tabs.php', ['tabs' => $tabs]); + $this->cgView->render('/tabs.php', ['tabs' => $tabs]); } } \ No newline at end of file diff --git a/kernel/widgets/TagTabsWidget.php b/kernel/widgets/TagTabsWidget.php new file mode 100644 index 0000000..a569dda --- /dev/null +++ b/kernel/widgets/TagTabsWidget.php @@ -0,0 +1,18 @@ + 'tag', + '/admin/tag_entity' => 'tag entity' + ]; + $this->cgView->render('/tabs.php', ['tabs' => $tabs]); + } +} \ No newline at end of file