perbaikan foto
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user