From c325b156bde0ef6a0aeb91c5ab778061031f0fc5 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 30 Aug 2024 16:14:31 +0300 Subject: [PATCH] crud menu --- app/controllers/MenuController.php | 106 ++++++++++++++++++ app/controllers/PostController.php | 20 +--- app/controllers/UserController.php | 15 --- app/foo.php | 47 -------- app/models/forms/CreateMenuForm.php | 20 ++++ app/services/MenuService.php | 54 +++++++++ app/services/UserService.php | 10 -- .../columns/menu/MenuDeleteActionColumn.php | 17 +++ .../columns/menu/MenuEditActionColumn.php | 17 +++ .../columns/menu/MenuViewActionColumn.php | 17 +++ .../{ => post}/PostDeleteActionColumn.php | 2 +- .../{ => post}/PostEditActionColumn.php | 2 +- .../{ => post}/PostViewActionColumn.php | 2 +- .../{ => user}/UserDeleteActionColumn.php | 3 +- .../{ => user}/UserEditActionColumn.php | 3 +- .../{ => user}/UserViewActionColumn.php | 2 +- kernel/FormModel.php | 1 - kernel/models/Menu.php | 11 ++ rout.php | 10 ++ views/admin/menu/form.php | 77 +++++++++++++ views/admin/menu/index.php | 49 ++++++++ views/admin/menu/view.php | 46 ++++++++ views/admin/post/form.php | 2 - views/admin/post/index.php | 6 +- views/admin/post/view.php | 14 --- views/admin/user/create.php | 1 - views/admin/user/index.php | 6 +- views/admin/user/view.php | 14 --- 28 files changed, 442 insertions(+), 132 deletions(-) create mode 100644 app/controllers/MenuController.php delete mode 100644 app/foo.php create mode 100644 app/models/forms/CreateMenuForm.php create mode 100644 app/services/MenuService.php create mode 100644 app/tables/columns/menu/MenuDeleteActionColumn.php create mode 100644 app/tables/columns/menu/MenuEditActionColumn.php create mode 100644 app/tables/columns/menu/MenuViewActionColumn.php rename app/tables/columns/{ => post}/PostDeleteActionColumn.php (90%) rename app/tables/columns/{ => post}/PostEditActionColumn.php (90%) rename app/tables/columns/{ => post}/PostViewActionColumn.php (90%) rename app/tables/columns/{ => user}/UserDeleteActionColumn.php (86%) rename app/tables/columns/{ => user}/UserEditActionColumn.php (74%) rename app/tables/columns/{ => user}/UserViewActionColumn.php (90%) create mode 100644 views/admin/menu/form.php create mode 100644 views/admin/menu/index.php create mode 100644 views/admin/menu/view.php delete mode 100644 views/admin/user/create.php diff --git a/app/controllers/MenuController.php b/app/controllers/MenuController.php new file mode 100644 index 0000000..75145bb --- /dev/null +++ b/app/controllers/MenuController.php @@ -0,0 +1,106 @@ +cgView->viewPath = ROOT_DIR . "/views/admin/"; + $this->cgView->layout = "layouts/main.php"; + } + + public function actionCreate(): void + { + $this->cgView->render("menu/form.php"); + } + + #[NoReturn] public function actionAdd(): void + { + $menuForm = new CreateMenuForm(); + $menuService = new MenuService(); + $menuForm->load($_REQUEST); + if ($menuForm->validate()){ + $menuItem = $menuService->create($menuForm); + if ($menuItem){ + $this->redirect("/admin/menu/" . $menuItem->id); + } + } + $this->redirect("/admin/menu/create"); + } + + /** + * @throws Exception + */ + public function actionIndex($page_number = 1): void + { + $this->cgView->render("menu/index.php", ['page_number' => $page_number]); + } + + /** + * @throws Exception + */ + public function actionView($id): void + { + $menuItem = Menu::find($id); + + if (!$menuItem){ + throw new Exception(message: "The menu item not found"); + } + $this->cgView->render("menu/view.php", ['menu' => $menuItem]); + } + + /** + * @throws RuntimeError + * @throws SyntaxError + * @throws LoaderError|Exception + */ + public function actionUpdate($id): void + { + $model = Menu::find($id); + if (!$model){ + throw new Exception(message: "The menu item not found"); + } + + $this->cgView->render("menu/form.php", ['model' => $model]); + } + + /** + * @throws Exception + */ + public function actionEdit($id): void + { + $menuItem = Menu::find($id); + if (!$menuItem){ + throw new Exception(message: "The menu item not found"); + } + $menuForm = new CreateMenuForm(); + $menuService = new MenuService(); + $menuForm->load($_REQUEST); + if ($menuForm->validate()){ + $menuItem = $menuService->update($menuForm, $menuItem); + if ($menuItem){ + $this->redirect("/admin/menu/" . $menuItem->id); + } + } + $this->redirect("/admin/menu/update/" . $id); + } + + #[NoReturn] public function actionDelete($id): void + { + Menu::find($id)->delete(); + $this->redirect("/admin/menu/"); + } + +} \ No newline at end of file diff --git a/app/controllers/PostController.php b/app/controllers/PostController.php index 3357781..1a309da 100644 --- a/app/controllers/PostController.php +++ b/app/controllers/PostController.php @@ -30,16 +30,10 @@ class PostController extends Controller $postForm = new CreatePostForm(); $postService = new PostService(); $postForm->load($_REQUEST); -// Debug::dd($_REQUEST); - if(UserService::check($_REQUEST['user_id'])) { -// Debug::dd(123); - if ($postForm->validate()) { - -// Debug::dd($postForm); - $post = $postService->create($postForm); - if ($post) { - $this->redirect("/admin/post/" . $post->id); - } + if ($postForm->validate()) { + $post = $postService->create($postForm); + if ($post) { + $this->redirect("/admin/post/" . $post->id); } } $this->redirect("/admin/post/create"); @@ -49,11 +43,9 @@ class PostController extends Controller * @throws Exception */ - public function actionIndex(): void + public function actionIndex($page_number = 1): void { - $contents = Post::all(); - - $this->cgView->render("post/index.php", ['contents' => $contents]); + $this->cgView->render("post/index.php", ['page_number' => $page_number]); } /** diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 3470fac..76fa475 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -2,31 +2,16 @@ namespace app\controllers; -use app\foo; -use app\helpers\Debug; use app\models\forms\CreateUserForm; use app\models\Question; use app\models\User; use app\services\UserService; -use app\tables\columns\UserEditActionColumn; -use app\tables\columns\UserViewActionColumn; use Exception; -use http\Message; -use Illuminate\Database\Eloquent\Collection; -use Illuminate\Support\Facades\DB; -use Itguild\Tables\ListJsonTable; -use Itguild\Tables\ViewJsonTable; use JetBrains\PhpStorm\NoReturn; -use kernel\App; use kernel\Controller; -use kernel\IGTabel\btn\PrimaryBtn; -use kernel\IGTabel\EloquentDataProvider; -use kernel\IGTabel\ListJsonTableEloquentCollection; -use kernel\IGTabel\ViewJsonTableEloquentModel; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; -use Twig\TwigFunction; class UserController extends Controller{ protected function init(): void diff --git a/app/foo.php b/app/foo.php deleted file mode 100644 index d006661..0000000 --- a/app/foo.php +++ /dev/null @@ -1,47 +0,0 @@ -informationArray = [ - "meta" => [ - "title" => $title, - "columns" => $columns, - "perPage" => 10, - "currentPage" => 1, - "params" => - [ - "class" => "table table-bordered", - "border" => "1" - ] - ], - "data" => $data - ]; - return $this->toJson($this->informationArray); - } - return null; - } - - /** - * @param array $infArr - * @return string|null - */ - protected function toJson(array $infArr): ?string - { - if ($infArr) - return json_encode($infArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); - return null; - } -} \ No newline at end of file diff --git a/app/models/forms/CreateMenuForm.php b/app/models/forms/CreateMenuForm.php new file mode 100644 index 0000000..df81fc4 --- /dev/null +++ b/app/models/forms/CreateMenuForm.php @@ -0,0 +1,20 @@ + '', + 'icon_file' => '', + 'icon_font' => '', + 'label' => 'required|min-str-len:1|max-str-len:50', + 'url' => 'required|min-str-len:1', + 'status' => '' + ]; + } +} \ No newline at end of file diff --git a/app/services/MenuService.php b/app/services/MenuService.php new file mode 100644 index 0000000..854913a --- /dev/null +++ b/app/services/MenuService.php @@ -0,0 +1,54 @@ +parent_id = $form_model->getItem('parent_id'); + $model->icon_file = $form_model->getItem('icon_file'); + $model->icon_font = $form_model->getItem('icon_font'); + $model->label = $form_model->getItem('label'); + $model->url = $form_model->getItem('url'); + $model->status = $form_model->getItem('status'); + if ($model->save()){ + return $model; + } + + return false; + } + + public function update(FormModel $form_model, Menu $menuItem): false|Menu + { + $menuItem->parent_id = $form_model->getItem('parent_id'); + $menuItem->icon_file = $form_model->getItem('icon_file'); + $menuItem->icon_font = $form_model->getItem('icon_font'); + $menuItem->label = $form_model->getItem('label'); + $menuItem->url = $form_model->getItem('url'); + $menuItem->status = $form_model->getItem('status'); + if ($menuItem->save()){ + return $menuItem; + } + + return false; + } + + public static function createLabelArr(): array + { + foreach (Menu::all()->toArray() as $menuItem) { + $labelArr[$menuItem['id']] = $menuItem['label']; + } + if (!empty($labelArr)) { + return $labelArr; + } + return []; + } + +} \ No newline at end of file diff --git a/app/services/UserService.php b/app/services/UserService.php index cffdbf6..dadac68 100644 --- a/app/services/UserService.php +++ b/app/services/UserService.php @@ -34,16 +34,6 @@ class UserService return false; } - public static function check(int $user_id): bool - { - - if (User::where(['id' => $user_id])->first()) - { - return true; - } - return false; - } - public static function createUsernameArr(): array { foreach (User::all()->toArray() as $user) { diff --git a/app/tables/columns/menu/MenuDeleteActionColumn.php b/app/tables/columns/menu/MenuDeleteActionColumn.php new file mode 100644 index 0000000..b767dae --- /dev/null +++ b/app/tables/columns/menu/MenuDeleteActionColumn.php @@ -0,0 +1,17 @@ +baseUrl . $this->prefix . $this->id; + return " Удалить "; + } +} \ No newline at end of file diff --git a/app/tables/columns/menu/MenuEditActionColumn.php b/app/tables/columns/menu/MenuEditActionColumn.php new file mode 100644 index 0000000..2ffec2e --- /dev/null +++ b/app/tables/columns/menu/MenuEditActionColumn.php @@ -0,0 +1,17 @@ +baseUrl . $this->prefix . $this->id; + return " Редактировать "; + } +} \ No newline at end of file diff --git a/app/tables/columns/menu/MenuViewActionColumn.php b/app/tables/columns/menu/MenuViewActionColumn.php new file mode 100644 index 0000000..802dac0 --- /dev/null +++ b/app/tables/columns/menu/MenuViewActionColumn.php @@ -0,0 +1,17 @@ +baseUrl . $this->prefix . $this->id; + return " Просмотр "; + } +} \ No newline at end of file diff --git a/app/tables/columns/PostDeleteActionColumn.php b/app/tables/columns/post/PostDeleteActionColumn.php similarity index 90% rename from app/tables/columns/PostDeleteActionColumn.php rename to app/tables/columns/post/PostDeleteActionColumn.php index 2db370f..c79d8e7 100644 --- a/app/tables/columns/PostDeleteActionColumn.php +++ b/app/tables/columns/post/PostDeleteActionColumn.php @@ -1,6 +1,6 @@ baseUrl . $this->prefix . $this->id . $this->prefix . "update"; $link = $this->baseUrl . $this->prefix . $this->id; return " Редактировать "; } diff --git a/app/tables/columns/UserViewActionColumn.php b/app/tables/columns/user/UserViewActionColumn.php similarity index 90% rename from app/tables/columns/UserViewActionColumn.php rename to app/tables/columns/user/UserViewActionColumn.php index a7ccdae..74cb479 100644 --- a/app/tables/columns/UserViewActionColumn.php +++ b/app/tables/columns/user/UserViewActionColumn.php @@ -1,6 +1,6 @@ validator->validate($this->data, $this->rules()); } public function getErrors(): array diff --git a/kernel/models/Menu.php b/kernel/models/Menu.php index 4429821..c93b163 100644 --- a/kernel/models/Menu.php +++ b/kernel/models/Menu.php @@ -5,6 +5,17 @@ namespace kernel\models; use app\helpers\Debug; use Illuminate\Database\Eloquent\Model; +/** + * @property int $id + * @property int parent_id + * @property string icon_file + * @property string icon_font + * @property string label + * @property string url + * @property int status + * @method static find($id) + */ + class Menu extends Model { protected $table = 'menu'; diff --git a/rout.php b/rout.php index df83f23..af79fd3 100644 --- a/rout.php +++ b/rout.php @@ -28,4 +28,14 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->any("/edit/{id}", [\app\controllers\PostController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\app\controllers\PostController::class, 'actionDelete']); }); + App::$collector->group(["prefix" => "menu"], function (RouteCollector $router){ + App::$collector->get('/', [\app\controllers\MenuController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\app\controllers\MenuController::class, 'actionIndex']); + App::$collector->get('/create', [\app\controllers\MenuController::class, 'actionCreate']); + App::$collector->post("/", [\app\controllers\MenuController::class, 'actionAdd']); + App::$collector->get('/{id}', [\app\controllers\MenuController::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\controllers\MenuController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\app\controllers\MenuController::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\app\controllers\MenuController::class, 'actionDelete']); + }); }); \ No newline at end of file diff --git a/views/admin/menu/form.php b/views/admin/menu/form.php new file mode 100644 index 0000000..dc7fa19 --- /dev/null +++ b/views/admin/menu/form.php @@ -0,0 +1,77 @@ +beginForm(isset($model) ? "/admin/menu/edit/" . $model->id : "/admin/menu"); + +$form->field(class: \itguild\forms\inputs\Select::class, name: "parent_id", params: [ + 'class' => "form-control", + 'value' => $model->parent_id ?? '' +]) + ->setLabel("Родительский пункт меню") + ->setOptions(\app\services\MenuService::createLabelArr()) + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "icon_file", params: [ + 'class' => "form-control", + 'value' => $model->icon_file ?? '' +]) + ->setLabel("Путь к иконке") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "icon_font", params: [ + 'class' => "form-control", + 'value' => $model->icon_font ?? '' +]) + ->setLabel("Иконка") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "label", params: [ + 'class' => "form-control", + 'value' => $model->label ?? '' +]) + ->setLabel("Заголовок") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "url", params: [ + 'class' => "form-control", + 'value' => $model->url ?? '' +]) + ->setLabel("URL") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "status", params: [ + 'class' => "form-control", + 'value' => strval($model->status) ?? '1' +]) + ->setLabel("Статус") + ->render(); +?> +
+
+ field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [ + 'class' => "btn btn-primary ", + 'value' => 'Отправить', + 'typeInput' => 'submit' + ]) + ->render(); + ?> +
+
+ field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [ + 'class' => "btn btn-warning", + 'value' => 'Сбросить', + 'typeInput' => 'reset' + ]) + ->render(); + ?> +
+
+endForm(); diff --git a/views/admin/menu/index.php b/views/admin/menu/index.php new file mode 100644 index 0000000..9fd78fe --- /dev/null +++ b/views/admin/menu/index.php @@ -0,0 +1,49 @@ + $page_number, + 'perPage' => 3, + 'params' => ["class" => "table table-bordered", "border" => "2"], + 'baseUrl' => "/admin/menu", +])); +$table->columns([ + 'parent_id' => (function ($data) { + if ($data == 0) return null; + return Menu::find($data)->label; + }), + 'created_at' => function ($data) { + if (!$data){ + return null; + } + + return (new DateTimeImmutable($data))->format("d-m-Y"); + }, + 'updated_at' => function ($data) { + if (!$data){ + return null; + } + + return (new DateTimeImmutable($data))->format("d-m-Y"); + } +]); +$table->beforePrint(function () { + return PrimaryBtn::create("Создать", "/admin/menu/create")->fetch(); + //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); +}); +$table->addAction(MenuViewActionColumn::class); +$table->addAction(MenuEditActionColumn::class); +$table->addAction(MenuDeleteActionColumn::class); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/views/admin/menu/view.php b/views/admin/menu/view.php new file mode 100644 index 0000000..92aac76 --- /dev/null +++ b/views/admin/menu/view.php @@ -0,0 +1,46 @@ + ["class" => "table table-bordered", "border" => "2"], + 'baseUrl' => "/admin/menu", +])); +$table->beforePrint(function () use ($menu) { + $btn = PrimaryBtn::create("Список", "/admin/menu")->fetch(); + $btn .= SuccessBtn::create("Редактировать", "/admin/menu/update/" . $menu->id)->fetch(); + $btn .= DangerBtn::create("Удалить", "/admin/menu/delete/" . $menu->id)->fetch(); + return $btn; +}); +$table->rows([ + 'parent_id' => (function ($data) { + if ($data == 0) return null; + return Menu::find($data)->label; + }), + 'created_at' => function ($data) { + if (!$data){ + return null; + } + + return (new DateTimeImmutable($data))->format("d-m-Y"); + }, + 'updated_at' => function ($data) { + if (!$data){ + return null; + } + + return (new DateTimeImmutable($data))->format("d-m-Y"); + } +]); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/views/admin/post/form.php b/views/admin/post/form.php index b784876..114dc38 100644 --- a/views/admin/post/form.php +++ b/views/admin/post/form.php @@ -4,7 +4,6 @@ */ use app\models\Post; -use app\models\User; $form = new \itguild\forms\ActiveForm(); $form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post"); @@ -18,7 +17,6 @@ $form->field(class: \itguild\forms\inputs\TextArea::class, name: "content", para ->setLabel("Пост") ->render(); $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params: [ -//$form->field(class: \itguild\forms\inputs\Select::class, name: "username", params: [ 'class' => "form-control", 'value' => $model->user_id ?? '' ]) diff --git a/views/admin/post/index.php b/views/admin/post/index.php index aa59896..c2b760b 100644 --- a/views/admin/post/index.php +++ b/views/admin/post/index.php @@ -7,9 +7,9 @@ use app\models\Post; use app\models\User; -use app\tables\columns\PostDeleteActionColumn; -use app\tables\columns\PostEditActionColumn; -use app\tables\columns\PostViewActionColumn; +use app\tables\columns\post\PostDeleteActionColumn; +use app\tables\columns\post\PostEditActionColumn; +use app\tables\columns\post\PostViewActionColumn; use Itguild\EloquentTable\EloquentDataProvider; use Itguild\EloquentTable\ListEloquentTable; use kernel\IGTabel\btn\PrimaryBtn; diff --git a/views/admin/post/view.php b/views/admin/post/view.php index 489ad90..410201f 100644 --- a/views/admin/post/view.php +++ b/views/admin/post/view.php @@ -11,20 +11,6 @@ use kernel\IGTabel\btn\DangerBtn; use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\SuccessBtn; -//$dataProvider = new ViewJsonTableEloquentModel($content, [ -// 'params' => ["class" => "table table-bordered", "border" => "2"], -// 'baseUrl' => "/admin/user", -//]); -//$table = new ViewJsonTable($dataProvider->getJson()); -//$table->beforeTable(function () use ($content) { -// $btn = PrimaryBtn::create("Список", "/admin/post")->fetch(); -// $btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $content->id)->fetch(); -// $btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $content->id)->fetch(); -// return $btn; -//}); -//$table->create(); -//$table->render(); - $table = new ViewEloquentTable(new ViewJsonTableEloquentModel($content, [ 'params' => ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/post", diff --git a/views/admin/user/create.php b/views/admin/user/create.php deleted file mode 100644 index b3d9bbc..0000000 --- a/views/admin/user/create.php +++ /dev/null @@ -1 +0,0 @@ - ["class" => "table table-bordered", "border" => "2"], -// 'baseUrl' => "/admin/user", -//]); -//$table = new ViewJsonTable($dataProvider->getJson()); -//$table->beforeTable(function () use ($user) { -// $btn = PrimaryBtn::create("Список", "/admin/user")->fetch(); -// $btn .= SuccessBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch(); -// $btn .= DangerBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch(); -// return $btn; -//}); -//$table->create(); -//$table->render(); - $table = new ViewEloquentTable(new ViewJsonTableEloquentModel($user, [ 'params' => ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/user",