feat: certificate template management and generation (Fasa 7)
- CertificateService: Intervention Image v3 text overlay on template - GenerateCertificateJob: queued generation with retry logic - SendCertificateEmailJob: stub (implemented in Fasa 8) - CertificateTemplateController: upload, config editor, preview, test generate - Admin/CertificateController: list, generate-all, email-all - Public/CertificateController: show with questionnaire gate, download - DejaVuSans fonts bundled under resources/fonts - Views: admin template/certificate management, public certificate download Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,104 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\GenerateCertificateJob;
|
||||
use App\Models\Certificate;
|
||||
use App\Models\Program;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CertificateController extends Controller
|
||||
{
|
||||
//
|
||||
public function index(Program $program): View
|
||||
{
|
||||
$certificates = $program->certificates()
|
||||
->with('participant')
|
||||
->latest()
|
||||
->paginate(50);
|
||||
|
||||
$stats = [
|
||||
'total' => $program->certificates()->count(),
|
||||
'generated' => $program->certificates()->whereIn('status', ['generated', 'emailed', 'downloaded'])->count(),
|
||||
'pending' => $program->certificates()->where('status', 'pending')->count(),
|
||||
'failed' => $program->certificates()->where('status', 'failed')->count(),
|
||||
'emailed' => $program->certificates()->where('status', 'emailed')->count(),
|
||||
];
|
||||
|
||||
return view('admin.programs.certificates.index', compact('program', 'certificates', 'stats'));
|
||||
}
|
||||
|
||||
public function generateAll(Request $request, Program $program): RedirectResponse
|
||||
{
|
||||
$template = $program->certificateTemplate;
|
||||
if (! $template) {
|
||||
return back()->with('error', 'Template sijil belum ditetapkan untuk program ini.');
|
||||
}
|
||||
|
||||
// Find all attended participants without certificates
|
||||
$existingIds = $program->certificates()->pluck('participant_id')->toArray();
|
||||
|
||||
$attended = $program->attendances()
|
||||
->whereNotIn('participant_id', $existingIds)
|
||||
->get();
|
||||
|
||||
if ($attended->isEmpty() && $program->certificates()->count() === 0) {
|
||||
return back()->with('error', 'Tiada peserta yang hadir untuk dijana sijil.');
|
||||
}
|
||||
|
||||
$sequence = $program->certificates()->count();
|
||||
$created = 0;
|
||||
|
||||
foreach ($attended as $attendance) {
|
||||
$sequence++;
|
||||
$cert = Certificate::firstOrCreate(
|
||||
['program_id' => $program->id, 'participant_id' => $attendance->participant_id],
|
||||
[
|
||||
'certificate_template_id' => $template->id,
|
||||
'certificate_no' => $this->buildCertNo($program, $sequence),
|
||||
'status' => 'pending',
|
||||
]
|
||||
);
|
||||
|
||||
if ($cert->wasRecentlyCreated || $cert->status === 'failed') {
|
||||
$cert->update(['certificate_template_id' => $template->id, 'status' => 'pending']);
|
||||
GenerateCertificateJob::dispatch($cert);
|
||||
$created++;
|
||||
}
|
||||
}
|
||||
|
||||
// Re-queue failed certificates
|
||||
$failed = $program->certificates()->where('status', 'failed')->get();
|
||||
foreach ($failed as $cert) {
|
||||
$cert->update(['status' => 'pending', 'error_message' => null]);
|
||||
GenerateCertificateJob::dispatch($cert);
|
||||
$created++;
|
||||
}
|
||||
|
||||
return back()->with('success', "Penjanaan sijil telah diantri untuk {$created} peserta.");
|
||||
}
|
||||
|
||||
public function emailAll(Program $program): RedirectResponse
|
||||
{
|
||||
$toEmail = $program->certificates()
|
||||
->whereIn('status', ['generated'])
|
||||
->whereNull('emailed_at')
|
||||
->count();
|
||||
|
||||
if ($toEmail === 0) {
|
||||
return back()->with('error', 'Tiada sijil yang sedia untuk dihantar emel.');
|
||||
}
|
||||
|
||||
// Dispatch email blast job — implemented in Fasa 8
|
||||
\App\Jobs\SendCertificateEmailJob::dispatchBatch($program);
|
||||
|
||||
return back()->with('success', "Penghantaran emel sijil dijadualkan untuk {$toEmail} peserta.");
|
||||
}
|
||||
|
||||
private function buildCertNo(Program $program, int $seq): string
|
||||
{
|
||||
$year = now()->format('Y');
|
||||
$prefix = strtoupper(substr(preg_replace('/[^A-Za-z]/', '', $program->title), 0, 4));
|
||||
return sprintf('%s/%s/%04d', $prefix ?: 'ECT', $year, $seq);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,134 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\CertificateTemplate;
|
||||
use App\Models\Program;
|
||||
use App\Services\CertificateService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CertificateTemplateController extends Controller
|
||||
{
|
||||
//
|
||||
public function show(Program $program): View
|
||||
{
|
||||
$template = $program->certificateTemplate;
|
||||
return view('admin.programs.template.show', compact('program', 'template'));
|
||||
}
|
||||
|
||||
public function store(Request $request, Program $program): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'template_image' => 'required|image|mimes:jpg,jpeg,png|max:10240',
|
||||
]);
|
||||
|
||||
$file = $request->file('template_image');
|
||||
$filename = $file->getClientOriginalName();
|
||||
$path = $file->store('templates/' . $program->id, 'local');
|
||||
|
||||
// Deactivate previous templates
|
||||
$program->certificateTemplates()->update(['is_active' => false]);
|
||||
|
||||
$program->certificateTemplates()->create([
|
||||
'original_filename' => $filename,
|
||||
'image_path' => $path,
|
||||
'is_active' => true,
|
||||
'uploaded_by' => auth()->id(),
|
||||
'config_json' => $this->defaultConfig($file->getPath() . '/' . $file->getFilename()),
|
||||
]);
|
||||
|
||||
return redirect()->route('admin.programs.template.show', $program)
|
||||
->with('success', 'Template sijil berjaya dimuat naik. Konfigurasi kedudukan teks di bawah.');
|
||||
}
|
||||
|
||||
public function updateConfig(Request $request, Program $program): RedirectResponse
|
||||
{
|
||||
$template = $program->certificateTemplate;
|
||||
abort_if(! $template, 404);
|
||||
|
||||
$request->validate([
|
||||
'fields' => 'required|array',
|
||||
'fields.*.x' => 'required|integer|min:0',
|
||||
'fields.*.y' => 'required|integer|min:0',
|
||||
'fields.*.font_size' => 'required|integer|min:8|max:200',
|
||||
'fields.*.font_color' => 'required|string|max:20',
|
||||
'fields.*.align' => 'required|in:left,center,right',
|
||||
]);
|
||||
|
||||
$config = $template->config_json ?? [];
|
||||
$config['fields'] = array_merge($config['fields'] ?? [], $request->fields);
|
||||
|
||||
$template->update(['config_json' => $config]);
|
||||
|
||||
return redirect()->route('admin.programs.template.show', $program)
|
||||
->with('success', 'Konfigurasi template berjaya dikemaskini.');
|
||||
}
|
||||
|
||||
public function destroy(Program $program): RedirectResponse
|
||||
{
|
||||
$template = $program->certificateTemplate;
|
||||
abort_if(! $template, 404);
|
||||
|
||||
Storage::disk('local')->delete($template->image_path);
|
||||
$template->delete();
|
||||
|
||||
return redirect()->route('admin.programs.template.show', $program)
|
||||
->with('success', 'Template sijil dipadam.');
|
||||
}
|
||||
|
||||
public function preview(Program $program): Response
|
||||
{
|
||||
$template = $program->certificateTemplate;
|
||||
abort_if(! $template, 404);
|
||||
|
||||
$content = Storage::disk('local')->get($template->image_path);
|
||||
|
||||
return response($content, 200, [
|
||||
'Content-Type' => 'image/jpeg',
|
||||
'Content-Disposition' => 'inline',
|
||||
'Cache-Control' => 'private, max-age=3600',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testGenerate(Request $request, Program $program, CertificateService $service): Response
|
||||
{
|
||||
$template = $program->certificateTemplate;
|
||||
abort_if(! $template, 404);
|
||||
|
||||
$sampleName = $request->input('sample_name', 'NAMA PESERTA CONTOH');
|
||||
$sampleNo = $request->input('sample_no', 'ECT/2025/0001');
|
||||
|
||||
$imageData = $service->generatePreview($template, $sampleName, $sampleNo);
|
||||
|
||||
return response($imageData, 200, [
|
||||
'Content-Type' => 'image/jpeg',
|
||||
'Content-Disposition' => 'inline; filename="preview.jpg"',
|
||||
]);
|
||||
}
|
||||
|
||||
private function defaultConfig(string $imagePath): array
|
||||
{
|
||||
[$width, $height] = getimagesize($imagePath) + [0, 0, 0, 0];
|
||||
|
||||
$cx = (int) round(($width ?: 1600) / 2);
|
||||
$cy = (int) round(($height ?: 1100) * 0.52);
|
||||
|
||||
return [
|
||||
'width' => $width ?: 1600,
|
||||
'height' => $height ?: 1100,
|
||||
'fields' => [
|
||||
'name' => [
|
||||
'x' => $cx,
|
||||
'y' => $cy,
|
||||
'font_size' => 52,
|
||||
'font_color' => '#1a3a6b',
|
||||
'font_file' => 'DejaVuSans-Bold.ttf',
|
||||
'align' => 'center',
|
||||
'valign' => 'top',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user