update beta

This commit is contained in:
Билай Станислав 2024-07-24 16:29:00 +03:00
parent 043a699c6d
commit 0e13d2c418
4 changed files with 65 additions and 24 deletions

View File

@ -8,6 +8,7 @@ use app\models\forms\CreateUserForm;
use app\models\Question;
use app\models\User;
use app\services\UserService;
use app\tables\columns\UserEditActionColumn;
use app\tables\columns\UserViewActionColumn;
use Exception;
use http\Message;
@ -29,12 +30,20 @@ class UserController extends Controller{
$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");
// Debug::dd($userService->create($userForm));
$this->redirect("/admin/user/" . User::latest()->first()['id']);
}
else
{
$this->redirect("/admin/user/create");
}
}
public function actionQuestionCount($user_id)
{
@ -57,6 +66,7 @@ class UserController extends Controller{
]);
$table = new ListJsonTable($dataProvider->getJson());
$table->addAction(UserViewActionColumn::class);
$table->addAction(UserEditActionColumn::class);
$table->create();
$table->render();
}));
@ -87,22 +97,40 @@ class UserController extends Controller{
echo $this->twig->render('user_table.html.twig');
}
public function actionUpdate(): void
public function actionUpdate($id): void
{
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
echo $this->twig->render('user_update.html.twig');
}
public function actionEdit(): void
public function actionEdit($id): void
{
$_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
// $_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($_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);
// }
}
public function actionDelete($id): void

View File

@ -0,0 +1,17 @@
<?php
namespace app\tables\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class UserEditActionColumn extends ActionColumn
{
protected string $prefix = "/";
public function fetch(): string
{
// $link = $this->baseUrl . $this->prefix . $this->id . $this->prefix . "update";
$link = $this->baseUrl . $this->prefix . "update" . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-primary'>Редактировать</a> ";
}
}

View File

@ -19,13 +19,14 @@ $router->get('/', [MainController::class, 'actionIndex']);
$router->get('/example', [MainController::class, 'actionExample']);
$router->group(["prefix" => "admin"], function (RouteCollector $router){
$router->group(["prefix" => "user"], function (RouteCollector $router){
$router->get('/create', [\app\controllers\UserController::class, 'actionCreate']);
$router->get('/update', [\app\controllers\UserController::class, 'actionUpdate']);
$router->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']);
$router->get('/', [\app\controllers\UserController::class, 'actionIndex']);
$router->get('/{id}', [\app\controllers\UserController::class, 'actionView']);
$router->get('/create', [\app\controllers\UserController::class, 'actionCreate']);
$router->post("/", [\app\controllers\UserController::class, 'actionAdd']);
$router->post("/edit", [\app\controllers\UserController::class, 'actionEdit']);
$router->get('/{id}', [\app\controllers\UserController::class, 'actionView']);
// $router->get('/{id}/update', [\app\controllers\UserController::class, 'actionUpdate']);
$router->any('/update/{id}', [\app\controllers\UserController::class, 'actionUpdate']);
$router->any("/edit/{id}", [\app\controllers\UserController::class, 'actionEdit']);
$router->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']);
});
$router->group(["prefix" => "question"], function (RouteCollector $router){
$router->get('/create', [QuestionController::class, 'actionCreate']);

View File

@ -2,11 +2,6 @@
{% block content %}
<form action="/admin/user/edit" method="post">
id пользователя:<br>
<label>
<input type = "text" name = "id" required size="50" autofocus placeholder="id">
</label> <br> <br>
Логин:<br>
<label>
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин">
@ -14,12 +9,12 @@
Пароль:<br>
<label>
<input type = "text" name = "password_hash" required size="50" placeholder="Пароль">
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br>
<label>
<input type="Email" name="email" required>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить">