orderBy('tanggal', 'desc') ->orderBy('created_at', 'desc') ->paginate(10); $title = 'Pengumuman'; return view('pengumuman.index', compact('pengumuman', 'title')); } public function create() { $title = 'Buat Pengumuman'; return view('pengumuman.create', compact('title')); } public function store(StorePengumumanRequest $request) { $data = $request->validated(); $data['dibuat_oleh'] = Auth::id(); $data['tanggal'] = now()->toDateString(); if ($request->hasFile('foto')) { $data['foto'] = $request->file('foto')->store('uploads/pengumuman/foto', 'public'); } if ($request->hasFile('lampiran')) { $data['lampiran'] = $request->file('lampiran')->store('uploads/pengumuman/lampiran', 'public'); } $pengumuman = Pengumuman::create($data); app(NotifikasiService::class)->kirimBroadcast( 'pengumuman_baru', '📢 Pengumuman Baru', $pengumuman->judul, ['id_pengumuman' => $pengumuman->id_pengumuman] ); broadcast(new PengumumanCreated( $pengumuman->id_pengumuman, $pengumuman->judul, \Illuminate\Support\Str::limit(strip_tags($pengumuman->isi ?? ''), 100) )); return redirect()->route('pengumuman.index') ->with('success', 'Pengumuman berhasil ditambahkan.'); } public function edit(Pengumuman $pengumuman) { $title = 'Edit Pengumuman'; return view('pengumuman.edit', compact('pengumuman', 'title')); } public function update(UpdatePengumumanRequest $request, Pengumuman $pengumuman) { $data = $request->validated(); if ($request->hasFile('foto')) { if ($pengumuman->foto) { Storage::disk('public')->delete($pengumuman->foto); } $data['foto'] = $request->file('foto')->store('uploads/pengumuman/foto', 'public'); } if ($request->hasFile('lampiran')) { if ($pengumuman->lampiran) { Storage::disk('public')->delete($pengumuman->lampiran); } $data['lampiran'] = $request->file('lampiran')->store('uploads/pengumuman/lampiran', 'public'); } $pengumuman->update($data); return redirect()->route('pengumuman.index') ->with('success', 'Pengumuman berhasil diperbarui.'); } public function destroy(Pengumuman $pengumuman) { try { if ($pengumuman->foto) { Storage::disk('public')->delete($pengumuman->foto); } if ($pengumuman->lampiran) { Storage::disk('public')->delete($pengumuman->lampiran); } $pengumuman->delete(); return redirect()->route('pengumuman.index') ->with('success', 'Pengumuman berhasil dihapus.'); } catch (\Exception $e) { return redirect()->back()->with('error', 'Gagal menghapus pengumuman: ' . $e->getMessage()); } } public function bulkDelete(\Illuminate\Http\Request $request) { try { $ids = $request->input('ids'); if (empty($ids) || !is_array($ids)) { return redirect()->back()->with('error', 'Tidak ada pengumuman yang dipilih.'); } $pengumumanList = Pengumuman::whereIn('id_pengumuman', $ids)->get(); foreach ($pengumumanList as $pengumuman) { if ($pengumuman->foto) { Storage::disk('public')->delete($pengumuman->foto); } if ($pengumuman->lampiran) { Storage::disk('public')->delete($pengumuman->lampiran); } $pengumuman->delete(); } return redirect()->route('pengumuman.index') ->with('success', 'Pengumuman yang dipilih berhasil dihapus.'); } catch (\Exception $e) { return redirect()->back()->with('error', 'Gagal menghapus pengumuman: ' . $e->getMessage()); } } }