93 lines
2.6 KiB
PHP
93 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Announcement;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PengumumanController extends Controller
|
|
{
|
|
/**
|
|
* Menampilkan daftar semua pengumuman.
|
|
*/
|
|
public function index()
|
|
{
|
|
$semuaPengumuman = Announcement::latest()->get();
|
|
return view('admin.pengumuman.index', [
|
|
'pageTitle' => 'Manajemen Pengumuman',
|
|
'semuaPengumuman' => $semuaPengumuman,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Menampilkan form untuk membuat pengumuman baru.
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('admin.pengumuman.create', [
|
|
'pageTitle' => 'Buat Pengumuman Baru',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Menampilkan form untuk mengedit pengumuman yang ada.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$pengumuman = Announcement::findOrFail($id);
|
|
|
|
return view('admin.pengumuman.edit', [
|
|
'pageTitle' => 'Edit Pengumuman: ' . $pengumuman->title,
|
|
'pengumuman' => $pengumuman,
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'content' => 'required|string',
|
|
'type' => 'required|in:info,warning,success,danger',
|
|
'icon' => 'nullable|string|max:50',
|
|
]);
|
|
|
|
// Provide default icon if not provided
|
|
if (!isset($validated['icon']) || empty($validated['icon'])) {
|
|
$validated['icon'] = 'bi-megaphone';
|
|
}
|
|
|
|
Announcement::create($validated);
|
|
|
|
return redirect()->route('admin.pengumuman.index')->with('success', 'Pengumuman berhasil ditambahkan.');
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$pengumuman = Announcement::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'content' => 'required|string',
|
|
'type' => 'required|in:info,warning,success,danger',
|
|
'icon' => 'nullable|string|max:50',
|
|
]);
|
|
|
|
// Provide default icon if not provided
|
|
if (!isset($validated['icon']) || empty($validated['icon'])) {
|
|
$validated['icon'] = 'bi-megaphone';
|
|
}
|
|
|
|
$pengumuman->update($validated);
|
|
|
|
return redirect()->route('admin.pengumuman.index')->with('success', 'Pengumuman berhasil diperbarui.');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$pengumuman = Announcement::findOrFail($id);
|
|
$pengumuman->delete();
|
|
|
|
return redirect()->route('admin.pengumuman.index')->with('success', 'Pengumuman berhasil dihapus.');
|
|
}
|
|
} |