perbaikan foto dan penambahan basic data foto objek jaminan, print-out

This commit is contained in:
majid
2024-12-29 06:10:45 +07:00
parent 9a24751f65
commit e62524e683
19 changed files with 1175 additions and 676 deletions

View File

@@ -25,6 +25,7 @@ use Modules\Location\Models\District;
use Modules\Location\Models\Village;
use Modules\Lpj\Models\PosisiKavling;
use Modules\Lpj\Models\KondisiFisikTanah;
use Modules\Lpj\Models\FotoObjekJaminan;
use Modules\Lpj\Models\KetinggianTanah;
use Modules\Lpj\Models\SifatBangunan;
use Modules\Lpj\Models\JenisJaminan;
@@ -284,16 +285,18 @@ class SurveyorController extends Controller
{
$validatedData = $this->validateFotoRequest($request);
try {
$log = [];
$photoCategories = [
'rute_menuju_lokasi' => ['foto_rute', 'name_rute'],
'object_jaminan' => ['foto_objek', 'name_objek', 'deskripsi_objek'],
'lingkungan' => ['foto_lingkungan', 'name_lingkungan'],
'foto_lantai_unit' => ['foto_lantai_unit', 'name_lantai_unit'],
'foto_lantai_lainnya' => ['foto_lantai_lainnya', 'name_lantai_lainnya'],
'foto_rute_lainnya' => ['foto_rute_lainnya', 'name_rute_lainnya'],
'rute_menuju_lokasi',
'foto_lingkungan',
];
$lainnya =[
'foto_rute_lainnya' => ['foto_rute_lainnya', 'name_rute_lainnya'],
'foto_lantai_lainnya' => ['foto_lantai_lainnya', 'name_lantai_lainnya']
];
$inspeksi = Inspeksi::firstOrNew([
'permohonan_id' => $request->input('permohonan_id'),
'dokument_id' => $request->input('dokument_id')
@@ -306,37 +309,62 @@ class SurveyorController extends Controller
$formatFotojson = $existingData;
if ($request->has('foto_lantai_unit')) {
$formatFotojson['foto_lantai_unit'] = $this->processFotoLantaiUnit($request);
if ($request->hasFile('foto_objek')) {
$existingObjekJaminan = $formatFotojson['object_jaminan'] ?? [];
$formatFotojson['object_jaminan'] = $this->processObjekJaminanPhotos(
$request,
$existingObjekJaminan
);
}
foreach ($photoCategories as $category => $fields) {
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];
$descriptionField = $fields[2] ?? null;
if ($request->hasFile($photoField)) {
$newPhotos = $this->processPhotoCategory(
$newPhotos = $this->processPhotoLainnya(
$request,
$fields,
$existingData[$category] ?? [],
$category // Pass category to the function
$existingData[$category] ?? []
);
$formatFotojson[$category] = $newPhotos;
}
}
// Process single files
$singleFiles = ['foto_basement', 'foto_gerbang', 'pendamping'];
foreach ($singleFiles as $file) {
if ($request->hasFile($file)) {
// Delete old file if exists
if (isset($existingData[$file])) {
$this->deleteFile($existingData[$file]);
// Hapus file lama jika ada
if (isset($formatFotojson[$file])) {
$this->deleteOfFile($formatFotojson[$file]);
}
// Upload dan simpan file baru
$formatFotojson[$file] = $this->uploadFile($request->file($file), $file);
}
}
@@ -344,7 +372,7 @@ class SurveyorController extends Controller
$inspeksi->foto_form = json_encode($formatFotojson);
$inspeksi->save();
return response()->json(['success' => true, 'message' => 'Data berhasil disimpan', 'data' => $formatFotojson], 200);
return response()->json(['success' => true, 'message' => 'Data berhasil disimpan', 'data' => $formatFotojson, 'log' => $log ], 200);
} catch (Exception $e) {
return response()->json(['success' => false, 'message' => 'Failed to upload: ' . $e->getMessage()], 500);
}
@@ -353,19 +381,134 @@ class SurveyorController extends Controller
/**
* Process a photo category and its subcategories
*/
private function processPhotoCategory(Request $request, array $fields, array $existingPhotos = [], $category = null)
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);
foreach ($categoryPhotos as $index => $file) {
// Validate the file
if (!$file instanceof \Illuminate\Http\UploadedFile || !$file->isValid()) {
continue;
}
// Generate a unique filename for the photo
$filename = $this->generateUniqueFileName($file, "{$categoryKey}_{$index}");
if (isset($processedPhotos[$categoryKey])) {
$processedPhotos[$categoryKey][0][] = [
'path' => $this->uploadFile($file, $filename),
'name' => ucfirst($namePrefix) . ' - ' . ($index + 1),
];
} else {
$processedPhotos[$categoryKey] = [
[
[
'path' => $this->uploadFile($file, $filename),
'name' => ucfirst($namePrefix) . ' - ' . ($index + 1),
]
]
];
}
}
return $processedPhotos;
}
private function processObjekJaminanPhotos(Request $request, array $existingPhotos = [])
{
$photoField = 'foto_objek';
$nameField = 'name_objek';
$descriptionField = 'deskripsi_objek';
// Mulai dengan data lama
$result = $existingPhotos;
if ($request->hasFile($photoField)) {
$newFiles = $request->file($photoField, []);
$newNames = $request->input($nameField, []);
$newDescriptions = $request->input($descriptionField, []);
foreach ($newFiles as $key => $file) {
// Cari atau buat entri berdasarkan nama objek
$existingIndex = $this->findObjekJaminanIndexByName($result, $newNames[$key]);
// Buat entri baru untuk file yang diunggah
$newPhotoEntry = [
$nameField => $newNames[$key],
$photoField => $this->uploadFile($file, $photoField . '.' . $key),
$descriptionField => $newDescriptions[$key] ?? '',
];
// Jika sudah ada, update; jika belum, tambahkan
if ($existingIndex !== false) {
// Hapus file lama jika ada
if (isset($result[$existingIndex][$photoField])) {
$this->deleteOfFile($result[$existingIndex][$photoField]);
}
$result[$existingIndex] = $newPhotoEntry;
} else {
$result[] = $newPhotoEntry;
}
}
}
// Pastikan array tetap terurut dengan benar
return array_values($result);
}
/**
* Cari index objek jaminan berdasarkan nama objek
*/
private function findObjekJaminanIndexByName(array $existingPhotos, string $name)
{
foreach ($existingPhotos as $index => $photo) {
if (isset($photo['name_objek']) && $photo['name_objek'] === $name) {
return $index;
}
}
return false;
}
private function processFotoLantaiUnit(Request $request)
{
$processedFotoLantaiUnit = [];
$fotoLantaiUnit = $request->file('foto_lantai_unit', []);
foreach ($fotoLantaiUnit as $lantaiKey => $files) {
$processedFiles = [];
foreach ($files as $index => $file) {
if (!$file || !$file->isValid()) {
continue;
}
$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;
}
}
return $processedFotoLantaiUnit;
}
private function processPhotoLainnya(Request $request, array $fields, array $existingPhotos = [])
{
$result = $existingPhotos; // Start with existing photos
$photoField = $fields[0];
$nameField = $fields[1];
$descriptionField = $fields[2] ?? null;
if ($category === 'object_jaminan') {
// If it's 'object_jaminan', do not overwrite existing photos, just add new ones
$existingPhotos = $existingPhotos ?: [];
$result = $existingPhotos;
}
if ($request->hasFile($photoField)) {
$newFiles = $request->file($photoField, []);
$newNames = $request->input($nameField, []);
@@ -375,7 +518,7 @@ class SurveyorController extends Controller
foreach ($newFiles as $key => $file) {
// Create new photo entry
$newPhotoEntry = [
$nameField => $newNames[$key] ?? '',
$nameField => $newNames[$key] ?? '', // Use new name if provided
$photoField => $this->uploadFile($file, $photoField . '.' . $key)
];
@@ -392,70 +535,12 @@ class SurveyorController extends Controller
return $result;
}
public function replaceFotoLantaiUnit(Request $request)
{
// Pastikan foto lantai unit ada dalam request
if ($request->has('foto_lantai_unit')) {
$fotoLantaiUnit = $request->file('foto_lantai_unit', []);
// Target lantai dan foto yang ingin diubah
$lantaiKey = $request->input('lantai_key'); // Misal: 1
$indexToReplace = $request->input('index'); // Misal: 0
// Cek jika lantaiKey ada dan file yang akan diganti ada
if (isset($fotoLantaiUnit[$lantaiKey]) && isset($fotoLantaiUnit[$lantaiKey][$indexToReplace])) {
$file = $fotoLantaiUnit[$lantaiKey][$indexToReplace];
// Pastikan file valid
if ($file && $file->isValid()) {
// Generate filename baru untuk foto yang diganti
$filename = $this->generateUniqueFileName($file, "lantai_unit_{$lantaiKey}_{$indexToReplace}");
// Gantikan foto yang lama dengan yang baru
$processedFoto = [
'path' => $this->uploadFile($file, $filename . '.' . $file->getClientOriginalExtension()),
'name' => "Foto Lantai {$lantaiKey} - " . ($indexToReplace + 1),
];
// Update foto lantai unit yang sudah diproses
$fotoLantaiUnit[$lantaiKey][$indexToReplace] = $processedFoto;
}
}
}
// Kembalikan data foto lantai unit yang sudah diperbarui
return response()->json(['foto_lantai_unit' => $fotoLantaiUnit]);
}
// Fungsi helper untuk generate nama file unik
private function generateUniqueFileName($file, $prefix = '')
{
$extension = $file->getClientOriginalExtension();
return $prefix . '_' . uniqid() . '.' . $extension;
}
private function categoryHasNewFiles(Request $request, array $fields): bool
{
$photoField = $fields[0]; // First element is usually the photo field
return $request->hasFile($photoField) ||
(is_array($request->file($photoField)) && count($request->file($photoField)) > 0);
}
private function deleteFilesForCategory($categoryData)
{
if (!is_array($categoryData)) {
return;
}
foreach ($categoryData as $item) {
if (isset($item['foto'])) {
$this->deleteOfFile($item['foto']);
}
}
}
private function deleteOfFile($filePath)
{
if ($filePath && Storage::exists($filePath)) {
@@ -475,7 +560,7 @@ class SurveyorController extends Controller
'pendamping' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'foto_objek.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'name_objek.*' => 'required|string|max:255',
'foto_lingkungan.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'name_lingkungan.*' => 'required|string|max:255',
'foto_rute.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'name_rute.*' => 'required|string|max:255',
@@ -492,8 +577,7 @@ class SurveyorController extends Controller
'name_lantai_unit' => 'array',
'foto_lantai_unit' => 'array',
'foto_lantai_unit.*' => 'array',
]);
}
@@ -1113,6 +1197,7 @@ class SurveyorController extends Controller
$dokumentId = $validated['dokument'];
$jaminanId = $validated['jenisjaminan'];
$fotoObjekJaminan = FotoObjekJaminan::all();
$permohonan = $this->getPermohonanJaminanId($id, $dokumentId, $jaminanId);
@@ -1127,7 +1212,7 @@ class SurveyorController extends Controller
$formFoto = json_decode($inpeksi->foto_form, true);
}
$fotoJaminan = null;
return view('lpj::surveyor.components.foto', compact('permohonan', 'surveyor', 'branches', 'provinces', 'fotoJaminan', 'formFoto'));
return view('lpj::surveyor.components.foto', compact('permohonan', 'surveyor', 'branches', 'provinces', 'fotoJaminan', 'formFoto', 'fotoObjekJaminan'));
}
/**
@@ -1312,6 +1397,7 @@ class SurveyorController extends Controller
'posisi-unit' => ['Posisi unit', 'posisi-unit', PosisiUnit::class],
'bentuk-unit' => ['Bentuk unit', 'bentuk-unit', BentukUnit::class],
'fasilitas-objek' => ['Fasilitas Umum Dekat Objek', 'fasilitas-objek', FasilitasObjek::class],
'foto-objek-jaminan' => ['Foto Objek Jaminan', 'foto-objek-jaminan', FotoObjekJaminan::class],
];
@@ -1344,6 +1430,10 @@ class SurveyorController extends Controller
$validate['spek_kategori_bagunan_id'] = $request->spek_kategori_bagunan_id;
}
if ($type == 'foto-objek-jaminan') {
$validate['kategori'] = $request->kategori;
}
// Check if the provided type exists in the modelClasses
if (!$modelClass) {
return redirect()
@@ -1552,6 +1642,7 @@ class SurveyorController extends Controller
'Fasilitas Umum Dekat Objek' => FasilitasObjek::class,
'Merupakan Daerah' => MerupakanDaerah::class,
'Jenis unit' => JenisUnit::class,
'Foto Objek Jaminan' => FotoObjekJaminan::class,
];
@@ -1695,6 +1786,7 @@ class SurveyorController extends Controller
'merupakan-daerah' => MerupakanDaerah::class,
'jenis-unit' => JenisUnit::class,
'perkerasan-jalan' => PerkerasanJalan::class,
'foto-objek-jaminan' => FotoObjekJaminan::class
];
@@ -1770,6 +1862,7 @@ class SurveyorController extends Controller
'posisi-unit' => ['Posisi unit', 'posisi-unit'],
'bentuk-unit' => ['Bentuk unit', 'bentuk-unit'],
'fasilitas-objek' => ['Fasilitas Umum Dekat Objek', 'fasilitas-objek'],
'foto-objek-jaminan' => ['Foto Objek Jaminan', 'foto-objek-jaminan'],
];
private function getAssetData(array $data): array