diff --git a/TP6/app/helpers/strings.php b/TP6/app/helpers/strings.php new file mode 100644 index 0000000000000000000000000000000000000000..1b1f58a8d011ffe931c17ccaae4efc377cb33011 --- /dev/null +++ b/TP6/app/helpers/strings.php @@ -0,0 +1,13 @@ +<?php +/** + * Vérifie qu'une chaîne de caractère est bien au format alphanumérique (a-zA-Zs0-9|_) + * @param $string String + * @return bool boolean + */ +function isAlphaNumeric($string) : bool +{ + if(!preg_match("/^([a-zA-Z0-9|_]*)$/", $string)){ + return false; + } + return true; +} \ No newline at end of file diff --git a/TP6/controller/ControleurAuteur.php b/TP6/controller/ControleurAuteur.php index e7435431afa7fd5dc584e6f71bb2771c3d876370..3ac902f094b64752a56898f2bbd3eb9ee9fe031d 100644 --- a/TP6/controller/ControleurAuteur.php +++ b/TP6/controller/ControleurAuteur.php @@ -43,6 +43,10 @@ class ControleurAuteur extends ControleurObjet ]); } + /** + * Charge la page de création d'auteur + * @return string + */ public static function creerAuteur() : string { $insertMessage = []; @@ -62,5 +66,3 @@ class ControleurAuteur extends ControleurObjet ]); } } - -?> diff --git a/TP6/controller/ControleurObjet.php b/TP6/controller/ControleurObjet.php index 04f34485eedd31828911f013ce59e4c153a7235d..4f71ed4ba970788a2b70e747916930f72c8d0097 100644 --- a/TP6/controller/ControleurObjet.php +++ b/TP6/controller/ControleurObjet.php @@ -12,4 +12,12 @@ class ControleurObjet $titre = "Listes des " . strtolower(static::$object) . "s"; return $titre; } + + public static function creerObjet() : string + { + return view('formulaire-creation-auteur', [ + 'pageTitle' => 'Créer un auteur', + 'insertMessage' => $insertMessage + ]); + } } \ No newline at end of file diff --git a/TP6/index.php b/TP6/index.php index 0804c198327327e175ab2289aa0ff9354b548fa2..1463542eafae2e4be24bda999b48b85b957f4e04 100644 --- a/TP6/index.php +++ b/TP6/index.php @@ -4,6 +4,7 @@ ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once("app/helpers/config.php"); +require_once("app/helpers/strings.php"); require_once("app/helpers/views.php"); require_once("app/models/Database.php"); diff --git a/TP6/models/Objet.php b/TP6/models/Objet.php index 28c122325cdc9b7e41661e22ab2b031059f4a91e..24a0740a7a7d4e4bf243b67492285e036147a157 100644 --- a/TP6/models/Objet.php +++ b/TP6/models/Objet.php @@ -32,4 +32,64 @@ class Objet { $this->$name = $value; } + + /** + * Permet d'insérer un objet dans la base de données + * @param String $tableName + * @param array $columns + * @return array|string[] + */ + public static function insertInDatabase(String $tableName, array $columns) : array + { + $result["status"] = "sucess"; + $result["message"] = "Insertion réussie."; + + // On va vérifier que $tableName est bien en format alphanumérique + if(!isAlphaNumeric($tableName)){ + return [ + "status" => "fail", + "message" => "Le format du nom de la table est incorrect." + ]; + } + + // Maintenant on va vérifier que la table existe + try{ + $tableExistsQuery = Database::pdo()->query("SELECT 1 FROM {$tableName} LIMIT 1"); + } catch (PDOException $e) { + return [ + "status" => "fail", + "message" => "La table {$tableName} ne semble pas exister.", + "pdoError" => $e + ]; + } + + // Nous pouvons commencer l'insertion + + // On va vérifier que l'index de chaque est bien en format alphanumérique + foreach($columns as $index => $value){ + if(!isAlphaNumeric($index)){ + return [ + "status" => "fail", + "message" => "Le format " . htmlspecialchars($index) . " est incorrect." + ]; + } + } + + // On prépare la requête + $queryString = ("INSERT INTO {$tableName} (" . implode(", ", array_keys($columns)) + . ") VALUES (:" . implode(", :", array_keys($columns)) . ")"); + $insertQuery = Database::pdo()->prepare($queryString); + try{ + // Et on l'exécute + $insertQuery->execute($columns); + } catch (PDOException $e) { + return [ + "status" => "fail", + "message" => "L'insertion de l'objet a échoué.", + "pdoError" => $e + ]; + } + + return $result; + } } \ No newline at end of file diff --git a/TP6/resources/views/formulaire-creation-auteur.php b/TP6/resources/views/formulaire-creation-objet.php similarity index 100% rename from TP6/resources/views/formulaire-creation-auteur.php rename to TP6/resources/views/formulaire-creation-objet.php