diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index c7facf0..c37cd6e 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -43,6 +43,36 @@ class AjaxController extends Controller 'message' => 'Upload failed.', ]); } + public function uploadTempImage(Request $request) + { + $request->validate([ + 'file.*' => 'required|file|mimes:png,jpg,jpeg|max:5120', + ]); + + $uploadedFiles = []; + + if ($request->hasFile('file')) { + foreach ($request->file('file') as $file) { + $filename = uniqid() . '-' . time() . '.' . $file->getClientOriginalExtension(); + $path = $file->storeAs('tmp_uploads', $filename); + + $uploadedFiles[] = [ + 'filename' => $filename, + 'url' => asset('storage/tmp_uploads/' . $filename), + ]; + } + + return response()->json([ + 'success' => true, + 'files' => $uploadedFiles, // Kembalikan semua file + ]); + } + + return response()->json([ + 'success' => false, + 'message' => 'Upload failed.', + ]); + } public function sekolahNpsn(Request $request) { // dd($request->all()); diff --git a/app/Http/Controllers/FrontController.php b/app/Http/Controllers/FrontController.php index 6972483..f9b7e07 100644 --- a/app/Http/Controllers/FrontController.php +++ b/app/Http/Controllers/FrontController.php @@ -23,6 +23,7 @@ class FrontController extends Controller return view('kegiatan_detail',$data); } function galeri() { - return view('galeri'); + $data['kegiatan'] = Kegiatan::orderBy('KegiatanId','DESC')->paginate(8); + return view('galeri',$data); } } diff --git a/app/Http/Controllers/Modules/Konten/KegiatanController.php b/app/Http/Controllers/Modules/Konten/KegiatanController.php index 3499b6f..c5033bd 100644 --- a/app/Http/Controllers/Modules/Konten/KegiatanController.php +++ b/app/Http/Controllers/Modules/Konten/KegiatanController.php @@ -45,13 +45,14 @@ class KegiatanController extends Controller $action .= ''; $action .= ''; } + $image = json_decode($row->image); $_data[] = [ 'no' => $key+1, 'id' => encode_id($row->KegiatanId), 'judul' => $row->judul, 'created_at' => dateTime($row->created_at), - 'image' => '', + 'image' => '', 'action' => $action, ]; @@ -93,30 +94,46 @@ class KegiatanController extends Controller $keg->kategori = $request->kategori; $keg->body = $request->body; - if (@$request->hasFile('image')) { - $file = $request->file('image'); - $destinationPath = public_path('uploads/kegiatan'); - $current = Carbon::now()->format('Y/m/d'); - $path = $destinationPath . '/' . $current; - $fileName = $file->getClientOriginalName(); - $fileMime = $file->getClientMimeType(); - $fileExtension = $file->getClientOriginalExtension(); - $fileSize = $file->getSize(); - if(($fileExtension != 'jpg') && ($fileExtension != 'jpeg') && ($fileExtension != 'png')){ - return redirect()->back()->with([ - 'message' => 'Maaf File Harus Berupa PNG,JPEG,JPG!', - 'type' => "error" - ]); - } - $newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension; + if(@$request->input_upload_files){ - if (!File::exists($path)) { - File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); - } + $filePaths = []; + if(is_array(@$request->input_upload_files)){ - $filePathTim = 'kegiatan/' . $current . '/' . $newFilename; - $uploaded = $file->move($path, $newFilename); - $keg->image = $filePathTim; + $oldImages = json_decode($keg->image, true) ?? []; + foreach (@$request->input_upload_files as $file) { + $tempPath = storage_path('app/tmp_uploads/' . $file); + + if (file_exists($tempPath)) { + $path = public_path('uploads/kegiatan/' . date('Y') ); + if (!File::exists($path)) { + File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); + } + + $newPath = 'uploads/kegiatan/'.date('Y').'/'.$file; + copy($tempPath, public_path($newPath)); + $newfilePaths[] = $newPath; + } + } + + // gabungkan file lama + file baru + $filePaths = array_merge($oldImages, $newfilePaths); + }else{ + $tempPath = storage_path('app/tmp_uploads/' . @$request->input_upload_files); + + if (file_exists($tempPath)) { + $path = public_path('uploads/kegiatan/' . date('Y') ); + if (!File::exists($path)) { + File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); + } + + $newPath = 'uploads/kegiatan/'.date('Y').'/'.@$request->input_upload_files; + copy($tempPath, public_path($newPath)); + $filePaths[] = $newPath; + } + } + + $path = $filePaths; + $keg->image = json_encode($path); } $keg->save(); }else{ @@ -126,31 +143,69 @@ class KegiatanController extends Controller $keg->kategori = $request->kategori; $keg->body = $request->body; - if (@$request->hasFile('image')) { - $file = $request->file('image'); - $destinationPath = public_path('uploads/kegiatan'); - $current = Carbon::now()->format('Y/m/d'); - $path = $destinationPath . '/' . $current; - $fileName = $file->getClientOriginalName(); - $fileMime = $file->getClientMimeType(); - $fileExtension = $file->getClientOriginalExtension(); - $fileSize = $file->getSize(); - if(($fileExtension != 'jpg') && ($fileExtension != 'jpeg') && ($fileExtension != 'png')){ - return redirect()->back()->with([ - 'message' => 'Maaf File Harus Berupa PNG,JPEG,JPG!', - 'type' => "error" - ]); - } - $newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension; + // if (@$request->hasFile('image')) { + // $file = $request->file('image'); + // $destinationPath = public_path('uploads/kegiatan'); + // $current = Carbon::now()->format('Y/m/d'); + // $path = $destinationPath . '/' . $current; + // $fileName = $file->getClientOriginalName(); + // $fileMime = $file->getClientMimeType(); + // $fileExtension = $file->getClientOriginalExtension(); + // $fileSize = $file->getSize(); + // if(($fileExtension != 'jpg') && ($fileExtension != 'jpeg') && ($fileExtension != 'png')){ + // return redirect()->back()->with([ + // 'message' => 'Maaf File Harus Berupa PNG,JPEG,JPG!', + // 'type' => "error" + // ]); + // } + // $newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension; - if (!File::exists($path)) { - File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); - } + // if (!File::exists($path)) { + // File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); + // } - $filePathTim = 'kegiatan/' . $current . '/' . $newFilename; - $uploaded = $file->move($path, $newFilename); - $keg->image = $filePathTim; + // $filePathTim = 'kegiatan/' . $current . '/' . $newFilename; + // $uploaded = $file->move($path, $newFilename); + // $keg->image = $filePathTim; + // } + + if(@$request->input_upload_files){ + + $filePaths = []; + if(is_array(@$request->input_upload_files)){ + foreach (@$request->input_upload_files as $file) { + $tempPath = storage_path('app/tmp_uploads/' . $file); + + if (file_exists($tempPath)) { + $path = public_path('uploads/kegiatan/' . date('Y') ); + if (!File::exists($path)) { + File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); + } + + $newPath = 'uploads/kegiatan/'.date('Y').'/'.$file; + copy($tempPath, public_path($newPath)); + $filePaths[] = $newPath; + } + } + }else{ + $tempPath = storage_path('app/tmp_uploads/' . @$request->input_upload_files); + + if (file_exists($tempPath)) { + $path = public_path('uploads/kegiatan/' . date('Y') ); + if (!File::exists($path)) { + File::isDirectory($path) or File::makeDirectory($path, 0777, true, true); + } + + $newPath = 'uploads/kegiatan/'.date('Y').'/'.@$request->input_upload_files; + copy($tempPath, public_path($newPath)); + $filePaths[] = $newPath; + } + } + + $path = $filePaths; + $keg->image = json_encode($path); } + $keg->save(); } return redirect()->back()->with([ @@ -224,4 +279,34 @@ class KegiatanController extends Controller return response()->json(['success' => true,'message' => 'Berhasil update data','type' => 'success']); } + function imageDelete(Request $request,$id){ + $keyId = decode_id($id); + $keg = Kegiatan::where('KegiatanId',$keyId)->first(); + + // Ambil nama file yang mau dihapus + $fileToDelete = $request->url; // contoh: 'uploads/kegiatan/2025/file2.pdf' + + // Ambil data lama + $images = json_decode($keg->image, true) ?? []; + + // Cari dan hapus dari array + $newImages = array_filter($images, function ($img) use ($fileToDelete) { + return $img !== $fileToDelete; + }); + + // Hapus file fisik juga + $fullPath = public_path($fileToDelete); + if (File::exists($fullPath)) { + File::delete($fullPath); + } + + // Simpan array baru ke DB + $keg->image = json_encode(array_values($newImages)); // reset index array + $keg->save(); + + return response()->json([ + 'status' => 'success', + 'message' => 'Image deleted successfully', + ]); + } } diff --git a/database/migrations/2025_08_08_085907_create_kegiatans_table.php b/database/migrations/2025_08_08_085907_create_kegiatans_table.php index 7dc47b7..5d67497 100644 --- a/database/migrations/2025_08_08_085907_create_kegiatans_table.php +++ b/database/migrations/2025_08_08_085907_create_kegiatans_table.php @@ -17,7 +17,7 @@ return new class extends Migration $table->string('judul')->nullable(); $table->text('body')->nullable(); $table->string('kategori')->nullable(); - $table->string('image')->nullable(); + $table->json('image')->nullable(); $table->timestampsTz(); }); } diff --git a/resources/views/galeri.blade.php b/resources/views/galeri.blade.php index ef6efdb..035779d 100644 --- a/resources/views/galeri.blade.php +++ b/resources/views/galeri.blade.php @@ -6,98 +6,36 @@
+ @foreach($kegiatan as $dataKeg) + image); + ?>
- Galeri + Galeri
-

Dokumentasi SDN 15 Ciracas

+

{{ $dataKeg->judul }}

{{--

1 Agustus 2025

--}} {{-- --}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi SDN 05 Pondok Kelapa

- {{--

1 Agustus 2025

--}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi SMAN 67 Jakarta

- {{--

1 Agustus 2025

--}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi MAN 2 Jakarta

- {{--

1 Agustus 2025

--}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi SDN 04 Lebak Bulus

- {{--

1 Agustus 2025

--}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi SMAN 10 Jakarta

- {{--

1 Agustus 2025

--}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi SDN 07 Pulogadung

- {{--

1 Agustus 2025

--}} - - Lihat Detail - -
-
-
- Galeri -
-

Dokumentasi SMPN 197 Jakarta

- {{--

1 Agustus 2025

--}} - + Lihat Detail
+ @endforeach