CRUD tag entity

This commit is contained in:
Билай Станислав 2024-11-25 16:58:01 +03:00
parent 745707ee09
commit 4920d3b08e
15 changed files with 327 additions and 17 deletions

View File

@ -29,7 +29,8 @@ class TagModule extends Module
*/ */
public function init(): void 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([ $this->menuService->createItem([
"label" => "Тэги", "label" => "Тэги",
@ -80,6 +81,6 @@ class TagModule extends Module
public function getInputs(string $entity, Model $model) public function getInputs(string $entity, Model $model)
{ {
Debug::dd($tag); // Debug::dd($tag);
} }
} }

View File

@ -0,0 +1,107 @@
<?php
namespace kernel\app_modules\tag\controllers;
use Exception;
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\services\TagEntityService;
use kernel\app_modules\tag\services\TagService;
use kernel\helpers\Debug;
use kernel\modules\menu\service\MenuService;
class TagEntityController extends AdminController
{
private TagEntityService $tagEntityService;
protected function init(): void
{
parent::init();
$this->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";
}
}

View File

@ -15,7 +15,7 @@ return new class extends Migration
$table->increments('id'); $table->increments('id');
$table->string('label', 255)->nullable(false); $table->string('label', 255)->nullable(false);
$table->string('entity', 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->string('slug', 255)->unique();
$table->integer('status')->default(1); $table->integer('status')->default(1);
$table->timestamps(); $table->timestamps();

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->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');
}
};

View File

@ -0,0 +1,27 @@
<?php
namespace kernel\app_modules\tag\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property int $tag_id
* @property string $entity
* @property int $entity_id
*/
class TagEntity extends Model
{
protected $table = 'tag_entity';
protected $fillable = ['$tag_id', 'entity', 'entity_id'];
public static function labels(): array
{
return [
'$tag_id' => 'тег',
'entity' => 'Сущность',
'entity_id' => 'Идентификатор сущности',
];
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace kernel\app_modules\tag\models\forms;
use kernel\FormModel;
class CreateTagEntityForm extends FormModel
{
public function rules(): array
{
return [
'tag_id' => 'required',
'entity' => '',
'entity_id' => '',
];
}
}

View File

@ -5,17 +5,19 @@ use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector; use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) { App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
App::$collector->group(["prefix" => "tag"], function (CGRouteCollector $router){ 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('/', [\app\modules\tag\controllers\TagController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\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->get('/create', [\app\modules\tag\controllers\TagController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\tag\controllers\TagController::class, 'actionAdd']); App::$collector->post("/", [\app\modules\tag\controllers\TagController::class, 'actionAdd']);
App::$collector->get('/{id}', [\app\modules\tag\controllers\TagController::class, 'actionView']); 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('/update/{id}', [\app\modules\tag\controllers\TagController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\tag\controllers\TagController::class, 'actionEdit']); 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->get('/delete/{id}', [\app\modules\tag\controllers\TagController::class, 'actionDelete']);
}); });
App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router){ App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) {
App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']); App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']);
}); });
});
}); });

View File

@ -0,0 +1,23 @@
<?php
use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector;
App::$collector->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']);
});
});
});

View File

@ -0,0 +1,39 @@
<?php
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\Slug;
class TagEntityService
{
public function create(FormModel $form_model): false|TagEntity
{
$model = new TagEntity();
$model->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;
}
}

View File

@ -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\ViewActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class);

View File

@ -0,0 +1,40 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $menuItem
* @var int $page_number
*/
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\tag\models\Tag;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\models\Menu;
use kernel\modules\menu\table\columns\MenuDeleteActionColumn;
use kernel\modules\menu\table\columns\MenuEditActionColumn;
use kernel\modules\menu\table\columns\MenuViewActionColumn;
$table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [
'currentPage' => $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();

View File

@ -23,6 +23,7 @@ class MigrationService
public function runAtPath(string $path = ROOT_DIR . '/migrations'): array public function runAtPath(string $path = ROOT_DIR . '/migrations'): array
{ {
$path = getConst($path); $path = getConst($path);
// Debug::dd($path);
try { try {
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
@ -31,7 +32,7 @@ class MigrationService
return $m->run($path); return $m->run($path);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Exception('Не удалось поднять играции'); throw new \Exception('Не удалось поднять миграции');
} }
} }

View File

@ -13,6 +13,6 @@ class ModuleTabsWidget extends Widget
'/admin' => 'Локальные', '/admin' => 'Локальные',
'/admin/module_shop_client' => 'Каталог' '/admin/module_shop_client' => 'Каталог'
]; ];
$this->cgView->render('/module_tabs.php', ['tabs' => $tabs]); $this->cgView->render('/tabs.php', ['tabs' => $tabs]);
} }
} }

View File

@ -0,0 +1,18 @@
<?php
namespace kernel\widgets;
use kernel\helpers\Debug;
use kernel\Widget;
class TagTabsWidget extends Widget
{
public function run(): void
{
$tabs = [
'/admin/tag' => 'tag',
'/admin/tag_entity' => 'tag entity'
];
$this->cgView->render('/tabs.php', ['tabs' => $tabs]);
}
}