diff --git a/app/modules/slider/SliderModule.php b/app/modules/slider/SliderModule.php new file mode 100644 index 0000000..41839b5 --- /dev/null +++ b/app/modules/slider/SliderModule.php @@ -0,0 +1,38 @@ +menuService = new MenuService(); + $this->migrationService = new MigrationService(); + } + + /** + * @throws \Exception + */ + public function init(): void + { + $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/slider/migrations"); + + $this->menuService->createItem([ + "label" => "Слайдер", + "url" => "/admin/slider", + "slug" => "slider", + ]); + } + + public function deactivate(): void + { + $this->menuService->removeItemBySlug("slider"); + } +} \ No newline at end of file diff --git a/app/modules/slider/controllers/SliderController.php b/app/modules/slider/controllers/SliderController.php new file mode 100644 index 0000000..a2f5226 --- /dev/null +++ b/app/modules/slider/controllers/SliderController.php @@ -0,0 +1,35 @@ +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 new file mode 100644 index 0000000..65a4bfe --- /dev/null +++ b/app/modules/slider/manifest.json @@ -0,0 +1,13 @@ +{ + "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/migrations/2024_12_03_124749_create_slider_table.php b/app/modules/slider/migrations/2024_12_03_124749_create_slider_table.php new file mode 100644 index 0000000..0761ec2 --- /dev/null +++ b/app/modules/slider/migrations/2024_12_03_124749_create_slider_table.php @@ -0,0 +1,31 @@ +schema->create('slider', function (Blueprint $table) { + $table->increments('id'); + $table->string('title', 255)->nullable(false); + $table->string('additional_information', 255)->nullable(false); + $table->string('content', 255)->nullable(false); + $table->integer('status')->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + \kernel\App::$db->schema->dropIfExists('slider'); + } +}; diff --git a/app/modules/slider/models/Slider.php b/app/modules/slider/models/Slider.php new file mode 100644 index 0000000..d960dcf --- /dev/null +++ b/app/modules/slider/models/Slider.php @@ -0,0 +1,39 @@ + 'Заголовок', + 'additional_information' => 'Дополнительная информация', + 'content' => 'Контент' + ]; + } + + public static function getStatus(): array + { + return [ + self::DISABLE_STATUS => "Не активный", + self::ACTIVE_STATUS => "Активный", + ]; + } + +} \ No newline at end of file diff --git a/app/modules/slider/routs/slider.php b/app/modules/slider/routs/slider.php new file mode 100644 index 0000000..b25a24c --- /dev/null +++ b/app/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('/', [\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/app/modules/slider/views/index.php b/app/modules/slider/views/index.php new file mode 100644 index 0000000..e652162 --- /dev/null +++ b/app/modules/slider/views/index.php @@ -0,0 +1,43 @@ + $page_number, + 'perPage' => 8, + 'params' => ["class" => "table table-bordered", "border" => "2"], + 'baseUrl' => "/admin/photo", +])); + +$table->beforePrint(function () { + return IconBtnCreateWidget::create(['url' => '/admin/slider/create'])->run(); +}); + +$table->columns([ + "status" => [ + "value" => function ($cell) { + return Slider::getStatus()[$cell]; + }] +]); + +$table->addAction(function($row) { + return IconBtnViewWidget::create(['url' => '/admin/slider/view/' . $row['id']])->run(); +}); +$table->addAction(function($row) { + return IconBtnEditWidget::create(['url' => '/admin/slider/update/' . $row['id']])->run(); +}); +$table->addAction(function($row) { + return IconBtnDeleteWidget::create(['url' => '/admin/slider/delete/' . $row['id']])->run(); +}); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/app/modules/slider/views/view.php b/app/modules/slider/views/view.php new file mode 100644 index 0000000..8c799f6 --- /dev/null +++ b/app/modules/slider/views/view.php @@ -0,0 +1,29 @@ + ["class" => "table table-bordered", "border" => "2"], + '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(); + return $btn; +}); +$table->rows([ + 'status' => (function ($data) { + return \app\modules\slider\models\Slider::getStatus()[$data]; + }) +]); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/kernel/EntityRelation.php b/kernel/EntityRelation.php index 1d118c2..6050f20 100644 --- a/kernel/EntityRelation.php +++ b/kernel/EntityRelation.php @@ -123,7 +123,7 @@ class EntityRelation } } - return false; + return []; } public static function addEntityRelation(string $entity, string $property): bool diff --git a/kernel/FormModel.php b/kernel/FormModel.php index 998d13b..92bc880 100644 --- a/kernel/FormModel.php +++ b/kernel/FormModel.php @@ -59,6 +59,16 @@ class FormModel return false; } + public function validateForUpdate(): bool + { + $res = $this->validator->validate($this->data, $this->rulesForUpdate()); + if (!$res) { + return true; + } + + return false; + } + public function getErrors(): array { return $this->validator->getProcessedErrors(); diff --git a/kernel/modules/user/controllers/UserController.php b/kernel/modules/user/controllers/UserController.php index 47d439f..fdd8534 100644 --- a/kernel/modules/user/controllers/UserController.php +++ b/kernel/modules/user/controllers/UserController.php @@ -5,9 +5,11 @@ namespace kernel\modules\user\controllers; use Exception; use JetBrains\PhpStorm\NoReturn; use kernel\AdminController; +use kernel\EntityRelation; use kernel\modules\user\models\forms\CreateUserForm; use kernel\modules\user\models\User; use kernel\modules\user\service\UserService; +use kernel\Request; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; @@ -35,6 +37,11 @@ class UserController extends AdminController $userForm->load($_REQUEST); if ($userForm->validate()){ $user = $this->userService->create($userForm); + + + $entityRelation = new EntityRelation(); + $entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request()); + if ($user){ $this->redirect("/admin/user/view/" . $user->id); } @@ -91,8 +98,12 @@ class UserController extends AdminController $userForm = new CreateUserForm(); $userService = new UserService(); $userForm->load($_REQUEST); - if ($userForm->validate()){ + if ($userForm->validateForUpdate()){ $user = $userService->update($userForm, $user); + + $entityRelation = new EntityRelation(); + $entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request()); + if ($user){ $this->redirect("/admin/user/view/" . $user->id); } @@ -100,9 +111,20 @@ class UserController extends AdminController $this->redirect("/admin/user/update/" . $id); } + /** + * @throws Exception + */ #[NoReturn] public function actionDelete($id): void { - User::find($id)->delete(); + $user = User::find($id)->first(); + if (!$user){ + throw new Exception(message: "The user not found"); + } + + $entityRelation = new EntityRelation(); + $entityRelation->deleteEntityRelation(entity: "user", model: $user); + + $user->delete(); $this->redirect("/admin/user/"); } diff --git a/kernel/modules/user/models/forms/CreateUserForm.php b/kernel/modules/user/models/forms/CreateUserForm.php index 632cfd9..73c5f77 100644 --- a/kernel/modules/user/models/forms/CreateUserForm.php +++ b/kernel/modules/user/models/forms/CreateUserForm.php @@ -16,4 +16,13 @@ class CreateUserForm extends FormModel ]; } + public function rulesForUpdate(): array + { + return [ + 'username' => 'required|min-str-len:5|max-str-len:30', + 'password' => '', + 'email' => 'required|email' + ]; + } + } \ No newline at end of file diff --git a/kernel/modules/user/service/UserService.php b/kernel/modules/user/service/UserService.php index 8619038..077edab 100644 --- a/kernel/modules/user/service/UserService.php +++ b/kernel/modules/user/service/UserService.php @@ -3,6 +3,7 @@ namespace kernel\modules\user\service; use kernel\FormModel; +use kernel\helpers\Debug; use kernel\modules\user\models\User; class UserService @@ -25,7 +26,9 @@ class UserService { $user->username = $form_model->getItem('username'); $user->email = $form_model->getItem('email'); - $user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT); + if ($form_model->getItem('password')) { + $user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT); + } if ($user->save()){ return $user; } diff --git a/kernel/modules/user/views/form.php b/kernel/modules/user/views/form.php index 66ce4d5..e32d35f 100644 --- a/kernel/modules/user/views/form.php +++ b/kernel/modules/user/views/form.php @@ -6,7 +6,7 @@ use kernel\modules\user\models\User; $form = new \itguild\forms\ActiveForm(); -$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user"); +$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user", enctype: 'multipart/form-data'); $form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [ 'class' => "form-control",