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', 'lpjData',
'formFoto', 'formFoto',
'basicData', 'basicData',
'inspeksi', 'inspeksi',
'lpj', 'lpj',
'statusLpj', 'statusLpj',
'alamat', 'alamat',

View File

@@ -384,44 +384,48 @@ class SurveyorController extends Controller
return $result; return $result;
} }
private function processFotoLantaiUnit(Request $request) public function replaceFotoLantaiUnit(Request $request)
{ {
$processedFotoLantaiUnit = []; // Pastikan foto lantai unit ada dalam request
if ($request->has('foto_lantai_unit')) {
// Ambil semua file foto lantai unit $fotoLantaiUnit = $request->file('foto_lantai_unit', []);
$fotoLantaiUnit = $request->file('foto_lantai_unit', []);
// Target lantai dan foto yang ingin diubah
foreach ($fotoLantaiUnit as $lantaiKey => $files) { $lantaiKey = $request->input('lantai_key'); // Misal: 1
$processedFiles = []; $indexToReplace = $request->input('index'); // Misal: 0
foreach ($files as $index => $file) { // 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 // Pastikan file valid
if ($file && $file->isValid()) { if ($file && $file->isValid()) {
// Generate unique filename // Generate filename baru untuk foto yang diganti
$filename = $this->generateUniqueFileName($file, "lantai_unit_{$lantaiKey}_{$index}"); $filename = $this->generateUniqueFileName($file, "lantai_unit_{$lantaiKey}_{$indexToReplace}");
$processedFiles[] = [ // Gantikan foto yang lama dengan yang baru
'path' => $this->uploadFile($file, $filename . '.' . $index), $processedFoto = [
'name' => "Foto Lantai {$lantaiKey} - " . ($index + 1), '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 = '') // Fungsi helper untuk generate nama file unik
{ private function generateUniqueFileName($file, $prefix = '')
$extension = $file->getClientOriginalExtension(); {
return $prefix . '_' . uniqid() . '.' . $extension; $extension = $file->getClientOriginalExtension();
} return $prefix . '_' . uniqid() . '.' . $extension;
}
private function categoryHasNewFiles(Request $request, array $fields): bool private function categoryHasNewFiles(Request $request, array $fields): bool
{ {
@@ -476,11 +480,11 @@ private function generateUniqueFileName($file, $prefix = '')
'name_basement.*' => 'nullable|string|max:255', 'name_basement.*' => 'nullable|string|max:255',
'foto_gerbang' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize, 'foto_gerbang' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp,bmp,tiff,heic,heif|max:'. $maxSize,
'name_gerbang' => 'nullable|string|max:255', 'name_gerbang' => 'nullable|string|max:255',
'name_lantai_unit' => 'array', 'name_lantai_unit' => 'array',
'foto_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']); $query->whereRaw('LOWER(status) = ?', ['assign']);
if(!Auth::user()->hasRole('administrator')) { if (!Auth::user()->hasRole('administrator')) {
$query->whereHas('penilaian.userPenilai', function ($q) { $query->whereHas('penilaian.userPenilai', function ($q) {
$q->where('user_id', Auth::user()->id); $q->where('user_id', Auth::user()->id);
$q->where('role', 'surveyor'); $q->where('role', 'surveyor');
@@ -1582,7 +1586,7 @@ private function generateUniqueFileName($file, $prefix = '')
if ($type == 'Spek Bangunan') { if ($type == 'Spek Bangunan') {
$data = $query->with(['bangunanKategori'])->get(); $data = $query->with(['bangunanKategori'])->get();
}else{ } else {
$data = $query->get(); $data = $query->get();
} }

View File

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

View File

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