From 939a4fe03ec4a1d8d318408d9237a936841a28e3 Mon Sep 17 00:00:00 2001 From: kapsona777 Date: Mon, 18 Aug 2025 19:45:38 +0400 Subject: [PATCH] added feature to save image into database and after upload it can be chose by dropdown --- public/userarea/parts.js | 24 ++++++++++--- public/userarea/save_annotated_photo.php | 46 +++++++++++++++++++----- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/public/userarea/parts.js b/public/userarea/parts.js index 68512f1..0d2c671 100644 --- a/public/userarea/parts.js +++ b/public/userarea/parts.js @@ -104,6 +104,11 @@ $(document).ready(function () { const selector = $(''); photos.forEach((photo, index) => { const option = $('').val(photo).text(`Photo ${index + 1}`); + // display option with photo name if available + if (photo.includes("/")) { + const photoName = photo.split("/").pop(); + option.text(`Photo ${index + 1} - ${photoName}`); + } selector.append(option); }); @@ -637,23 +642,34 @@ $(document).ready(function () { const dataURL = canvas.toDataURL("image/png"); const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); - const defaultName = `photo_${$("#partsModal").data("iddatadb")}_${timestamp}.png`; + const iddatadb = $("#partsModal").data("iddatadb"); // ๐ŸŸข แƒ”แƒก แƒ’แƒแƒ“แƒแƒ•แƒแƒ’แƒ–แƒแƒ•แƒœแƒแƒ— + const defaultName = `photo_${iddatadb}_${timestamp}.png`; + const newName = prompt("Inserisci il nome del file (senza estensione):", defaultName.split(".png")[0]); + if (newName) { const finalName = newName + "_" + timestamp + ".png"; $.ajax({ url: "save_annotated_photo.php", method: "POST", - data: { dataURL: dataURL, filename: finalName }, + data: { + dataURL: dataURL, + filename: finalName, + iddatadb: iddatadb // ๐ŸŸข แƒ’แƒแƒ“แƒแƒ•แƒชแƒ”แƒ›แƒ— PHPโ€“แƒก + }, success: function (response) { if (response.success) { alert("Foto salvata con successo: " + response.file_path); + // update photo list or refresh the modal + $("#samplePhoto").attr("src", response.file_path); + loadPhoto(iddatadb); // reload to update markers + clearCanvasMarkers(); // reset markers after save } else { - alert("Errore nel salvataggio: " + response.message); + alert("Errore: " + response.message); } }, error: function (xhr, status, error) { - alert("Errore nel salvataggio della foto: " + error); + alert("Errore Ajax: " + error); }, }); } diff --git a/public/userarea/save_annotated_photo.php b/public/userarea/save_annotated_photo.php index 244d36b..3745b08 100644 --- a/public/userarea/save_annotated_photo.php +++ b/public/userarea/save_annotated_photo.php @@ -1,25 +1,53 @@ false, 'message' => 'Dati mancanti']); exit; } try { + // --- แƒคแƒแƒ˜แƒšแƒ˜แƒก แƒจแƒ”แƒœแƒแƒฎแƒ•แƒ --- $data = explode(',', $dataURL)[1]; $decodedData = base64_decode($data); - $filePath = '../photostrf/annotated/' . $filename; // Crea una sottocartella 'annotated' per le foto modificate - if (!file_exists('../photostrf/annotated')) { - mkdir('../photostrf/annotated', 0777, true); + + $dirPath = '../photostrf/annotated'; + if (!file_exists($dirPath)) { + mkdir($dirPath, 0777, true); } + + $filePath = $dirPath . '/' . $filename; file_put_contents($filePath, $decodedData); - echo json_encode(['success' => true, 'file_path' => $filePath, 'message' => 'Foto salvata con successo']); + + $db = DBHandlerSelect::getInstance(); + $pdo = $db->getConnection(); + + // --- แƒ‘แƒแƒ–แƒแƒจแƒ˜ แƒฉแƒแƒฌแƒ”แƒ แƒ --- + $stmt = $pdo->prepare(" + INSERT INTO datadb_photos (iddatadb, file_path, file_name, uploaded_at, uploaded_by) + VALUES (:iddatadb, :file_path, :file_name, NOW(), :uploaded_by) + "); + $stmt->execute([ + ':iddatadb' => $iddatadb, + ':file_path' => $filePath, + ':file_name' => $filename, + ':uploaded_by'=> $iduserlogin + ]); + + echo json_encode([ + 'success' => true, + 'file_path' => $filePath, + 'message' => 'Foto salvata con successo e registrata nel DB' + ]); + } catch (Exception $e) { - echo json_encode(['success' => false, 'message' => 'Errore nel salvataggio: ' . $e->getMessage()]); + echo json_encode(['success' => false, 'message' => 'Errore: ' . $e->getMessage()]); }