fix(survyor/penilai): perbaikan view print-resume, lampiran, foto-lampiran, foto, header, main dan controller penilai dan surveyor

This commit is contained in:
majid
2025-02-25 08:50:03 +07:00
parent e4cec2a9a2
commit dfa364cdd8
10 changed files with 245 additions and 318 deletions

View File

@@ -491,56 +491,78 @@ class SurveyorController extends Controller
{
if ($request->hasFile($paramName)) {
$files = $request->file($paramName);
// Pastikan $files adalah array
if (!is_array($files)) {
$files = [$files];
}
$formatFotoData = [];
$nomor_registrasi = $request->nomor_registrasi;
// Generate a unique timestamp for this batch
$batchTimestamp = time();
foreach ($files as $index => $file) {
$timestamp = time();
$originalName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
// Validasi nama file
if (empty($originalName)) {
$originalName = "file_{$timestamp}";
$originalName = "file_{$batchTimestamp}";
}
$uniqueFileName = "{$timestamp}.{$extension}";
// Use batchTimestamp and index to ensure uniqueness
$uniqueFileName = "{$batchTimestamp}_{$index}.{$extension}";
// Simpan file
$nomor_registrasi = $request->nomor_registrasi;
$path = $file->storeAs("surveyor/{$paramName}/{$nomor_registrasi}", $uniqueFileName, 'public');
$fotoData = [
'name' => pathinfo($originalName, PATHINFO_FILENAME),
'path' => $path,
'category' => 'lainnya',
'sub' => null,
'description' => null,
'created_by' => Auth::user()->name,
'created_at' => now()->toDateTimeString(),
];
$formatFotoData[] = $fotoData;
// Check if this file already exists in formatFotojson
$fileExists = false;
if (isset($formatFotojson[$paramName]) && is_array($formatFotojson[$paramName])) {
foreach ($formatFotojson[$paramName] as $existingFile) {
// Check if the original file name matches
if (isset($existingFile['name']) &&
$existingFile['name'] === pathinfo($originalName, PATHINFO_FILENAME)) {
$fileExists = true;
break;
}
}
}
// Only add if this file doesn't already exist
if (!$fileExists) {
$fotoData = [
'name' => pathinfo($originalName, PATHINFO_FILENAME),
'path' => $path,
'category' => 'lainnya',
'sub' => null,
'description' => null,
'created_by' => Auth::user()->name,
'created_at' => now()->toDateTimeString(),
];
$formatFotoData[] = $fotoData;
}
}
// Struktur JSON yang konsisten
if (!isset($formatFotojson[$paramName])) {
$formatFotojson[$paramName] = $formatFotoData;
} else {
$formatFotojson[$paramName] = array_merge(
$formatFotojson[$paramName] ?? [],
$formatFotoData
);
// Only update if we have new photos to add
if (!empty($formatFotoData)) {
// Struktur JSON yang konsisten
if (!isset($formatFotojson[$paramName])) {
$formatFotojson[$paramName] = $formatFotoData;
} else {
$formatFotojson[$paramName] = array_merge(
$formatFotojson[$paramName] ?? [],
$formatFotoData
);
}
return $formatFotoData;
}
return $formatFotoData;
}
return [];
}
@@ -699,80 +721,6 @@ class SurveyorController extends Controller
return false;
}
public function hapusLantai(Request $request)
{
$permohonanId = $request->permohonan_id;
$dokumentId = $request->dokument_id;
// Normalisasi path foto
$cleanRequestPath = str_replace(['storage/', 'surveyor/'], '', $request->foto_path);
$inspeksi = Inspeksi::firstOrNew(
['permohonan_id' => $permohonanId, 'dokument_id' => $dokumentId]
);
$fotoLantaiUnit = json_decode($inspeksi->foto_form, true);
// Konversi lantai ke string untuk konsistensi
$lantai = (string)$request->lantai;
// Cek apakah lantai ada di array
$pat = null;
if (isset($fotoLantaiUnit['foto_lantai_unit'][$lantai])) {
// Filter foto, hapus foto yang sesuai
$fotoLantaiUnit['foto_lantai_unit'][$lantai] = array_filter(
$fotoLantaiUnit['foto_lantai_unit'][$lantai],
function ($foto) use ($cleanRequestPath) {
// Normalisasi path foto yang tersimpan
$storedPath = str_replace(['storage/', 'surveyor/'], '', $foto['path']);
// Jika path cocok, hapus file dari storage
if ($storedPath === $cleanRequestPath) {
// Hapus file dari storage dengan berbagai kemungkinan path
$possiblePaths = [
'storage/surveyor/lantai_unit/' . $cleanRequestPath,
'storage/surveyor/' . $cleanRequestPath,
'storage/' . $cleanRequestPath,
$cleanRequestPath
];
foreach ($possiblePaths as $path) {
if (Storage::disk('public')->exists($path)) {
Storage::disk('public')->delete($path);
break;
}
}
return false; // Hapus dari array
}
return true;
}
);
// Reset index array
$fotoLantaiUnit['foto_lantai_unit'][$lantai] = array_values($fotoLantaiUnit['foto_lantai_unit'][$lantai]);
// Hapus lantai jika tidak ada foto
if (empty($fotoLantaiUnit['foto_lantai_unit'][$lantai])) {
unset($fotoLantaiUnit['foto_lantai_unit'][$lantai]);
}
// Update inspeksi
$inspeksi->foto_form = json_encode($fotoLantaiUnit);
$inspeksi->save();
return response()->json([
'success' => true,
'foto_lantai_unit' => $pat
]);
}
return response()->json([
'success' => false,
'message' => 'gagal menghapus'
], 404);
}
private function generateUniqueFileName($file, $prefix = '')
{
@@ -797,7 +745,7 @@ class SurveyorController extends Controller
'permohonan_id' => 'required',
'dokument_id' => 'required',
'nomor_registrasi' => 'required',
'foto_rute.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'upload_foto.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
]);
}