This commit is contained in:
2024-10-11 17:15:04 +02:00
parent a390944917
commit fdc4037786
11 changed files with 163 additions and 138 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
appli/vendor
appli/vendor
crud/vendor

View File

@@ -30,33 +30,8 @@ if (isset($_POST)) {
}
}
require_once('close.php');
?>
<!DOCTYPE html>
<html lang="en">
$vue = "add.twig";
$donnees = array();
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style/style.css"/>
<title>Ajouter utilisateur</title>
</head>
<body>
<form method="post">
<label for="login">Utilisateur</label>
<input type="text" name="login" id="login">
<label for="password">Mot de passe</label>
<input type="text" name="password" id="password" />
<label for="firstname">Prénom</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Nom de famille</label>
<input type="text" name="lastname" id="lastname" />
<label for="role">Role</label>
<input type="number" name="role" id="role">
<label for="description">Description</label>
<textarea name="description"></textarea>
<button>Enregistrer</button>
</form>
</body>
</html>
require_once('modele/twig.php');

View File

@@ -3,7 +3,7 @@ try {
$host = "localhost";
$user = "root";
$password = "2487169350";
$password = "motdepasse";
// Connexion à la bdd
$db = new PDO("mysql:host=$host;dbname=cruddb", $user, $password);

View File

@@ -21,29 +21,9 @@ if (isset($_GET['id']) && !empty($_GET['id'])) {
header('Location: index.php');
}
require_once('close.php');
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Détails de l'utilisateur</title>
<link rel="stylesheet" href="style/style.css" />
</head>
<body>
<div>
<h1>Détails pour lutilisateur <?= $user['login'] ?></h1>
<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>
<p>
<button onclick="window.location.href='edit.php?id=<?= $user['id'] ?>'">Modifier</button>
<button onclick="window.location.href='delete.php?id=<?= $user['id'] ?>'">Supprimer</button>
</p>
</div>
</body>
$vue = "details.twig";
$donnees = array("user" => $user);
</html>
require_once('modele/twig.php');

View File

@@ -31,43 +31,8 @@ if (isset($_GET['id']) && !empty($_GET['id'])) {
$result = $query->fetch();
}
require_once('close.php');
?>
<!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 rel="stylesheet"
href="style/style.css">
</head>
$vue = "edit.twig";
$donnees = array("user" => $result);
<body>
<div>
<h1>Modifier un utilisateur</h1>
<form method="post">
<p>
<label for="login">Login</label>
<input type="text" name="login" id="login" value="<?= $result['login']
?>">
</p>
<p>
<label for="description">Description</label>
<input type="text" name="description" id="description" value="<?=
$result['description'] ?>">
</p>
<p>
<label for="role">Role</label>
<input type="number" name="role" id="role" value="<?= $result['role']
?>">
</p>
<p>
<button>Enregistrer</button>
</p>
<input type="hidden" name="id" value="<?= $result['id'] ?>">
</form>
</div>
</body>
</html>
require_once('modele/twig.php');

View File

@@ -1,4 +1,5 @@
<?php
// On inclut la connexion à la base
require_once('connect.php');
// On écrit notre requête
@@ -10,52 +11,8 @@ $query->execute();
// On stocke le résultat dans un tableau associatif
$result = $query->fetchAll(PDO::FETCH_ASSOC);
require_once('close.php');
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style/style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Liste des utilisateurs</title>
</head>
$vue = "index.twig";
$donnees = array("users" => $result);
<body>
<div>
<h1>Liste des utilisateurs</h1>
<table>
<thead>
<th>ID</th>
<th>Login</th>
<th>Nom</th>
<th>Prenom</th>
<th>Rôle</th>
<th>Actions</th>
</thead>
<tbody>
<?php
foreach ($result as $user) {
?>
<tr>
<td><?= $user['id'] ?></td>
<td><?= $user['login'] ?></td>
<td><?= $user['firstname'] ?></td>
<td><?= $user['lastname'] ?></td>
<td><?= $user['role'] ?></td>
<td>
<button onclick="window.location.href='details.php?id=<?= $user['id'] ?>'">Voir</button>
<button onclick="window.location.href='edit.php?id=<?= $user['id'] ?>'">Modifier</button>
<button onclick="window.location.href='delete.php?id=<?= $user['id'] ?>'">Supprimer</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<button onclick="window.location.href='add.php'">Ajouter</button>
</div>
</body>
</html>
require_once('modele/twig.php');

12
crud/modele/twig.php Normal file
View File

@@ -0,0 +1,12 @@
<?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);

28
crud/vue/add.twig Normal file
View File

@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style/style.css"/>
<title>Ajouter utilisateur</title>
</head>
<body>
<form method="post">
<label for="login">Utilisateur</label>
<input type="text" name="login" id="login">
<label for="password">Mot de passe</label>
<input type="text" name="password" id="password" />
<label for="firstname">Prénom</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Nom de famille</label>
<input type="text" name="lastname" id="lastname" />
<label for="role">Role</label>
<input type="number" name="role" id="role">
<label for="description">Description</label>
<textarea name="description"></textarea>
<button>Enregistrer</button>
</form>
</body>
</html>

25
crud/vue/details.twig Normal file
View File

@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Détails de l'utilisateur</title>
<link rel="stylesheet" href="style/style.css" />
</head>
<body>
<div>
<h1>Détails pour lutilisateur {{ user.login }}</h1>
<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>
<p>
<button onclick="window.location.href='edit.php?id={{ user.id }}'">Modifier</button>
<button onclick="window.location.href='delete.php?id={{ user.id }}'">Supprimer</button>
</p>
</div>
</body>
</html>

36
crud/vue/edit.twig Normal file
View File

@@ -0,0 +1,36 @@
<!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 rel="stylesheet"
href="style/style.css">
</head>
<body>
<div>
<h1>Modifier un utilisateur</h1>
<form method="post">
<p>
<label for="login">Login</label>
<input type="text" name="login" id="login" value="{{ user.login }}">
</p>
<p>
<label for="description">Description</label>
<input type="text" name="description" id="description" value="{{ user.description }}">
</p>
<p>
<label for="role">Role</label>
<input type="number" name="role" id="role" value="{{ user.role }}">
</p>
<p>
<button>Enregistrer</button>
</p>
<input type="hidden" name="id" value="{{ user.id }}">
</form>
</div>
</body>
</html>

46
crud/vue/index.twig Normal file
View File

@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style/style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Liste des utilisateurs</title>
</head>
<body>
<div>
<h1>Liste des utilisateurs</h1>
<table>
<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 onclick="window.location.href='details.php?id={{ user.id }}'">Voir</button>
<button onclick="window.location.href='edit.php?id= {{ user.id }}'">Modifier</button>
<button onclick="window.location.href='delete.php?id= {{ user.id }}'">Supprimer</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button onclick="window.location.href='add.php'">Ajouter</button>
</div>
</body>
</html>