From 7489e999ef4dd37ca06f4701a24a24687de8027d Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 27 Dec 2024 15:16:21 +0300 Subject: [PATCH] add profile routs --- .../user/controllers/UserController.php | 42 ++++++++++- kernel/modules/user/routs/user.php | 6 +- kernel/modules/user/views/form_profile.php | 74 +++++++++++++++++++ kernel/modules/user/views/view_profile.php | 56 ++++++++++++++ 4 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 kernel/modules/user/views/form_profile.php create mode 100644 kernel/modules/user/views/view_profile.php diff --git a/kernel/modules/user/controllers/UserController.php b/kernel/modules/user/controllers/UserController.php index ddf0651..60a6028 100644 --- a/kernel/modules/user/controllers/UserController.php +++ b/kernel/modules/user/controllers/UserController.php @@ -150,7 +150,47 @@ class UserController extends AdminController if (!$user){ throw new Exception(message: "The user not found"); } - $this->cgView->render("view.php", ['user' => $user]); + $this->cgView->render("view_profile.php", ['user' => $user]); + } + + public function actionProfileUpdate(): void + { + $model = UserService::getAuthUser(); + if (!$model){ + throw new Exception(message: "The user not found"); + } + + $this->cgView->render("form_profile.php", ['model' => $model]); + } + + public function actionProfileEdit(): void + { + $user = UserService::getAuthUser(); + if (!$user){ + throw new Exception(message: "The user not found"); + } + + $userForm = new CreateUserForm(); + $userService = new UserService(); + $userForm->load($_REQUEST); + + if (isset($_FILES['user_photo']) && $_FILES['user_photo']['error'] === UPLOAD_ERR_OK) { + $file = new FileUpload($_FILES['user_photo'], ['jpg', 'jpeg', 'png']); + $file->upload(); + $userForm->setItem('user_photo', $file->getUploadFile()); + } + + 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/profile"); + } + } + $this->redirect("/admin/user/profile/update"); } } \ No newline at end of file diff --git a/kernel/modules/user/routs/user.php b/kernel/modules/user/routs/user.php index 7360b89..25eda62 100644 --- a/kernel/modules/user/routs/user.php +++ b/kernel/modules/user/routs/user.php @@ -17,7 +17,11 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->any('/update/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionUpdate']); App::$collector->any("/edit/{id}", [\kernel\modules\user\controllers\UserController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionDelete']); - App::$collector->get('/profile', [\kernel\modules\user\controllers\UserController::class, 'actionProfile']); + App::$collector->group(["prefix" => "profile"], callback: function (RouteCollector $router) { + App::$collector->get('/', [\kernel\modules\user\controllers\UserController::class, 'actionProfile']); + App::$collector->get('/update', [\kernel\modules\user\controllers\UserController::class, 'actionProfileUpdate']); + App::$collector->post('/edit', [\kernel\modules\user\controllers\UserController::class, 'actionProfileEdit']); + }); }); }); }); \ No newline at end of file diff --git a/kernel/modules/user/views/form_profile.php b/kernel/modules/user/views/form_profile.php new file mode 100644 index 0000000..0c9ca1a --- /dev/null +++ b/kernel/modules/user/views/form_profile.php @@ -0,0 +1,74 @@ +beginForm("/admin/user/profile/edit", enctype: 'multipart/form-data'); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [ + 'class' => "form-control", + 'placeholder' => 'Логин', + 'value' => $model->username ?? '' +]) + ->setLabel("Логин") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "password", params: [ + 'class' => "form-control", + 'type' => "password", +]) + ->setLabel("Пароль") + ->render(); + +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "email", params: [ + 'class' => "form-control", + 'type' => "email", + 'placeholder' => 'test@mail.ru', + 'value' => $model->email ?? '' +]) + ->setLabel("Email") + ->render(); + +if (!empty($model->user_photo)){ + echo "
"; +} +$form->field(class: \itguild\forms\inputs\File::class, name: "user_photo", params: [ + 'class' => "form-control", + 'value' => $model->user_photo ?? '' +]) + ->setLabel("Фото профиля") + ->render(); + +$entityRelations = new \kernel\EntityRelation(); +if (!isset($model)) { + $model = new User(); +} +$entityRelations->renderEntityAdditionalPropertyFormBySlug("user", $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/kernel/modules/user/views/view_profile.php b/kernel/modules/user/views/view_profile.php new file mode 100644 index 0000000..b9b027f --- /dev/null +++ b/kernel/modules/user/views/view_profile.php @@ -0,0 +1,56 @@ + ["class" => "table table-bordered", "border" => "2"], + 'baseUrl' => "/admin/user", +])); +$table->beforePrint(function () use ($user) { + $btn = IconBtnEditWidget::create(['url' => '/admin/user/profile/edit'])->run(); + $btn .= IconBtnDeleteWidget::create(['url' => '/admin/user/delete/' . $user->id])->run(); + return $btn; +}); + +$entityRelation = new \kernel\EntityRelation(); +$additionals = $entityRelation->getEntityAdditionalProperty("user", $user); + +foreach ($additionals as $key => $additional) { + $table->addRow($key, function () use ($additional) { + return $additional; + }, ['after' => 'email']); +} + +$table->rows([ + 'user_photo' => function ($data) { + return $data ? "" : ""; + }, + '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