moved things
This commit is contained in:
@@ -1,2 +0,0 @@
|
|||||||
<?php
|
|
||||||
$db = null;
|
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
@@ -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');
|
|
||||||
@@ -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');
|
|
||||||
@@ -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');
|
|
||||||
@@ -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');
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/* inclure l'autoloader */
|
/* inclure l'autoloader */
|
||||||
require_once 'vendor/autoload.php';
|
require_once('../vendor/autoload.php');
|
||||||
/* templates chargés à partir du système de fichiers (répertoire vue) */
|
/* templates chargés à partir du système de fichiers (répertoire vue) */
|
||||||
$loader = new Twig\Loader\FilesystemLoader('vue');
|
$loader = new Twig\Loader\FilesystemLoader('../templates');
|
||||||
/* options : prod = cache dans le répertoire cache, dev = pas de cache */
|
/* options : prod = cache dans le répertoire cache, dev = pas de cache */
|
||||||
$options_prod = array('cache' => 'cache', 'autoescape' => true);
|
$options_prod = array('cache' => 'cache', 'autoescape' => true);
|
||||||
$options_dev = array('cache' => false, 'autoescape' => true);
|
$options_dev = array('cache' => false, 'autoescape' => true);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once('connect.php');
|
|
||||||
|
require_once('../src/User.php');
|
||||||
|
|
||||||
if (isset($_POST)) {
|
if (isset($_POST)) {
|
||||||
if (
|
if (
|
||||||
isset($_POST['login']) && !empty($_POST['login'])
|
isset($_POST['login']) && !empty($_POST['login'])
|
||||||
@@ -15,23 +17,13 @@ if (isset($_POST)) {
|
|||||||
$role = strip_tags($_POST['role']);
|
$role = strip_tags($_POST['role']);
|
||||||
$firstname = strip_tags($_POST['firstname']);
|
$firstname = strip_tags($_POST['firstname']);
|
||||||
$description = strip_tags($_POST['description']);
|
$description = strip_tags($_POST['description']);
|
||||||
$sql = "INSERT INTO `users` (`login`, `password`, `firstname`, `lastname`, `description`, `role`, `enabled`) VALUES (:login, :password, :firstname, :lastname, :description, :role, :enabled);";
|
AddUser($login, $password, $lastname, $role, $firstname, $description);
|
||||||
$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 !";
|
$_SESSION['message'] = "Utilisateur ajouté avec succès !";
|
||||||
header('Location: index.php');
|
header('Location: index.php');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
require_once('close.php');
|
|
||||||
|
|
||||||
$vue = "add.twig";
|
$vue = "users/add.twig";
|
||||||
$donnees = array();
|
$donnees = array();
|
||||||
|
|
||||||
require_once('modele/twig.php');
|
require_once('../modele/twig.php');
|
||||||
8
tpCrudTwig/public/delete.php
Normal file
8
tpCrudTwig/public/delete.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
require_once('../src/User.php');
|
||||||
|
|
||||||
|
if (isset($_GET['id']) && !empty($_GET['id'])) {
|
||||||
|
$id = strip_tags($_GET['id']);
|
||||||
|
DeleteUser($id);
|
||||||
|
header('Location: index.php');
|
||||||
|
}
|
||||||
20
tpCrudTwig/public/details.php
Normal file
20
tpCrudTwig/public/details.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
require_once('../src/User.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');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$vue = "users/details.twig";
|
||||||
|
$donnees = array("user" => $user);
|
||||||
|
|
||||||
|
require_once('../modele/twig.php');
|
||||||
28
tpCrudTwig/public/edit.php
Normal file
28
tpCrudTwig/public/edit.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('../src/User.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']);
|
||||||
|
UpdateUser($id, $login, $description, $role);
|
||||||
|
header('Location: index.php');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset($_GET['id']) && !empty($_GET['id'])) {
|
||||||
|
$id = strip_tags($_GET['id']);
|
||||||
|
$result = GetUser($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$vue = "users/edit.twig";
|
||||||
|
$donnees = array("user" => $result);
|
||||||
|
|
||||||
|
require_once('../modele/twig.php');
|
||||||
9
tpCrudTwig/public/index.php
Normal file
9
tpCrudTwig/public/index.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// On inclut la connexion à la base
|
||||||
|
require_once('../src/User.php');
|
||||||
|
|
||||||
|
$vue = "users/index.twig";
|
||||||
|
$donnees = array("users" => GetUsers());
|
||||||
|
|
||||||
|
require_once('../modele/twig.php');
|
||||||
25
tpCrudTwig/src/DataBase.php
Normal file
25
tpCrudTwig/src/DataBase.php
Normal 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;
|
||||||
|
}
|
||||||
0
tpCrudTwig/src/Role.php
Normal file
0
tpCrudTwig/src/Role.php
Normal file
88
tpCrudTwig/src/User.php
Normal file
88
tpCrudTwig/src/User.php
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('DataBase.php');
|
||||||
|
|
||||||
|
function GetUsers()
|
||||||
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
|
ConnectDataBase();
|
||||||
|
|
||||||
|
$sql = 'SELECT * FROM `users`';
|
||||||
|
$query = $db->prepare($sql);
|
||||||
|
$query->execute();
|
||||||
|
$result = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
CloseDataBase();
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function GetUser(int $id)
|
||||||
|
{
|
||||||
|
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 $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(int $id, string $login, string $description, string $role)
|
||||||
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
|
ConnectDataBase();
|
||||||
|
|
||||||
|
$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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
0
tpCrudTwig/templates/base.twig
Normal file
0
tpCrudTwig/templates/base.twig
Normal file
@@ -12,9 +12,6 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="alert alert-success" role="alert">
|
|
||||||
A simple success alert—check it out!
|
|
||||||
</div>
|
|
||||||
<div class="container text-center bg-light">
|
<div class="container text-center bg-light">
|
||||||
<h1 class="">Liste des utilisateurs</h1>
|
<h1 class="">Liste des utilisateurs</h1>
|
||||||
<table class="table table-striped table-bordered table-condensed">
|
<table class="table table-striped table-bordered table-condensed">
|
||||||
Reference in New Issue
Block a user