rename folder
This commit is contained in:
37
tpCrudTwig/add.php
Normal file
37
tpCrudTwig/add.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?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');
|
||||
2
tpCrudTwig/close.php
Normal file
2
tpCrudTwig/close.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
$db = null;
|
||||
5
tpCrudTwig/composer.json
Normal file
5
tpCrudTwig/composer.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"twig/twig": "^3.14"
|
||||
}
|
||||
}
|
||||
14
tpCrudTwig/connect.php
Normal file
14
tpCrudTwig/connect.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?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();
|
||||
}
|
||||
11
tpCrudTwig/delete.php
Normal file
11
tpCrudTwig/delete.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?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');
|
||||
29
tpCrudTwig/details.php
Normal file
29
tpCrudTwig/details.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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');
|
||||
38
tpCrudTwig/edit.php
Normal file
38
tpCrudTwig/edit.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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');
|
||||
18
tpCrudTwig/index.php
Normal file
18
tpCrudTwig/index.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?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');
|
||||
12
tpCrudTwig/modele/twig.php
Normal file
12
tpCrudTwig/modele/twig.php
Normal 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);
|
||||
37
tpCrudTwig/style/style.css
Normal file
37
tpCrudTwig/style/style.css
Normal file
@@ -0,0 +1,37 @@
|
||||
table {
|
||||
width: 100%;
|
||||
margin: auto;
|
||||
line-height: 2em;
|
||||
}
|
||||
|
||||
table tr td {
|
||||
border-style: solid;
|
||||
border-color: black;
|
||||
border-width: 0.1em;
|
||||
text-align: center;
|
||||
padding: 2px;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: gray;
|
||||
}
|
||||
|
||||
table tr:nth-child(even) {
|
||||
background-color: darkgray;
|
||||
}
|
||||
|
||||
button {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
body {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
}
|
||||
|
||||
div {
|
||||
background-color: lightgray;
|
||||
padding: 10px;
|
||||
text-align: center;
|
||||
}
|
||||
44
tpCrudTwig/test.html
Normal file
44
tpCrudTwig/test.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Bootstrap Example</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<h2>Horizontal form</h2>
|
||||
<form class="form-horizontal" action="/action_page.php">
|
||||
<div class="form-group">
|
||||
<label class="control-label col-sm-2" for="email">Email:</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="control-label col-sm-2" for="pwd">Password:</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<div class="checkbox">
|
||||
<label><input type="checkbox" name="remember"> Remember me</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
33
tpCrudTwig/vue/add.twig
Normal file
33
tpCrudTwig/vue/add.twig
Normal file
@@ -0,0 +1,33 @@
|
||||
<!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>
|
||||
29
tpCrudTwig/vue/details.twig
Normal file
29
tpCrudTwig/vue/details.twig
Normal file
@@ -0,0 +1,29 @@
|
||||
<!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 l’utilisateur {{ 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>
|
||||
30
tpCrudTwig/vue/edit.twig
Normal file
30
tpCrudTwig/vue/edit.twig
Normal file
@@ -0,0 +1,30 @@
|
||||
<!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>
|
||||
51
tpCrudTwig/vue/index.twig
Normal file
51
tpCrudTwig/vue/index.twig
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
<!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="alert alert-success" role="alert">
|
||||
A simple success alert—check it out!
|
||||
</div>
|
||||
<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>
|
||||
Reference in New Issue
Block a user