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(); +?> +