From ccd46636cf65a4628d94ee307014da446a0d408c Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 9 Dec 2024 12:57:12 +0300 Subject: [PATCH] slider CRUD --- .../slider/controllers/SliderController.php | 35 ------ app/modules/slider/manifest.json | 13 -- app/modules/slider/routs/slider.php | 20 --- .../secure/controllers/SecureController.php | 29 ++++- .../secure/models/forms/RegisterForm.php | 19 +++ kernel/modules/secure/routs/secure.php | 2 + kernel/modules/secure/views/login.php | 10 +- kernel/modules/secure/views/register.php | 71 +++++++++++ .../modules/slider/SliderModule.php | 4 +- .../slider/controllers/SliderController.php | 118 ++++++++++++++++++ kernel/modules/slider/manifest.json | 13 ++ .../2024_12_03_124749_create_slider_table.php | 0 .../modules/slider/models/Slider.php | 11 +- .../slider/models/forms/CreateSliderForm.php | 19 +++ kernel/modules/slider/routs/slider.php | 20 +++ .../modules/slider/services/SliderService.php | 39 ++++++ kernel/modules/slider/views/form.php | 85 +++++++++++++ .../modules/slider/views/index.php | 4 +- {app => kernel}/modules/slider/views/view.php | 8 +- 19 files changed, 438 insertions(+), 82 deletions(-) delete mode 100644 app/modules/slider/controllers/SliderController.php delete mode 100644 app/modules/slider/manifest.json delete mode 100644 app/modules/slider/routs/slider.php create mode 100644 kernel/modules/secure/models/forms/RegisterForm.php create mode 100644 kernel/modules/secure/views/register.php rename {app => kernel}/modules/slider/SliderModule.php (86%) create mode 100644 kernel/modules/slider/controllers/SliderController.php create mode 100644 kernel/modules/slider/manifest.json rename {app => kernel}/modules/slider/migrations/2024_12_03_124749_create_slider_table.php (100%) rename {app => kernel}/modules/slider/models/Slider.php (75%) create mode 100644 kernel/modules/slider/models/forms/CreateSliderForm.php create mode 100644 kernel/modules/slider/routs/slider.php create mode 100644 kernel/modules/slider/services/SliderService.php create mode 100644 kernel/modules/slider/views/form.php rename {app => kernel}/modules/slider/views/index.php (93%) rename {app => kernel}/modules/slider/views/view.php (58%) diff --git a/app/modules/slider/controllers/SliderController.php b/app/modules/slider/controllers/SliderController.php deleted file mode 100644 index a2f5226..0000000 --- a/app/modules/slider/controllers/SliderController.php +++ /dev/null @@ -1,35 +0,0 @@ -cgView->viewPath = KERNEL_APP_MODULES_DIR . "/photo/views/"; - } - - - public function actionIndex($page_number = 1): void - { - $this->cgView->render("index.php", ['page_number' => $page_number]); - } - - /** - * @throws Exception - */ - public function actionView($id): void - { - $slide = Slider::find($id); - - if (!$slide){ - throw new Exception(message: "The slide not found"); - } - $this->cgView->render("view.php", ['slider' => $slide]); - } -} \ No newline at end of file diff --git a/app/modules/slider/manifest.json b/app/modules/slider/manifest.json deleted file mode 100644 index 65a4bfe..0000000 --- a/app/modules/slider/manifest.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "Slider", - "version": "0.1", - "author": "ITGuild", - "slug": "slider", - "type": "entity", - "description": "Slider module", - "app_module_path": "{APP}/modules/{slug}", - "module_class": "app\\modules\\slider\\SliderModule", - "module_class_file": "{APP}/modules/slider/SliderModule.php", - "routs": "routs/slider.php", - "dependence": "menu, user" -} diff --git a/app/modules/slider/routs/slider.php b/app/modules/slider/routs/slider.php deleted file mode 100644 index b25a24c..0000000 --- a/app/modules/slider/routs/slider.php +++ /dev/null @@ -1,20 +0,0 @@ -group(["prefix" => "admin"], function (CgRouteCollector $router) { - App::$collector->group(["before" => "auth"], function (RouteCollector $router) { - App::$collector->group(["prefix" => "slider"], function (CGRouteCollector $router) { - App::$collector->get('/', [\app\modules\slider\controllers\SliderController::class, 'actionIndex']); - App::$collector->get('/page/{page_number}', [\app\modules\slider\controllers\SliderController::class, 'actionIndex']); - App::$collector->get('/create', [\app\modules\slider\controllers\SliderController::class, 'actionCreate']); - App::$collector->post("/", [\app\modules\slider\controllers\SliderController::class, 'actionAdd']); - App::$collector->get('/view/{id}', [\app\modules\slider\controllers\SliderController::class, 'actionView']); - App::$collector->any('/update/{id}', [\app\modules\slider\controllers\SliderController::class, 'actionUpdate']); - App::$collector->any("/edit/{id}", [\app\modules\slider\controllers\SliderController::class, 'actionEdit']); - App::$collector->get('/delete/{id}', [\app\modules\slider\controllers\SliderController::class]); - }); - }); -}); \ No newline at end of file diff --git a/kernel/modules/secure/controllers/SecureController.php b/kernel/modules/secure/controllers/SecureController.php index e698424..bdea8ab 100644 --- a/kernel/modules/secure/controllers/SecureController.php +++ b/kernel/modules/secure/controllers/SecureController.php @@ -7,6 +7,7 @@ use kernel\AdminController; use kernel\Flash; use kernel\helpers\Debug; use kernel\modules\secure\models\forms\LoginForm; +use kernel\modules\secure\models\forms\RegisterForm; use kernel\modules\user\service\UserService; class SecureController extends AdminController @@ -16,7 +17,6 @@ class SecureController extends AdminController protected function init(): void { parent::init(); -// $this->cgView->viewPath = KERNEL_DIR . "/views/secure/"; $this->cgView->viewPath = KERNEL_MODULES_DIR. "/secure/views/"; $this->cgView->layout = "/login.php"; $this->userService = new UserService(); @@ -61,4 +61,31 @@ class SecureController extends AdminController $this->redirect("/", code: 302); } + public function actionRegister() + { + $this->cgView->render('register.php'); + } + + public function actionRegistration(): void + { + $regForm = new RegisterForm(); + $regForm->load($_REQUEST); + + if ($this->userService->getByField('username', $regForm->getItem("username"))) { + Flash::setMessage("error", "Username already exists."); + $this->redirect("/admin/register", code: 302); + } + + if ($this->userService->getByField('email', $regForm->getItem("email"))) { + Flash::setMessage("error", "Email already exists."); + $this->redirect("/admin/register", code: 302); + } + + $user = $this->userService->create($regForm); + if ($user){ + setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false); + $this->redirect("/admin", code: 302); + } + } + } \ No newline at end of file diff --git a/kernel/modules/secure/models/forms/RegisterForm.php b/kernel/modules/secure/models/forms/RegisterForm.php new file mode 100644 index 0000000..9d4a18e --- /dev/null +++ b/kernel/modules/secure/models/forms/RegisterForm.php @@ -0,0 +1,19 @@ + 'required|min-str-len:5|max-str-len:50', + 'email' => 'required|email|max-str-len:50', + 'password' => 'required|min-str-len:6|max-str-len:50', + ]; + } + +} \ No newline at end of file diff --git a/kernel/modules/secure/routs/secure.php b/kernel/modules/secure/routs/secure.php index 84b37c3..0377475 100644 --- a/kernel/modules/secure/routs/secure.php +++ b/kernel/modules/secure/routs/secure.php @@ -14,6 +14,8 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->get('/login', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogin']); App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']); App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']); + App::$collector->get('/register', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegister']); + App::$collector->post('/registration', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegistration']); }); App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ diff --git a/kernel/modules/secure/views/login.php b/kernel/modules/secure/views/login.php index c6cd7c3..9503fce 100644 --- a/kernel/modules/secure/views/login.php +++ b/kernel/modules/secure/views/login.php @@ -24,6 +24,9 @@
+
+

Авторизация

+
@@ -48,10 +51,15 @@
-
+ + +
diff --git a/kernel/modules/secure/views/register.php b/kernel/modules/secure/views/register.php new file mode 100644 index 0000000..ecc8752 --- /dev/null +++ b/kernel/modules/secure/views/register.php @@ -0,0 +1,71 @@ + +
+ +
+
+
+ Trendy Pants and Shoes +
+
+
+ +
+

Регистрация

+
+ + + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ +
+
+ + +
+ +
+ + + + + +
+
+
+
+
+ \ No newline at end of file diff --git a/app/modules/slider/SliderModule.php b/kernel/modules/slider/SliderModule.php similarity index 86% rename from app/modules/slider/SliderModule.php rename to kernel/modules/slider/SliderModule.php index 7f7db1f..b8e1ed1 100644 --- a/app/modules/slider/SliderModule.php +++ b/kernel/modules/slider/SliderModule.php @@ -1,6 +1,6 @@ migrationService->runAtPath("{APP}/modules/slider/migrations"); + $this->migrationService->runAtPath("{KERNEL_MODULES}/slider/migrations"); $this->menuService->createItem([ "label" => "Слайдер", diff --git a/kernel/modules/slider/controllers/SliderController.php b/kernel/modules/slider/controllers/SliderController.php new file mode 100644 index 0000000..ca60934 --- /dev/null +++ b/kernel/modules/slider/controllers/SliderController.php @@ -0,0 +1,118 @@ +cgView->viewPath = KERNEL_MODULES_DIR . "/slider/views/"; + $this->sliderService = new SliderService(); + } + + public function actionCreate(): void + { + $this->cgView->render("form.php"); + } + + #[NoReturn] public function actionAdd(): void + { + $sliderForm = new CreateSliderForm(); + $sliderForm->load($_REQUEST); + if ($sliderForm->validate()) { + $slider = $this->sliderService->create($sliderForm); + + $entityRelation = new EntityRelation(); + $entityRelation->saveEntityRelation(entity: "post", model: $slider, request: new Request()); + + if ($slider) { + $this->redirect("/admin/slider/view/" . $slider->id); + } + } + $this->redirect("/admin/slider/create", 302); + } + + public function actionIndex($page_number = 1): void + { + $this->cgView->render("index.php", ['page_number' => $page_number]); + } + + /** + * @throws Exception + */ + public function actionView($id): void + { + $slide = Slider::find($id); + + if (!$slide){ + throw new Exception(message: "The slide not found"); + } + $this->cgView->render("view.php", ['slider' => $slide]); + } + + /** + * @throws Exception + */ + public function actionUpdate(int $id): void + { + $model = Slider::find($id); + if (!$model){ + throw new Exception(message: "The slide not found"); + } + + $this->cgView->render("form.php", ['model' => $model]); + } + + /** + * @throws Exception + */ + public function actionEdit(int $id): void + { + $slide = Slider::find($id); + if (!$slide){ + throw new Exception(message: "The slide not found"); + } + $sliderForm = new CreateSliderForm(); + $sliderForm->load($_REQUEST); + if ($sliderForm->validate()) { + $slide = $this->sliderService->update($sliderForm, $slide); + + $entityRelation = new EntityRelation(); + $entityRelation->saveEntityRelation(entity: "slider", model: $slide, request: new Request()); + + if ($slide) { + $this->redirect("/admin/slider/view/" . $slide->id, 302); + } + } + $this->redirect("/admin/slider/update/" . $id, 302); + } + + /** + * @throws Exception + */ + #[NoReturn] public function actionDelete(int $id): void + { + $slide = Slider::find($id)->first(); + if (!$slide){ + throw new Exception(message: "The slide not found"); + } + + $entityRelation = new EntityRelation(); + $entityRelation->deleteEntityRelation(entity: "slider", model: $slide); + + $slide->delete(); + $this->redirect("/admin/slider/", 302); + } +} \ No newline at end of file diff --git a/kernel/modules/slider/manifest.json b/kernel/modules/slider/manifest.json new file mode 100644 index 0000000..8ad66d5 --- /dev/null +++ b/kernel/modules/slider/manifest.json @@ -0,0 +1,13 @@ +{ + "name": "Slider", + "version": "0.1", + "author": "ITGuild", + "slug": "slider", + "type": "entity", + "description": "Slider module", + "module_class": "kernel\\modules\\slider\\SliderModule", + "module_class_file": "{KERNEL_MODULES}/slider/SliderModule.php", + "routs": "routs/slider.php", + "migration_path": "migrations", + "dependence": "menu" +} diff --git a/app/modules/slider/migrations/2024_12_03_124749_create_slider_table.php b/kernel/modules/slider/migrations/2024_12_03_124749_create_slider_table.php similarity index 100% rename from app/modules/slider/migrations/2024_12_03_124749_create_slider_table.php rename to kernel/modules/slider/migrations/2024_12_03_124749_create_slider_table.php diff --git a/app/modules/slider/models/Slider.php b/kernel/modules/slider/models/Slider.php similarity index 75% rename from app/modules/slider/models/Slider.php rename to kernel/modules/slider/models/Slider.php index d960dcf..26c9b5a 100644 --- a/app/modules/slider/models/Slider.php +++ b/kernel/modules/slider/models/Slider.php @@ -1,14 +1,15 @@ 'Заголовок', + 'content' => 'Контент', + 'link' => 'Ссылка', 'additional_information' => 'Дополнительная информация', - 'content' => 'Контент' + 'status' => 'Статус' ]; } diff --git a/kernel/modules/slider/models/forms/CreateSliderForm.php b/kernel/modules/slider/models/forms/CreateSliderForm.php new file mode 100644 index 0000000..b772d05 --- /dev/null +++ b/kernel/modules/slider/models/forms/CreateSliderForm.php @@ -0,0 +1,19 @@ + 'required|min-str-len:5', + 'additional_information' => 'nullable', + 'content' => 'required|min-str-len:10', + 'link' => '', + 'status' => 'required|integer', + ]; + } +} \ No newline at end of file diff --git a/kernel/modules/slider/routs/slider.php b/kernel/modules/slider/routs/slider.php new file mode 100644 index 0000000..703138a --- /dev/null +++ b/kernel/modules/slider/routs/slider.php @@ -0,0 +1,20 @@ +group(["prefix" => "admin"], function (CgRouteCollector $router) { + App::$collector->group(["before" => "auth"], function (RouteCollector $router) { + App::$collector->group(["prefix" => "slider"], function (CGRouteCollector $router) { + App::$collector->get('/', [\kernel\modules\slider\controllers\SliderController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\kernel\modules\slider\controllers\SliderController::class, 'actionIndex']); + App::$collector->get('/create', [\kernel\modules\slider\controllers\SliderController::class, 'actionCreate']); + App::$collector->post("/", [\kernel\modules\slider\controllers\SliderController::class, 'actionAdd']); + App::$collector->get('/view/{id}', [\kernel\modules\slider\controllers\SliderController::class, 'actionView']); + App::$collector->any('/update/{id}', [\kernel\modules\slider\controllers\SliderController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\kernel\modules\slider\controllers\SliderController::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\kernel\modules\slider\controllers\SliderController::class, 'actionDelete']); + }); + }); +}); \ No newline at end of file diff --git a/kernel/modules/slider/services/SliderService.php b/kernel/modules/slider/services/SliderService.php new file mode 100644 index 0000000..cdbf335 --- /dev/null +++ b/kernel/modules/slider/services/SliderService.php @@ -0,0 +1,39 @@ +title = $form_model->getItem('title'); + $model->content = $form_model->getItem('content'); + $model->additional_information = $form_model->getItem('additional_information'); + $model->link = $form_model->getItem('link'); + if ($model->save()){ + return $model; + } + + return false; + } + + public function update(FormModel $form_model, Slider $slider): false|Slider + { + $slider->title = $form_model->getItem('title'); + $slider->content = $form_model->getItem('content'); + $slider->additional_information = $form_model->getItem('additional_information'); + $slider->link = $form_model->getItem('link'); + if ($slider->save()){ + return $slider; + } + + return false; + } +} \ No newline at end of file diff --git a/kernel/modules/slider/views/form.php b/kernel/modules/slider/views/form.php new file mode 100644 index 0000000..4f2e416 --- /dev/null +++ b/kernel/modules/slider/views/form.php @@ -0,0 +1,85 @@ +beginForm(isset($model) ? "/admin/slider/edit/" . $model->id : "/admin/slider", 'multipart/form-data'); + +$form->field(\itguild\forms\inputs\TextInput::class, 'title', [ + 'class' => "form-control", + 'placeholder' => 'Заголовок', + 'value' => $model->title ?? '' +]) + ->setLabel("Заголовок") + ->render(); + + +$form->field(class: \itguild\forms\inputs\TextArea::class, name: "content", params: [ + 'class' => "form-control", + 'placeholder' => 'Контент', + 'rows' => '10', + 'value' => $model->content ?? '' +]) + ->setLabel("Контент") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "additional_information", params: [ + 'class' => "form-control", + 'placeholder' => 'Дополнительная информация', + 'rows' => '10', + 'value' => $model->additional_information ?? '' +]) + ->setLabel("Дополнительная информация") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "link", params: [ + 'class' => "form-control", + 'placeholder' => 'Ссылка', + 'rows' => '10', + 'value' => $model->link ?? '' +]) + ->setLabel("Ссылка") + ->render(); + +$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [ + 'class' => "form-control", + 'value' => $model->status ?? '' +]) + ->setLabel("Статус") + ->setOptions(Slider::getStatus()) + ->render(); + +$entityRelations = new \kernel\EntityRelation(); +if (!isset($model)) { + $model = new Slider(); +} +$entityRelations->renderEntityAdditionalPropertyFormBySlug("slider", $model); + +?> +
+
+ 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/app/modules/slider/views/index.php b/kernel/modules/slider/views/index.php similarity index 93% rename from app/modules/slider/views/index.php rename to kernel/modules/slider/views/index.php index e652162..1952757 100644 --- a/app/modules/slider/views/index.php +++ b/kernel/modules/slider/views/index.php @@ -3,7 +3,7 @@ * @var int $page_number */ -use app\modules\slider\models\Slider; +use kernel\modules\slider\models\Slider; use Itguild\EloquentTable\EloquentDataProvider; use Itguild\EloquentTable\ListEloquentTable; use kernel\app_modules\photo\models\Photo; @@ -16,7 +16,7 @@ $table = new ListEloquentTable(new EloquentDataProvider(Slider::class, [ 'currentPage' => $page_number, 'perPage' => 8, 'params' => ["class" => "table table-bordered", "border" => "2"], - 'baseUrl' => "/admin/photo", + 'baseUrl' => "/admin/slider", ])); $table->beforePrint(function () { diff --git a/app/modules/slider/views/view.php b/kernel/modules/slider/views/view.php similarity index 58% rename from app/modules/slider/views/view.php rename to kernel/modules/slider/views/view.php index 8c799f6..39c7289 100644 --- a/app/modules/slider/views/view.php +++ b/kernel/modules/slider/views/view.php @@ -15,14 +15,14 @@ $table = new ViewEloquentTable(new ViewJsonTableEloquentModel($slider, [ 'baseUrl' => "/admin/slider", ])); $table->beforePrint(function () use ($slider) { - $btn = PrimaryBtn::create("Список", "/admin/slider")->fetch(); - $btn .= SuccessBtn::create("Редактировать", "/admin/slider/update/" . $slider->id)->fetch(); - $btn .= DangerBtn::create("Удалить", "/admin/slider/delete/" . $slider->id)->fetch(); + $btn = \kernel\widgets\IconBtn\IconBtnListWidget::create(['url' => "/admin/slider"])->run(); + $btn .= \kernel\widgets\IconBtn\IconBtnEditWidget::create(['url' => "/admin/slider/update/" . $slider->id])->run(); + $btn .= \kernel\widgets\IconBtn\IconBtnDeleteWidget::create(['url' => "/admin/slider/delete/" . $slider->id])->run(); return $btn; }); $table->rows([ 'status' => (function ($data) { - return \app\modules\slider\models\Slider::getStatus()[$data]; + return \kernel\modules\slider\models\Slider::getStatus()[$data]; }) ]); $table->create();