diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index d0eb0e2..72b4b45 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -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,11 +30,19 @@ 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"); } - $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 diff --git a/app/tables/columns/UserEditActionColumn.php b/app/tables/columns/UserEditActionColumn.php new file mode 100644 index 0000000..b2ff3b3 --- /dev/null +++ b/app/tables/columns/UserEditActionColumn.php @@ -0,0 +1,17 @@ +baseUrl . $this->prefix . $this->id . $this->prefix . "update"; + $link = $this->baseUrl . $this->prefix . "update" . $this->prefix . $this->id; + return " Редактировать "; + } +} \ No newline at end of file diff --git a/index.php b/index.php index d3873c3..2fe36dd 100644 --- a/index.php +++ b/index.php @@ -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']); diff --git a/views/user_update.html.twig b/views/user_update.html.twig index 4ea5ed3..65a46f6 100644 --- a/views/user_update.html.twig +++ b/views/user_update.html.twig @@ -2,11 +2,6 @@ {% block content %}