From 9736a6be78912dffb76ed26fb88514d7800142d2 Mon Sep 17 00:00:00 2001
From: Sofiane Lasri <alasri250@gmail.com>
Date: Tue, 22 Nov 2022 16:32:23 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20des=20boutons=20de=20modification=20d'o?=
 =?UTF-8?q?bjets=20et=20suppression=20des=20messages=20de=20r=C3=A9ussites?=
 =?UTF-8?q?.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 controller/ControleurAdherent.php | 3 +++
 controller/ControleurAuteur.php   | 3 +++
 controller/ControleurLivre.php    | 3 +++
 controller/ControleurObjet.php    | 4 +++-
 index.php                         | 1 +
 models/Objet.php                  | 5 +----
 resources/views/data-grid.php     | 4 ++++
 7 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/controller/ControleurAdherent.php b/controller/ControleurAdherent.php
index 390ab02..c8611b0 100644
--- a/controller/ControleurAdherent.php
+++ b/controller/ControleurAdherent.php
@@ -15,6 +15,9 @@ class ControleurAdherent extends ControleurObjet
         $adherents = Adherent::getAllAdherents();
         $objects = [];
         foreach ($adherents as $adherent){
+            $object['id_name'] = "login";
+            $object['id_value'] = $adherent->login;
+            $object['type'] = "Adherent";
             $object['title'] = $adherent->prenomAdherent . " " . $adherent->nomAdherent;
             $object['desc'] = "ID: $adherent->login<br>Date d'adhésion: {$adherent->dateAdhesion->format("Y-m-d")}";
             $object['url'] = "index.php?action=lireAdherent&login=$adherent->login";
diff --git a/controller/ControleurAuteur.php b/controller/ControleurAuteur.php
index 7936ad2..eddce32 100644
--- a/controller/ControleurAuteur.php
+++ b/controller/ControleurAuteur.php
@@ -16,6 +16,9 @@ class ControleurAuteur extends ControleurObjet
 
         $objects = [];
         foreach ($auteurs as $auteur){
+            $object['id_name'] = "numAuteur";
+            $object['id_value'] = $auteur->numAuteur;
+            $object['type'] = "Auteur";
             $object['title'] = $auteur->prenom . " " . $auteur->nom;
             $object['desc'] = "ID: $auteur->numAuteur<br>Date de naissance: $auteur->anneeNaissance";
             $object['url'] = "index.php?action=lireAuteur&numAuteur=$auteur->numAuteur";
diff --git a/controller/ControleurLivre.php b/controller/ControleurLivre.php
index f3ab48a..f3c14a4 100644
--- a/controller/ControleurLivre.php
+++ b/controller/ControleurLivre.php
@@ -16,6 +16,9 @@ class ControleurLivre extends ControleurObjet
 
         $objects = [];
         foreach ($livres as $livre){
+            $object['id_name'] = "numLivre";
+            $object['id_value'] = $livre->numLivre;
+            $object['type'] = "Livre";
             $object['title'] = $livre->titre;
             $object['desc'] = "ID: $livre->numLivre<br>Année de parution: $livre->anneeParution";
             $object['url'] = "index.php?action=lireLivre&numLivre=$livre->numLivre";
diff --git a/controller/ControleurObjet.php b/controller/ControleurObjet.php
index 039a97b..a97605d 100644
--- a/controller/ControleurObjet.php
+++ b/controller/ControleurObjet.php
@@ -49,8 +49,10 @@ class ControleurObjet
 
     public static function modifyObject() : string
     {
-        if(!empty($_GET["tableName"]) && !empty($_GET["id"])){
+        if(!empty($_GET["tableName"]) && !empty($_GET["id_name"]) && !empty($_GET["id_value"])){
 
+            $object = Objet::getObject($_GET["tableName"], ['name' => $_GET["id_name"], 'value' => $_GET["id_value"]]);
+            return json_encode($object);
         }
         return "";
     }
diff --git a/index.php b/index.php
index 029da6f..60e332d 100644
--- a/index.php
+++ b/index.php
@@ -29,6 +29,7 @@ if (empty($_REQUEST["action"])) {
             "lireLivre"     => ControleurLivre::lireLivre(),
             "lireLivres"    => ControleurLivre::lireLivres(),
             "describeTable" => ControleurObjet::describeTable(),
+            "modifierObjet" => ControleurObjet::modifyObject(),
             default         => ControleurAuteur::lireAuteur(),
         };
     } catch (Exception $e) {
diff --git a/models/Objet.php b/models/Objet.php
index 43cd3e8..1ed669f 100644
--- a/models/Objet.php
+++ b/models/Objet.php
@@ -42,7 +42,6 @@ class Objet
     public static function addObject(String $tableName, array $columns) : array
     {
         $result["status"] = "success";
-        $result["message"] = "Insertion réussie";
 
         $validation = Objet::verifyTableAndFormat($tableName, $columns);
         if($validation["status"] !== "success"){
@@ -109,7 +108,6 @@ class Objet
     public static function modifyObject(String $tableName, array $id, array $columns) : array
     {
         $result["status"] = "success";
-        $result["message"] = "Modification réussie";
 
         $validation = Objet::verifyTableAndFormat($tableName, $columns);
         if($validation["status"] !== "success"){
@@ -163,7 +161,7 @@ class Objet
         $insertQuery = Database::pdo()->prepare($queryString);
         try{
             // Et on l'exécute
-            $insertQuery->execute([$id["name"]]);
+            $insertQuery->execute([$id["value"]]);
         } catch (PDOException $e) {
             return [
                 "status" => "fail",
@@ -174,7 +172,6 @@ class Objet
 
         return [
             "status" => "success",
-            "message" => "Récupération réussie.",
             "data" => $insertQuery->fetch(PDO::FETCH_ASSOC)
         ];
     }
diff --git a/resources/views/data-grid.php b/resources/views/data-grid.php
index 982e309..ee77f58 100644
--- a/resources/views/data-grid.php
+++ b/resources/views/data-grid.php
@@ -18,6 +18,10 @@
                    href="{{ $object['url'] }}">
                     Lire les détails
                 </a>
+                <a class="btn btn-outline-primary"
+                href="index.php?action=modifierObjet&tableName={{ $object['type'] }}&id_name={{ $object['id_name'] }}&id_value={{ $object['id_value'] }}">
+                    Modifier
+                </a>
             </div>
         </div>
         @endforeach
-- 
GitLab