From 653bf674c9ba022bbb6d0a24fc28d07ba7ea3849 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 25 Jul 2024 13:23:50 +0300 Subject: [PATCH] user crud --- app/controllers/UserController.php | 55 +++++++-------- app/models/User.php | 1 + app/services/UserService.php | 12 ++++ composer.json | 3 +- composer.lock | 107 ++++++++++++++++++++++++++++- kernel/IGTabel/btn/DangerBtn.php | 24 +++++++ kernel/IGTabel/btn/SuccessBtn.php | 24 +++++++ views/admin/user/form.php | 72 ++++++++++++++----- views/admin/user/view.php | 6 +- 9 files changed, 255 insertions(+), 49 deletions(-) create mode 100644 kernel/IGTabel/btn/DangerBtn.php create mode 100644 kernel/IGTabel/btn/SuccessBtn.php diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index a478284..f2d2c8d 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -50,6 +50,8 @@ class UserController extends Controller{ $this->redirect("/admin/user/create"); } + + public function actionQuestionCount($user_id) { return Question::where('user_id', $user_id)->count(); @@ -99,41 +101,36 @@ class UserController extends Controller{ */ public function actionUpdate($id): void { - $this->cgView->render("user/form.php"); + $model = User::find($id); + if (!$model){ + throw new Exception(message: "The user not found"); + } + + $this->cgView->render("user/form.php", ['model' => $model]); } + /** + * @throws Exception + */ public function actionEdit($id): void { -// $_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT); -// -// $user = User::find($_REQUEST['id']); -// $user->username = $_REQUEST['username']; -// $user->email = $_REQUEST['email']; -// $user->password_hash = $_REQUEST['password_hash']; -// $user->save(); - -// $user = User::find($id); -// if (!$user){ -// throw new Exception(message: "The user not found"); -// } -// $userForm = new CreateUserForm(); -// $userService = new UserService(); -// $userForm->load($_REQUEST); -//// Debug::prn($userForm->validate()); -//// Debug::dd($userForm->getErrors()); -// if ($userForm->validate()){ -//// Debug::prn($userService); -// -// $userService->create($userForm); -// $this->redirect("/admin/user/" . User::find($id)['id']); -// } -// else -// { -// $this->redirect("/admin/user/update/" . $id); -// } + $user = User::find($id); + if (!$user){ + throw new Exception(message: "The user not found"); + } + $userForm = new CreateUserForm(); + $userService = new UserService(); + $userForm->load($_REQUEST); + if ($userForm->validate()){ + $user = $userService->update($userForm, $user); + if ($user){ + $this->redirect("/admin/user/" . $user->id); + } + } + $this->redirect("/admin/user/update/" . $id); } - public function actionDelete($id): void + #[NoReturn] public function actionDelete($id): void { User::find($id)->delete(); $this->redirect("/admin/user/"); diff --git a/app/models/User.php b/app/models/User.php index 40622fd..546cc93 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -3,6 +3,7 @@ namespace app\models; use Illuminate\Database\Eloquent\Model; /** + * @property int $id * @property string $username * @property string $email * @property string $password_hash diff --git a/app/services/UserService.php b/app/services/UserService.php index 3ed5c67..1cae793 100644 --- a/app/services/UserService.php +++ b/app/services/UserService.php @@ -21,4 +21,16 @@ class UserService return false; } + public function update(FormModel $form_model, User $user): false|User + { + $user->username = $form_model->getItem('username'); + $user->email = $form_model->getItem('email'); + $user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT); + if ($user->save()){ + return $user; + } + + return false; + } + } \ No newline at end of file diff --git a/composer.json b/composer.json index 9b8953e..d43b17a 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "twig/twig": "^3.0", "twbs/bootstrap": "5.0.2", "itguild/tables": "^0.1.3", - "madesimple/php-form-validator": "^2.9" + "madesimple/php-form-validator": "^2.9", + "itguild/forms": "^0.1.1" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 3437802..01e9fd7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f638e86e2b30657f939e5afa8d66beb9", + "content-hash": "6f3aaa23fb01733bc2ac85dfaaf0dec3", "packages": [ { "name": "brick/math", @@ -723,6 +723,65 @@ }, "time": "2024-07-16T13:48:58+00:00" }, + { + "name": "itguild/forms", + "version": "0.1.1", + "source": { + "type": "git", + "url": "https://git.itguild.info/ItGuild/forms_bundle.git", + "reference": "0f00069165f8eae4271aed6cffdb4b4ecb89c1b8" + }, + "require": { + "itguild/php-cg-select-v2": "^0.1.0", + "rakit/validation": "^1.4", + "twbs/bootstrap": "5.0.2", + "vlucas/phpdotenv": "^5.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "itguild\\forms\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kavalar", + "email": "apuc06@mail.ru" + } + ], + "time": "2024-07-25T09:37:21+00:00" + }, + { + "name": "itguild/php-cg-select-v2", + "version": "v0.1", + "source": { + "type": "git", + "url": "https://git.itguild.info/apuc/php-cg-select-v2", + "reference": "30c7844d96efaaab35b125db533a064f2254e169" + }, + "type": "library", + "autoload": { + "psr-4": { + "Itguild\\PhpCgSelectV2\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "ISC" + ], + "authors": [ + { + "name": "Kavalar", + "email": "apuc06@mail.ru" + } + ], + "description": "Wrapper for CG-Select", + "time": "2023-06-07T22:35:51+00:00" + }, { "name": "itguild/tables", "version": "0.1.6", @@ -1169,6 +1228,52 @@ }, "time": "2021-10-29T13:26:27+00:00" }, + { + "name": "rakit/validation", + "version": "v1.4.0", + "source": { + "type": "git", + "url": "https://github.com/rakit/validation.git", + "reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rakit/validation/zipball/ff003a35cdf5030a5f2482299f4c93f344a35b29", + "reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=7.0" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.2", + "phpunit/phpunit": "^6.5", + "squizlabs/php_codesniffer": "^3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Rakit\\Validation\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Muhammad Syifa", + "email": "emsifa@gmail.com" + } + ], + "description": "PHP Laravel like standalone validation library", + "support": { + "issues": "https://github.com/rakit/validation/issues", + "source": "https://github.com/rakit/validation/tree/v1.4.0" + }, + "time": "2020-08-27T05:07:01+00:00" + }, { "name": "symfony/clock", "version": "v7.1.1", diff --git a/kernel/IGTabel/btn/DangerBtn.php b/kernel/IGTabel/btn/DangerBtn.php new file mode 100644 index 0000000..67afce4 --- /dev/null +++ b/kernel/IGTabel/btn/DangerBtn.php @@ -0,0 +1,24 @@ +btn = "$title"; + } + + public function fetch(): string + { + return $this->btn; + } + + public static function create(string $title, string $url): DangerBtn + { + return new self($title, $url); + } + +} \ No newline at end of file diff --git a/kernel/IGTabel/btn/SuccessBtn.php b/kernel/IGTabel/btn/SuccessBtn.php new file mode 100644 index 0000000..14d352f --- /dev/null +++ b/kernel/IGTabel/btn/SuccessBtn.php @@ -0,0 +1,24 @@ +btn = "$title"; + } + + public function fetch(): string + { + return $this->btn; + } + + public static function create(string $title, string $url): SuccessBtn + { + return new self($title, $url); + } + +} \ No newline at end of file diff --git a/views/admin/user/form.php b/views/admin/user/form.php index 360137c..f05a7a1 100644 --- a/views/admin/user/form.php +++ b/views/admin/user/form.php @@ -1,19 +1,59 @@ -
- Логин:
-

+ -

+use app\models\User; - Email адрес:
-

+$form = new \itguild\forms\ActiveForm(); +$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user"); - - -
\ No newline at end of file +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [ + 'class' => "form-control", + 'placeholder' => 'Логин', + 'model' => $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(); + +?> +
+
+ 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/views/admin/user/view.php b/views/admin/user/view.php index 67e93d3..452caed 100644 --- a/views/admin/user/view.php +++ b/views/admin/user/view.php @@ -4,7 +4,9 @@ * @var \Illuminate\Database\Eloquent\Collection $user */ use Itguild\Tables\ViewJsonTable; +use kernel\IGTabel\btn\DangerBtn; use kernel\IGTabel\btn\PrimaryBtn; +use kernel\IGTabel\btn\SuccessBtn; use kernel\IGTabel\ViewJsonTableEloquentModel; $dataProvider = new ViewJsonTableEloquentModel($user, [ @@ -14,8 +16,8 @@ $dataProvider = new ViewJsonTableEloquentModel($user, [ $table = new ViewJsonTable($dataProvider->getJson()); $table->beforeTable(function () use ($user) { $btn = PrimaryBtn::create("Список", "/admin/user")->fetch(); - $btn .= PrimaryBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch(); - $btn .= PrimaryBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch(); + $btn .= SuccessBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch(); + $btn .= DangerBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch(); return $btn; }); $table->create();