From 68493b64fd87f4955b4727633564a3dcf3ed1a3d Mon Sep 17 00:00:00 2001
From: SofianeLasri <alasri250@gmail.com>
Date: Sun, 13 Nov 2022 13:52:27 +0100
Subject: [PATCH] Objet::insertInDatabase(String $tableName, array $columns)

---
 TP6/app/helpers/strings.php                   | 13 ++++
 TP6/controller/ControleurAuteur.php           |  6 +-
 TP6/controller/ControleurObjet.php            |  8 +++
 TP6/index.php                                 |  1 +
 TP6/models/Objet.php                          | 60 +++++++++++++++++++
 ...teur.php => formulaire-creation-objet.php} |  0
 6 files changed, 86 insertions(+), 2 deletions(-)
 create mode 100644 TP6/app/helpers/strings.php
 rename TP6/resources/views/{formulaire-creation-auteur.php => formulaire-creation-objet.php} (100%)

diff --git a/TP6/app/helpers/strings.php b/TP6/app/helpers/strings.php
new file mode 100644
index 0000000..1b1f58a
--- /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 e743543..3ac902f 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 04f3448..4f71ed4 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 0804c19..1463542 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 28c1223..24a0740 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
-- 
GitLab