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 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)

View File

@ -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, );
}
}

View File

@ -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);

View File

@ -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/");
// }
}

View File

@ -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();

View File

@ -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();
});
}

View File

@ -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' => 'Статус',
];

View File

@ -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' => 'Идентификатор сущности',
];

View File

@ -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' => ''
];

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->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']);
});

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->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()){

View File

@ -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 ?? ''

View File

@ -1,6 +1,5 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $menuItem
* @var int $page_number
*/
@ -8,22 +7,17 @@ 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, [
$table = new ListEloquentTable(new EloquentDataProvider(\kernel\app_modules\tag\models\TagEntity::class, [
'currentPage' => $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();

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(
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',

View File

@ -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);
?>

View File

@ -18,7 +18,7 @@
</ul>
</li>
<?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>
</li>
<?php endif; ?>