1041 lines
30 KiB
PHP
1041 lines
30 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Badges;
|
|
|
|
use chillerlan\QRCode\Output\QRGdImagePNG;
|
|
use chillerlan\QRCode\QRCode;
|
|
use chillerlan\QRCode\QROptions;
|
|
use Dompdf\Dompdf;
|
|
use Dompdf\Options;
|
|
use Illuminate\Http\Response;
|
|
use Throwable;
|
|
|
|
class BadgePdfService
|
|
{
|
|
private const BADGE_WIDTH_IN = 2.36;
|
|
|
|
private const BADGE_HEIGHT_IN = 3.54;
|
|
|
|
private const CELL_WIDTH_IN = 2.55;
|
|
|
|
private const CELL_HEIGHT_IN = 3.85;
|
|
|
|
private const PAGE_MARGIN_IN = 0.18;
|
|
|
|
/** @var list<string> */
|
|
private array $tempQrFiles = [];
|
|
|
|
public function __construct(
|
|
protected BadgeStudentLookupService $studentLookupService,
|
|
protected BadgeUserLookupService $userLookupService,
|
|
protected BadgePrintLogService $printLogService,
|
|
protected BadgeTextFormatter $formatter,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Letter landscape PDF, 8 badges per page — students and/or staff (FPDF + QR PNG).
|
|
*
|
|
* @param int[] $studentIds Primary keys on `students.id`
|
|
* @param int[] $userIds Staff/admin/teacher keys on `users.id`
|
|
*/
|
|
public function generate(
|
|
array $studentIds,
|
|
array $userIds,
|
|
?string $schoolYear,
|
|
array $rolesMap = [],
|
|
array $classesMap = [],
|
|
?int $actorId = null
|
|
): Response {
|
|
$studentIds = $this->formatter->normalizeIds($studentIds);
|
|
$userIds = $this->formatter->normalizeIds($userIds);
|
|
|
|
$schoolName = (string) config('badges.school_name', 'Al Rahma Sunday School');
|
|
$motto = (string) config('badges.motto', '');
|
|
$studentRoleLabel = (string) config('badges.role_label', 'Student');
|
|
$footerBlock = $this->footerBlock($schoolName);
|
|
$logoPath = $this->logoFilePath();
|
|
|
|
$rows = [];
|
|
|
|
$students = $this->studentLookupService->fetchForBadges($studentIds, $schoolYear);
|
|
foreach ($students as $row) {
|
|
try {
|
|
$rows[] = array_merge($row, [
|
|
'_badge_kind' => 'student',
|
|
'role_subline' => $studentRoleLabel,
|
|
'show_grade' => true,
|
|
'grade_label' => 'Grade',
|
|
'_qr_base64' => base64_encode($this->renderQrPng((string) $row['school_id'])),
|
|
]);
|
|
} catch (Throwable) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
foreach ($userIds as $uid) {
|
|
$staffRow = $this->buildStaffBadgeRow((int) $uid, $schoolYear, $rolesMap, $classesMap);
|
|
if ($staffRow === null) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$qrPayload = (string) ($staffRow['user_id_for_qr'] ?? $staffRow['user_id'] ?? '');
|
|
if ($qrPayload === '' || !preg_match('/^[A-Za-z0-9_-]+$/', $qrPayload)) {
|
|
$qrPayload = (string) (int) ($staffRow['user_id'] ?? 0);
|
|
}
|
|
|
|
$rows[] = array_merge($staffRow, [
|
|
'_badge_kind' => 'staff',
|
|
'_qr_base64' => base64_encode($this->renderQrPng($qrPayload)),
|
|
]);
|
|
} catch (Throwable) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Use the fixed-coordinate FPDF renderer for badges so the output
|
|
// always stays at a strict 4x2 layout (8 badges per page).
|
|
$binary = $this->renderWithFpdf(
|
|
$rows,
|
|
$schoolName,
|
|
$motto,
|
|
$studentRoleLabel,
|
|
$footerBlock,
|
|
$logoPath
|
|
);
|
|
|
|
$logIds = array_values(array_unique(array_merge($studentIds, $userIds)));
|
|
$this->printLogService->logSafely(
|
|
userIds: $logIds,
|
|
actorId: $actorId,
|
|
schoolYear: $schoolYear,
|
|
rolesMap: $rolesMap,
|
|
classesMap: $classesMap,
|
|
copies: 1
|
|
);
|
|
|
|
return response((string) $binary, 200, [
|
|
'Content-Type' => 'application/pdf',
|
|
'Content-Disposition' => 'inline; filename="Badges.pdf"',
|
|
]);
|
|
}
|
|
|
|
protected function renderWithDompdf(
|
|
array $rows,
|
|
string $schoolName,
|
|
string $motto,
|
|
string $footerBlock
|
|
): string {
|
|
$html = $this->buildHtmlDocument($rows, $schoolName, $motto, $footerBlock);
|
|
|
|
$options = new Options();
|
|
$options->set('isHtml5ParserEnabled', true);
|
|
$options->set('isRemoteEnabled', true);
|
|
$options->set('defaultFont', 'DejaVu Sans');
|
|
|
|
$dompdf = new Dompdf($options);
|
|
$dompdf->loadHtml($html, 'UTF-8');
|
|
$dompdf->setPaper('A4', 'landscape');
|
|
$dompdf->render();
|
|
|
|
return $dompdf->output();
|
|
}
|
|
|
|
protected function renderWithFpdf(
|
|
array $rows,
|
|
string $schoolName,
|
|
string $motto,
|
|
string $studentRoleLabel,
|
|
string $footerBlock,
|
|
?string $logoPath
|
|
): string {
|
|
$this->tempQrFiles = [];
|
|
|
|
try {
|
|
if (!class_exists('FPDF', false)) {
|
|
require_once base_path('app/ThirdParty/fpdf/fpdf.php');
|
|
}
|
|
|
|
$pdf = new \FPDF('L', 'in', 'Letter');
|
|
$pdf->SetAutoPageBreak(false);
|
|
|
|
$pages = array_chunk($rows, 8);
|
|
|
|
if ($pages === []) {
|
|
$pdf->AddPage('L', 'Letter');
|
|
$pdf->SetFont('Helvetica', '', 12);
|
|
$pdf->SetTextColor(0, 0, 0);
|
|
$pdf->SetXY(self::PAGE_MARGIN_IN, self::PAGE_MARGIN_IN);
|
|
$pdf->MultiCell(
|
|
$pdf->GetPageWidth() - 2 * self::PAGE_MARGIN_IN,
|
|
0.3,
|
|
$this->formatter->toPdf(
|
|
'No valid badges: check user/student ids, roles, or school ids for QR.'
|
|
),
|
|
0,
|
|
'L'
|
|
);
|
|
} else {
|
|
foreach ($pages as $pageRows) {
|
|
$pdf->AddPage('L', 'Letter');
|
|
$slots = array_pad($pageRows, 8, null);
|
|
|
|
for ($gridRow = 0; $gridRow < 2; $gridRow++) {
|
|
for ($gridCol = 0; $gridCol < 4; $gridCol++) {
|
|
$idx = ($gridRow * 4) + $gridCol;
|
|
$row = $slots[$idx];
|
|
if (!is_array($row)) {
|
|
continue;
|
|
}
|
|
|
|
$qrBase64 = trim((string) ($row['_qr_base64'] ?? ''));
|
|
if ($qrBase64 === '') {
|
|
continue;
|
|
}
|
|
|
|
$tmpPath = $this->writeTempQrPng(base64_decode($qrBase64, true) ?: '');
|
|
if ($tmpPath === null) {
|
|
continue;
|
|
}
|
|
|
|
$row['_qr_tmp'] = $tmpPath;
|
|
|
|
$cellX = self::PAGE_MARGIN_IN + $gridCol * self::CELL_WIDTH_IN;
|
|
$cellY = self::PAGE_MARGIN_IN + $gridRow * self::CELL_HEIGHT_IN;
|
|
$badgeX = $cellX + (self::CELL_WIDTH_IN - self::BADGE_WIDTH_IN) / 2;
|
|
$badgeY = $cellY + (self::CELL_HEIGHT_IN - self::BADGE_HEIGHT_IN) / 2;
|
|
|
|
$this->drawBadge(
|
|
$pdf,
|
|
$row,
|
|
$badgeX,
|
|
$badgeY,
|
|
self::BADGE_WIDTH_IN,
|
|
self::BADGE_HEIGHT_IN,
|
|
$schoolName,
|
|
$motto,
|
|
$studentRoleLabel,
|
|
$footerBlock,
|
|
$logoPath
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $pdf->Output('S');
|
|
} finally {
|
|
foreach ($this->tempQrFiles as $path) {
|
|
if (is_string($path) && $path !== '' && is_file($path)) {
|
|
@unlink($path);
|
|
}
|
|
}
|
|
$this->tempQrFiles = [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $rolesMap
|
|
* @param array<string, mixed> $classesMap
|
|
* @return ?array<string, mixed>
|
|
*/
|
|
protected function buildStaffBadgeRow(
|
|
int $userId,
|
|
?string $schoolYear,
|
|
array $rolesMap,
|
|
array $classesMap
|
|
): ?array {
|
|
$info = $this->userLookupService->getUserInfoById($userId, $schoolYear);
|
|
if (!$info || !is_array($info)) {
|
|
return null;
|
|
}
|
|
|
|
$resolvedId = (int) ($info['user_id'] ?? $userId);
|
|
|
|
$postedRole = $rolesMap[$resolvedId] ?? null;
|
|
$roleResolved = $postedRole !== null
|
|
? (string) $postedRole
|
|
: $this->formatter->resolveRole($info);
|
|
|
|
$roleResolved = $this->formatter->formatRole($roleResolved);
|
|
$info['role_resolved'] = $roleResolved;
|
|
|
|
if (!empty($classesMap[$resolvedId])) {
|
|
$info['class_section_name'] = (string) $classesMap[$resolvedId];
|
|
}
|
|
|
|
$class = $this->formatter->norm($info['class_section_name'] ?? '');
|
|
$class = $this->formatter->formatClass($class);
|
|
|
|
$detectSrc = strtolower($this->formatter->norm(
|
|
($info['roles'] ?? '') !== '' ? (string) $info['roles'] : $roleResolved
|
|
));
|
|
$isTeacherish = str_contains($detectSrc, 'teacher') || preg_match('/\bta\b/', $detectSrc);
|
|
$isAdmin = str_contains($detectSrc, 'admin');
|
|
|
|
if ($isTeacherish && $class !== '') {
|
|
if (strtolower($class) === 'youth') {
|
|
$display = 'Youth ' . $roleResolved;
|
|
} elseif ($class === 'KG') {
|
|
$display = 'KG ' . $roleResolved;
|
|
} else {
|
|
$display = 'Grade ' . $class . ' ' . $roleResolved;
|
|
}
|
|
} elseif ($roleResolved !== '') {
|
|
$display = $roleResolved;
|
|
} elseif ($class !== '') {
|
|
$display = $class;
|
|
} else {
|
|
$display = 'STAFF';
|
|
}
|
|
|
|
$gradeCol = $class !== '' ? $class : '—';
|
|
$year = $this->formatter->norm((string) ($info['school_year'] ?? ($schoolYear ?? '')));
|
|
$schoolId = trim((string) ($info['school_id'] ?? ''));
|
|
|
|
if ($schoolId === '' || !preg_match('/^[A-Za-z0-9_-]+$/', $schoolId)) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'user_id' => $resolvedId,
|
|
'user_id_for_qr' => $schoolId,
|
|
'fullname' => $info['name'] ?? 'STAFF',
|
|
'grade' => $gradeCol,
|
|
'academic_year' => $year !== '' ? $year : '—',
|
|
'school_id' => $schoolId,
|
|
'role_subline' => $display,
|
|
'show_grade' => $isTeacherish && !$isAdmin && $gradeCol !== '—',
|
|
'grade_label' => 'Class / Grade',
|
|
];
|
|
}
|
|
|
|
protected function buildHtmlDocument(
|
|
array $rows,
|
|
string $schoolName,
|
|
string $motto,
|
|
string $footerBlock
|
|
): string {
|
|
$logoDataUri = $this->logoDataUri();
|
|
$badgesHtml = '';
|
|
$pages = array_chunk($rows, 8);
|
|
|
|
if ($pages === []) {
|
|
$pages = [[]];
|
|
}
|
|
|
|
foreach ($pages as $pageRows) {
|
|
$badgesHtml .= '<div class="page"><table class="grid">';
|
|
|
|
for ($rowIndex = 0; $rowIndex < 2; $rowIndex++) {
|
|
$badgesHtml .= '<tr>';
|
|
|
|
for ($colIndex = 0; $colIndex < 4; $colIndex++) {
|
|
$slot = ($rowIndex * 4) + $colIndex;
|
|
$badgesHtml .= '<td>';
|
|
|
|
if (isset($pageRows[$slot]) && is_array($pageRows[$slot])) {
|
|
$badgesHtml .= $this->buildBadgeHtml($pageRows[$slot], $schoolName, $motto, $footerBlock, $logoDataUri);
|
|
}
|
|
|
|
$badgesHtml .= '</td>';
|
|
}
|
|
|
|
$badgesHtml .= '</tr>';
|
|
}
|
|
|
|
$badgesHtml .= '</table></div>';
|
|
}
|
|
|
|
return '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Badges PDF</title><style>'
|
|
. $this->badgeCss()
|
|
. '</style></head><body>'
|
|
. $badgesHtml
|
|
. '</body></html>';
|
|
}
|
|
|
|
protected function buildBadgeHtml(
|
|
array $row,
|
|
string $schoolName,
|
|
string $motto,
|
|
string $footerBlock,
|
|
?string $logoDataUri
|
|
): string
|
|
{
|
|
$kind = (string) ($row['_badge_kind'] ?? 'student');
|
|
$idLabel = $kind === 'staff' ? 'User ID' : 'School ID';
|
|
$title = $this->escapeHtml((string) ($row['fullname'] ?? ''));
|
|
$role = $this->escapeHtml((string) ($row['role_subline'] ?? 'Student'));
|
|
$gradeLabel = $this->escapeHtml((string) ($row['grade_label'] ?? ($kind === 'staff' ? 'Class / Assignment' : 'Grade')));
|
|
$grade = $this->escapeHtml((string) ($row['grade'] ?? '—'));
|
|
$year = $this->escapeHtml((string) ($row['academic_year'] ?? '—'));
|
|
$schoolId = $this->escapeHtml((string) ($row['school_id'] ?? '—'));
|
|
$qrBase64 = trim((string) ($row['_qr_base64'] ?? ''));
|
|
$footer = nl2br($this->escapeHtml($footerBlock));
|
|
$barcodeDigits = preg_replace('/[^A-Za-z0-9]/', '', (string) ($row['school_id'] ?? ''));
|
|
$barcodeDigits = $barcodeDigits !== '' ? str_repeat($barcodeDigits, 4) : '12345678901234567890';
|
|
$showGrade = (bool) ($row['show_grade'] ?? ($kind === 'student'));
|
|
|
|
$infoRows = '';
|
|
if ($showGrade) {
|
|
$infoRows .= '
|
|
<div class="info-row">
|
|
<span class="icon-badge">G</span>
|
|
<span class="info-copy">
|
|
<span class="label">' . $gradeLabel . '</span>
|
|
<span class="value">' . $grade . '</span>
|
|
</span>
|
|
</div>';
|
|
}
|
|
|
|
$infoRows .= '
|
|
<div class="info-row">
|
|
<span class="icon-badge">Y</span>
|
|
<span class="info-copy">
|
|
<span class="label">Academic Year</span>
|
|
<span class="value">' . $year . '</span>
|
|
</span>
|
|
</div>
|
|
|
|
<div class="info-row">
|
|
<span class="icon-badge">ID</span>
|
|
<span class="info-copy">
|
|
<span class="label">' . $this->escapeHtml($idLabel) . '</span>
|
|
<span class="value">' . $schoolId . '</span>
|
|
</span>
|
|
</div>';
|
|
|
|
return '
|
|
<div class="badge">
|
|
<div class="header-wrap">
|
|
<div class="header">
|
|
<div class="header-mark">'
|
|
. ($logoDataUri !== null
|
|
? '<img src="' . $logoDataUri . '" alt="School Logo">'
|
|
: '<span>AS</span>') .
|
|
'</div>
|
|
<div class="header-copy">
|
|
<div class="school-name">' . $this->escapeHtml($schoolName) . '</div>
|
|
<div class="sub-school">High School</div>
|
|
<div class="motto">' . $this->escapeHtml($motto) . '</div>
|
|
</div>
|
|
</div>
|
|
<div class="header-accent"></div>
|
|
<div class="header-tail"></div>
|
|
</div>
|
|
|
|
<div class="body">
|
|
<div class="student-name">' . mb_strtoupper($title, 'UTF-8') . '</div>
|
|
<div class="name-divider">
|
|
<span class="divider-line"></span>
|
|
<span class="divider-dot"></span>
|
|
<span class="divider-line"></span>
|
|
</div>
|
|
<div class="role">' . mb_strtoupper($role, 'UTF-8') . '</div>
|
|
|
|
<table class="content">
|
|
<tr>
|
|
<td class="info">
|
|
' . $infoRows . '
|
|
</td>
|
|
|
|
<td class="qr-col">
|
|
<div class="qr-box">'
|
|
. ($qrBase64 !== ''
|
|
? '<img src="data:image/png;base64,' . $qrBase64 . '" alt="QR Code">'
|
|
: '') .
|
|
'</div>
|
|
<div class="scan-pill">Scan to verify</div>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<div class="barcode-rule"></div>
|
|
<div class="barcode">' . $this->escapeHtml($barcodeDigits) . '</div>
|
|
</div>
|
|
|
|
<div class="footer">
|
|
<span class="footer-mark">'
|
|
. ($logoDataUri !== null
|
|
? '<img src="' . $logoDataUri . '" alt="School Logo">'
|
|
: '<span>S</span>') .
|
|
'</span>
|
|
<span class="footer-copy">' . $footer . '</span>
|
|
</div>
|
|
</div>
|
|
';
|
|
}
|
|
|
|
protected function badgeCss(): string
|
|
{
|
|
return <<<'CSS'
|
|
@page {
|
|
size: A4 landscape;
|
|
margin: 0.18in;
|
|
}
|
|
|
|
* {
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
margin: 0;
|
|
font-family: DejaVu Sans, Arial, Helvetica, sans-serif;
|
|
color: #111;
|
|
}
|
|
|
|
.page {
|
|
width: 100%;
|
|
page-break-after: always;
|
|
}
|
|
|
|
.page:last-child {
|
|
page-break-after: auto;
|
|
}
|
|
|
|
.grid {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
table-layout: fixed;
|
|
}
|
|
|
|
.grid td {
|
|
width: 25%;
|
|
vertical-align: top;
|
|
padding: 0.03in;
|
|
height: 3.32in;
|
|
}
|
|
|
|
.badge {
|
|
width: 2.12in;
|
|
height: 3.18in;
|
|
border: 1.2px solid #1f6b3a;
|
|
border-radius: 0.18in;
|
|
overflow: hidden;
|
|
position: relative;
|
|
margin: 0 auto;
|
|
background: #ffffff;
|
|
box-shadow: 0 0.03in 0.10in rgba(0, 0, 0, 0.15);
|
|
}
|
|
|
|
.header-wrap {
|
|
position: relative;
|
|
height: 0.92in;
|
|
}
|
|
|
|
.header {
|
|
background: #1f6b3a;
|
|
color: #ffffff;
|
|
padding: 0.10in 0.10in 0.06in;
|
|
height: 0.64in;
|
|
border-radius: 0.18in 0.18in 0 0;
|
|
}
|
|
|
|
.header-accent {
|
|
position: absolute;
|
|
left: 0.28in;
|
|
right: 0.28in;
|
|
top: 0.64in;
|
|
height: 0.02in;
|
|
background: #9fd6b0;
|
|
}
|
|
|
|
.header-tail {
|
|
width: 0;
|
|
height: 0;
|
|
margin: 0 auto;
|
|
border-left: 0.92in solid transparent;
|
|
border-right: 0.92in solid transparent;
|
|
border-top: 0.22in solid #1f6b3a;
|
|
}
|
|
|
|
.header-mark {
|
|
float: left;
|
|
width: 0.40in;
|
|
height: 0.40in;
|
|
border: 2px solid #ffffff;
|
|
border-radius: 50%;
|
|
text-align: center;
|
|
overflow: hidden;
|
|
background: rgba(255,255,255,0.08);
|
|
}
|
|
|
|
.header-mark img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.header-mark span {
|
|
display: block;
|
|
line-height: 0.36in;
|
|
font-size: 12pt;
|
|
font-weight: 800;
|
|
}
|
|
|
|
.header-copy {
|
|
margin-left: 0.48in;
|
|
text-align: center;
|
|
}
|
|
|
|
.school-name {
|
|
font-size: 8.8pt;
|
|
font-weight: 800;
|
|
line-height: 1.1;
|
|
margin: 0;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.8px;
|
|
}
|
|
|
|
.sub-school {
|
|
font-size: 5.8pt;
|
|
font-weight: 800;
|
|
text-transform: uppercase;
|
|
letter-spacing: 2px;
|
|
margin-top: 0.02in;
|
|
}
|
|
|
|
.motto {
|
|
font-size: 4.8pt;
|
|
font-weight: 700;
|
|
margin-top: 0.03in;
|
|
letter-spacing: 0.4px;
|
|
min-height: 0.08in;
|
|
text-transform: uppercase;
|
|
color: #f4d34c;
|
|
}
|
|
|
|
.body {
|
|
padding: 0.00in 0.10in 0.40in;
|
|
}
|
|
|
|
.student-name {
|
|
text-align: center;
|
|
color: #15512c;
|
|
font-size: 10.6pt;
|
|
font-weight: 800;
|
|
line-height: 1.05;
|
|
text-transform: uppercase;
|
|
margin: 0.08in 0 0.03in;
|
|
}
|
|
|
|
.name-divider {
|
|
text-align: center;
|
|
margin-bottom: 0.03in;
|
|
}
|
|
|
|
.divider-line {
|
|
display: inline-block;
|
|
width: 0.60in;
|
|
border-top: 1px solid #8fc7a0;
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.divider-dot {
|
|
display: inline-block;
|
|
width: 0.05in;
|
|
height: 0.05in;
|
|
margin: 0 0.04in;
|
|
background: #1f6b3a;
|
|
transform: rotate(45deg);
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.role {
|
|
text-align: center;
|
|
color: #1f6b3a;
|
|
font-size: 6.2pt;
|
|
font-weight: 700;
|
|
text-transform: uppercase;
|
|
letter-spacing: 1.1px;
|
|
margin-bottom: 0.08in;
|
|
}
|
|
|
|
.content {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
table-layout: fixed;
|
|
}
|
|
|
|
.content td {
|
|
vertical-align: top;
|
|
}
|
|
|
|
.info {
|
|
width: 58%;
|
|
padding-right: 0.06in;
|
|
border-right: 1px solid #d6d6d6;
|
|
}
|
|
|
|
.info-row {
|
|
margin-bottom: 0.07in;
|
|
padding-bottom: 0.04in;
|
|
border-bottom: 1px solid #d9d9d9;
|
|
}
|
|
|
|
.info-row:last-child {
|
|
border-bottom: 0;
|
|
margin-bottom: 0;
|
|
padding-bottom: 0;
|
|
}
|
|
|
|
.icon-badge {
|
|
display: inline-block;
|
|
width: 0.22in;
|
|
height: 0.22in;
|
|
line-height: 0.22in;
|
|
margin-right: 0.04in;
|
|
border-radius: 50%;
|
|
background: #1f6b3a;
|
|
color: #ffffff;
|
|
text-align: center;
|
|
font-size: 5pt;
|
|
font-weight: 800;
|
|
vertical-align: top;
|
|
}
|
|
|
|
.info-copy {
|
|
display: inline-block;
|
|
width: 0.92in;
|
|
}
|
|
|
|
.label {
|
|
display: block;
|
|
font-size: 4.8pt;
|
|
font-weight: 700;
|
|
text-transform: uppercase;
|
|
color: #333333;
|
|
margin-bottom: 0.01in;
|
|
}
|
|
|
|
.value {
|
|
display: block;
|
|
font-size: 7.6pt;
|
|
font-weight: 800;
|
|
color: #1f6b3a;
|
|
word-wrap: break-word;
|
|
}
|
|
|
|
.qr-col {
|
|
width: 42%;
|
|
text-align: center;
|
|
padding-left: 0.06in;
|
|
}
|
|
|
|
.qr-box {
|
|
display: inline-block;
|
|
border: 1px solid #1f6b3a;
|
|
border-radius: 0.08in;
|
|
padding: 0.04in;
|
|
background: #ffffff;
|
|
min-width: 0.92in;
|
|
min-height: 0.92in;
|
|
}
|
|
|
|
.qr-box img {
|
|
width: 0.84in;
|
|
height: 0.84in;
|
|
display: block;
|
|
}
|
|
|
|
.scan {
|
|
margin-top: 0.04in;
|
|
font-size: 5.5pt;
|
|
font-weight: 700;
|
|
text-transform: uppercase;
|
|
color: #1f6b3a;
|
|
}
|
|
|
|
.scan-pill {
|
|
margin-top: 0.03in;
|
|
display: inline-block;
|
|
background: #1f6b3a;
|
|
color: #ffffff;
|
|
border-radius: 0.06in;
|
|
font-size: 5pt;
|
|
font-weight: 700;
|
|
text-transform: uppercase;
|
|
padding: 0.025in 0.08in;
|
|
}
|
|
|
|
.barcode-rule {
|
|
margin-top: 0.07in;
|
|
border-top: 1px solid #8fc7a0;
|
|
}
|
|
|
|
.barcode {
|
|
margin: 0.04in auto 0;
|
|
text-align: center;
|
|
font-family: "Courier New", monospace;
|
|
font-size: 9pt;
|
|
letter-spacing: 0.2px;
|
|
color: #111111;
|
|
}
|
|
|
|
.footer {
|
|
position: absolute;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
background: #1f6b3a;
|
|
color: #ffffff;
|
|
padding: 0.06in 0.06in;
|
|
font-size: 4.8pt;
|
|
font-weight: 700;
|
|
line-height: 1.2;
|
|
}
|
|
|
|
.footer-mark {
|
|
display: inline-block;
|
|
width: 0.18in;
|
|
height: 0.18in;
|
|
line-height: 0.18in;
|
|
margin-right: 0.05in;
|
|
border: 1.5px solid #ffffff;
|
|
border-radius: 50%;
|
|
text-align: center;
|
|
vertical-align: top;
|
|
font-size: 6pt;
|
|
overflow: hidden;
|
|
background: rgba(255,255,255,0.08);
|
|
}
|
|
|
|
.footer-mark img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.footer-mark span {
|
|
display: block;
|
|
}
|
|
|
|
.footer-copy {
|
|
display: inline-block;
|
|
width: 1.64in;
|
|
vertical-align: top;
|
|
}
|
|
CSS;
|
|
}
|
|
|
|
protected function escapeHtml(string $value): string
|
|
{
|
|
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
protected function writeTempQrPng(string $png): ?string
|
|
{
|
|
if ($png === '') {
|
|
return null;
|
|
}
|
|
|
|
$tmpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'badge_qr_' . bin2hex(random_bytes(8)) . '.png';
|
|
if (@file_put_contents($tmpPath, $png) === false) {
|
|
return null;
|
|
}
|
|
|
|
$this->tempQrFiles[] = $tmpPath;
|
|
|
|
return $tmpPath;
|
|
}
|
|
|
|
protected function drawBadge(
|
|
\FPDF $pdf,
|
|
array $student,
|
|
float $x,
|
|
float $y,
|
|
float $w,
|
|
float $h,
|
|
string $schoolName,
|
|
string $motto,
|
|
string $defaultStudentRoleLabel,
|
|
string $footerBlock,
|
|
?string $logoPath
|
|
): void {
|
|
$kind = (string) ($student['_badge_kind'] ?? 'student');
|
|
$idLabel = $kind === 'staff' ? 'USER ID' : 'SCHOOL ID';
|
|
$showGrade = (bool) ($student['show_grade'] ?? ($kind === 'student'));
|
|
$gradeLabel = (string) ($student['grade_label'] ?? ($kind === 'staff' ? 'CLASS / ASSIGNMENT' : 'GRADE'));
|
|
|
|
$blue = [31, 107, 58];
|
|
$blueLight = [53, 140, 81];
|
|
|
|
$qrPath = (string) ($student['_qr_tmp'] ?? '');
|
|
$fullName = $this->formatter->toPdf((string) ($student['fullname'] ?? ''));
|
|
$grade = $this->formatter->toPdf((string) ($student['grade'] ?? ''));
|
|
$year = $this->formatter->toPdf((string) ($student['academic_year'] ?? ''));
|
|
$schoolId = $this->formatter->toPdf((string) ($student['school_id'] ?? ''));
|
|
|
|
$schoolNamePdf = $this->formatter->toPdf($schoolName);
|
|
$mottoPdf = $this->formatter->toPdf($motto);
|
|
$rolePdf = $this->formatter->toPdf((string) ($student['role_subline'] ?? $defaultStudentRoleLabel));
|
|
|
|
$pdf->SetDrawColor($blue[0], $blue[1], $blue[2]);
|
|
$pdf->SetLineWidth(0.003);
|
|
$pdf->Rect($x, $y, $w, $h);
|
|
|
|
$headerH = 0.55;
|
|
$footerH = 0.38;
|
|
|
|
$pdf->SetFillColor($blue[0], $blue[1], $blue[2]);
|
|
$pdf->Rect($x, $y, $w, $headerH, 'F');
|
|
|
|
$pdf->SetTextColor(255, 255, 255);
|
|
$pdf->SetFont('Helvetica', 'B', 8.5);
|
|
$pdf->SetXY($x + 0.06, $y + 0.12);
|
|
$pdf->Cell($w - 0.12, 0.1, $schoolNamePdf, 0, 0, 'C');
|
|
|
|
if ($mottoPdf !== '') {
|
|
$pdf->SetFont('Helvetica', 'B', 5.2);
|
|
$pdf->SetXY($x + 0.06, $y + $headerH - 0.12);
|
|
$pdf->Cell($w - 0.12, 0.08, $mottoPdf, 0, 0, 'C');
|
|
}
|
|
|
|
$bodyTop = $y + $headerH + 0.06;
|
|
$pdf->SetTextColor(21, 81, 44);
|
|
$pdf->SetFont('Helvetica', 'B', 9.5);
|
|
$pdf->SetXY($x + 0.07, $bodyTop);
|
|
$pdf->Cell($w - 0.14, 0.11, strtoupper($fullName), 0, 1, 'C');
|
|
|
|
$pdf->SetTextColor(27, 86, 177);
|
|
$pdf->SetFont('Helvetica', 'B', 6);
|
|
$pdf->SetX($x + 0.07);
|
|
$pdf->Cell($w - 0.14, 0.07, strtoupper($rolePdf), 0, 1, 'C');
|
|
|
|
$ruleY = $bodyTop + 0.22;
|
|
$pdf->SetDrawColor(215, 222, 234);
|
|
$pdf->Line($x + 0.08, $ruleY, $x + $w - 0.08, $ruleY);
|
|
|
|
$infoLeft = $x + 0.08;
|
|
$infoW = $w * 0.52;
|
|
$labelY = $ruleY + 0.05;
|
|
$infoRows = [];
|
|
if ($showGrade) {
|
|
$infoRows[] = [$gradeLabel, $grade];
|
|
}
|
|
$infoRows[] = ['ACADEMIC YEAR', $year];
|
|
$infoRows[] = [$idLabel, $schoolId];
|
|
|
|
foreach ($infoRows as $index => [$label, $value]) {
|
|
$pdf->SetTextColor(51, 51, 51);
|
|
$pdf->SetFont('Helvetica', 'B', 5);
|
|
$pdf->SetXY($infoLeft, $labelY);
|
|
$pdf->Cell($infoW, 0.05, $label, 0, 1, 'L');
|
|
|
|
$pdf->SetTextColor($blueLight[0], $blueLight[1], $blueLight[2]);
|
|
$pdf->SetFont('Helvetica', 'B', 7.5);
|
|
$pdf->SetX($infoLeft);
|
|
if ($index === array_key_last($infoRows)) {
|
|
$pdf->MultiCell($infoW, 0.09, $value, 0, 'L');
|
|
$labelY = $pdf->GetY() + 0.015;
|
|
} else {
|
|
$pdf->Cell($infoW, 0.08, $value, 0, 1, 'L');
|
|
$labelY = $pdf->GetY() + 0.01;
|
|
}
|
|
}
|
|
|
|
$qrSize = 0.38;
|
|
$qrX = $x + $w - 0.08 - $qrSize;
|
|
$qrY = $ruleY + 0.04;
|
|
|
|
if ($qrPath !== '' && is_file($qrPath)) {
|
|
try {
|
|
$pdf->SetDrawColor($blueLight[0], $blueLight[1], $blueLight[2]);
|
|
$pdf->Rect($qrX - 0.03, $qrY - 0.03, $qrSize + 0.06, $qrSize + 0.06);
|
|
$pdf->Image($qrPath, $qrX, $qrY, $qrSize, $qrSize, 'PNG');
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
|
|
$pdf->SetTextColor($blueLight[0], $blueLight[1], $blueLight[2]);
|
|
$pdf->SetFont('Helvetica', 'B', 4.5);
|
|
$pdf->SetXY($qrX - 0.05, $qrY + $qrSize + 0.02);
|
|
$pdf->Cell($qrSize + 0.1, 0.05, 'SCAN TO VERIFY', 0, 0, 'C');
|
|
|
|
$fy = $y + $h - $footerH;
|
|
$pdf->SetFillColor($blue[0], $blue[1], $blue[2]);
|
|
$pdf->Rect($x, $fy, $w, $footerH, 'F');
|
|
|
|
if ($logoPath !== null && is_readable($logoPath)) {
|
|
try {
|
|
$smallLogo = 0.1;
|
|
$pdf->Image($logoPath, $x + $w / 2 - $smallLogo / 2, $fy + 0.03, $smallLogo, $smallLogo);
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
|
|
$pdf->SetTextColor(255, 255, 255);
|
|
$pdf->SetFont('Helvetica', 'B', 4.5);
|
|
$lines = preg_split('/\r\n|\r|\n/', $footerBlock) ?: [$footerBlock];
|
|
$lineY = $fy + 0.15;
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if ($line === '') {
|
|
continue;
|
|
}
|
|
$pdf->SetXY($x + 0.04, $lineY);
|
|
$pdf->Cell($w - 0.08, 0.055, $this->formatter->toPdf($line), 0, 1, 'C');
|
|
$lineY += 0.055;
|
|
}
|
|
}
|
|
|
|
protected function renderQrPng(string $payload): string
|
|
{
|
|
$qrOptions = new QROptions([
|
|
'version' => 5,
|
|
'outputInterface' => QRGdImagePNG::class,
|
|
'outputBase64' => false,
|
|
'eccLevel' => QRCode::ECC_M,
|
|
'scale' => 4,
|
|
]);
|
|
|
|
$qr = new QRCode($qrOptions);
|
|
|
|
return $qr->render($payload);
|
|
}
|
|
|
|
protected function logoFilePath(): ?string
|
|
{
|
|
return $this->formatter->firstExisting([
|
|
public_path('assets/images/school_logo.png'),
|
|
public_path('assets/images/logo.png'),
|
|
]);
|
|
}
|
|
|
|
protected function logoDataUri(): ?string
|
|
{
|
|
$path = $this->logoFilePath();
|
|
if ($path === null || !is_readable($path)) {
|
|
return null;
|
|
}
|
|
|
|
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
|
$mime = match ($ext) {
|
|
'jpg', 'jpeg' => 'image/jpeg',
|
|
'gif' => 'image/gif',
|
|
'webp' => 'image/webp',
|
|
default => 'image/png',
|
|
};
|
|
|
|
$bytes = @file_get_contents($path);
|
|
if ($bytes === false || $bytes === '') {
|
|
return null;
|
|
}
|
|
|
|
return 'data:' . $mime . ';base64,' . base64_encode($bytes);
|
|
}
|
|
|
|
protected function footerBlock(string $schoolName): string
|
|
{
|
|
$address = trim((string) config('badges.footer_address', ''));
|
|
if ($address !== '') {
|
|
return $schoolName . "\n" . $address;
|
|
}
|
|
|
|
return $schoolName;
|
|
}
|
|
}
|