Compare commits

..

10 Commits

Author SHA1 Message Date
a17c21d424 remove footer block 2024-10-25 16:59:43 +02:00
9ba40ae6c9 change button names 2024-10-25 16:55:13 +02:00
52faa489fa enorme push 2024-10-25 16:49:52 +02:00
829d121268 add roles 2024-10-24 18:03:57 +02:00
e2b3062a22 twig render function 2024-10-24 17:44:47 +02:00
e7ef57817a add User class 2024-10-24 12:44:34 +02:00
2a49d95fff remove test 2024-10-23 21:46:07 +02:00
aeca2e31df moved things 2024-10-23 21:45:15 +02:00
3fc8d2fd74 update gitignore 2024-10-23 16:03:52 +02:00
7739199863 rename folder 2024-10-23 16:03:28 +02:00
38 changed files with 760 additions and 303 deletions

4
.gitignore vendored
View File

@@ -1,3 +1,3 @@
appli/vendor appli/vendor
crud/vendor tpCrudTwig/vendor
crud/connect.php tpCrudTwig/connect.php

View File

@@ -1,37 +0,0 @@
<?php
require_once('connect.php');
if (isset($_POST)) {
if (
isset($_POST['login']) && !empty($_POST['login'])
&& isset($_POST['password']) && !empty($_POST['password'])
&& isset($_POST['lastname']) && !empty($_POST['lastname'])
&& isset($_POST['firstname']) && !empty($_POST['firstname'])
&& isset($_POST['role']) && !empty($_POST['role'])
&& isset($_POST['description']) && !empty($_POST['description'])
) {
$login = strip_tags($_POST['login']);
$password = strip_tags($_POST['password']);
$lastname = strip_tags($_POST['lastname']);
$role = strip_tags($_POST['role']);
$firstname = strip_tags($_POST['firstname']);
$description = strip_tags($_POST['description']);
$sql = "INSERT INTO `users` (`login`, `password`, `firstname`, `lastname`, `description`, `role`, `enabled`) VALUES (:login, :password, :firstname, :lastname, :description, :role, :enabled);";
$query = $db->prepare($sql);
$query->bindValue(':login', $login, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->bindValue(':firstname', $firstname, PDO::PARAM_STR);
$query->bindValue(':lastname', $lastname, PDO::PARAM_STR);
$query->bindValue(':description', $description, PDO::PARAM_STR);
$query->bindValue(':role', $role, PDO::PARAM_INT);
$query->bindValue(':enabled', 1, PDO::PARAM_INT);
$query->execute();
$_SESSION['message'] = "Utilisateur ajouté avec succès !";
header('Location: index.php');
}
}
require_once('close.php');
$vue = "add.twig";
$donnees = array();
require_once('modele/twig.php');

View File

@@ -1,2 +0,0 @@
<?php
$db = null;

View File

@@ -1,14 +0,0 @@
<?php
try {
$host = "localhost";
$user = "root";
$password = "motdepasse";
// Connexion à la bdd
$db = new PDO("mysql:host=$host;dbname=cruddb", $user, $password);
$db->exec('SET NAMES "UTF8"');
} catch (PDOException $e) {
echo 'Erreur : ' . $e->getMessage();
die();
}

View File

@@ -1,11 +0,0 @@
<?php
require_once('connect.php');
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
$sql = "DELETE FROM `users` WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
header('Location: index.php');
}
require_once('close.php');

View File

@@ -1,29 +0,0 @@
<?php
session_start();
// On inclut la connexion à la base
require_once('connect.php');
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
// On écrit notre requête
$sql = 'SELECT * FROM `users` WHERE `id`=:id';
// On prépare la requête
$query = $db->prepare($sql);
// On attache les valeurs
$query->bindValue(':id', $id, PDO::PARAM_STR);
// On exécute la requête
$query->execute();
// On stocke le résultat dans un tableau associatif
$user = $query->fetch();
if (!$user) {
header('Location: index.php');
}
} else {
header('Location: index.php');
}
require_once('close.php');
$vue = "details.twig";
$donnees = array("user" => $user);
require_once('modele/twig.php');

View File

@@ -1,38 +0,0 @@
<?php
require_once('connect.php');
if (isset($_POST)) {
if (
isset($_POST['id']) && !empty($_POST['id'])
&& isset($_POST['login']) && !empty($_POST['login'])
&& isset($_POST['description']) && !empty($_POST['description'])
&& isset($_POST['role']) && !empty($_POST['role'])
) {
$id = strip_tags($_GET['id']);
$login = strip_tags($_POST['login']);
$description = strip_tags($_POST['description']);
$role = strip_tags($_POST['role']);
$sql = "UPDATE `users` SET `login`=:login, `description`=:description,
`role`=:role WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':login', $login, PDO::PARAM_STR);
$query->bindValue(':description', $description, PDO::PARAM_STR);
$query->bindValue(':role', $role, PDO::PARAM_INT);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
header('Location: index.php');
}
}
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
$sql = "SELECT * FROM `users` WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
$result = $query->fetch();
}
require_once('close.php');
$vue = "edit.twig";
$donnees = array("user" => $result);
require_once('modele/twig.php');

View File

@@ -1,18 +0,0 @@
<?php
// On inclut la connexion à la base
require_once('connect.php');
// On écrit notre requête
$sql = 'SELECT * FROM `users`';
// On prépare la requête
$query = $db->prepare($sql);
// On exécute la requête
$query->execute();
// On stocke le résultat dans un tableau associatif
$result = $query->fetchAll(PDO::FETCH_ASSOC);
require_once('close.php');
$vue = "index.twig";
$donnees = array("users" => $result);
require_once('modele/twig.php');

View File

@@ -1,12 +0,0 @@
<?php
/* inclure l'autoloader */
require_once 'vendor/autoload.php';
/* templates chargés à partir du système de fichiers (répertoire vue) */
$loader = new Twig\Loader\FilesystemLoader('vue');
/* options : prod = cache dans le répertoire cache, dev = pas de cache */
$options_prod = array('cache' => 'cache', 'autoescape' => true);
$options_dev = array('cache' => false, 'autoescape' => true);
/* stocker la configuration */
$twig = new Twig\Environment($loader);
/* charger+compiler le template, exécuter, envoyer le résultat au navigateur */
echo $twig->render($vue, $donnees);

View File

@@ -1,33 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
{# <link rel="stylesheet" href="style/style.css"/> #}
{# <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> #}
<title>Ajouter utilisateur</title>
</head>
<body>
<div class="container text-center bg-light">
<h1>Ajouter un utilisateur</h1>
<form class="form-group" role="search" method="post">
<label for="login">Utilisateur</label>
<input type="text" class="form-control" name="login" id="login">
<label for="password">Mot de passe</label>
<input type="password" class="form-control" name="password" id="password" />
<label for="firstname">Prénom</label>
<input type="text" class="form-control" name="firstname" id="firstname">
<label for="lastname">Nom de famille</label>
<input type="text" class="form-control" name="lastname" id="lastname" />
<label for="role">Role</label>
<input type="number" class="form-control" name="role" id="role">
<label for="description">Description</label>
<textarea name="description" class="form-control"></textarea>
<input type="submit" class="btn btn-success" name="Enregistrer" value="Enregistrer"/>
</form>
</div>
</body>
</html>

View File

@@ -1,29 +0,0 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Détails de l'utilisateur</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
{# <link rel="stylesheet" href="style/style.css" /> #}
{# <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> #}
</head>
<body>
<div class="container text-center bg-light">
<h1>Détails pour lutilisateur {{ user.login }}</h1>
<div class="container bg-white w-25 border my-2">
<p>ID : {{ user.id }}</p>
<p>Login : {{ user.login }}</p>
<p>FirstName : {{ user.firstname }}</p>
<p>LastName : {{ user.lastname }}</p>
<p>Role : {{ user.role }}</p>
</div>
<div>
<button type="button" class="btn btn-warning" onclick="window.location.href='edit.php?id={{ user.id }}'">Modifier</button>
<button type="button" class="btn btn-danger" onclick="window.location.href='delete.php?id={{ user.id }}'">Supprimer</button>
</div>
</div>
</body>
</html>

View File

@@ -1,30 +0,0 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modification de l'utilisateur</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
{# <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> #}
{# <link rel="stylesheet"
href="style/style.css"> #}
</head>
<body>
<div class="container text-center bg-light">
<h1>Modifier un utilisateur</h1>
<form method="post">
<label for="login">Login</label>
<input type="text" class="form-control" name="login" id="login" value="{{ user.login }}">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" id="description" value="{{ user.description }}">
<label for="role">Role</label>
<input type="number" class="form-control" name="role" id="role" value="{{ user.role }}">
<input type="submit" class="btn btn-success" name="Enregistrer" value="Enregistrer"/>
<input type="hidden" name="id" value="{{ user.id }}">
</form>
</div>
</body>
</html>

View File

@@ -1,48 +0,0 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
{# <link rel="stylesheet" href="style/style.css" /> #}
{# <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> #}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Liste des utilisateurs</title>
</head>
<body>
<div class="container text-center bg-light">
<h1 class="">Liste des utilisateurs</h1>
<table class="table table-striped table-bordered table-condensed">
<thead>
<th>ID</th>
<th>Login</th>
<th>Nom</th>
<th>Prenom</th>
<th>Rôle</th>
<th>Actions</th>
</thead>
<tbody>
{# afficher les utilisateurs #}
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.login }}</td>
<td>{{ user.lastname }}</td>
<td>{{ user.firstname }}</td>
<td>{{ user.role }}</td>
<td>
<button type="button" class="btn btn-info" onclick="window.location.href='details.php?id={{ user.id }}'">Voir</button>
<button type="button" class="btn btn-warning" onclick="window.location.href='edit.php?id= {{ user.id }}'">Modifier</button>
<button type="button" class="btn btn-danger" onclick="window.location.href='delete.php?id= {{ user.id }}'">Supprimer</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-success" onclick="window.location.href='add.php'">Ajouter</button>
</div>
</body>
</html>

5
tpCrudTwig/composer.json Normal file
View File

@@ -0,0 +1,5 @@
{
"require": {
"twig/twig": "^3.14"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@@ -0,0 +1,12 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/User.php");
require_once($SERVER_ROOT . "/src/Twig.php");
renderTwig("users/index.twig", array("users" => GetUsers()));
$_SESSION['message'] = $_SESSION['error'] = "";

View File

@@ -0,0 +1,21 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/Role.php");
require_once($SERVER_ROOT . "/src/Twig.php");
if (isset($_POST)) {
if (
isset($_POST['name']) && !empty($_POST['name'])
) {
$name = strip_tags($_POST['name']);
AddRole($name);
$_SESSION['message'] = "Rôle ajouté avec succès !";
header('Location: /tpCrudTwig/public/roles/list.php');
}
}
renderTwig("roles/add.twig", array());

View File

@@ -0,0 +1,14 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/Role.php");
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
DeleteRole($id);
$_SESSION['message'] = "Rôle retiré avec succès !";
header('Location: list.php');
}

View File

View File

@@ -0,0 +1,32 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/Role.php");
require_once($SERVER_ROOT . "/src/Twig.php");
if (isset($_POST)) {
if (
isset($_POST['id']) && !empty($_POST['id'])
&& isset($_POST['name']) && !empty($_POST['name'])
) {
$id = strip_tags($_GET['id']);
$name = strip_tags($_POST['name']);
$role = GetRole($id);
$role->name = $name;
UpdateRole($role);
$_SESSION['message'] = "Rôle modifié avec succès !";
header('Location: /tpCrudTwig/public/roles/list.php');
}
}
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
$role = GetRole($id);
}
renderTwig("roles/edit.twig", array("role" => $role));

View File

@@ -0,0 +1,12 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/Role.php");
require_once($SERVER_ROOT . "/src/Twig.php");
renderTwig("roles/index.twig", ["roles" => GetRoles()]);
$_SESSION['message'] = $_SESSION['error'] = "";

View File

@@ -0,0 +1,37 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/User.php");
require_once($SERVER_ROOT . "/src/Role.php");
require_once($SERVER_ROOT . "/src/Twig.php");
if (isset($_POST)) {
if (
isset($_POST['login']) && !empty($_POST['login'])
&& isset($_POST['password']) && !empty($_POST['password'])
&& isset($_POST['lastname']) && !empty($_POST['lastname'])
&& isset($_POST['firstname']) && !empty($_POST['firstname'])
&& isset($_POST['role']) && !empty($_POST['role'])
&& isset($_POST['description']) && !empty($_POST['description'])
) {
$login = strip_tags($_POST['login']);
$password = strip_tags($_POST['password']);
$lastname = strip_tags($_POST['lastname']);
$role = strip_tags($_POST['role']);
$firstname = strip_tags($_POST['firstname']);
$description = strip_tags($_POST['description']);
if (GetRole((int) $role) == null) {
$_SESSION['error'] = "Erreur lors de l'ajout de l'utilisateur";
header('Location: /tpCrudTwig/public/index.php');
} else {
AddUser($login, $password, $lastname, $role, $firstname, $description);
$_SESSION['message'] = "Utilisateur ajouté avec succès !";
header('Location: /tpCrudTwig/public/index.php');
}
}
}
renderTwig("users/add.twig", ["roles" => GetRoles()]);

View File

@@ -0,0 +1,14 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/User.php");
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
DeleteUser($id);
$_SESSION['message'] = "Utilisateur supprimé avec succès !";
header('Location: ../index.php');
}

View File

@@ -0,0 +1,20 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/User.php");
require_once($SERVER_ROOT . "/src/Twig.php");
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
$user = GetUser($id);
if (!$user) {
header('Location: index.php');
}
} else {
header('Location: index.php');
}
renderTwig("users/details.twig", array("user" => $user));

View File

@@ -0,0 +1,37 @@
<?php
session_start();
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . "/src/User.php");
require_once($SERVER_ROOT . "/src/Twig.php");
if (isset($_POST)) {
if (
isset($_POST['id']) && !empty($_POST['id'])
&& isset($_POST['login']) && !empty($_POST['login'])
&& isset($_POST['description']) && !empty($_POST['description'])
&& isset($_POST['role']) && !empty($_POST['role'])
) {
$id = strip_tags($_GET['id']);
$login = strip_tags($_POST['login']);
$description = strip_tags($_POST['description']);
$role = strip_tags($_POST['role']);
$user = GetUser($id);
$user->login = $login;
$user->description = $description;
$user->role = GetRole((int)$role);
UpdateUser($user);
$_SESSION['message'] = "Utilisateur modifié avec succès !";
header('Location: /tpCrudTwig/public/index.php');
}
}
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = strip_tags($_GET['id']);
$user = GetUser($id);
}
renderTwig("users/edit.twig", array("user" => $user, "roles" => GetRoles()));

View File

@@ -0,0 +1,25 @@
<?php
function ConnectDataBase()
{
global $db;
try {
$host = "localhost";
$user = "root";
$password = "motdepasse";
// Connexion à la bdd
$db = new PDO("mysql:host=$host;dbname=cruddb", $user, $password);
$db->exec('SET NAMES "UTF8"');
} catch (PDOException $e) {
echo 'Erreur : ' . $e->getMessage();
die();
}
}
function CloseDataBase()
{
global $db;
$db = null;
}

100
tpCrudTwig/src/Role.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
require_once('DataBase.php');
class Role
{
public int $id;
public string $name;
function __construct(array $table)
{
$this->id = $table['id'];
$this->name = $table['name'];
}
}
function AddRole(string $name): void
{
global $db;
ConnectDataBase();
$sql = "INSERT INTO `roles` (`name`) VALUES (:name);";
$query = $db->prepare($sql);
$query->bindValue(':name', $name, PDO::PARAM_STR);
$query->execute();
CloseDataBase();
}
function GetRole(int $id)
{
global $db;
ConnectDataBase();
$sql = "SELECT * FROM `roles` WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
$result = $query->fetch();
CloseDataBase();
if (empty($result))
return null;
return new Role($result);
}
function GetRoles(): array
{
global $db;
ConnectDataBase();
$sql = 'SELECT * FROM `roles`';
$query = $db->prepare($sql);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
CloseDataBase();
$roles = array();
foreach ($result as $role) {
array_push($roles, new Role($role));
}
return $roles;
}
function UpdateRole(Role $role): void
{
global $db;
ConnectDataBase();
$sql = "UPDATE `roles` SET `name`=:name WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':name', $role->name, PDO::PARAM_STR);
$query->bindValue(':id', $role->id, PDO::PARAM_INT);
$query->execute();
CloseDataBase();
}
function DeleteRole(int $id)
{
global $db;
ConnectDataBase();
$sql = "DELETE FROM `roles` WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
CloseDataBase();
}

25
tpCrudTwig/src/Twig.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
$SERVER_ROOT = $_SERVER['DOCUMENT_ROOT'] . "/tpCrudTwig";
require_once($SERVER_ROOT . '/vendor/autoload.php');
function renderTwig(string $file, $donnees)
{
global $SERVER_ROOT;
/* templates chargés à partir du système de fichiers (répertoire vue) */
$loader = new Twig\Loader\FilesystemLoader($SERVER_ROOT . "/templates");
/* options : prod = cache dans le répertoire cache, dev = pas de cache */
$options_prod = array('cache' => 'cache', 'autoescape' => true);
$options_dev = array('cache' => false, 'autoescape' => true);
/* stocker la configuration */
$twig = new Twig\Environment($loader);
$message = isset($_SESSION['message']) ? $_SESSION['message'] : "";
$error = isset($_SESSION['error']) ? $_SESSION['error'] : "";
$donnees["message"] = $message;
$donnees["error"] = $error;
/* charger+compiler le template, exécuter, envoyer le résultat au navigateur */
echo $twig->render($file, $donnees);
}

119
tpCrudTwig/src/User.php Normal file
View File

@@ -0,0 +1,119 @@
<?php
require_once('DataBase.php');
require_once('Role.php');
class User
{
public int $id;
public string $login;
public string $password;
public string $firstname;
public string $lastname;
public string $description;
public Role $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 = GetRole((int) $table['role']);
$this->enabled = $table['enabled'];
}
}
function GetUsers(): array
{
global $db;
ConnectDataBase();
$sql = 'SELECT * FROM `users`';
$query = $db->prepare($sql);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
CloseDataBase();
$users = array();
foreach ($result as $user) {
array_push($users, new User($user));
}
return $users;
}
function GetUser(int $id): User
{
global $db;
ConnectDataBase();
$sql = "SELECT * FROM `users` WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
$result = $query->fetch();
CloseDataBase();
return new User($result);
}
function AddUser(string $login, string $password, string $lastname, int $role, string $firstname, string $description)
{
global $db;
ConnectDataBase();
$sql = "INSERT INTO `users` (`login`, `password`, `firstname`, `lastname`, `description`, `role`, `enabled`) VALUES (:login, :password, :firstname, :lastname, :description, :role, :enabled);";
$query = $db->prepare($sql);
$query->bindValue(':login', $login, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->bindValue(':firstname', $firstname, PDO::PARAM_STR);
$query->bindValue(':lastname', $lastname, PDO::PARAM_STR);
$query->bindValue(':description', $description, PDO::PARAM_STR);
$query->bindValue(':role', $role, PDO::PARAM_INT);
$query->bindValue(':enabled', 1, PDO::PARAM_INT);
$query->execute();
CloseDataBase();
}
function UpdateUser(User $user)
{
global $db;
ConnectDataBase();
$sql = "UPDATE `users` SET `login`=:login, `description`=:description,
`role`=:role WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':login', $user->login, PDO::PARAM_STR);
$query->bindValue(':description', $user->description, PDO::PARAM_STR);
$query->bindValue(':role', $user->role->id, PDO::PARAM_INT);
$query->bindValue(':id', $user->id, PDO::PARAM_INT);
$query->execute();
CloseDataBase();
}
function DeleteUser(int $id)
{
global $db;
ConnectDataBase();
$sql = "DELETE FROM `users` WHERE `id`=:id;";
$query = $db->prepare($sql);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
CloseDataBase();
}

View File

@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<title>{% block title %}{% endblock %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-dark-subtle">
<a class="navbar-brand" href="/tpCrudTwig/public/index.php">TP Crud
<img src="/tpCrudTwig/public/images/suisse.png" alt="suisse" width="30" height="30"
class="d-inline-block align-text-top">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle active" href="#" role="button" data-bs-toggle="dropdown"
aria-expanded="false">
Utilisateurs
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/tpCrudTwig/public/index.php">Liste des utilisateurs</a>
</li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/tpCrudTwig/public/users/add.php">Ajouter un
utilisateur</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle active" href="#" role="button" data-bs-toggle="dropdown"
aria-expanded="false">
Roles
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/tpCrudTwig/public/roles/list.php">Liste des rôles</a>
</li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/tpCrudTwig/public/roles/add.php">Ajouter un rôle</a>
</li>
</ul>
</li>
</div>
</div>
</nav>
</header>
<div class="container text-center bg-light p-2">
{% block content %}
{% endblock %}
{% if message is not empty %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
{% if error is not empty %}
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ error }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endif %}
</div>
<footer class="footer fixed-bottom text-center m-1 bg-dark-subtle">
&copy; Copyright {{ "now"|date('Y') }} de Simon Pribylski.
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>

View File

@@ -0,0 +1,16 @@
{% extends "base.twig" %}
{% block title %}Ajouter rôle{% endblock %}
{% block content %}
<h1>Ajouter un rôle</h1>
<form class="form-group" role="search" method="post">
<label for="name">Nom du rôle</label>
<input type="text" class="form-control" name="name" id="name">
<input type="submit" class="btn btn-success" value="Ajouter" name="Ajouter" />
</form>
{% endblock %}

View File

@@ -0,0 +1,17 @@
{% extends "base.twig" %}
{% block title %}Modification du rôle{% endblock %}
{% block content %}
<h1>Modifier un rôle</h1>
<form method="post">
<label for="name">Nom</label>
<input type="text" class="form-control" name="name" id="name" value="{{ role.name }}">
<input type="submit" class="btn btn-success" name="Enregistrer" value="Enregistrer" />
<input type="hidden" name="id" value="{{ role.id }}">
</form>
{% endblock %}

View File

@@ -0,0 +1,36 @@
{% extends "base.twig" %}
{% block title %}Liste des rôles{% endblock %}
{% block content %}
<h1 class="">Liste des rôles</h1>
<table class="table table-striped table-bordered table-condensed">
<thead>
<th>ID</th>
<th>Nom</th>
</thead>
<tbody>
{# afficher les roles #}
{% for role in roles %}
<tr>
<td>{{ role.id }}</td>
<td>{{ role.name }}</td>
<td>
<button type="button" class="btn btn-info"
onclick="window.location.href='details.php?id={{ role.id }}'">Voir</button>
<button type="button" class="btn btn-warning"
onclick="window.location.href='edit.php?id={{ role.id }}'">Modifier</button>
<button type="button" class="btn btn-danger"
onclick="window.location.href='delete.php?id={{ role.id }}'">Supprimer</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-success mb-2" onclick="window.location.href='add.php'">Ajouter</button>
{% endblock %}

View File

@@ -0,0 +1,35 @@
{% extends "base.twig" %}
{% block title %}Ajouter utilisateur{% endblock %}
{% block content %}
<h1>Ajouter un utilisateur</h1>
<form class="form-group" role="search" method="post">
<label for="login">Utilisateur</label>
<input type="text" class="form-control" name="login" id="login">
<label for="password">Mot de passe</label>
<input type="password" class="form-control" name="password" id="password" />
<label for="firstname">Prénom</label>
<input type="text" class="form-control" name="firstname" id="firstname">
<label for="lastname">Nom de famille</label>
<input type="text" class="form-control" name="lastname" id="lastname" />
<label for="role">Role</label>
<select class="form-control" name="role" id="role-select">
{% for role in roles %}
<option value="{{ role.id }}">{{ role.name }}</option>
{% endfor %}
</select>
<label for="description">Description</label>
<textarea name="description" class="form-control"></textarea>
<input type="submit" class="btn btn-success" value="Ajouter" name="Ajouter" />
</form>
{% endblock %}

View File

@@ -0,0 +1,24 @@
{% extends "base.twig" %}
{% block title %}Détails de l'utilisateur{% endblock %}
{% block content %}
<h1>Détails pour lutilisateur {{ user.login }}</h1>
<div class="container bg-white w-25 border my-2">
<p>ID : {{ user.id }}</p>
<p>Login : {{ user.login }}</p>
<p>FirstName : {{ user.firstname }}</p>
<p>LastName : {{ user.lastname }}</p>
<p>Role : {{ user.role.name }}</p>
</div>
<div>
<button type="button" class="btn btn-warning"
onclick="window.location.href='edit.php?id={{ user.id }}'">Modifier</button>
<button type="button" class="btn btn-danger"
onclick="window.location.href='delete.php?id={{ user.id }}'">Supprimer</button>
</div>
{% endblock %}

View File

@@ -0,0 +1,28 @@
{% extends "base.twig" %}
{% block title %}Modification de l'utilisateur{% endblock %}
{% block content %}
<h1>Modifier un utilisateur</h1>
<form method="post">
<label for="login">Login</label>
<input type="text" class="form-control" name="login" id="login" value="{{ user.login }}">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" id="description" value="{{ user.description }}">
<label for="role">Role</label>
<select class="form-control" name="role" id="role-select">
{% for role in roles %}
<option value="{{ role.id }}" {% if role.id==user.role.id %} selected="selected" {% endif %}>{{ role.name }}
</option>
{% endfor %}
</select>
<input type="submit" class="btn btn-success m-2" name="Enregistrer" value="Enregistrer" />
<input type="hidden" name="id" value="{{ user.id }}">
</form>
{% endblock %}

View File

@@ -0,0 +1,43 @@
{% extends "base.twig" %}
{% block title %}Liste des utilisateurs{% endblock %}
{% block content %}
<h1 class="">Liste des utilisateurs</h1>
<table class="table table-striped table-bordered table-condensed">
<thead>
<th>ID</th>
<th>Login</th>
<th>Nom</th>
<th>Prenom</th>
<th>Rôle</th>
<th>Actions</th>
</thead>
<tbody>
{# afficher les utilisateurs #}
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.login }}</td>
<td>{{ user.lastname }}</td>
<td>{{ user.firstname }}</td>
<td>{{ user.role.name }}</td>
<td>
<button type="button" class="btn btn-info"
onclick="window.location.href='users/details.php?id={{ user.id }}'">Voir</button>
<button type="button" class="btn btn-warning"
onclick="window.location.href='users/edit.php?id= {{ user.id }}'">Modifier</button>
<button type="button" class="btn btn-danger"
onclick="window.location.href='users/delete.php?id= {{ user.id }}'">Supprimer</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-success mb-2" onclick="window.location.href='users/add.php'">Ajouter</button>
{% endblock %}