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");
}
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/");

View File

@ -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

View File

@ -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;
}
}

View File

@ -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": {

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",
"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",

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">
Логин:<br>
<label>
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин">
</label> <br> <br>
<?php
/**
* @var User $model
*/
Пароль:<br>
<label>
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
use app\models\User;
Email адрес: <br>
<label>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user");
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
$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();
?>
<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
*/
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();