update foto lantai

This commit is contained in:
majid
2024-12-27 16:52:02 +07:00
parent 78184094f6
commit 2005f546c5
4 changed files with 126 additions and 124 deletions

View File

@@ -656,7 +656,7 @@ class PenilaiController extends Controller
'lpjData',
'formFoto',
'basicData',
'inspeksi',
'inspeksi',
'lpj',
'statusLpj',
'alamat',

View File

@@ -384,44 +384,48 @@ class SurveyorController extends Controller
return $result;
}
private function processFotoLantaiUnit(Request $request)
public function replaceFotoLantaiUnit(Request $request)
{
$processedFotoLantaiUnit = [];
// Ambil semua file foto lantai unit
$fotoLantaiUnit = $request->file('foto_lantai_unit', []);
foreach ($fotoLantaiUnit as $lantaiKey => $files) {
$processedFiles = [];
foreach ($files as $index => $file) {
// 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 unique filename
$filename = $this->generateUniqueFileName($file, "lantai_unit_{$lantaiKey}_{$index}");
$processedFiles[] = [
'path' => $this->uploadFile($file, $filename . '.' . $index),
'name' => "Foto Lantai {$lantaiKey} - " . ($index + 1),
// 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;
}
}
// Simpan file yang diproses dengan kunci lantai
if (!empty($processedFiles)) {
$processedFotoLantaiUnit[$lantaiKey] = $processedFiles;
}
}
return $processedFotoLantaiUnit;
// 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;
}
// 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
{
@@ -476,11 +480,11 @@ private function generateUniqueFileName($file, $prefix = '')
'name_basement.*' => 'nullable|string|max:255',
'foto_gerbang' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'name_gerbang' => 'nullable|string|max:255',
'name_lantai_unit' => 'array',
'foto_lantai_unit' => 'array',
'foto_lantai_unit.*' => 'array',
'foto_lantai_unit.*' => 'array',
]);
}
@@ -1458,7 +1462,7 @@ private function generateUniqueFileName($file, $prefix = '')
$query->whereRaw('LOWER(status) = ?', ['assign']);
if(!Auth::user()->hasRole('administrator')) {
if (!Auth::user()->hasRole('administrator')) {
$query->whereHas('penilaian.userPenilai', function ($q) {
$q->where('user_id', Auth::user()->id);
$q->where('role', 'surveyor');
@@ -1582,7 +1586,7 @@ private function generateUniqueFileName($file, $prefix = '')
if ($type == 'Spek Bangunan') {
$data = $query->with(['bangunanKategori'])->get();
}else{
} else {
$data = $query->get();
}

View File

@@ -372,97 +372,96 @@
@endforeach
@if (isset($formFoto['foto_lantai_unit']))
<div class="flex flex-wrap gap-4 w-full">
<div class="w-full">
<div class="text-white py-4 flex items-center justify-between w-full">
<label class="form-label">
<span class="form-label">Lantai</span>
</label>
<button type="button" id="btnAddLantai" class="btn btn-primary btn-sm">
<i class="ki-filled ki-plus text-lg"></i> Tambah Lantai
</button>
</div>
<div class="flex flex-wrap gap-4 w-full">
<div class="w-full">
<div class="text-white py-4 flex items-center justify-between w-full">
<label class="form-label">
<span class="form-label">Lantai</span>
</label>
<button type="button" id="btnAddLantai" class="btn btn-primary btn-sm">
<i class="ki-filled ki-plus text-lg"></i> Tambah Lantai
</button>
</div>
<div id="lantaiContainer" class="w-full">
@foreach ($formFoto['foto_lantai_unit'] as $lantaiKey => $lantaiFotos)
<div class="lantai-item w-full mb-4">
<div class="flex w-full items-center justify-between">
<label class="form-label">Lantai {{ $lantaiKey }}</label>
<button type="button" class="btn btn-danger btn-sm btnRemoveLantai"
style="{{ $lantaiKey == 1 ? 'display: none;' : '' }}">
Hapus
</button>
</div>
<div class="dropzone w-full border-2 border-dashed border-gray-400 rounded-lg p-4 mt-2"
ondrop="handleDrop(event)" ondragover="allowDrop(event)">
<div class="preview-container grid grid-cols-2 md:grid-cols-4 gap-4">
@foreach ($lantaiFotos as $foto)
<div class="preview-item relative">
<img src="{{ asset('storage/' . $foto['path']) }}"
alt="{{ $foto['name'] }}"
class="w-full h-40 object-cover rounded-lg">
<button type="button"
class="absolute top-2 right-2 btn btn-sm btn-danger btn-icon btnRemoveFoto"
data-path="{{ $foto['path'] }}">
<i class="ki-solid ki-cross"></i>
</button>
</div>
@endforeach
<div id="lantaiContainer" class="w-full">
@foreach ($formFoto['foto_lantai_unit'] as $lantaiKey => $lantaiFotos)
<div class="lantai-item w-full mb-4">
<div class="flex w-full items-center justify-between">
<label class="form-label">Lantai {{ $lantaiKey }}</label>
<button type="button" class="btn btn-danger btn-sm btnRemoveLantai"
style="{{ $lantaiKey == 1 ? 'display: none;' : '' }}">
Hapus
</button>
</div>
<div class="dropzone w-full border-2 border-dashed border-gray-400 rounded-lg p-4 mt-2"
ondrop="handleDrop(event)" ondragover="allowDrop(event)">
<div class="preview-container grid grid-cols-2 md:grid-cols-4 gap-4">
@foreach ($lantaiFotos as $foto)
@if (is_array($foto) && isset($foto['path']) && isset($foto['name']))
<div class="preview-item relative">
<img src="{{ asset('storage/' . $foto['path']) }}"
alt="{{ $foto['name'] }}"
class="w-full h-40 object-cover rounded-lg">
<button type="button"
class="absolute top-2 right-2 btn btn-sm btn-danger btn-icon btnRemoveFoto"
data-path="{{ $foto['path'] }}">
<i class="ki-solid ki-cross"></i>
</button>
</div>
@else
<p>Invalid photo data.</p>
@endif
@endforeach
</div>
<input type="file" name="foto_lantai_unit[{{ $lantaiKey }}][]"
class="file-input hidden" multiple accept="image/*"
onchange="handleFileInput(this)" data-lantai="{{ $lantaiKey }}">
<button type="button" class="btn btn-light btn-sm btnUploadFiles mt-2">
<i class="ki-outline ki-upload"></i> Unggah Gambar
</button>
</div>
</div>
@endforeach
</div>
</div>
<input type="file"
name="foto_lantai_unit[{{ $lantaiKey }}][]"
class="file-input hidden"
multiple
accept="image/*"
onchange="handleFileInput(this)"
data-lantai="{{ $lantaiKey }}">
<button type="button" class="btn btn-light btn-sm btnUploadFiles mt-2">
<i class="ki-outline ki-upload"></i> Unggah Gambar
</button>
</div>
</div>
@endforeach
</div>
</div>
</div>
@else
@else
<div class="flex flex-wrap gap-4 w-full">
<div class="w-full">
<div class="text-white py-4 flex items-center justify-between w-full">
<label class="form-label">
<span class="form-label">Lantai</span>
</label>
<button type="button" id="btnAddLantai" class="btn btn-primary btn-sm">
<i class="ki-filled ki-plus text-lg"></i> Tambah Lantai
</button>
</div>
<div class="flex flex-wrap gap-4 w-full">
<div class="w-full">
<div class="text-white py-4 flex items-center justify-between w-full">
<label class="form-label">
<span class="form-label">Lantai</span>
</label>
<button type="button" id="btnAddLantai" class="btn btn-primary btn-sm">
<i class="ki-filled ki-plus text-lg"></i> Tambah Lantai
</button>
</div>
<div id="lantaiContainer" class="w-full">
<!-- Template Lantai -->
<div class="lantai-item w-full mb-4">
<div class="flex w-full items-center justify-between">
<label class="form-label">Lantai 1</label>
<button type="button" class="btn btn-danger btn-sm btnRemoveLantai"
style="display: none;">
Hapus
</button>
</div>
<div class="dropzone w-full border-2 border-dashed border-gray-400 rounded-lg p-4 mt-2"
ondrop="handleDrop(event)" ondragover="allowDrop(event)">
<div class="preview-container grid grid-cols-2 md:grid-cols-4 gap-4"></div>
<input type="file" name="foto_lantai_unit[1][]" class="file-input hidden"
multiple accept="image/*" onchange="handleFileInput(this)" data-lantai="1">
<button type="button" class="btn btn-light btn-sm btnUploadFiles mt-2">
<i class="ki-outline ki-upload"></i> Unggah Gambar
</button>
<div id="lantaiContainer" class="w-full">
<!-- Template Lantai -->
<div class="lantai-item w-full mb-4">
<div class="flex w-full items-center justify-between">
<label class="form-label">Lantai 1</label>
<button type="button" class="btn btn-danger btn-sm btnRemoveLantai"
style="display: none;">
Hapus
</button>
</div>
<div class="dropzone w-full border-2 border-dashed border-gray-400 rounded-lg p-4 mt-2"
ondrop="handleDrop(event)" ondragover="allowDrop(event)">
<div class="preview-container grid grid-cols-2 md:grid-cols-4 gap-4"></div>
<input type="file" name="foto_lantai_unit[1][]" class="file-input hidden"
multiple accept="image/*" onchange="handleFileInput(this)"
data-lantai="1">
<button type="button" class="btn btn-light btn-sm btnUploadFiles mt-2">
<i class="ki-outline ki-upload"></i> Unggah Gambar
</button>
</div>
</div>
</div>
</div>
</div>
</div>
@endif
@endif
</div>
</div>
@@ -602,8 +601,7 @@
@include('lpj::surveyor.js.utils')
@push('scripts')
<script>
console.log('@json($formFoto)');
// console.log('@json($formFoto)');
document.addEventListener("DOMContentLoaded", () => {
const lantaiContainer = document.getElementById("lantaiContainer");
@@ -783,7 +781,7 @@
}
});
console.log(formData.value);
@@ -810,7 +808,7 @@
window.location.href =
'{{ route('surveyor.show', ['id' => $permohonan->id]) }}';
}
// console.log(response);
console.log(response);
});
} else {

View File

@@ -187,8 +187,8 @@
setupInputHandlers('inputContainerRute', 'btnRute', 'Foto Rute Menuju Lokasi', 'file-input',
'delete-btn');
setupInputHandlers('inputContainerLantai', 'btnLantai', 'Foto Lantai', 'file-input',
'delete-btn');
// setupInputHandlers('inputContainerLantai', 'btnLantai', 'Foto Lantai', 'file-input',
// 'delete-btn');
setupInputHandlers('inputContainerLingkungan', 'btnLingkungan', 'Lingkungan', 'file-input',
'delete-btn');