perbaikan fominspeksi upload foto
This commit is contained in:
@@ -11,6 +11,7 @@ use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Lpj\Exports\BasicDataSurveyorExport;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Http\Response;
|
||||
use Modules\Lpj\Models\Debiture;
|
||||
@@ -313,7 +314,6 @@ class SurveyorController extends Controller
|
||||
'permohonan_id' => $request->input('permohonan_id'),
|
||||
'dokument_id' => $request->input('dokument_id')
|
||||
]);
|
||||
|
||||
// Get existing foto_form data if it exists
|
||||
$existingData = $inspeksi->exists && $inspeksi->foto_form
|
||||
? json_decode($inspeksi->foto_form, true)
|
||||
@@ -321,6 +321,12 @@ class SurveyorController extends Controller
|
||||
|
||||
$formatFotojson = $existingData;
|
||||
|
||||
// Upload berbagai jenis foto
|
||||
$this->handleFileUpload($request, 'rute_menuju_lokasi', $formatFotojson);
|
||||
$this->handleFileUpload($request, 'foto_lingkungan', $formatFotojson);
|
||||
|
||||
$this->processFotoLantaiUnit($request, $formatFotojson);
|
||||
|
||||
if ($request->hasFile('foto_objek')) {
|
||||
$existingObjekJaminan = $formatFotojson['object_jaminan'] ?? [];
|
||||
$formatFotojson['object_jaminan'] = $this->processObjekJaminanPhotos(
|
||||
@@ -329,27 +335,6 @@ class SurveyorController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
if ($request->has('foto_lantai_unit')) {
|
||||
$existingLantaiUnit = $formatFotojson['foto_lantai_unit'] ?? [];
|
||||
$newLantaiUnit = $this->processFotoLantaiUnit($request);
|
||||
|
||||
foreach ($newLantaiUnit as $key => $newFiles) {
|
||||
$existingLantaiUnit[$key] = array_merge($existingLantaiUnit[$key] ?? [], $newFiles);
|
||||
}
|
||||
|
||||
$formatFotojson['foto_lantai_unit'] = $existingLantaiUnit;
|
||||
$log['foto_lantai_unit'] = $formatFotojson['foto_lantai_unit'];
|
||||
}
|
||||
|
||||
foreach ($photoCategories as $key) {
|
||||
if ($request->has($key)) {
|
||||
$newPhotos = $this->processPhotoCategories($request, $key);
|
||||
$existingPhotos = $formatFotojson[$key] ?? [];
|
||||
$formatFotojson[$key] = array_merge($existingPhotos, $newPhotos);
|
||||
$log[$key] = $formatFotojson[$key];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($lainnya as $category => $fields) {
|
||||
$photoField = $fields[0];
|
||||
$nameField = $fields[1];
|
||||
@@ -365,7 +350,7 @@ class SurveyorController extends Controller
|
||||
$formatFotojson[$category] = $newPhotos;
|
||||
}
|
||||
}
|
||||
// Process single files
|
||||
// // Process single files
|
||||
$singleFiles = ['foto_basement', 'foto_gerbang', 'pendamping'];
|
||||
foreach ($singleFiles as $file) {
|
||||
if ($request->hasFile($file)) {
|
||||
@@ -381,56 +366,251 @@ class SurveyorController extends Controller
|
||||
}
|
||||
|
||||
// Update record
|
||||
$inspeksi->foto_form = json_encode($formatFotojson);
|
||||
$inspeksi->save();
|
||||
if (!empty($formatFotojson)) {
|
||||
$inspeksi->foto_form = json_encode($formatFotojson);
|
||||
$inspeksi->save();
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Data berhasil disimpan', 'data' => $formatFotojson, 'log' => $log ], 200);
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Data berhasil disimpan',
|
||||
'data' => $formatFotojson,
|
||||
'log' => $log
|
||||
], 200);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Tidak ada data untuk disimpan'
|
||||
], 400);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['success' => false, 'message' => 'Failed to upload: ' . $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a photo category and its subcategories
|
||||
* Process a photo
|
||||
*/
|
||||
private function processPhotoCategories(Request $request, string $categoryKey)
|
||||
{
|
||||
$processedPhotos = [];
|
||||
$categoryPhotos = $request->file($categoryKey, []);
|
||||
if (is_array($categoryPhotos)) {
|
||||
$categoryPhotos = array_merge(...array_values($categoryPhotos));
|
||||
}
|
||||
$namePrefix = str_replace('_', ' ', $categoryKey);
|
||||
protected function processFotoLantaiUnit(Request $request, &$formatFotojson)
|
||||
{
|
||||
// Pastikan foto_lantai_unit sudah ada di formatFotojson
|
||||
if (!isset($formatFotojson['foto_lantai_unit'])) {
|
||||
$formatFotojson['foto_lantai_unit'] = [];
|
||||
}
|
||||
|
||||
// Ambil nama-nama lantai dari request
|
||||
$lantaiNama = $request->input('lantai_nama', []);
|
||||
|
||||
// Tambahan: gunakan lantai_index jika tersedia
|
||||
$lantaiIndex = $request->input('lantai_index', null);
|
||||
|
||||
// Cek apakah ada file foto lantai yang diunggah
|
||||
$lantaiFiles = $request->file('foto_lantai_unit', []);
|
||||
|
||||
// Proses setiap file foto lantai
|
||||
foreach ($lantaiFiles as $index => $files) {
|
||||
// Pastikan $files adalah array
|
||||
if (!is_array($files)) {
|
||||
$files = [$files];
|
||||
}
|
||||
|
||||
// Gunakan lantai_index jika tersedia, jika tidak gunakan cara sebelumnya
|
||||
$lantaiNomor = $lantaiIndex ?? ($index + 1);
|
||||
|
||||
// Inisialisasi array untuk lantai ini jika belum ada
|
||||
if (!isset($formatFotojson['foto_lantai_unit'][$lantaiNomor])) {
|
||||
$formatFotojson['foto_lantai_unit'][$lantaiNomor] = [];
|
||||
}
|
||||
|
||||
foreach ($files as $fileIndex => $file) {
|
||||
// Validasi file
|
||||
if (!$file->isValid()) {
|
||||
continue; // Lewati file yang tidak valid
|
||||
}
|
||||
|
||||
// Generate nama file unik
|
||||
$uniqueFileName = 'lantai_unit_' . $lantaiNomor . '_' . $fileIndex . '_' . Str::random(10) . '.' . $file->getClientOriginalExtension();
|
||||
|
||||
// Simpan file dengan nama asli
|
||||
$path = $file->storeAs(
|
||||
'surveyor/lantai_unit',
|
||||
$uniqueFileName . '/' . time() . '_' . $file->getClientOriginalName(),
|
||||
'public'
|
||||
);
|
||||
|
||||
// Buat nama foto
|
||||
$fotoName = "Foto Lantai {$lantaiNomor} - " . ($fileIndex + 1);
|
||||
|
||||
// Tambahkan detail foto ke array
|
||||
$fotoDetail = [
|
||||
'path' => $path,
|
||||
'name' => $fotoName
|
||||
];
|
||||
|
||||
// Tambahkan ke array foto lantai unit sesuai struktur
|
||||
$formatFotojson['foto_lantai_unit'][$lantaiNomor][] = $fotoDetail;
|
||||
}
|
||||
}
|
||||
|
||||
return $formatFotojson;
|
||||
}
|
||||
|
||||
|
||||
|
||||
foreach ($categoryPhotos as $index => $file) {
|
||||
// Validate the file
|
||||
if (!$file instanceof \Illuminate\Http\UploadedFile || !$file->isValid()) {
|
||||
continue;
|
||||
private function handleFileUpload(Request $request, $paramName, &$formatFotojson)
|
||||
{
|
||||
if ($request->hasFile($paramName)) {
|
||||
$files = $request->file($paramName);
|
||||
|
||||
// Pastikan $files adalah array
|
||||
if (!is_array($files)) {
|
||||
$files = [$files];
|
||||
}
|
||||
|
||||
// Generate a unique filename for the photo
|
||||
$filename = $this->generateUniqueFileName($file, "{$categoryKey}_{$index}");
|
||||
$formatFotoData = [];
|
||||
|
||||
if (isset($processedPhotos[$categoryKey])) {
|
||||
$processedPhotos[$categoryKey][0][] = [
|
||||
'path' => $this->uploadFile($file, $filename),
|
||||
'name' => ucfirst($namePrefix) . ' - ' . ($index + 1),
|
||||
foreach ($files as $index => $file) {
|
||||
$timestamp = time();
|
||||
$originalName = $file->getClientOriginalName();
|
||||
$uniqueFileName = "{$timestamp}_{$originalName}";
|
||||
|
||||
// Simpan file
|
||||
$path = $file->storeAs("surveyor/{$paramName}", $uniqueFileName, 'public');
|
||||
|
||||
$fotoData = [
|
||||
'path' => $path,
|
||||
'name' => ucwords(str_replace('_', ' ', $paramName)) . ' - ' . ($index + 1)
|
||||
];
|
||||
} else {
|
||||
$processedPhotos[$categoryKey] = [
|
||||
[
|
||||
[
|
||||
'path' => $this->uploadFile($file, $filename),
|
||||
'name' => ucfirst($namePrefix) . ' - ' . ($index + 1),
|
||||
]
|
||||
|
||||
$formatFotoData[] = $fotoData;
|
||||
}
|
||||
|
||||
// Struktur JSON yang konsisten
|
||||
if (!isset($formatFotojson[$paramName][$paramName][0])) {
|
||||
$formatFotojson[$paramName] = [
|
||||
$paramName => [
|
||||
$formatFotoData
|
||||
]
|
||||
];
|
||||
} else {
|
||||
$formatFotojson[$paramName][$paramName][0] = array_merge(
|
||||
$formatFotojson[$paramName][$paramName][0] ?? [],
|
||||
$formatFotoData
|
||||
);
|
||||
}
|
||||
|
||||
return $formatFotoData;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function hapusFoto(Request $request)
|
||||
{
|
||||
try {
|
||||
$permohonanId = $request->permohonan_id;
|
||||
$dokumentId = $request->dokument_id;
|
||||
$paramName = $request->param_name;
|
||||
|
||||
$cleanRequestPath = str_replace('/storage/', '', $request->path);
|
||||
|
||||
$inspeksi = Inspeksi::firstOrNew(
|
||||
['permohonan_id' => $permohonanId, 'dokument_id' => $dokumentId]
|
||||
);
|
||||
$fotoForm = json_decode($inspeksi->foto_form, true);
|
||||
|
||||
if (isset($fotoForm[$paramName][$paramName][0])) {
|
||||
// Filter foto yang akan dihapus
|
||||
|
||||
$tes = null;
|
||||
$fotoForm[$paramName][$paramName][0] = array_values(
|
||||
array_filter(
|
||||
$fotoForm[$paramName][$paramName][0],
|
||||
function ($foto) use ($cleanRequestPath) {
|
||||
if ($foto['path'] === $cleanRequestPath) {
|
||||
// Hapus file dari storage
|
||||
\Storage::disk('public')->delete($cleanRequestPath);
|
||||
return false; // Hapus elemen ini dari array
|
||||
}
|
||||
$tes = $foto['path'];
|
||||
return true;
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// Reset array index
|
||||
$fotoForm[$paramName][$paramName][0] = array_values($fotoForm[$paramName][$paramName][0]);
|
||||
|
||||
$inspeksi->foto_form = $fotoForm;
|
||||
$inspeksi->save();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Foto berhasil dihapus',
|
||||
], 200);
|
||||
}
|
||||
|
||||
return response()->json(['success' => false], 400);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
// Metode untuk mendapatkan foto yang sudah diupload
|
||||
public function getFoto(Request $request)
|
||||
{
|
||||
$permohonanId = $request->permohonan_id;
|
||||
$dokumentId = $request->dokument_id;
|
||||
$paramName = $request->param_name;
|
||||
|
||||
$inspeksi = Inspeksi::firstOrNew(
|
||||
['permohonan_id' => $permohonanId, 'dokument_id' => $dokumentId]
|
||||
);
|
||||
|
||||
// Decode foto_form
|
||||
$fotoForm = json_decode($inspeksi->foto_form, true) ?? [];
|
||||
|
||||
// Cari foto berdasarkan param name
|
||||
$fotos = $this->findFotoByParamName($fotoForm, $paramName);
|
||||
|
||||
|
||||
$param = $paramName == 'rute_menuju_lokasi' ? $fotos['rute_menuju_lokasi'][0] : $fotos['foto_lingkungan'][0];
|
||||
$data = array_map(function ($foto) {
|
||||
return [
|
||||
'name' => $foto['name'] ?? 'Foto',
|
||||
'path' => \Storage::url($foto['path']),
|
||||
];
|
||||
}, $param);
|
||||
|
||||
return response()->json([
|
||||
'fotos' => $data,
|
||||
'success' => !empty($data)
|
||||
]);
|
||||
}
|
||||
|
||||
private function findFotoByParamName($fotoForm, $paramName)
|
||||
{
|
||||
// Mapping parameter name ke struktur JSON
|
||||
$paramMapping = [
|
||||
'rute_menuju_lokasi' => ['rute_menuju_lokasi'],
|
||||
'foto_lingkungan' => ['foto_lingkungan'],
|
||||
];
|
||||
|
||||
// // Traverse array menggunakan mapping
|
||||
$fotos = $fotoForm;
|
||||
if (isset($paramMapping[$paramName])) {
|
||||
foreach ($paramMapping[$paramName] as $key) {
|
||||
$fotos = $fotos[$key] ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
return $processedPhotos;
|
||||
return is_array($fotos) ? $fotos : [];
|
||||
}
|
||||
|
||||
|
||||
private function processObjekJaminanPhotos(Request $request, array $existingPhotos = [])
|
||||
{
|
||||
$photoField = 'foto_objek';
|
||||
@@ -486,34 +666,79 @@ class SurveyorController extends Controller
|
||||
return false;
|
||||
}
|
||||
|
||||
private function processFotoLantaiUnit(Request $request)
|
||||
public function hapusLantai(Request $request)
|
||||
{
|
||||
$processedFotoLantaiUnit = [];
|
||||
$fotoLantaiUnit = $request->file('foto_lantai_unit', []);
|
||||
$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);
|
||||
|
||||
foreach ($fotoLantaiUnit as $lantaiKey => $files) {
|
||||
$processedFiles = [];
|
||||
|
||||
foreach ($files as $index => $file) {
|
||||
if (!$file || !$file->isValid()) {
|
||||
continue;
|
||||
// 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;
|
||||
}
|
||||
);
|
||||
|
||||
$filename = $this->generateUniqueFileName($file, "lantai_unit_{$lantaiKey}_{$index}");
|
||||
$processedFiles[] = [
|
||||
'path' => $this->uploadFile($file, $filename),
|
||||
'name' => "Foto Lantai {$lantaiKey} - " . ($index + 1),
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($processedFiles)) {
|
||||
$processedFotoLantaiUnit[$lantaiKey] = $processedFiles;
|
||||
// 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 $processedFotoLantaiUnit;
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'gagal menghapus'
|
||||
], 404);
|
||||
}
|
||||
|
||||
|
||||
private function processPhotoLainnya(Request $request, array $fields, array $existingPhotos = [])
|
||||
{
|
||||
$result = $existingPhotos; // Start with existing photos
|
||||
|
||||
Reference in New Issue
Block a user