crud tag entity, some fix tag

This commit is contained in:
Билай Станислав 2024-11-27 14:01:21 +03:00
parent 4920d3b08e
commit 8a9f156235
20 changed files with 155 additions and 133 deletions

View File

@ -5,6 +5,7 @@ namespace app\modules\tag;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\TextInputBuilder; use itguild\forms\builders\TextInputBuilder;
use kernel\app_modules\tag\models\Tag; use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\models\TagEntity;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\helpers\Slug; use kernel\helpers\Slug;
use kernel\Module; use kernel\Module;
@ -30,7 +31,7 @@ class TagModule extends Module
public function init(): void public function init(): void
{ {
$this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag"); $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([ $this->menuService->createItem([
"label" => "Тэги", "label" => "Тэги",
@ -51,17 +52,16 @@ class TagModule extends Module
public function deactivate(): void public function deactivate(): void
{ {
$this->menuService->removeItemBySlug("tag"); $this->menuService->removeItemBySlug("tag");
$this->menuService->removeItemBySlug("tag_settings");
OptionService::removeOptionByKey("entity_tag_list"); OptionService::removeOptionByKey("entity_tag_list");
} }
public function formInputs(string $entity, Model $model = null): void public function formInputs(string $entity, Model $model = null): void
{ {
$tag = Tag::where("entity", $entity)->where("entity_id", $model->id)->first();
$input = TextInputBuilder::build("tag", [ $input = TextInputBuilder::build("tag", [
'class' => 'form-control', 'class' => 'form-control',
'placeholder' => 'Теги', 'placeholder' => 'Теги',
'value' => $tag->label ?? "" 'value' => Tag::find($model->id)->label ?? ""
]); ]);
$input->setLabel("Теги"); $input->setLabel("Теги");
$input->create()->render(); $input->create()->render();
@ -70,12 +70,16 @@ class TagModule extends Module
public function saveInputs(string $entity, Model $model, Request $request): void public function saveInputs(string $entity, Model $model, Request $request): void
{ {
$tag = new Tag(); $tag = new Tag();
$tag->entity = $entity;
$tag->entity_id = $model->id;
$tag->label = $request->post('tag'); $tag->label = $request->post('tag');
$tag->status = Tag::ACTIVE_STATUS; $tag->status = Tag::ACTIVE_STATUS;
$tag->slug = Slug::createSlug($request->post('tag'), $model); $tag->slug = Slug::createSlug($request->post('tag'), $model);
$tag->save(); $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) public function getInputs(string $entity, Model $model)

View File

@ -171,7 +171,7 @@ class EntityRelation
{ {
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug); $moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
if ($moduleClass and method_exists($moduleClass, "saveInputs")) { if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
$moduleClass->saveInputs($entity, $model, $request); $moduleClass->saveInputs($entity, $model, $request, );
} }
} }

View File

@ -17,7 +17,7 @@ class TagController extends AdminController
protected function init(): void protected function init(): void
{ {
parent::init(); 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(); $this->tagService = new TagService();
} }
@ -33,7 +33,7 @@ class TagController extends AdminController
if ($tagForm->validate()){ if ($tagForm->validate()){
$tag = $this->tagService->create($tagForm); $tag = $this->tagService->create($tagForm);
if ($tag){ if ($tag){
$this->redirect("/admin/tag/" . $tag->id); $this->redirect("/admin/tag/view/" . $tag->id);
} }
} }
$this->redirect("/admin/tag/create"); $this->redirect("/admin/tag/create");
@ -85,7 +85,7 @@ class TagController extends AdminController
if ($tagForm->validate()) { if ($tagForm->validate()) {
$tag = $tagService->update($tagForm, $tag); $tag = $tagService->update($tagForm, $tag);
if ($tag) { if ($tag) {
$this->redirect("/admin/tag/" . $tag->id); $this->redirect("/admin/tag/view/" . $tag->id);
} }
} }
$this->redirect("/admin/tag/update/" . $id); $this->redirect("/admin/tag/update/" . $id);

View File

@ -7,6 +7,7 @@ use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController; use kernel\AdminController;
use kernel\app_modules\tag\models\forms\CreateTagForm; use kernel\app_modules\tag\models\forms\CreateTagForm;
use kernel\app_modules\tag\models\Tag; 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\TagEntityService;
use kernel\app_modules\tag\services\TagService; use kernel\app_modules\tag\services\TagService;
use kernel\helpers\Debug; use kernel\helpers\Debug;
@ -22,23 +23,23 @@ class TagEntityController extends AdminController
$this->tagEntityService = new TagEntityService(); $this->tagEntityService = new TagEntityService();
} }
public function actionCreate(): void // public function actionCreate(): void
{ // {
$this->cgView->render("form.php"); // $this->cgView->render("form.php");
} // }
//
#[NoReturn] public function actionAdd(): void // #[NoReturn] public function actionAdd(): void
{ // {
$tagForm = new CreateTagForm(); // $tagForm = new CreateTagForm();
$tagForm->load($_REQUEST); // $tagForm->load($_REQUEST);
if ($tagForm->validate()){ // if ($tagForm->validate()){
$tag = $this->tagEntityService->create($tagForm); // $tag = $this->tagEntityService->create($tagForm);
if ($tag){ // if ($tag){
$this->redirect("/admin/tag/" . $tag->id); // $this->redirect("/admin/tag/" . $tag->id);
} // }
} // }
$this->redirect("/admin/tag/create"); // $this->redirect("/admin/tag/create");
} // }
public function actionIndex($page_number = 1): void public function actionIndex($page_number = 1): void
{ {
@ -50,58 +51,53 @@ class TagEntityController extends AdminController
*/ */
public function actionView($id): void public function actionView($id): void
{ {
$tag = Tag::find($id); $tagEntity = TagEntity::find($id);
if (!$tag){ if (!$tagEntity){
throw new Exception(message: "The tag not found"); 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 // * @throws Exception
*/ // */
public function actionUpdate($id): void // public function actionUpdate($id): void
{ // {
$model = Tag::find($id); // $model = Tag::find($id);
if (!$model){ // if (!$model){
throw new Exception(message: "The tag not found"); // throw new Exception(message: "The tag not found");
} // }
//
$this->cgView->render("form.php", ['model' => $model]); // $this->cgView->render("form.php", ['model' => $model]);
} // }
//
/** // /**
* @throws Exception // * @throws Exception
*/ // */
public function actionEdit($id): void // public function actionEdit($id): void
{ // {
$tag = Tag::find($id); // $tag = Tag::find($id);
if (!$tag){ // if (!$tag){
throw new Exception(message: "The tag not found"); // throw new Exception(message: "The tag not found");
} // }
$tagForm = new CreateTagForm(); // $tagForm = new CreateTagForm();
$tagService = new TagService(); // $tagService = new TagService();
$tagForm->load($_REQUEST); // $tagForm->load($_REQUEST);
if ($tagForm->validate()) { // if ($tagForm->validate()) {
$tag = $tagService->update($tagForm, $tag); // $tag = $tagService->update($tagForm, $tag);
if ($tag) { // if ($tag) {
$this->redirect("/admin/tag/" . $tag->id); // $this->redirect("/admin/tag/" . $tag->id);
} // }
} // }
$this->redirect("/admin/tag/update/" . $id); // $this->redirect("/admin/tag/update/" . $id);
} // }
//
#[NoReturn] public function actionDelete($id): void // #[NoReturn] public function actionDelete($id): void
{ // {
$post = Tag::find($id)->first(); // $post = Tag::find($id)->first();
$post->delete(); // $post->delete();
$this->redirect("/admin/tag/"); // $this->redirect("/admin/tag/");
} // }
public function actionSettings(): void
{
echo "tag settings";
}
} }

View File

@ -14,8 +14,6 @@ return new class extends Migration
\kernel\App::$db->schema->create('tag', function (Blueprint $table) { \kernel\App::$db->schema->create('tag', function (Blueprint $table) {
$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->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

@ -13,9 +13,9 @@ return new class extends Migration
{ {
\kernel\App::$db->schema->create('tag_entity', function (Blueprint $table) { \kernel\App::$db->schema->create('tag_entity', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->integer('tag_id', 255)->nullable(false); $table->integer('tag_id')->nullable(false);
$table->string('entity', 255)->nullable(false); $table->string('entity', 255)->nullable(false);
$table->integer('entity_id', 255)->nullable(false); $table->integer('entity_id')->nullable(false);
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -19,14 +19,12 @@ class Tag extends Model
protected $table = 'tag'; protected $table = 'tag';
protected $fillable = ['label', 'entity', 'entity_id', 'slug', 'status']; protected $fillable = ['label', 'slug', 'status'];
public static function labels(): array public static function labels(): array
{ {
return [ return [
'label' => 'Заголовок', 'label' => 'Заголовок',
'entity' => 'Сущность',
'entity_id' => 'Идентификатор сущность',
'slug' => 'Slug', 'slug' => 'Slug',
'status' => 'Статус', 'status' => 'Статус',
]; ];

View File

@ -14,12 +14,12 @@ class TagEntity extends Model
{ {
protected $table = 'tag_entity'; protected $table = 'tag_entity';
protected $fillable = ['$tag_id', 'entity', 'entity_id']; protected $fillable = ['tag_id', 'entity', 'entity_id'];
public static function labels(): array public static function labels(): array
{ {
return [ return [
'$tag_id' => 'тег', 'tag_id' => 'тег',
'entity' => 'Сущность', 'entity' => 'Сущность',
'entity_id' => 'Идентификатор сущности', 'entity_id' => 'Идентификатор сущности',
]; ];

View File

@ -11,8 +11,6 @@ class CreateTagForm extends FormModel
{ {
return [ return [
'label' => 'required|min-str-len:5|max-str-len:30', 'label' => 'required|min-str-len:5|max-str-len:30',
'entity' => 'required|min-str-len:1|max-str-len:50',
'entity_id' => 'required',
'slug' => '', 'slug' => '',
'status' => '' 'status' => ''
]; ];

View File

@ -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->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" => "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->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

@ -1,23 +0,0 @@
<?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

@ -13,8 +13,6 @@ class TagService
{ {
$model = new Tag(); $model = new Tag();
$model->label = $form_model->getItem('label'); $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->status = $form_model->getItem('status');
$model->slug = Slug::createSlug($form_model->getItem('label'), Tag::class); $model->slug = Slug::createSlug($form_model->getItem('label'), Tag::class);
if ($model->save()){ if ($model->save()){
@ -30,8 +28,6 @@ class TagService
$tag->slug = Slug::createSlug($form_model->getItem('label'), Tag::class); $tag->slug = Slug::createSlug($form_model->getItem('label'), Tag::class);
} }
$tag->label = $form_model->getItem('label'); $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'); $tag->status = $form_model->getItem('status');
if ($tag->save()){ if ($tag->save()){

View File

@ -16,14 +16,6 @@ $form->field(class: \itguild\forms\inputs\TextInput::class, name: "label", param
->setLabel("Заголовок") ->setLabel("Заголовок")
->render(); ->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', [ $form->field(\itguild\forms\inputs\Select::class, 'status', [
'class' => "form-control", 'class' => "form-control",
'value' => $model->status ?? '' 'value' => $model->status ?? ''

View File

@ -1,6 +1,5 @@
<?php <?php
/** /**
* @var \Illuminate\Database\Eloquent\Collection $menuItem
* @var int $page_number * @var int $page_number
*/ */
@ -8,22 +7,17 @@ use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable; use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\tag\models\Tag; use kernel\app_modules\tag\models\Tag;
use kernel\IGTabel\btn\PrimaryBtn; 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, [ $table = new ListEloquentTable(new EloquentDataProvider(\kernel\app_modules\tag\models\TagEntity::class, [
'currentPage' => $page_number, 'currentPage' => $page_number,
'perPage' => 8, 'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tag", 'baseUrl' => "/admin/tag_entity",
])); ]));
$table->beforePrint(function () { $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/tag_entity/create")->fetch(); return PrimaryBtn::create("Создать", "/admin/tag_entity/create")->fetch();
}); });
$table->columns([ $table->columns([
"tag_id" => [ "tag_id" => [
"value" => function ($data) { "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\DeleteActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class); $table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class);
\kernel\widgets\ModuleTabsWidget::create()->run(); \kernel\widgets\TagTabsWidget::create()->run();
$table->create(); $table->create();
$table->render(); $table->render();

View File

@ -0,0 +1,30 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $tagEntity
*/
use kernel\modules\user\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($tagEntity, [
'params' => ["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();

View File

@ -69,7 +69,7 @@ class AdminConsoleController extends ConsoleController
$this->optionService->createFromParams( $this->optionService->createFromParams(
key: "active_modules", key: "active_modules",
value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\"]}", value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\", \"post\", \"option\"]}",
label: "Активные модули" label: "Активные модули"
); );
$this->out->r("create option active_modules", "green"); $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->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([ $this->menuService->createItem([
"label" => "Настройки", "label" => "Настройки",
"url" => "#", "url" => "#",
@ -131,6 +150,13 @@ class AdminConsoleController extends ConsoleController
]); ]);
$this->out->r("create item menu admin-themes", "green"); $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 = new CreateUserForm();
$user->load([ $user->load([
'username' => 'admin', 'username' => 'admin',

View File

@ -34,6 +34,9 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params
->render(); ->render();
$entityRelations = new \kernel\EntityRelation(); $entityRelations = new \kernel\EntityRelation();
if (!isset($model)) {
$model = new Post();
}
$entityRelations->renderEntityAdditionalPropertyFormBySlug("post", $model); $entityRelations->renderEntityAdditionalPropertyFormBySlug("post", $model);
?> ?>

View File

@ -18,7 +18,7 @@
</ul> </ul>
</li> </li>
<?php else: ?> <?php else: ?>
<li class="<?= $item->url === \kernel\Request::getUrlPath() || $item->url . "/module_shop_client" === \kernel\Request::getUrlPath() ? "active" : "" ?>"> <li class="<?= $item->url === \kernel\Request::getUrlPath() ? "active" : "" ?>">
<a href="<?= $item->url ?>"><?= $item->label ?></a> <a href="<?= $item->url ?>"><?= $item->label ?></a>
</li> </li>
<?php endif; ?> <?php endif; ?>