From ed87b44262f26d37a192d36239bd3573ee9fcd8a Mon Sep 17 00:00:00 2001
From: SofianeLasri <alasri250@gmail.com>
Date: Sun, 20 Nov 2022 15:26:49 +0100
Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20TP=207.=20Contournement=20d'un=20p?=
 =?UTF-8?q?robl=C3=A8me=20=C3=A0=20cause=20de=20l'usage=20de=20grandes=20r?=
 =?UTF-8?q?egex,=20ajout=20du=20support=20des=20commentaires=20sur=20les?=
 =?UTF-8?q?=20colones.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 controller/ControleurObjet.php                | 14 ++++++--
 index.php                                     |  5 +--
 models/Objet.php                              |  4 +--
 resources/views/formulaire-creation-objet.php | 33 ++++++++++++++++---
 4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/controller/ControleurObjet.php b/controller/ControleurObjet.php
index df12d81..039a97b 100644
--- a/controller/ControleurObjet.php
+++ b/controller/ControleurObjet.php
@@ -6,7 +6,7 @@ class ControleurObjet
      * Affiche la page de cration d'un objet et se charge également de sa création.
      * @return string
      */
-    public static function creerObjet() : string
+    public static function createObject() : string
     {
         $createdObject = [];
         // Si reçoie une requête POST pour créer un objet et qu'on a plus d'une entrée
@@ -36,8 +36,8 @@ class ControleurObjet
      */
     public static function describeTable() : string
     {
-        if(!empty($_GET["describeTable"])){
-            return json_encode(Objet::describeObject($_GET["describeTable"]));
+        if(!empty($_GET["tableName"])){
+            return json_encode(Objet::describeObject($_GET["tableName"]));
         } else {
             return json_encode([
                 "status" => "fail",
@@ -46,4 +46,12 @@ class ControleurObjet
             ]);
         }
     }
+
+    public static function modifyObject() : string
+    {
+        if(!empty($_GET["tableName"]) && !empty($_GET["id"])){
+
+        }
+        return "";
+    }
 }
\ No newline at end of file
diff --git a/index.php b/index.php
index 1806364..029da6f 100644
--- a/index.php
+++ b/index.php
@@ -1,7 +1,8 @@
 <?php
+error_reporting(E_ALL);
 ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
-error_reporting(E_ALL);
+ini_set('pcre.jit', 0); // Car sinon @content se bloque à 6196
 
 require_once("app/helpers/config.php");
 require_once("app/helpers/strings.php");
@@ -22,7 +23,7 @@ if (empty($_REQUEST["action"])) {
         echo match ($_REQUEST["action"]) {
             "lireAuteur"    => ControleurAuteur::lireAuteur(),
             "lireAuteurs"   => ControleurAuteur::lireAuteurs(),
-            "creerObjet"    => ControleurObjet::creerObjet(),
+            "creerObjet"    => ControleurObjet::createObject(),
             "lireAdherent"  => ControleurAdherent::lireAdherent(),
             "lireAdherents" => ControleurAdherent::lireAdherents(),
             "lireLivre"     => ControleurLivre::lireLivre(),
diff --git a/models/Objet.php b/models/Objet.php
index ad2df90..654d931 100644
--- a/models/Objet.php
+++ b/models/Objet.php
@@ -54,7 +54,7 @@ class Objet
 
         // Maintenant on va vérifier que la table existe
         try{
-            $tableExistsQuery = Database::pdo()->query("SELECT 1 FROM $tableName LIMIT 1");
+            Database::pdo()->query("SELECT 1 FROM $tableName LIMIT 1");
         } catch (PDOException $e) {
             return [
                 "status" => "fail",
@@ -108,7 +108,7 @@ class Objet
             ];
         }else{
             try{
-                $query = Database::pdo()->query("DESCRIBE $tableName");
+                $query = Database::pdo()->query("SHOW FULL COLUMNS FROM $tableName");
                 $columns = $query->fetchAll(PDO::FETCH_ASSOC);
             } catch (PDOException $e) {
                 return [
diff --git a/resources/views/formulaire-creation-objet.php b/resources/views/formulaire-creation-objet.php
index d5e49f3..5ea2049 100644
--- a/resources/views/formulaire-creation-objet.php
+++ b/resources/views/formulaire-creation-objet.php
@@ -56,7 +56,7 @@
     async function loadObjectTypeForm(objectType){
         objectForm.innerHTML = "";
 
-        await fetch('?action=describeTable&describeTable=' + objectType)
+        await fetch('?action=describeTable&tableName=' + objectType)
             .then(res => res.json())
             .then((out) => {
                 console.log('Output: ', out);
@@ -72,11 +72,25 @@
                         // On évite de proposer les clés primaires auto incrémentables
                         if(out.columns[i].Extra !== "auto_increment"){
                             let columnType = out.columns[i].Type;
-                            let match = columnType.match(/([a-z]*)/);
+                            let typeMatch = columnType.match(/([a-z]*)/);
+                            let comment = null;
+
+                            if(out.columns[i].Comment !== ""){
+                                let splitedComment = out.columns[i].Comment.split(',');
+                                for(let c = 0; c<splitedComment.length; c++){
+                                    let commentMatch = splitedComment[c].match(/(^[^%].*[^%]$)/);
+                                    if(commentMatch && commentMatch !== undefined){
+                                        comment = commentMatch[1];
+                                    }
+                                }
+
+                            }
+
                             objectForm.appendChild(
                                 createInputDiv(
-                                    typesList[match[1]],
-                                    out.columns[i].Field
+                                    typesList[typeMatch[1]],
+                                    out.columns[i].Field,
+                                    comment
                                 )
                             );
                         }
@@ -106,7 +120,7 @@
         objectForm.appendChild(submitButton);
     }
 
-    function createInputDiv(type, name){
+    function createInputDiv(type, name, description=null){
         let mainContainer = document.createElement("div");
         mainContainer.classList.add("mb-3");
 
@@ -129,6 +143,15 @@
 
         mainContainer.appendChild(input);
 
+        if(description != null) {
+            let small = document.createElement("small");
+            small.classList.add("form-text");
+            small.classList.add("text-muted");
+            small.innerText = description;
+
+            mainContainer.appendChild(small);
+        }
+
         return mainContainer;
     }
 </script>
-- 
GitLab