diff --git a/models/Objet.php b/models/Objet.php index 654d931af57f9ce32a271e9de0e0f1dc227964a1..67aef6b4e4ca194df64403762071ff736ca825f0 100644 --- a/models/Objet.php +++ b/models/Objet.php @@ -44,35 +44,9 @@ class Objet $result["status"] = "success"; $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{ - 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." - ]; - } + $validation = Objet::verifyTableAndFormat($tableName, $columns); + if($validation["status"] !== "success"){ + return $validation; } // On prépare la requête @@ -124,4 +98,81 @@ 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"){ + return $validation; + } + if(empty($id["name"]) || empty($id["value"]) || !isAlphaNumeric($id["name"])){ + return [ + "status" => "fail", + "message" => "L'identifiant de la table envoyé est incorrect!" + ]; + } + + // On prépare la requête + $queryString = ("UPDATE $tableName SET " . http_build_query($columns, '', ',') . " WHERE " . $id["name"] . "=:" . $id["name"]); + $insertQuery = Database::pdo()->prepare($queryString); + try{ + // Et on l'exécute + $insertQuery->execute($columns); + } catch (PDOException $e) { + return [ + "status" => "fail", + "message" => "La modification de l'objet a échoué.", + "pdoError" => $e + ]; + } + + return $result; + } + + /** + * Permet de vérifier le format du nom de la table et des nom des colonnes + vérifier l'existance de la table. + * @param String $tableName + * @param array $columns + * @return array|string[] + */ + public static function verifyTableAndFormat(string $tableName, array $columns) : array + { + // 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{ + 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." + ]; + } + } + return [ + "status" => "success", + "message" => "Validation réussie." + ]; + } } \ No newline at end of file