From 7ad20993d9924197918e66d4caaf3257deb1d422 Mon Sep 17 00:00:00 2001 From: kapsona777 Date: Mon, 11 Aug 2025 16:30:24 +0400 Subject: [PATCH] added controller for QR photo upload and inserted route for it. added functional for multiple photo upload. --- .../Userarea/UploadPhotosMobileController.php | 74 ++++++++++++++++ public/userarea/load_photo.php | 15 ++-- public/userarea/modal_parts.php | 37 ++++---- public/userarea/parts.js | 86 +++++++++++++------ public/userarea/photos_popup.php | 4 +- public/userarea/upload_photos_mobile.php | 2 +- resources/views/layouts/app.blade.php | 1 + routes/web.php | 8 ++ 8 files changed, 174 insertions(+), 53 deletions(-) create mode 100644 app/Http/Controllers/Userarea/UploadPhotosMobileController.php diff --git a/app/Http/Controllers/Userarea/UploadPhotosMobileController.php b/app/Http/Controllers/Userarea/UploadPhotosMobileController.php new file mode 100644 index 0000000..ecbc7d7 --- /dev/null +++ b/app/Http/Controllers/Userarea/UploadPhotosMobileController.php @@ -0,0 +1,74 @@ +query('iddatadb'); + + if (empty($iddatadb)) { + return response('ID riga non fornito', 400); + } + + // Show the upload form + return view('userarea.upload_photos_mobile', [ + 'iddatadb' => $iddatadb + ]); + } + + public function upload(Request $request) + { + // Validation + $request->validate([ + 'photo' => 'required|file|mimes:jpeg,png,gif,heic,heif|max:5120', // 5MB + 'iddatadb' => 'required|integer' + ]); + + $iddatadb = $request->input('iddatadb'); + $photo = $request->file('photo'); + $iduserlogin = auth()->id(); // assuming Laravel authentication + + // Check if user exists + $userExists = DB::table('auth_users')->where('id', $iduserlogin)->exists(); + if (!$userExists) { + return response()->json(['success' => false, 'message' => 'Utente non valido']); + } + + // Upload folder + $uploadDir = public_path('photostrf'); + if (!is_dir($uploadDir)) { + mkdir($uploadDir, 0755, true); + } + if (!is_writable($uploadDir)) { + return response()->json(['success' => false, 'message' => 'La cartella photostrf non รจ scrivibile']); + } + + // New filename + $timestamp = now()->format('YmdHis'); + $originalName = pathinfo($photo->getClientOriginalName(), PATHINFO_FILENAME); + $extension = strtolower($photo->getClientOriginalExtension()); + + $newFileName = "{$iddatadb}-{$timestamp}-{$originalName}.{$extension}"; + $destination = $uploadDir . '/' . $newFileName; + + // Move uploaded file + $photo->move($uploadDir, $newFileName); + + // Save DB record + DB::table('datadb_photos')->insert([ + 'iddatadb' => $iddatadb, + 'file_path' => $newFileName, + 'file_name' => $newFileName, + 'uploaded_by' => $iduserlogin + ]); + + return response()->json(['success' => true, 'message' => 'Foto caricata con successo']); + } +} diff --git a/public/userarea/load_photo.php b/public/userarea/load_photo.php index 34ca8e2..f3ac03b 100644 --- a/public/userarea/load_photo.php +++ b/public/userarea/load_photo.php @@ -14,13 +14,18 @@ if (!$iddatadb) { } try { - $stmt = $pdo->prepare("SELECT file_path FROM datadb_photos WHERE iddatadb = :iddatadb LIMIT 1"); + // Adjust the query to select all photo paths for the given iddatadb + $stmt = $pdo->prepare("SELECT file_path FROM datadb_photos WHERE iddatadb = :iddatadb"); $stmt->execute([':iddatadb' => $iddatadb]); - $photo = $stmt->fetch(PDO::FETCH_ASSOC); + $photos = $stmt->fetchAll(PDO::FETCH_ASSOC); - if ($photo && $photo['file_path']) { - $fullPath = '../photostrf/' . $photo['file_path']; // Assumi che le foto siano nella cartella photostrf - echo json_encode(['success' => true, 'file_path' => $fullPath]); + if ($photos && count($photos) > 0) { + // Prepare an array of full file paths + $photoPaths = array_map(function($photo) { + return '../photostrf/' . $photo['file_path']; // Assuming the photos are stored in the "photostrf" folder + }, $photos); + + echo json_encode(['success' => true, 'photos' => $photoPaths]); // Return an array of photo paths } else { echo json_encode(['success' => false, 'message' => 'Nessuna foto trovata']); } diff --git a/public/userarea/modal_parts.php b/public/userarea/modal_parts.php index b1e00cd..a3d7eba 100644 --- a/public/userarea/modal_parts.php +++ b/public/userarea/modal_parts.php @@ -13,29 +13,32 @@ - - - - - + + + + + - - - - - + + + + +
Num. ParteDescrizione ParteAzioni
Num. ParteDescrizione ParteAzioni
- - - - - -
+ + + + + +
Foto del Campione
+
Foto del campione @@ -142,4 +145,4 @@ padding: 0.1rem 0.3rem; font-size: 0.8rem; } - \ No newline at end of file + diff --git a/public/userarea/parts.js b/public/userarea/parts.js index 3b5690c..5e12a7d 100644 --- a/public/userarea/parts.js +++ b/public/userarea/parts.js @@ -61,35 +61,19 @@ $(document).ready(function () { method: "GET", data: { iddatadb: iddatadb }, success: function (response) { - if (response.success && response.file_path) { - const img = $("#samplePhoto"); - img.attr("src", response.file_path); - img.on("load", function () { - const container = img.parent(); - const canvas = document.getElementById("photoCanvas"); - const containerWidth = container.width(); - const containerHeight = container.height(); - const scaleX = containerWidth / img[0].naturalWidth; - const scaleY = containerHeight / img[0].naturalHeight; - const scale = Math.min(scaleX, scaleY); - canvas.width = img[0].naturalWidth * scale; - canvas.height = img[0].naturalHeight * scale; - canvas.style.width = `${containerWidth}px`; - canvas.style.height = `${containerHeight}px`; - const ctx = canvas.getContext("2d"); - ctx.clearRect(0, 0, canvas.width, canvas.height); - ctx.drawImage( - img.get(0), - 0, - 0, - canvas.width, - canvas.height, - ); - updateMarkers(); - }); + if (response.success) { + if (response.photos && response.photos.length > 1) { + // Multiple photos available, create a selector + showPhotoSelector(response.photos); + } else if (response.photos && response.photos.length === 1) { + // Only one photo, load it directly + loadSinglePhoto(response.photos[0]); + } else { + $("#samplePhoto").attr("src", ""); + alert("Nessuna foto trovata per questo TRF."); + } } else { - $("#samplePhoto").attr("src", ""); - alert("Nessuna foto trovata per questo TRF."); + alert(response.message || "Errore nel caricamento della foto."); } }, error: function (xhr, status, error) { @@ -98,6 +82,52 @@ $(document).ready(function () { }); } + function showPhotoSelector(photos) { + const selectorContainer = $("#photoSelectorContainer"); // A div to hold the photo selector + selectorContainer.empty(); // Clear any previous options + + // Create a dropdown or buttons to select a photo + const selector = $(''); + photos.forEach((photo, index) => { + const option = $('').val(photo).text(`Photo ${index + 1}`); + selector.append(option); + }); + + selector.on("change", function () { + const selectedPhoto = $(this).val(); + loadSinglePhoto(selectedPhoto); + }); + + selectorContainer.append(selector); + selectorContainer.show(); // Make sure the selector is visible + } + + function loadSinglePhoto(photoPath) { + const img = $("#samplePhoto"); + img.attr("src", photoPath); + + img.on("load", function () { + const container = img.parent(); + const canvas = document.getElementById("photoCanvas"); + const containerWidth = container.width(); + const containerHeight = container.height(); + const scaleX = containerWidth / img[0].naturalWidth; + const scaleY = containerHeight / img[0].naturalHeight; + const scale = Math.min(scaleX, scaleY); + + canvas.width = img[0].naturalWidth * scale; + canvas.height = img[0].naturalHeight * scale; + canvas.style.width = `${containerWidth}px`; + canvas.style.height = `${containerHeight}px`; + + const ctx = canvas.getContext("2d"); + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.drawImage(img.get(0), 0, 0, canvas.width, canvas.height); + + updateMarkers(); + }); + } + function addNewRow(nextPartNumber, isMix = false) { const description = isMix ? "Mix" : ""; const newRow = ` diff --git a/public/userarea/photos_popup.php b/public/userarea/photos_popup.php index f9d6a93..63d92c8 100644 --- a/public/userarea/photos_popup.php +++ b/public/userarea/photos_popup.php @@ -44,7 +44,7 @@ $photos = $stmt->fetchAll(PDO::FETCH_ASSOC); $photoBasePath = '../photostrf/'; // Genera l'URL per il QR code -$baseUrl = "http://localhost/trf_certest/public/userarea/"; // Sostituisci con il tuo dominio +$baseUrl = "http://localhost:8000/userarea/"; // Sostituisci con il tuo dominio $uploadUrl = $baseUrl . "upload_photos_mobile.php?iddatadb=" . $iddatadb; // Genera il QR code con endroid/qr-code 6.0.6 @@ -224,4 +224,4 @@ $result->saveToFile($qrCodeFile); font-size: 16px; color: white; } - \ No newline at end of file + diff --git a/public/userarea/upload_photos_mobile.php b/public/userarea/upload_photos_mobile.php index 946708f..e16d421 100644 --- a/public/userarea/upload_photos_mobile.php +++ b/public/userarea/upload_photos_mobile.php @@ -142,4 +142,4 @@ $sampleCode = $row['sample_code'] ?? 'Non disponibile'; - \ No newline at end of file + diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 1fa6f66..341dde0 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -42,6 +42,7 @@ + @yield('scripts') @hook('app:scripts') diff --git a/routes/web.php b/routes/web.php index 1db981a..078d859 100644 --- a/routes/web.php +++ b/routes/web.php @@ -188,3 +188,11 @@ Route::group(['prefix' => 'install'], function () { Route::get('complete', 'InstallController@complete')->name('install.complete'); Route::get('error', 'InstallController@error')->name('install.error'); }); + +use App\Vanguard\Http\Controllers\Userarea\UploadPhotosMobileController; + +Route::get('/userarea/upload-photos-mobile', [UploadPhotosMobileController::class, 'index']) + ->name('userarea.upload-photos-mobile.index'); + +Route::post('/userarea/upload-photos-mobile', [UploadPhotosMobileController::class, 'upload']) + ->name('userarea.upload-photos-mobile.upload');