user crud

This commit is contained in:
Kavalar 2024-07-25 13:23:50 +03:00
parent 4d922096a9
commit 653bf674c9
9 changed files with 255 additions and 49 deletions

View File

@ -50,6 +50,8 @@ class UserController extends Controller{
$this->redirect("/admin/user/create"); $this->redirect("/admin/user/create");
} }
public function actionQuestionCount($user_id) public function actionQuestionCount($user_id)
{ {
return Question::where('user_id', $user_id)->count(); return Question::where('user_id', $user_id)->count();
@ -99,41 +101,36 @@ class UserController extends Controller{
*/ */
public function actionUpdate($id): void 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 public function actionEdit($id): void
{ {
// $_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT); $user = User::find($id);
// if (!$user){
// $user = User::find($_REQUEST['id']); throw new Exception(message: "The user not found");
// $user->username = $_REQUEST['username']; }
// $user->email = $_REQUEST['email']; $userForm = new CreateUserForm();
// $user->password_hash = $_REQUEST['password_hash']; $userService = new UserService();
// $user->save(); $userForm->load($_REQUEST);
if ($userForm->validate()){
// $user = User::find($id); $user = $userService->update($userForm, $user);
// if (!$user){ if ($user){
// throw new Exception(message: "The user not found"); $this->redirect("/admin/user/" . $user->id);
// } }
// $userForm = new CreateUserForm(); }
// $userService = new UserService(); $this->redirect("/admin/user/update/" . $id);
// $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);
// }
} }
public function actionDelete($id): void #[NoReturn] public function actionDelete($id): void
{ {
User::find($id)->delete(); User::find($id)->delete();
$this->redirect("/admin/user/"); $this->redirect("/admin/user/");

View File

@ -3,6 +3,7 @@ namespace app\models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
* @property int $id
* @property string $username * @property string $username
* @property string $email * @property string $email
* @property string $password_hash * @property string $password_hash

View File

@ -21,4 +21,16 @@ class UserService
return false; 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;
}
} }

View File

@ -9,7 +9,8 @@
"twig/twig": "^3.0", "twig/twig": "^3.0",
"twbs/bootstrap": "5.0.2", "twbs/bootstrap": "5.0.2",
"itguild/tables": "^0.1.3", "itguild/tables": "^0.1.3",
"madesimple/php-form-validator": "^2.9" "madesimple/php-form-validator": "^2.9",
"itguild/forms": "^0.1.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

107
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "f638e86e2b30657f939e5afa8d66beb9", "content-hash": "6f3aaa23fb01733bc2ac85dfaaf0dec3",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@ -723,6 +723,65 @@
}, },
"time": "2024-07-16T13:48:58+00:00" "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", "name": "itguild/tables",
"version": "0.1.6", "version": "0.1.6",
@ -1169,6 +1228,52 @@
}, },
"time": "2021-10-29T13:26:27+00:00" "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", "name": "symfony/clock",
"version": "v7.1.1", "version": "v7.1.1",

View File

@ -0,0 +1,24 @@
<?php
namespace kernel\IGTabel\btn;
class DangerBtn
{
protected string $btn = '';
public function __construct(string $title, string $url)
{
$this->btn = "<a class='btn btn-danger' href='$url' style='margin: 3px; width: 150px;' >$title</a>";
}
public function fetch(): string
{
return $this->btn;
}
public static function create(string $title, string $url): DangerBtn
{
return new self($title, $url);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace kernel\IGTabel\btn;
class SuccessBtn
{
protected string $btn = '';
public function __construct(string $title, string $url)
{
$this->btn = "<a class='btn btn-success' href='$url' style='margin: 3px; width: 150px;' >$title</a>";
}
public function fetch(): string
{
return $this->btn;
}
public static function create(string $title, string $url): SuccessBtn
{
return new self($title, $url);
}
}

View File

@ -1,19 +1,59 @@
<form action="/admin/user/" method="post"> <?php
Логин:<br> /**
<label> * @var User $model
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин"> */
</label> <br> <br>
Пароль:<br> use app\models\User;
<label>
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br> $form = new \itguild\forms\ActiveForm();
<label> $form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user");
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить"> $form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [
<input type="reset"> 'class' => "form-control",
</form> '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();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -4,7 +4,9 @@
* @var \Illuminate\Database\Eloquent\Collection $user * @var \Illuminate\Database\Eloquent\Collection $user
*/ */
use Itguild\Tables\ViewJsonTable; use Itguild\Tables\ViewJsonTable;
use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
use kernel\IGTabel\ViewJsonTableEloquentModel; use kernel\IGTabel\ViewJsonTableEloquentModel;
$dataProvider = new ViewJsonTableEloquentModel($user, [ $dataProvider = new ViewJsonTableEloquentModel($user, [
@ -14,8 +16,8 @@ $dataProvider = new ViewJsonTableEloquentModel($user, [
$table = new ViewJsonTable($dataProvider->getJson()); $table = new ViewJsonTable($dataProvider->getJson());
$table->beforeTable(function () use ($user) { $table->beforeTable(function () use ($user) {
$btn = PrimaryBtn::create("Список", "/admin/user")->fetch(); $btn = PrimaryBtn::create("Список", "/admin/user")->fetch();
$btn .= PrimaryBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch(); $btn .= SuccessBtn::create("Редактировать", "/admin/user/update/" . $user->id)->fetch();
$btn .= PrimaryBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch(); $btn .= DangerBtn::create("Удалить", "/admin/user/delete/" . $user->id)->fetch();
return $btn; return $btn;
}); });
$table->create(); $table->create();