add User class

This commit is contained in:
2024-10-24 12:44:34 +02:00
parent 2a49d95fff
commit e7ef57817a
2 changed files with 48 additions and 13 deletions

View File

@@ -13,16 +13,22 @@ if (isset($_POST)) {
$login = strip_tags($_POST['login']); $login = strip_tags($_POST['login']);
$description = strip_tags($_POST['description']); $description = strip_tags($_POST['description']);
$role = strip_tags($_POST['role']); $role = strip_tags($_POST['role']);
UpdateUser($id, $login, $description, $role);
$user = GetUser($id);
$user->login = $login;
$user->description = $description;
$user->role = $role;
UpdateUser($user);
header('Location: index.php'); header('Location: index.php');
} }
} }
if (isset($_GET['id']) && !empty($_GET['id'])) { if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']); $id = strip_tags($_GET['id']);
$result = GetUser($id); $user = GetUser($id);
} }
$vue = "users/edit.twig"; $vue = "users/edit.twig";
$donnees = array("user" => $result); $donnees = array("user" => $user);
require_once('../modele/twig.php'); require_once('../modele/twig.php');

View File

@@ -2,7 +2,30 @@
require_once('DataBase.php'); require_once('DataBase.php');
function GetUsers() class User {
public int $id;
public string $login;
public string $password;
public string $firstname;
public string $lastname;
public string $description;
public int $role;
public bool $enabled;
function __construct(array $table)
{
$this->id = $table['id'];
$this->login = $table['login'];
$this->password = $table['password'];
$this->firstname = $table['firstname'];
$this->lastname = $table['lastname'];
$this->description = $table['description'];
$this->role = $table['role'];
$this->enabled = $table['enabled'];
}
}
function GetUsers() : array
{ {
global $db; global $db;
@@ -15,10 +38,16 @@ function GetUsers()
CloseDataBase(); CloseDataBase();
return $result; $users = array();
foreach ($result as $user) {
array_push($users, new User($user));
} }
function GetUser(int $id) return $users;
}
function GetUser(int $id) : User
{ {
global $db; global $db;
@@ -32,7 +61,7 @@ function GetUser(int $id)
CloseDataBase(); CloseDataBase();
return $result; return new User($result);
} }
function AddUser(string $login, string $password, string $lastname, int $role, string $firstname, string $description) function AddUser(string $login, string $password, string $lastname, int $role, string $firstname, string $description)
@@ -55,7 +84,7 @@ function AddUser(string $login, string $password, string $lastname, int $role, s
CloseDataBase(); CloseDataBase();
} }
function UpdateUser(int $id, string $login, string $description, string $role) function UpdateUser(User $user)
{ {
global $db; global $db;
@@ -64,10 +93,10 @@ function UpdateUser(int $id, string $login, string $description, string $role)
$sql = "UPDATE `users` SET `login`=:login, `description`=:description, $sql = "UPDATE `users` SET `login`=:login, `description`=:description,
`role`=:role WHERE `id`=:id;"; `role`=:role WHERE `id`=:id;";
$query = $db->prepare($sql); $query = $db->prepare($sql);
$query->bindValue(':login', $login, PDO::PARAM_STR); $query->bindValue(':login', $user->login, PDO::PARAM_STR);
$query->bindValue(':description', $description, PDO::PARAM_STR); $query->bindValue(':description', $user->description, PDO::PARAM_STR);
$query->bindValue(':role', $role, PDO::PARAM_INT); $query->bindValue(':role', $user->role, PDO::PARAM_INT);
$query->bindValue(':id', $id, PDO::PARAM_INT); $query->bindValue(':id', $user->id, PDO::PARAM_INT);
$query->execute(); $query->execute();
CloseDataBase(); CloseDataBase();