Exercice préparatoire au TP 2: Solution orientée objet

index.php | admin/database.php | admin/employeDao.php | admin/employe.php | admin/abstractDao.php

index.php

<?php require("./admin/database.php"); require("./admin/employeDao.php"); if ($_SERVER['REQUEST_METHOD'] === 'POST'){ if (isset($_POST['Ajouter'])) { if (!empty($_POST['prenom']) && !empty($_POST['nom']) && !empty($_POST['codeUsager']) && !empty($_POST['age']) && !empty($_POST['departement'])) { $empl = new Employe($_POST['prenom'], $_POST['nom'], $_POST['codeUsager'], $_POST['age'], $_POST['departement']); EmployeDao::add($empl); } else { echo '<script>alert("Il faut remplir toutes les informations pour ajouter un employé !!")</script>'; } } elseif (isset($_POST['Supprimer'])) { if (!empty($_POST['supCodeUsager'])) { EmployeDao::delete($_POST['supCodeUsager']); } else { echo '<script>alert("Veuillez indiquer le code d\'usager de l\'employé à supprimer !!")</script>'; } } } ?> <!DOCTYPE html> <html lang="fr"> <head> <style> table{ background-color: lightblue; margin-left: 40px; margin-bottom: 40px; width: 550px; text-align: center; border: solid black 1px; border-collapse: collapse; } td, tr{ padding: 20px; border: solid black 2px; } tr:first-child td{ padding: 1px; font-weight: bold; } body{ font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; font-size: large; } #lblAjouter, #lblSupprimer{ font-size: 20px; margin: 20px; } #inputAjouter, #inputSupprimer{ margin: 20px; } #Ajouter, #Supprimer { padding: 10px; } #inputAjouter, #lblAjouter, #ajouter, #inputSupprimer, #lblSupprimer, #supprimer{ display:inline-block; } */ fieldset{ width: 200px; } </style> <meta charset="UTF-8"> <title>Gestion d'employé</title> </head> <body> <table> <tr> <?php $stmt = EmployeDao::getColumnsName(); foreach($stmt as $row) { echo "<td>" . ucfirst($row['COLUMN_NAME']) ."</td>"; } echo "</tr>"; $stmt = EmployeDao::list(); foreach($stmt as $row) { echo "<tr>"; echo "<td>" . $row['prenom'] . "</td>"; echo "<td>" . $row['nom'] . "</td>"; echo "<td>" . $row['codeUsager'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "<td>" . $row['departement'] . "</td>"; echo "</tr>"; } ?> </table> <form method="POST"> <fieldset> <div id="lblAjouter"> <label>Prénom : </label><br> <label>Nom : </label><br> <label>Code d'usager : </label><br> <label>Âge : </label><br> <label>Département : </label><br> </div> <div id="inputAjouter"> <input type="text" name="prenom"><br> <input type="text" name="nom"><br> <input type="text" name="codeUsager"><br> <input type="number" name="age"><br> <select id="departement" name="departement"> <option value="1">Administration</option> <option value="2">Comptabilité</option> <option value="3">Informatique</option> <option value="4">Vente</option> </select> </div> <div id="ajouter"> <input type="submit" value="Ajouter" name="Ajouter"> </div><br> <div id="lblSupprimer"> <label>Code d'usager : </label><br> </div> <div id="inputSupprimer"> <input type="text" name="supCodeUsager"><br> </div> <div id="supprimer"> <input type="submit" value="Supprimer" name="Supprimer"> </div> </fieldset> </form> </body> </html>

admin/database.php

<?php class Database { private static $host = "127.0.0.1"; public static $databaseName = "demonstration"; private static $user = "demonstration"; private static $password = "jmleprof"; private static $charset = "utf8"; private static $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]; private static $pdo = null; public static function connect() { try { self::$pdo = new PDO("mysql:host=" . self::$host . ";dbname=" . self::$databaseName . ";charset=" . self::$charset, self::$user, self::$password, self::$options); } catch (PDOException $e) { throw new Exception($e->getMessage(), $e->getCode()); } return self::$pdo; } public static function disconnect() { self::$pdo = null; } } ?>

admin/employeDao.php

<?php require_once("./admin/database.php"); require_once("./admin/employe.php"); require_once("./admin/abstractDao.php"); class EmployeDao extends AbstractDao { public static function add($employe) { $sql = "INSERT INTO employe(prenom, nom, codeUsager, age, departement) VALUES (:prenom, :nom, :codeUsager, :age, :departement)"; $lastId = self::process($sql, ["prenom" => $employe->getPrenom(), "nom" => $employe->getNom(), "codeUsager" => $employe->getCodeUsager(), "age" => $employe->getAge(), "departement" => $employe->getDepartement()]); return $lastId; } public static function delete($codeUsager) { $sql = "DELETE FROM employe WHERE codeUsager = :codeUsager "; self::process($sql, ["codeUsager" => $codeUsager]); } public static function list() { $pdo = Database::connect(); $stmt = $pdo->query("SELECT * FROM employe"); Database::disconnect(); return $stmt; } public static function get($codeUsager) { $sql = "SELECT * FROM employe WHERE codeUsager = :codeUsager"; $stmt = self::process($sql, ["codeUsager" => $codeUsager]); $newEmploye = new Employe($stmt["prenom"],$stmt["nom"],$stmt["codeUsager"],$stmt["age"],$stmt["departement"]); return $newEmploye; } public static function getColumnsName() { $pdo = Database::connect(); $stmt = $pdo->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . Database::$databaseName ."' AND TABLE_NAME = 'employe' AND COLUMN_KEY <> 'PRI' ORDER BY ORDINAL_POSITION;"); Database::disconnect(); return $stmt; } } ?>

admin/employe.php

<?php class Employe { private $id = 0; private $prenom; private $nom; private $codeUsager; private $age; private $departement; public function __construct($prenom, $nom, $codeUsager, $age, $departement) { $this->prenom = $prenom; $this->nom = $nom; $this->codeUsager = $codeUsager; $this->age = $age; $this->departement = $departement; } public function getId() { return $this->id; } public function getPrenom() { return $this->prenom; } public function getNom() { return $this->nom; } public function getCodeUsager() { return $this->codeUsager; } public function getAge() { return $this->age; } public function getDepartement() { return $this->departement; } } ?>

admin/abstractDao.php

<?php abstract class AbstractDao { public static function process($sql, $parameters) { $pdo = Database::connect(); try{ $stmt = $pdo->prepare($sql); $stmt->execute($parameters); } catch(Exception $e){ throw new Exception($e->getMessage()); exit; } $lastId = $pdo->lastInsertId(); Database::disconnect(); return $lastId; } } ?>