diff --git a/app/Http/Controllers/SurveyorController.php b/app/Http/Controllers/SurveyorController.php index 3d11584..45069f7 100644 --- a/app/Http/Controllers/SurveyorController.php +++ b/app/Http/Controllers/SurveyorController.php @@ -348,20 +348,23 @@ class SurveyorController extends Controller ? json_decode($inspeksi->foto_form, true) : []; - $formatFotojson = $existingData; + + + if (!$this->isValidFormat($existingData)) { + $existingData = []; + } // Upload berbagai jenis foto - $this->handleFileUploadBaru($request, 'rute_menuju_lokasi', $formatFotojson); + $newFoto = $this->handleFileUpload($request, 'upload_foto', $existingData); - // Update record - if (!empty($formatFotojson)) { - $inspeksi->foto_form = json_encode($formatFotojson); + if (!empty($newFoto)) { + $inspeksi->foto_form = json_encode($existingData); $inspeksi->save(); return response()->json([ 'success' => true, 'message' => 'Data berhasil disimpan', - 'data' => $formatFotojson + 'file' => $newFoto ], 200); } @@ -374,6 +377,21 @@ class SurveyorController extends Controller } } + /** + * Validasi apakah format JSON sesuai dengan yang diinginkan. + * + * @param array $data + * @return bool + */ +private function isValidFormat($data) +{ + if (!isset($data['upload_foto']) || !is_array($data['upload_foto'])) { + return false; + } + + return true; +} + public function updateFoto(Request $request) { @@ -399,14 +417,14 @@ class SurveyorController extends Controller $fotoForm = json_decode($inspeksi->foto_form, true) ?? []; - if (!isset($fotoForm['rute_menuju_lokasi'])) { - $fotoForm['rute_menuju_lokasi'] = []; + if (!isset($fotoForm['upload_foto'])) { + $fotoForm['upload_foto'] = []; } $existingIndex = null; if (!empty($validated['path'])) { - foreach ($fotoForm['rute_menuju_lokasi'] as $index => $foto) { + foreach ($fotoForm['upload_foto'] as $index => $foto) { if ($foto['path'] === $cleanRequestPath) { $existingIndex = $index; break; @@ -428,8 +446,8 @@ class SurveyorController extends Controller ]; if ($existingIndex !== null) { - $existingFoto = $fotoForm['rute_menuju_lokasi'][$existingIndex]; - $fotoForm['rute_menuju_lokasi'][$existingIndex] = [ + $existingFoto = $fotoForm['upload_foto'][$existingIndex]; + $fotoForm['upload_foto'][$existingIndex] = [ 'name' => $validated['name'], 'description' => $validated['description'], 'category' => $validated['category'], @@ -441,7 +459,7 @@ class SurveyorController extends Controller 'updated_at' => now()->toDateTimeString(), ]; } else { - $fotoForm['rute_menuju_lokasi'][] = $newFotoData; + $fotoForm['upload_foto'][] = $newFotoData; } // Simpan kembali data ke database @@ -462,7 +480,7 @@ class SurveyorController extends Controller } - private function handleFileUploadBaru(Request $request, $paramName, &$formatFotojson) + private function handleFileUpload(Request $request, $paramName, &$formatFotojson) { if ($request->hasFile($paramName)) { $files = $request->file($paramName); diff --git a/resources/views/surveyor/components/foto.blade.php b/resources/views/surveyor/components/foto.blade.php index ade8ef5..f46aea2 100644 --- a/resources/views/surveyor/components/foto.blade.php +++ b/resources/views/surveyor/components/foto.blade.php @@ -6,7 +6,7 @@ @section('content') - + {{-- --}} @@ -186,8 +182,8 @@

Upload Foto

-
-
+
+
@@ -200,11 +196,6 @@
- {{-- --}}
@@ -228,6 +219,7 @@ + * Kosongkan jika tidak ada
@@ -235,15 +227,15 @@
- Jika tidak ada kosongkan + * Kosongkan jika tidak ada
- + {{-- --}}
@@ -274,13 +266,14 @@ @include('lpj::surveyor.js.fotojs') @include('lpj::surveyor.js.utils') @push('scripts') - + {{-- --}}