- Replace is_admin boolean with role enum('super_admin','admin') via migration
- ProgramPolicy: admin program can only view/edit/delete own programs
- EnsureIsAdmin: accepts both roles; EnsureSuperAdmin: super_admin only
- UserController + views: super_admin can manage admin accounts
- Sidebar: user management link & role badge gated on isSuperAdmin()
- Fix Controller base class: add AuthorizesRequests trait
- Fix tests: replace nonAdmin() (invalid enum) with adminProgram() against super_admin-only route
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
155 lines
5.0 KiB
PHP
155 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\StoreProgramRequest;
|
|
use App\Http\Requests\Admin\UpdateProgramRequest;
|
|
use App\Models\Program;
|
|
use App\Services\AuditLogService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ProgramController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$query = Program::with('creator')
|
|
->withCount(['attendances', 'programParticipants'])
|
|
->latest();
|
|
|
|
// Admin program hanya nampak program sendiri
|
|
if (auth()->user()->isAdminProgram()) {
|
|
$query->where('created_by', auth()->id());
|
|
}
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where(function ($q) use ($request) {
|
|
$q->where('title', 'like', '%' . $request->search . '%')
|
|
->orWhere('organizer', 'like', '%' . $request->search . '%')
|
|
->orWhere('location', 'like', '%' . $request->search . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->status);
|
|
}
|
|
|
|
$programs = $query->paginate(15)->withQueryString();
|
|
|
|
return view('admin.programs.index', compact('programs'));
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('admin.programs.create');
|
|
}
|
|
|
|
public function store(StoreProgramRequest $request): RedirectResponse
|
|
{
|
|
$program = Program::create([
|
|
...$request->validated(),
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
|
|
AuditLogService::log('program.created', $program);
|
|
|
|
return redirect()
|
|
->route('admin.programs.show', $program)
|
|
->with('success', 'Program "' . $program->title . '" berjaya ditambah.');
|
|
}
|
|
|
|
public function show(Program $program): View
|
|
{
|
|
$this->authorize('view', $program);
|
|
|
|
$program->load([
|
|
'qrCode',
|
|
'certificateTemplate',
|
|
'questionnaire.questionnaireSet.questions',
|
|
]);
|
|
|
|
$stats = [
|
|
'total_participants' => $program->programParticipants()->count(),
|
|
'pre_registered' => $program->programParticipants()->where('is_pre_registered', true)->count(),
|
|
'walk_in' => $program->programParticipants()->where('registration_source', 'walk_in')->count(),
|
|
'total_attendances' => $program->attendances()->count(),
|
|
'total_certificates' => $program->certificates()->count(),
|
|
'generated_certificates'=> $program->certificates()->whereIn('status', ['generated', 'emailed', 'downloaded'])->count(),
|
|
];
|
|
|
|
return view('admin.programs.show', compact('program', 'stats'));
|
|
}
|
|
|
|
public function edit(Program $program): View
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
return view('admin.programs.edit', compact('program'));
|
|
}
|
|
|
|
public function update(UpdateProgramRequest $request, Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
$old = $program->only([
|
|
'title', 'status', 'checkin_start_at', 'checkin_end_at',
|
|
'ecert_download_start_at', 'ecert_download_end_at',
|
|
]);
|
|
|
|
$program->update($request->validated());
|
|
|
|
AuditLogService::log('program.updated', $program, $old);
|
|
|
|
return redirect()
|
|
->route('admin.programs.show', $program)
|
|
->with('success', 'Maklumat program berjaya dikemas kini.');
|
|
}
|
|
|
|
public function destroy(Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $program);
|
|
|
|
if ($program->attendances()->exists()) {
|
|
return back()->with('error', 'Program tidak boleh dipadam kerana sudah ada rekod kehadiran.');
|
|
}
|
|
|
|
$title = $program->title;
|
|
AuditLogService::log('program.deleted', $program);
|
|
$program->delete();
|
|
|
|
return redirect()
|
|
->route('admin.programs.index')
|
|
->with('success', 'Program "' . $title . '" berjaya dipadam.');
|
|
}
|
|
|
|
public function publish(Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
if ($program->status !== 'draft') {
|
|
return back()->with('error', 'Hanya program berstatus Draf boleh diterbitkan.');
|
|
}
|
|
|
|
$program->update(['status' => 'published']);
|
|
AuditLogService::log('program.published', $program);
|
|
|
|
return back()->with('success', 'Program berjaya diterbitkan.');
|
|
}
|
|
|
|
public function close(Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
if ($program->status !== 'published') {
|
|
return back()->with('error', 'Hanya program berstatus Diterbitkan boleh ditutup.');
|
|
}
|
|
|
|
$program->update(['status' => 'closed']);
|
|
AuditLogService::log('program.closed', $program);
|
|
|
|
return back()->with('success', 'Program berjaya ditutup.');
|
|
}
|
|
}
|