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