- ProgramController: full CRUD, publish, close, delete (guarded if attendance exists) - StoreProgramRequest + UpdateProgramRequest with Malay attribute names - AuditLogService: logs admin actions, redacts sensitive fields (no_kp, token, password) - Program index: search, status filter, pagination (Bootstrap 5) - Program create/edit: shared _form partial with all fields (dates, sessions, walk-in toggle) - Program show: tab layout (participants, qr, template, questionnaire, statistics) - Bootstrap 5 pagination via Paginator::useBootstrapFive() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
138 lines
4.5 KiB
PHP
138 lines
4.5 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();
|
|
|
|
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
|
|
{
|
|
$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
|
|
{
|
|
return view('admin.programs.edit', compact('program'));
|
|
}
|
|
|
|
public function update(UpdateProgramRequest $request, Program $program): RedirectResponse
|
|
{
|
|
$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
|
|
{
|
|
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
|
|
{
|
|
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
|
|
{
|
|
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.');
|
|
}
|
|
}
|