This commit is contained in:
Билай Станислав 2024-12-03 12:13:18 +03:00
parent c0bedcb5ee
commit aff394ae72
6 changed files with 49 additions and 5 deletions

View File

@ -123,7 +123,7 @@ class EntityRelation
} }
} }
return false; return [];
} }
public static function addEntityRelation(string $entity, string $property): bool public static function addEntityRelation(string $entity, string $property): bool

View File

@ -59,6 +59,16 @@ class FormModel
return false; return false;
} }
public function validateForUpdate(): bool
{
$res = $this->validator->validate($this->data, $this->rulesForUpdate());
if (!$res) {
return true;
}
return false;
}
public function getErrors(): array public function getErrors(): array
{ {
return $this->validator->getProcessedErrors(); return $this->validator->getProcessedErrors();

View File

@ -5,9 +5,11 @@ namespace kernel\modules\user\controllers;
use Exception; use Exception;
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController; use kernel\AdminController;
use kernel\EntityRelation;
use kernel\modules\user\models\forms\CreateUserForm; use kernel\modules\user\models\forms\CreateUserForm;
use kernel\modules\user\models\User; use kernel\modules\user\models\User;
use kernel\modules\user\service\UserService; use kernel\modules\user\service\UserService;
use kernel\Request;
use Twig\Error\LoaderError; use Twig\Error\LoaderError;
use Twig\Error\RuntimeError; use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError; use Twig\Error\SyntaxError;
@ -35,6 +37,11 @@ class UserController extends AdminController
$userForm->load($_REQUEST); $userForm->load($_REQUEST);
if ($userForm->validate()){ if ($userForm->validate()){
$user = $this->userService->create($userForm); $user = $this->userService->create($userForm);
$entityRelation = new EntityRelation();
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
if ($user){ if ($user){
$this->redirect("/admin/user/view/" . $user->id); $this->redirect("/admin/user/view/" . $user->id);
} }
@ -91,8 +98,12 @@ class UserController extends AdminController
$userForm = new CreateUserForm(); $userForm = new CreateUserForm();
$userService = new UserService(); $userService = new UserService();
$userForm->load($_REQUEST); $userForm->load($_REQUEST);
if ($userForm->validate()){ if ($userForm->validateForUpdate()){
$user = $userService->update($userForm, $user); $user = $userService->update($userForm, $user);
$entityRelation = new EntityRelation();
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
if ($user){ if ($user){
$this->redirect("/admin/user/view/" . $user->id); $this->redirect("/admin/user/view/" . $user->id);
} }
@ -100,9 +111,20 @@ class UserController extends AdminController
$this->redirect("/admin/user/update/" . $id); $this->redirect("/admin/user/update/" . $id);
} }
/**
* @throws Exception
*/
#[NoReturn] public function actionDelete($id): void #[NoReturn] public function actionDelete($id): void
{ {
User::find($id)->delete(); $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();
$this->redirect("/admin/user/"); $this->redirect("/admin/user/");
} }

View File

@ -16,4 +16,13 @@ class CreateUserForm extends FormModel
]; ];
} }
public function rulesForUpdate(): array
{
return [
'username' => 'required|min-str-len:5|max-str-len:30',
'password' => '',
'email' => 'required|email'
];
}
} }

View File

@ -3,6 +3,7 @@
namespace kernel\modules\user\service; namespace kernel\modules\user\service;
use kernel\FormModel; use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\modules\user\models\User; use kernel\modules\user\models\User;
class UserService class UserService
@ -25,7 +26,9 @@ class UserService
{ {
$user->username = $form_model->getItem('username'); $user->username = $form_model->getItem('username');
$user->email = $form_model->getItem('email'); $user->email = $form_model->getItem('email');
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT); if ($form_model->getItem('password')) {
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
}
if ($user->save()){ if ($user->save()){
return $user; return $user;
} }

View File

@ -6,7 +6,7 @@
use kernel\modules\user\models\User; use kernel\modules\user\models\User;
$form = new \itguild\forms\ActiveForm(); $form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user"); $form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user", enctype: 'multipart/form-data');
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [ $form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [
'class' => "form-control", 'class' => "form-control",