2024-07-03 14:41:15 +03:00
|
|
|
<?php
|
2024-09-06 16:53:20 +03:00
|
|
|
namespace kernel\modules\user\controllers;
|
2024-07-05 13:49:04 +03:00
|
|
|
|
|
|
|
|
2024-07-23 15:22:33 +03:00
|
|
|
use Exception;
|
2024-07-24 17:22:59 +03:00
|
|
|
use JetBrains\PhpStorm\NoReturn;
|
2024-09-10 15:45:12 +03:00
|
|
|
use kernel\AdminController;
|
2024-12-03 12:13:18 +03:00
|
|
|
use kernel\EntityRelation;
|
2024-09-10 15:45:12 +03:00
|
|
|
use kernel\modules\user\models\forms\CreateUserForm;
|
|
|
|
use kernel\modules\user\models\User;
|
|
|
|
use kernel\modules\user\service\UserService;
|
2024-12-03 12:13:18 +03:00
|
|
|
use kernel\Request;
|
2024-07-24 16:31:07 +03:00
|
|
|
use Twig\Error\LoaderError;
|
|
|
|
use Twig\Error\RuntimeError;
|
|
|
|
use Twig\Error\SyntaxError;
|
2024-07-03 14:41:15 +03:00
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
class UserController extends AdminController
|
|
|
|
{
|
|
|
|
|
|
|
|
private UserService $userService;
|
|
|
|
|
2024-07-24 17:22:59 +03:00
|
|
|
protected function init(): void
|
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
parent::init();
|
|
|
|
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/user/views/";
|
|
|
|
$this->userService = new UserService();
|
2024-07-24 17:22:59 +03:00
|
|
|
}
|
|
|
|
|
2024-07-08 16:20:25 +03:00
|
|
|
public function actionCreate(): void
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("form.php");
|
2024-07-09 16:08:50 +03:00
|
|
|
}
|
|
|
|
|
2024-07-24 17:22:59 +03:00
|
|
|
#[NoReturn] public function actionAdd(): void
|
2024-07-09 16:08:50 +03:00
|
|
|
{
|
2024-07-24 14:07:45 +03:00
|
|
|
$userForm = new CreateUserForm();
|
|
|
|
$userForm->load($_REQUEST);
|
|
|
|
if ($userForm->validate()){
|
2024-09-06 16:53:20 +03:00
|
|
|
$user = $this->userService->create($userForm);
|
2024-12-03 12:13:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
$entityRelation = new EntityRelation();
|
|
|
|
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
|
|
|
|
|
2024-07-24 17:22:59 +03:00
|
|
|
if ($user){
|
2024-10-31 16:12:02 +03:00
|
|
|
$this->redirect("/admin/user/view/" . $user->id);
|
2024-07-24 17:22:59 +03:00
|
|
|
}
|
2024-07-24 12:50:07 +03:00
|
|
|
}
|
2024-07-24 14:07:45 +03:00
|
|
|
$this->redirect("/admin/user/create");
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|
|
|
|
|
2024-07-16 13:37:34 +03:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2024-07-31 12:59:06 +03:00
|
|
|
public function actionIndex($page_number = 1): void
|
2024-07-05 13:49:04 +03:00
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
|
|
|
|
2024-07-23 15:22:33 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-08 16:20:25 +03:00
|
|
|
public function actionView($id): void
|
2024-07-05 13:49:04 +03:00
|
|
|
{
|
2024-07-09 16:08:50 +03:00
|
|
|
$user = User::find($id);
|
2024-07-24 12:50:07 +03:00
|
|
|
|
2024-07-23 15:22:33 +03:00
|
|
|
if (!$user){
|
|
|
|
throw new Exception(message: "The user not found");
|
|
|
|
}
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("view.php", ['user' => $user]);
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
2024-07-09 16:08:50 +03:00
|
|
|
|
2024-07-24 16:31:07 +03:00
|
|
|
/**
|
|
|
|
* @throws RuntimeError
|
|
|
|
* @throws SyntaxError
|
|
|
|
* @throws LoaderError
|
2024-09-23 15:33:48 +03:00
|
|
|
* @throws Exception
|
2024-07-24 16:31:07 +03:00
|
|
|
*/
|
2024-07-24 16:29:00 +03:00
|
|
|
public function actionUpdate($id): void
|
2024-07-09 16:08:50 +03:00
|
|
|
{
|
2024-07-25 13:23:50 +03:00
|
|
|
$model = User::find($id);
|
|
|
|
if (!$model){
|
|
|
|
throw new Exception(message: "The user not found");
|
|
|
|
}
|
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("form.php", ['model' => $model]);
|
2024-07-09 16:08:50 +03:00
|
|
|
}
|
|
|
|
|
2024-07-25 13:23:50 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-24 16:29:00 +03:00
|
|
|
public function actionEdit($id): void
|
2024-07-09 16:08:50 +03:00
|
|
|
{
|
2024-07-25 13:23:50 +03:00
|
|
|
$user = User::find($id);
|
|
|
|
if (!$user){
|
|
|
|
throw new Exception(message: "The user not found");
|
|
|
|
}
|
|
|
|
$userForm = new CreateUserForm();
|
|
|
|
$userService = new UserService();
|
|
|
|
$userForm->load($_REQUEST);
|
2024-12-03 12:13:18 +03:00
|
|
|
if ($userForm->validateForUpdate()){
|
2024-07-25 13:23:50 +03:00
|
|
|
$user = $userService->update($userForm, $user);
|
2024-12-03 12:13:18 +03:00
|
|
|
|
|
|
|
$entityRelation = new EntityRelation();
|
|
|
|
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
|
|
|
|
|
2024-07-25 13:23:50 +03:00
|
|
|
if ($user){
|
2024-10-31 16:12:02 +03:00
|
|
|
$this->redirect("/admin/user/view/" . $user->id);
|
2024-07-25 13:23:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->redirect("/admin/user/update/" . $id);
|
2024-07-09 16:08:50 +03:00
|
|
|
}
|
|
|
|
|
2024-12-03 12:13:18 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-25 13:23:50 +03:00
|
|
|
#[NoReturn] public function actionDelete($id): void
|
2024-07-09 16:08:50 +03:00
|
|
|
{
|
2024-12-03 12:13:18 +03:00
|
|
|
$user = User::find($id)->first();
|
|
|
|
if (!$user){
|
|
|
|
throw new Exception(message: "The user not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$entityRelation = new EntityRelation();
|
|
|
|
$entityRelation->deleteEntityRelation(entity: "user", model: $user);
|
|
|
|
|
|
|
|
$user->delete();
|
2024-07-25 11:55:31 +03:00
|
|
|
$this->redirect("/admin/user/");
|
2024-07-09 16:08:50 +03:00
|
|
|
}
|
|
|
|
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|