perbaikan foto objek jaminan ketika di update tidak hilang

This commit is contained in:
majid
2024-12-20 16:25:54 +07:00
parent 145b677dbc
commit 01408bbac3

View File

@@ -305,15 +305,19 @@ class SurveyorController extends Controller
$formatFotojson = $existingData;
// Process each photo category
foreach ($photoCategories as $category => $fields) {
// Only update if new files are provided
if ($this->categoryHasNewFiles($request, $fields)) {
// Delete old files for this category only
if (isset($existingData[$category])) {
$this->deleteFilesForCategory($existingData[$category]);
}
$formatFotojson[$category] = $this->processPhotoCategory($request, $fields);
$photoField = $fields[0];
$nameField = $fields[1];
$descriptionField = $fields[2] ?? null;
if ($request->hasFile($photoField)) {
$newPhotos = $this->processPhotoCategory(
$request,
$fields,
$existingData[$category] ?? []
);
$formatFotojson[$category] = $newPhotos;
}
}
@@ -342,29 +346,40 @@ class SurveyorController extends Controller
/**
* Process a photo category and its subcategories
*/
private function processPhotoCategory(Request $request, array $fields)
private function processPhotoCategory(Request $request, array $fields, array $existingPhotos = [])
{
$result = [];
$result = $existingPhotos; // Start with existing photos
$photoField = $fields[0];
$nameField = $fields[1];
$descriptionField = $fields[2] ?? null;
if ($request->hasFile($photoField)) {
foreach ($request->file($photoField, []) as $key => $value) {
$item = [];
$item[$fields[1]] = $request->input($fields[1] . '.' . $key);
$item[$photoField] = $this->uploadFile($value, $photoField . '.' . $key);
$newFiles = $request->file($photoField, []);
$newNames = $request->input($nameField, []);
$newDescriptions = $descriptionField ? $request->input($descriptionField, []) : [];
if (isset($fields[2])) {
$item[$fields[2]] = $request->input($fields[2] . '.' . $key);
// Process each new file
foreach ($newFiles as $key => $file) {
// Create new photo entry
$newPhotoEntry = [
$nameField => $newNames[$key] ?? '', // Use new name if provided
$photoField => $this->uploadFile($file, $photoField . '.' . $key)
];
// Add description if field exists
if ($descriptionField) {
$newPhotoEntry[$descriptionField] = $newDescriptions[$key] ?? '';
}
$result[] = $item;
// Add to result
$result[] = $newPhotoEntry;
}
}
return $result;
}
private function categoryHasNewFiles(Request $request, array $fields): bool
{
$photoField = $fields[0]; // First element is usually the photo field