|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\HR\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\HR\Models\HrAnnouncement; |
| 7 | +use Illuminate\Http\RedirectResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Inertia\Response; |
| 11 | + |
| 12 | +class HrAnnouncementController extends Controller |
| 13 | +{ |
| 14 | + public function index(Request $request): Response |
| 15 | + { |
| 16 | + $this->authorize('viewAny', HrAnnouncement::class); |
| 17 | + |
| 18 | + $announcements = HrAnnouncement::with('createdBy') |
| 19 | + ->when($request->target_audience, fn ($q) => $q->where('target_audience', $request->target_audience)) |
| 20 | + ->latest() |
| 21 | + ->paginate(20) |
| 22 | + ->withQueryString(); |
| 23 | + |
| 24 | + return Inertia::render('HR/Announcements/Index', [ |
| 25 | + 'announcements' => $announcements, |
| 26 | + 'filters' => $request->only(['target_audience']), |
| 27 | + ]); |
| 28 | + } |
| 29 | + |
| 30 | + public function store(Request $request): RedirectResponse |
| 31 | + { |
| 32 | + $this->authorize('create', HrAnnouncement::class); |
| 33 | + |
| 34 | + $validated = $request->validate([ |
| 35 | + 'title' => 'required|string|max:255', |
| 36 | + 'body' => 'required|string', |
| 37 | + 'target_audience' => 'nullable|string|in:all,department,role', |
| 38 | + 'department_id' => 'nullable|exists:departments,id', |
| 39 | + 'priority' => 'nullable|string|in:low,normal,high,urgent', |
| 40 | + 'publish_at' => 'nullable|date', |
| 41 | + 'expire_at' => 'nullable|date', |
| 42 | + ]); |
| 43 | + |
| 44 | + $announcement = HrAnnouncement::create([ |
| 45 | + 'tenant_id' => auth()->user()->tenant_id, |
| 46 | + 'created_by' => auth()->id(), |
| 47 | + 'title' => $validated['title'], |
| 48 | + 'body' => $validated['body'], |
| 49 | + 'target_audience' => $validated['target_audience'] ?? 'all', |
| 50 | + 'department_id' => $validated['department_id'] ?? null, |
| 51 | + 'priority' => $validated['priority'] ?? 'normal', |
| 52 | + 'publish_at' => $validated['publish_at'] ?? null, |
| 53 | + 'expire_at' => $validated['expire_at'] ?? null, |
| 54 | + ]); |
| 55 | + |
| 56 | + return redirect()->route('hr.announcements.show', $announcement); |
| 57 | + } |
| 58 | + |
| 59 | + public function show(HrAnnouncement $announcement): Response |
| 60 | + { |
| 61 | + $this->authorize('view', $announcement); |
| 62 | + |
| 63 | + $announcement->load(['createdBy', 'department']); |
| 64 | + |
| 65 | + return Inertia::render('HR/Announcements/Show', [ |
| 66 | + 'announcement' => $announcement, |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + public function publish(HrAnnouncement $announcement): RedirectResponse |
| 71 | + { |
| 72 | + $this->authorize('update', $announcement); |
| 73 | + |
| 74 | + $announcement->publish(); |
| 75 | + |
| 76 | + return redirect()->back()->with('success', 'Announcement published successfully.'); |
| 77 | + } |
| 78 | + |
| 79 | + public function archive(HrAnnouncement $announcement): RedirectResponse |
| 80 | + { |
| 81 | + $this->authorize('update', $announcement); |
| 82 | + |
| 83 | + $announcement->archive(); |
| 84 | + |
| 85 | + return redirect()->back()->with('success', 'Announcement archived successfully.'); |
| 86 | + } |
| 87 | + |
| 88 | + public function destroy(HrAnnouncement $announcement): RedirectResponse |
| 89 | + { |
| 90 | + $this->authorize('delete', $announcement); |
| 91 | + |
| 92 | + $announcement->delete(); |
| 93 | + |
| 94 | + return redirect()->route('hr.announcements.index'); |
| 95 | + } |
| 96 | +} |
0 commit comments