update controllers logic

This commit is contained in:
root
2026-04-23 00:04:35 -04:00
parent 1977a513df
commit ca4ba272fc
353 changed files with 13402 additions and 1301 deletions
+396 -126
View File
@@ -1,157 +1,233 @@
<?php
declare(strict_types=1);
namespace App\Services\Badges;
use FPDF;
use chillerlan\QRCode\Output\QRGdImagePNG;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
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 BadgeUserLookupService $lookupService,
protected BadgeStudentLookupService $studentLookupService,
protected BadgeUserLookupService $userLookupService,
protected BadgePrintLogService $printLogService,
protected BadgeTextFormatter $formatter
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 {
$userIds = $this->formatter->normalizeIds($userIds);
$this->tempQrFiles = [];
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->SetAutoPageBreak(false);
$badgeW = 95;
$badgeH = 67;
$marginL = 14;
$marginT = 6;
$gutterX = 0;
$gutterY = 0;
$cols = 2;
$rows = 4;
$perPage = $cols * $rows;
$pagesAdded = 0;
$i = 0;
$seen = [];
foreach ($userIds as $uid) {
$info = $this->lookupService->getUserInfoById($uid, $schoolYear);
if (!$info || !is_array($info)) {
continue;
try {
if (!class_exists('FPDF', false)) {
require_once base_path('app/ThirdParty/fpdf/fpdf.php');
}
$userId = (int) ($info['user_id'] ?? $uid);
$name = $this->formatter->norm($info['name'] ?? '');
$studentIds = $this->formatter->normalizeIds($studentIds);
$userIds = $this->formatter->normalizeIds($userIds);
$postedRole = $rolesMap[$userId] ?? null;
$roleResolved = $postedRole !== null
? $postedRole
: $this->formatter->resolveRole($info);
$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();
$roleResolved = $this->formatter->formatRole((string) $roleResolved);
$info['role_resolved'] = $roleResolved;
$rowsWithQr = [];
if (!empty($classesMap[$userId])) {
$info['class_section_name'] = (string) $classesMap[$userId];
$students = $this->studentLookupService->fetchForBadges($studentIds, $schoolYear);
foreach ($students as $row) {
try {
$png = $this->renderQrPng((string) $row['school_id']);
$tmpPath = $this->writeTempQrPng($png);
if ($tmpPath === null) {
continue;
}
$rowsWithQr[] = array_merge($row, [
'_badge_kind' => 'student',
'role_subline' => $studentRoleLabel,
'_qr_tmp' => $tmpPath,
]);
} catch (Throwable) {
continue;
}
}
$class = $this->formatter->norm($info['class_section_name'] ?? '');
foreach ($userIds as $uid) {
$staffRow = $this->buildStaffBadgeRow((int) $uid, $schoolYear, $rolesMap, $classesMap);
if ($staffRow === null) {
continue;
}
$badgeKey = strtolower(trim($userId . '|' . $name . '|' . $roleResolved . '|' . $class));
if (isset($seen[$badgeKey])) {
continue;
}
$seen[$badgeKey] = true;
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);
}
if (($i % $perPage) === 0) {
$pdf->AddPage();
$pagesAdded++;
$png = $this->renderQrPng($qrPayload);
$tmpPath = $this->writeTempQrPng($png);
if ($tmpPath === null) {
continue;
}
$rowsWithQr[] = array_merge($staffRow, [
'_badge_kind' => 'staff',
'_qr_tmp' => $tmpPath,
]);
} catch (Throwable) {
continue;
}
}
$indexOnPage = $i % $perPage;
$row = intdiv($indexOnPage, $cols);
$col = $indexOnPage % $cols;
$pdf = new \FPDF('L', 'in', 'Letter');
$pdf->SetAutoPageBreak(false);
$x = $marginL + $col * ($badgeW + $gutterX);
$y = $marginT + $row * ($badgeH + $gutterY);
$pages = array_chunk($rowsWithQr, 8);
$this->drawBadgeInCell($pdf, $info, $x, $y, $badgeW, $badgeH, $schoolYear);
$i++;
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 (for students) school id 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) || empty($row['_qr_tmp'])) {
continue;
}
$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
);
}
}
}
}
$binary = $pdf->Output('S');
$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"',
]);
} finally {
foreach ($this->tempQrFiles as $path) {
if (is_string($path) && $path !== '' && is_file($path)) {
@unlink($path);
}
}
$this->tempQrFiles = [];
}
if ($pagesAdded === 0) {
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);
$pdf->SetXY(10, 20);
$pdf->MultiCell(0, 6, 'No valid staff selected or data not found.', 0, 'L');
}
$pdfString = $pdf->Output('S');
$this->printLogService->logSafely(
userIds: $userIds,
actorId: $actorId,
schoolYear: $schoolYear,
rolesMap: $rolesMap,
classesMap: $classesMap,
copies: 1
);
return response($pdfString, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="Staff_Badges.pdf"',
]);
}
protected function drawBadgeInCell(FPDF $pdf, array $data, float $x, float $y, float $w, float $h, ?string $schoolYear): void
{
$pdf->SetFillColor(255, 255, 255);
$pdf->Rect($x, $y, $w, $h, 'F');
$pdf->SetDrawColor(200, 200, 200);
$pdf->Rect($x, $y, $w, $h);
$logoSize = 6;
if (!empty($data['school_logo']) && file_exists($data['school_logo'])) {
$pdf->Image($data['school_logo'], $x + $w - 6 - $logoSize, $y + 4, $logoSize + 3, $logoSize - 1);
}
if (!empty($data['isgl_logo']) && file_exists($data['isgl_logo'])) {
$pdf->Image($data['isgl_logo'], $x + 4, $y + 4, $logoSize + 3, $logoSize - 1);
/**
* @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;
}
$padX = 4;
$cursorY = $y + 4 + $logoSize + 2;
$resolvedId = (int) ($info['user_id'] ?? $userId);
$schoolName = strtoupper($this->formatter->norm($data['school_name'] ?? 'AL RAHMA SUNDAY SCHOOL'));
$pdf->SetFont('Arial', '', 16);
$pdf->SetXY($x + $padX, $cursorY);
$pdf->MultiCell($w - 2 * $padX, 5, $this->formatter->toPdf($schoolName), 0, 'C');
$postedRole = $rolesMap[$resolvedId] ?? null;
$roleResolved = $postedRole !== null
? (string) $postedRole
: $this->formatter->resolveRole($info);
$pdf->Ln(9);
$pdf->SetFont('Arial', 'B', 14);
$pdf->SetX($x + $padX);
$name = strtoupper($this->formatter->norm($data['name'] ?? 'STAFF'));
$pdf->Cell($w - 2 * $padX, 6, $this->formatter->toPdf($name), 0, 1, 'C');
$roleResolved = $this->formatter->formatRole($roleResolved);
$info['role_resolved'] = $roleResolved;
$roleResolved = $this->formatter->norm((string) ($data['role_resolved'] ?? ''));
if ($roleResolved === '') {
$roleResolved = $this->formatter->resolveRole($data);
}
if ($roleResolved !== '') {
$roleResolved = $this->formatter->formatRole($roleResolved);
if (!empty($classesMap[$resolvedId])) {
$info['class_section_name'] = (string) $classesMap[$resolvedId];
}
$classRaw = $this->formatter->norm($data['class_section_name'] ?? '');
$class = $this->formatter->formatClass($classRaw);
$class = $this->formatter->norm($info['class_section_name'] ?? '');
$class = $this->formatter->formatClass($class);
$detectSrc = strtolower($this->formatter->norm(
($data['roles_raw'] ?? '') !== '' ? $data['roles_raw'] : (($data['role_name_raw'] ?? '') !== '' ? $data['role_name_raw'] : $roleResolved)
($info['roles'] ?? '') !== '' ? (string) $info['roles'] : $roleResolved
));
$isTeacherish = str_contains($detectSrc, 'teacher') || preg_match('/\bta\b/', $detectSrc);
@@ -171,26 +247,220 @@ class BadgePdfService
$display = 'STAFF';
}
$pdf->Ln(6);
$pdf->SetFont('Arial', '', 14);
$displayOut = $this->formatter->toPdf($display);
$maxTextWidth = $w - 2 * $padX;
$best = $this->formatter->fitText($pdf, $displayOut, 11, 7, $maxTextWidth);
$pdf->SetFont('Arial', '', $best + 4);
$gradeCol = $class !== '' ? $class : '—';
$year = $this->formatter->norm((string) ($info['school_year'] ?? ($schoolYear ?? '')));
if ($pdf->GetStringWidth($displayOut) <= $maxTextWidth) {
$pdf->SetX($x + $padX);
$pdf->Cell($maxTextWidth, 5, $displayOut, 0, 1, 'C');
} else {
$pdf->SetX($x + $padX);
$pdf->MultiCell($maxTextWidth, 5, $displayOut, 0, 'C');
return [
'user_id' => $resolvedId,
'user_id_for_qr' => (string) $resolvedId,
'fullname' => $info['name'] ?? 'STAFF',
'grade' => $gradeCol,
'academic_year' => $year !== '' ? $year : '',
'school_id' => (string) $resolvedId,
'role_subline' => $display,
];
}
protected function writeTempQrPng(string $png): ?string
{
$tmpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'badge_qr_' . bin2hex(random_bytes(8)) . '.png';
if (@file_put_contents($tmpPath, $png) === false) {
return null;
}
$footerYear = $this->formatter->norm((string) ($schoolYear ?? ($data['school_year'] ?? '')));
if ($footerYear !== '') {
$pdf->SetFont('Arial', 'I', 14);
$pdf->SetXY($x + $padX, $y + $h - 9);
$pdf->Cell($w - 2 * $padX, 4, $this->formatter->toPdf($footerYear), 0, 0, 'C');
$this->tempQrFiles[] = $tmpPath;
return $tmpPath;
}
/**
* @param array<string, mixed> $student
*/
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';
$blue = [11, 58, 130];
$blueLight = [27, 76, 160];
$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');
$logoSize = 0.12;
if ($logoPath !== null && is_readable($logoPath)) {
try {
$pdf->Image($logoPath, $x + $w / 2 - $logoSize / 2, $y + 0.035, $logoSize, $logoSize);
} catch (Throwable) {
}
}
$pdf->SetTextColor(255, 255, 255);
$pdf->SetFont('Helvetica', 'B', 8.5);
$pdf->SetXY($x + 0.06, $y + 0.035 + $logoSize + 0.01);
$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(11, 47, 115);
$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;
$gradeLabel = $kind === 'staff' ? 'CLASS / ASSIGNMENT' : 'GRADE';
$pdf->SetTextColor(51, 51, 51);
$pdf->SetFont('Helvetica', 'B', 5);
$pdf->SetXY($infoLeft, $labelY);
$pdf->Cell($infoW, 0.05, $gradeLabel, 0, 1, 'L');
$pdf->SetTextColor($blueLight[0], $blueLight[1], $blueLight[2]);
$pdf->SetFont('Helvetica', 'B', 7.5);
$pdf->SetX($infoLeft);
$pdf->Cell($infoW, 0.08, $grade, 0, 1, 'L');
$pdf->SetTextColor(51, 51, 51);
$pdf->SetFont('Helvetica', 'B', 5);
$pdf->SetX($infoLeft);
$pdf->Cell($infoW, 0.05, 'ACADEMIC YEAR', 0, 1, 'L');
$pdf->SetTextColor($blueLight[0], $blueLight[1], $blueLight[2]);
$pdf->SetFont('Helvetica', 'B', 7.5);
$pdf->SetX($infoLeft);
$pdf->Cell($infoW, 0.08, $year, 0, 1, 'L');
$pdf->SetTextColor(51, 51, 51);
$pdf->SetFont('Helvetica', 'B', 5);
$pdf->SetX($infoLeft);
$pdf->Cell($infoW, 0.05, $idLabel, 0, 1, 'L');
$pdf->SetTextColor($blueLight[0], $blueLight[1], $blueLight[2]);
$pdf->SetFont('Helvetica', 'B', 7.5);
$pdf->SetX($infoLeft);
$pdf->MultiCell($infoW, 0.09, $schoolId, 0, 'L');
$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');
$smallLogo = 0.1;
if ($logoPath !== null && is_readable($logoPath)) {
try {
$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.03 + $smallLogo + 0.015;
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 footerBlock(string $schoolName): string
{
$address = trim((string) config('badges.footer_address', ''));
if ($address !== '') {
return $schoolName . "\n" . $address;
}
return $schoolName;
}
}