From df8264a8ece614993b1693266deddf0d08b56e1b Mon Sep 17 00:00:00 2001 From: jfgiraud Date: Thu, 20 Aug 2026 18:04:52 +0000 Subject: [PATCH] Front + gestion des utilisateurs + gestion des indices --- html/tableindice.html | 275 ++++++++++++- js/tableindice.js | 686 ++++++++++++++++++++++++-------- php/api_update_table_indice.php | 322 +++++++++++++++ 3 files changed, 1110 insertions(+), 173 deletions(-) create mode 100644 php/api_update_table_indice.php diff --git a/html/tableindice.html b/html/tableindice.html index 8ca2e71..54ef914 100644 --- a/html/tableindice.html +++ b/html/tableindice.html @@ -102,9 +102,7 @@ class="bg-slate-900 border border-slate-700 text-white text-xs rounded-xl px-4 py-2 focus:outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500" > - - @@ -144,66 +142,92 @@ ================================================= -->
- +
+ + + + + + + + + + + + + + + + + + + + + + @@ -239,9 +263,11 @@ class="px-6 py-5 border-b border-slate-700 flex justify-between items-center" >
-

Nouvel enregistrement

+

+ Nouvel enregistrement +

-

+

Ajouter une nouvelle action ou un indice

@@ -347,7 +373,7 @@ id="nouveauSymbole" required autocomplete="off" - class="w-full bg-slate-900 border border-slate-700 rounded-xl px-4 py-3 text-sm text-white uppercase placeholder-slate-500 focus:outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500" + class="w-full bg-slate-900 border border-slate-700 rounded-xl px-4 py-3 text-sm text-white uppercase placeholder-slate-500" placeholder="AC" /> @@ -435,6 +461,225 @@ + + + + diff --git a/js/tableindice.js b/js/tableindice.js index 344edea..0d452b9 100644 --- a/js/tableindice.js +++ b/js/tableindice.js @@ -7,27 +7,41 @@ */ const API_LIST_URL = "../php/api_list_full_table_indice.php"; + const API_CREATE_URL = "../php/api_create_table_indice.php"; +const API_UPDATE_URL = "../php/api_update_table_indice.php"; + /* * ========================================================= - * ELEMENTS DOM + * ELEMENTS TABLEAU * ========================================================= */ const tableauIndices = document.getElementById("tableauIndices"); + const compteur = document.getElementById("compteur"); + const message = document.getElementById("message"); const btnActualiser = document.getElementById("btnActualiser"); -const btnNouveau = document.getElementById("btnNouveau"); + const filtreIndice = document.getElementById("filtreIndice"); +/* + * ========================================================= + * ELEMENTS NOUVEAU + * ========================================================= + */ + +const btnNouveau = document.getElementById("btnNouveau"); + const modalNouvelEnregistrement = document.getElementById( "modalNouvelEnregistrement", ); const btnFermerModal = document.getElementById("btnFermerModal"); + const btnAnnuler = document.getElementById("btnAnnuler"); const formNouvelEnregistrement = document.getElementById( @@ -35,15 +49,18 @@ const formNouvelEnregistrement = document.getElementById( ); const nouvelIndice = document.getElementById("nouvelIndice"); + const nouveauNom = document.getElementById("nouveauNom"); + const nouvelIsin = document.getElementById("nouvelIsin"); + const nouveauSymbole = document.getElementById("nouveauSymbole"); const nouvelActif = document.getElementById("nouvelActif"); + const nouvelAfficher = document.getElementById("nouvelAfficher"); const messageNouveau = document.getElementById("messageNouveau"); -const btnSauvegarder = document.getElementById("btnSauvegarder"); /* * ========================================================= @@ -53,6 +70,14 @@ const btnSauvegarder = document.getElementById("btnSauvegarder"); let tousLesEnregistrements = []; +/* + * ========================================================= + * ID EN COURS DE MODIFICATION + * ========================================================= + */ + +let idEnregistrementModification = null; + /* * ========================================================= * AFFICHER MESSAGE GENERAL @@ -102,11 +127,11 @@ function cacherMessage() { /* * ========================================================= - * MESSAGE MODAL + * AFFICHER MESSAGE NOUVEAU / MODIFICATION * ========================================================= */ -function afficherMessageNouveau(texte, type = "info") { +function afficherMessageFormulaire(texte, type = "error") { messageNouveau.textContent = texte; messageNouveau.classList.remove( @@ -130,29 +155,24 @@ function afficherMessageNouveau(texte, type = "info") { "border-emerald-700", "text-emerald-300", ); - } else if (type === "error") { + } else { messageNouveau.classList.add( "bg-rose-900/50", "border-rose-700", "text-rose-300", ); - } else { - messageNouveau.classList.add( - "bg-slate-900", - "border-slate-700", - "text-slate-300", - ); } } /* * ========================================================= - * CACHER MESSAGE MODAL + * CACHER MESSAGE FORMULAIRE * ========================================================= */ -function cacherMessageNouveau() { +function cacherMessageFormulaire() { messageNouveau.classList.add("hidden"); + messageNouveau.textContent = ""; } @@ -177,15 +197,40 @@ function echapperHTML(valeur) { /* * ========================================================= - * CHECKBOX + * FORMAT DATE + * ========================================================= + */ + +function formaterDate(date) { + if (!date) { + return ""; + } + + return String(date); +} + +/* + * ========================================================= + * VALEUR BOOLEAN + * ========================================================= + */ + +function estActif(valeur) { + return Number(valeur) === 1 || valeur === true || valeur === "1"; +} + +/* + * ========================================================= + * CREER CHECKBOX * ========================================================= */ function creerCheckbox(valeur) { - const checked = Number(valeur) === 1 || valeur === true || valeur === "1"; + const checked = estActif(valeur); return `
+ +
`; } +/* + * ========================================================= + * CREER BOUTON MODIFIER + * ========================================================= + */ + +function creerBoutonModifier(id) { + return ` + + `; +} + /* * ========================================================= * CREER UNE LIGNE @@ -207,20 +283,32 @@ function creerCheckbox(valeur) { function creerLigne(enregistrement) { return ` - + - + - + - + - + - + - + - + - + + + + + @@ -279,13 +409,11 @@ function creerLigne(enregistrement) { /* * ========================================================= - * CONSTRUIRE LES FILTRES + * CONSTRUIRE LES OPTIONS DU FILTRE * ========================================================= */ function construireFiltres() { - const filtreActuel = filtreIndice.value; - const indices = [ ...new Set( tousLesEnregistrements @@ -308,6 +436,8 @@ function construireFiltres() { }); }); + const filtreActuel = filtreIndice.value; + filtreIndice.innerHTML = ""; /* @@ -317,6 +447,7 @@ function construireFiltres() { const optionTout = document.createElement("option"); optionTout.value = "tout"; + optionTout.textContent = "Tout"; filtreIndice.appendChild(optionTout); @@ -328,6 +459,7 @@ function construireFiltres() { const optionAfficher = document.createElement("option"); optionAfficher.value = "afficher"; + optionAfficher.textContent = "Afficher"; filtreIndice.appendChild(optionAfficher); @@ -339,6 +471,7 @@ function construireFiltres() { const optionInactif = document.createElement("option"); optionInactif.value = "inactif"; + optionInactif.textContent = "Inactif"; filtreIndice.appendChild(optionInactif); @@ -351,6 +484,7 @@ function construireFiltres() { const option = document.createElement("option"); option.value = "indice:" + indice; + option.textContent = indice; filtreIndice.appendChild(option); @@ -364,12 +498,16 @@ function construireFiltres() { return option.value === filtreActuel; }); - filtreIndice.value = optionExiste ? filtreActuel : "tout"; + if (optionExiste) { + filtreIndice.value = filtreActuel; + } else { + filtreIndice.value = "tout"; + } } /* * ========================================================= - * APPLIQUER LE FILTRE + * APPLIQUER FILTRE * ========================================================= */ @@ -378,20 +516,36 @@ function appliquerFiltre() { let enregistrementsFiltres; + /* + * Tout + */ + if (filtre === "tout") { enregistrementsFiltres = tousLesEnregistrements; + + /* + * Afficher + */ } else if (filtre === "afficher") { enregistrementsFiltres = tousLesEnregistrements.filter( function (enregistrement) { - return Number(enregistrement.afficher) === 1; + return estActif(enregistrement.afficher); }, ); + + /* + * Inactif + */ } else if (filtre === "inactif") { enregistrementsFiltres = tousLesEnregistrements.filter( function (enregistrement) { - return Number(enregistrement.actif) === 0; + return !estActif(enregistrement.actif); }, ); + + /* + * Indice + */ } else if (filtre.startsWith("indice:")) { const indiceRecherche = filtre.substring("indice:".length); @@ -403,10 +557,22 @@ function appliquerFiltre() { ); }, ); + + /* + * Sécurité + */ } else { enregistrementsFiltres = tousLesEnregistrements; } + /* + * Toujours trier par ID croissant. + */ + + enregistrementsFiltres = [...enregistrementsFiltres].sort(function (a, b) { + return Number(a.id) - Number(b.id); + }); + afficherDonneesFiltrees(enregistrementsFiltres); } @@ -424,39 +590,34 @@ function afficherDonneesFiltrees(enregistrements) { tableauIndices.innerHTML = ` + + `; return; } - /* - * IMPORTANT : - * tri systématique par ID croissant. - */ - - const donneesTriees = [...enregistrements].sort(function (a, b) { - return Number(a.id) - Number(b.id); - }); - let html = ""; - donneesTriees.forEach(function (enregistrement) { + enregistrements.forEach(function (enregistrement) { html += creerLigne(enregistrement); }); tableauIndices.innerHTML = html; compteur.textContent = - donneesTriees.length + - (donneesTriees.length > 1 ? " enregistrements" : " enregistrement"); + enregistrements.length + + (enregistrements.length > 1 ? " enregistrements" : " enregistrement"); } /* @@ -469,7 +630,7 @@ function afficherDonnees(data) { tousLesEnregistrements = Array.isArray(data) ? data : []; /* - * Tri global par ID croissant. + * Tri principal par ID croissant. */ tousLesEnregistrements.sort(function (a, b) { @@ -483,7 +644,7 @@ function afficherDonnees(data) { /* * ========================================================= - * CHARGER LES DONNEES + * CHARGER DONNEES * ========================================================= */ @@ -497,8 +658,11 @@ async function chargerDonnees() { try { const response = await fetch(API_LIST_URL, { method: "GET", + credentials: "same-origin", + cache: "no-store", + headers: { Accept: "application/json", }, @@ -526,12 +690,16 @@ async function chargerDonnees() { tableauIndices.innerHTML = ` + + `; @@ -550,28 +718,29 @@ async function chargerDonnees() { /* * ========================================================= - * OUVRIR MODAL + * OUVRIR POPUP NOUVEAU * ========================================================= */ -function ouvrirModal() { - cacherMessageNouveau(); +function ouvrirModalNouveau() { + formNouvelEnregistrement.reset(); /* * Valeurs par défaut */ - formNouvelEnregistrement.reset(); - nouvelActif.checked = true; + nouvelAfficher.checked = true; + cacherMessageFormulaire(); + modalNouvelEnregistrement.classList.remove("hidden"); modalNouvelEnregistrement.classList.add("flex"); /* - * Focus sur Indice + * Focus */ setTimeout(function () { @@ -581,7 +750,7 @@ function ouvrirModal() { /* * ========================================================= - * FERMER MODAL + * FERMER POPUP * ========================================================= */ @@ -590,57 +759,36 @@ function fermerModal() { modalNouvelEnregistrement.classList.remove("flex"); - cacherMessageNouveau(); + idEnregistrementModification = null; + + cacherMessageFormulaire(); } /* * ========================================================= - * SAUVEGARDER NOUVEL ENREGISTREMENT + * CREER NOUVEL ENREGISTREMENT * ========================================================= */ -async function sauvegarderNouvelEnregistrement() { - const donnees = { - indice: nouvelIndice.value.trim(), - nom: nouveauNom.value.trim(), - isin: nouvelIsin.value.trim().toUpperCase(), - symbole: nouveauSymbole.value.trim().toUpperCase(), - actif: nouvelActif.checked ? 1 : 0, - afficher: nouvelAfficher.checked ? 1 : 0, - }; +async function creerNouvelEnregistrement() { + const indice = nouvelIndice.value.trim(); + + const nom = nouveauNom.value.trim(); + + const isin = nouvelIsin.value.trim().toUpperCase(); + + const symbole = nouveauSymbole.value.trim().toUpperCase(); + + const actif = nouvelActif.checked ? 1 : 0; + + const afficher = nouvelAfficher.checked ? 1 : 0; /* - * Validation côté client + * Validation */ - if (!donnees.indice) { - afficherMessageNouveau("Le champ Indice est obligatoire.", "error"); - - nouvelIndice.focus(); - - return; - } - - if (!donnees.nom) { - afficherMessageNouveau("Le champ Nom est obligatoire.", "error"); - - nouveauNom.focus(); - - return; - } - - if (!donnees.isin) { - afficherMessageNouveau("Le champ ISIN est obligatoire.", "error"); - - nouvelIsin.focus(); - - return; - } - - if (!donnees.symbole) { - afficherMessageNouveau("Le champ Symbole est obligatoire.", "error"); - - nouveauSymbole.focus(); + if (!indice || !nom || !isin || !symbole) { + afficherMessageFormulaire("Tous les champs sont obligatoires.", "error"); return; } @@ -649,11 +797,13 @@ async function sauvegarderNouvelEnregistrement() { * Désactivation du bouton */ - btnSauvegarder.disabled = true; + const bouton = document.getElementById("btnSauvegarder"); - btnSauvegarder.textContent = "Sauvegarde..."; + bouton.disabled = true; - cacherMessageNouveau(); + bouton.textContent = "Sauvegarde..."; + + cacherMessageFormulaire(); try { const response = await fetch(API_CREATE_URL, { @@ -667,7 +817,14 @@ async function sauvegarderNouvelEnregistrement() { Accept: "application/json", }, - body: JSON.stringify(donnees), + body: JSON.stringify({ + indice: indice, + nom: nom, + isin: isin, + symbole: symbole, + actif: actif, + afficher: afficher, + }), }); const result = await response.json(); @@ -676,38 +833,243 @@ async function sauvegarderNouvelEnregistrement() { throw new Error( result && result.message ? result.message - : "Impossible de créer l'enregistrement.", + : "Erreur lors de la création.", ); } /* - * Succès + * Fermeture popup */ - afficherMessageNouveau("Enregistrement créé avec succès.", "success"); + fermerModal(); /* - * On recharge la table. + * Message général + */ + + afficherMessage("L'enregistrement a été créé avec succès.", "success"); + + /* + * Rechargement */ await chargerDonnees(); + } catch (error) { + console.error("Erreur création :", error); + + afficherMessageFormulaire(error.message, "error"); + } finally { + bouton.disabled = false; + + bouton.textContent = "Sauvegarder"; + } +} + +/* + * ========================================================= + * OUVRIR POPUP MODIFICATION + * ========================================================= + * + * On réutilise exactement la même popup HTML. + * + * ========================================================= + */ + +function ouvrirModalModification(id) { + const enregistrement = tousLesEnregistrements.find(function (item) { + return Number(item.id) === Number(id); + }); + + if (!enregistrement) { + afficherMessage("Impossible de trouver l'enregistrement.", "error"); + + return; + } + + /* + * Mémorisation de l'ID + */ + + idEnregistrementModification = Number(enregistrement.id); + + /* + * Remplissage du formulaire + */ + + nouvelIndice.value = enregistrement.indice || ""; + + nouveauNom.value = enregistrement.nom || ""; + + nouvelIsin.value = enregistrement.isin || ""; + + nouveauSymbole.value = enregistrement.symbole || ""; + + nouvelActif.checked = estActif(enregistrement.actif); + + nouvelAfficher.checked = estActif(enregistrement.afficher); + + /* + * Message + */ + + cacherMessageFormulaire(); + + /* + * Modifier le titre de la popup + */ + + const titre = modalNouvelEnregistrement.querySelector("h2"); + + if (titre) { + titre.textContent = "Modifier l'enregistrement"; + } + + /* + * Modifier le sous-titre + */ + + const sousTitre = modalNouvelEnregistrement.querySelector("h2 + p"); + + if (sousTitre) { + sousTitre.textContent = "Modifier une action ou un indice"; + } + + /* + * Modifier le texte du bouton + */ + + const bouton = document.getElementById("btnSauvegarder"); + + bouton.textContent = "Sauvegarder"; + + /* + * Ouverture + */ + + modalNouvelEnregistrement.classList.remove("hidden"); + + modalNouvelEnregistrement.classList.add("flex"); + + /* + * Focus + */ + + setTimeout(function () { + nouvelIndice.focus(); + }, 100); +} + +/* + * ========================================================= + * MODIFIER ENREGISTREMENT + * ========================================================= + */ + +async function modifierEnregistrement() { + if (idEnregistrementModification === null) { + afficherMessageFormulaire("Aucun enregistrement sélectionné.", "error"); + + return; + } + + const indice = nouvelIndice.value.trim(); + + const nom = nouveauNom.value.trim(); + + const isin = nouvelIsin.value.trim().toUpperCase(); + + const symbole = nouveauSymbole.value.trim().toUpperCase(); + + const actif = nouvelActif.checked ? 1 : 0; + + const afficher = nouvelAfficher.checked ? 1 : 0; + + /* + * Validation + */ + + if (!indice || !nom || !isin || !symbole) { + afficherMessageFormulaire("Tous les champs sont obligatoires.", "error"); + + return; + } + + /* + * Bouton + */ + + const bouton = document.getElementById("btnSauvegarder"); + + bouton.disabled = true; + + bouton.textContent = "Sauvegarde..."; + + cacherMessageFormulaire(); + + try { + const response = await fetch(API_UPDATE_URL, { + method: "POST", + + credentials: "same-origin", + + headers: { + "Content-Type": "application/json", + + Accept: "application/json", + }, + + body: JSON.stringify({ + id: idEnregistrementModification, + + indice: indice, + + nom: nom, + + isin: isin, + + symbole: symbole, + + actif: actif, + + afficher: afficher, + }), + }); + + const result = await response.json(); + + if (!response.ok || !result || result.status !== "success") { + throw new Error( + result && result.message + ? result.message + : "Erreur lors de la modification.", + ); + } /* - * Petite pause pour laisser voir - * le message de succès. + * Fermeture */ - setTimeout(function () { - fermerModal(); - }, 500); + fermerModal(); + + /* + * Message + */ + + afficherMessage("L'enregistrement a été modifié avec succès.", "success"); + + /* + * Rechargement + */ + + await chargerDonnees(); } catch (error) { - console.error("Erreur création enregistrement :", error); + console.error("Erreur modification :", error); - afficherMessageNouveau("Erreur : " + error.message, "error"); + afficherMessageFormulaire(error.message, "error"); } finally { - btnSauvegarder.disabled = false; + bouton.disabled = false; - btnSauvegarder.textContent = "Sauvegarder"; + bouton.textContent = "Sauvegarder"; } } @@ -734,15 +1096,16 @@ btnActualiser.addEventListener("click", function () { /* * ========================================================= * BOUTON NOUVEAU - * ========================================================= */ + * ========================================================= + */ btnNouveau.addEventListener("click", function () { - ouvrirModal(); + ouvrirModalNouveau(); }); /* * ========================================================= - * FERMETURE MODAL + * FERMER POPUP * ========================================================= */ @@ -756,19 +1119,58 @@ btnAnnuler.addEventListener("click", function () { /* * ========================================================= - * CLIC SUR LE FOND DU MODAL + * SUBMIT FORMULAIRE * ========================================================= */ -modalNouvelEnregistrement.addEventListener("click", function (event) { - if (event.target === modalNouvelEnregistrement) { - fermerModal(); +formNouvelEnregistrement.addEventListener("submit", function (event) { + event.preventDefault(); + + /* + * Si un ID est défini : + * modification. + * + * Sinon : + * création. + */ + + if (idEnregistrementModification !== null) { + modifierEnregistrement(); + } else { + creerNouvelEnregistrement(); } }); /* * ========================================================= - * TOUCHE ESC + * BOUTONS MODIFIER + * ========================================================= + * + * Les boutons sont créés dynamiquement. + * On utilise donc la délégation d'événement. + * + * ========================================================= + */ + +tableauIndices.addEventListener("click", function (event) { + const bouton = event.target.closest(".btnModifier"); + + if (!bouton) { + return; + } + + const id = bouton.dataset.id; + + if (!id) { + return; + } + + ouvrirModalModification(id); +}); + +/* + * ========================================================= + * FERMER AVEC ECHAP * ========================================================= */ @@ -781,38 +1183,6 @@ document.addEventListener("keydown", function (event) { } }); -/* - * ========================================================= - * SOUMISSION FORMULAIRE - * ========================================================= - */ - -formNouvelEnregistrement.addEventListener("submit", function (event) { - event.preventDefault(); - - sauvegarderNouvelEnregistrement(); -}); - -/* - * ========================================================= - * ISIN EN MAJUSCULES - * ========================================================= - */ - -nouvelIsin.addEventListener("input", function () { - nouvelIsin.value = nouvelIsin.value.toUpperCase(); -}); - -/* - * ========================================================= - * SYMBOLE EN MAJUSCULES - * ========================================================= - */ - -nouveauSymbole.addEventListener("input", function () { - nouveauSymbole.value = nouveauSymbole.value.toUpperCase(); -}); - /* * ========================================================= * INITIALISATION diff --git a/php/api_update_table_indice.php b/php/api_update_table_indice.php new file mode 100644 index 0000000..14ed4a9 --- /dev/null +++ b/php/api_update_table_indice.php @@ -0,0 +1,322 @@ + 'error', + 'message' => 'Méthode HTTP non autorisée.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +/* + * ========================================================= + * LECTURE DU JSON + * ========================================================= + */ + +$contenu = file_get_contents('php://input'); + +if ($contenu === false || trim($contenu) === '') { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Aucune donnée reçue.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +$data = json_decode( + $contenu, + true +); + +/* + * Vérification JSON + */ + +if (!is_array($data)) { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Données JSON invalides.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +/* + * ========================================================= + * RECUPERATION DES CHAMPS + * ========================================================= + */ + +$id = isset($data['id']) + ? (int) $data['id'] + : 0; + +$indice = isset($data['indice']) + ? trim((string) $data['indice']) + : ''; + +$nom = isset($data['nom']) + ? trim((string) $data['nom']) + : ''; + +$isin = isset($data['isin']) + ? strtoupper(trim((string) $data['isin'])) + : ''; + +$symbole = isset($data['symbole']) + ? strtoupper(trim((string) $data['symbole'])) + : ''; + +$actif = !empty($data['actif']) ? 1 : 0; + +$afficher = !empty($data['afficher']) ? 1 : 0; + +/* + * ========================================================= + * VALIDATION + * ========================================================= + */ + +if ($id <= 0) { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'ID invalide.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +if ($indice === '') { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Le champ Indice est obligatoire.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +if ($nom === '') { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Le champ Nom est obligatoire.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +if ($isin === '') { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Le champ ISIN est obligatoire.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +if ($symbole === '') { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Le champ Symbole est obligatoire.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +/* + * Vérification longueur ISIN. + */ + +if (strlen($isin) > 12) { + + http_response_code(400); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Le code ISIN ne doit pas dépasser 12 caractères.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; +} + +/* + * ========================================================= + * MODIFICATION + * ========================================================= + */ + +try { + + /* + * Vérification existence. + */ + + $sqlCheck = " + SELECT id + FROM indice + WHERE id = :id + LIMIT 1 + "; + + $stmtCheck = $pdo->prepare($sqlCheck); + + $stmtCheck->execute( + [ + ':id' => $id + ] + ); + + $existant = $stmtCheck->fetch(); + + if (!$existant) { + + http_response_code(404); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'L\'enregistrement demandé n\'existe pas.' + ], + JSON_UNESCAPED_UNICODE + ); + + exit; + } + + /* + * Mise à jour. + * + * updated_at est mis à jour automatiquement ici. + */ + + $sql = " + UPDATE indice + SET + indice = :indice, + nom = :nom, + isin = :isin, + symbole = :symbole, + actif = :actif, + afficher = :afficher, + updated_at = NOW() + WHERE id = :id + "; + + $stmt = $pdo->prepare($sql); + + $stmt->execute( + [ + ':indice' => $indice, + ':nom' => $nom, + ':isin' => $isin, + ':symbole' => $symbole, + ':actif' => $actif, + ':afficher' => $afficher, + ':id' => $id + ] + ); + + /* + * ===================================================== + * REPONSE + * ===================================================== + */ + + echo json_encode( + [ + 'status' => 'success', + 'message' => 'Enregistrement modifié avec succès.', + 'id' => $id + ], + JSON_UNESCAPED_UNICODE | + JSON_UNESCAPED_SLASHES + ); +} catch (PDOException $e) { + + /* + * Erreur MySQL. + */ + + http_response_code(500); + + echo json_encode( + [ + 'status' => 'error', + 'message' => 'Erreur lors de la modification de l\'enregistrement.' + ], + JSON_UNESCAPED_UNICODE + ); +}
ID Indice Nom ISIN Symbole Actif Afficher Mis à jour Créé le + Action +
+ ${echapperHTML(enregistrement.id)} + + + ${echapperHTML(enregistrement.nom)} + ${echapperHTML(enregistrement.isin)} + ${echapperHTML(enregistrement.symbole)} + ${creerCheckbox(enregistrement.actif)} + ${creerCheckbox(enregistrement.afficher)} - ${echapperHTML(enregistrement.updated_at)} + + ${echapperHTML(formaterDate(enregistrement.updated_at))} - ${echapperHTML(enregistrement.created_at)} + + ${echapperHTML(formaterDate(enregistrement.created_at))} + + ${creerBoutonModifier(enregistrement.id)}
Aucun enregistrement correspondant au filtre sélectionné.
Impossible de charger les données.