125 lines
6.0 KiB
Plaintext
125 lines
6.0 KiB
Plaintext
public function submitAbsenceAdmin()
|
|
{
|
|
$userId = (int)(session()->get('user_id') ?? 0);
|
|
if ($userId <= 0) {
|
|
return redirect()->to('login')->with('error', 'Please log in first.');
|
|
}
|
|
|
|
$semester = (string)($this->semester ?? '');
|
|
$schoolYear = (string)($this->schoolYear ?? '');
|
|
if ($schoolYear === '') {
|
|
return redirect()->to('/administrator/absence')->with('status', 'error')->with('message', 'Semester or school year not configured.');
|
|
}
|
|
$semesterResolver = new SemesterRangeService($this->configModel);
|
|
|
|
$dates = (array)($this->request->getPost('dates') ?? []);
|
|
$reasonType = trim((string)($this->request->getPost('reason_type') ?? ''));
|
|
$reasonText = trim((string)($this->request->getPost('reason') ?? ''));
|
|
|
|
if ($reasonText === '') {
|
|
return redirect()->to('/administrator/absence')
|
|
->with('status', 'error')
|
|
->with('message', 'Reason is required.');
|
|
}
|
|
|
|
$reasonBase = ($reasonType !== '') ? strtolower($reasonType) : '';
|
|
$reason = $reasonBase !== '' ? ($reasonBase . ': ' . $reasonText) : $reasonText;
|
|
|
|
$allowedDates = $this->allowedAbsenceDates();
|
|
$allowedSet = array_fill_keys($allowedDates, true);
|
|
|
|
$roleName = $this->userModel->getUserRole($userId) ?: null;
|
|
|
|
$saved = 0; $invalid = [];
|
|
$savedDates = [];
|
|
$dates = array_values(array_unique(array_map('strval', $dates)));
|
|
foreach ($dates as $d) {
|
|
$d = trim((string)$d);
|
|
if ($d === '') continue;
|
|
$dt = date_create_from_format('Y-m-d', $d);
|
|
if (!$dt || $dt->format('Y-m-d') !== $d || empty($allowedSet[$d])) {
|
|
$invalid[] = $d;
|
|
continue;
|
|
}
|
|
|
|
$semesterForDate = $semesterResolver->getSemesterForDate($d);
|
|
if ($semesterForDate === '') {
|
|
$semesterForDate = $semester;
|
|
}
|
|
|
|
$ok = $this->staffAttendanceModel->upsertOne(
|
|
userId: $userId,
|
|
roleName: $roleName,
|
|
date: $d,
|
|
semester: $semesterForDate,
|
|
schoolYear: $schoolYear,
|
|
status: StaffAttendanceModel::STATUS_ABSENT,
|
|
reason: $reason,
|
|
editorId: $userId
|
|
);
|
|
if ($ok) { $saved++; $savedDates[] = $d; }
|
|
}
|
|
|
|
if (!empty($invalid)) {
|
|
return redirect()->to('/administrator/absence')
|
|
->with('status', 'error')
|
|
->with('message', 'Invalid dates: ' . implode(', ', $invalid));
|
|
}
|
|
|
|
// Send notification email to principal with request details
|
|
try {
|
|
$user = $this->userModel->find($userId) ?: [];
|
|
$fullName = trim(($user['firstname'] ?? '') . ' ' . ($user['lastname'] ?? '')) ?: 'Administrator';
|
|
$userEmail = $user['email'] ?? '';
|
|
$role = $roleName ?: 'admin';
|
|
$dateList = !empty($savedDates) ? implode(', ', $savedDates) : implode(', ', $dates);
|
|
|
|
$subject = sprintf('TimeOff Request: %s (%s) — %s', $fullName, ucfirst((string)$role), $dateList ?: 'No dates');
|
|
$submittedAt = utc_now();
|
|
|
|
$body = '<div style="font-family:Arial,Helvetica,sans-serif;font-size:14px;line-height:1.5;">'
|
|
. '<h3 style="margin:0 0 8px;">New TimeOff Request</h3>'
|
|
. '<p style="margin:0 0 12px;">A staff time-off request was submitted from the administrator portal.</p>'
|
|
. '<table cellpadding="6" cellspacing="0" style="border-collapse:collapse;">'
|
|
. '<tr><td><strong>Name</strong></td><td>' . esc($fullName) . '</td></tr>'
|
|
. '<tr><td><strong>Email</strong></td><td>' . esc($userEmail) . '</td></tr>'
|
|
. '<tr><td><strong>Role</strong></td><td>' . esc((string)$role) . '</td></tr>'
|
|
. '<tr><td><strong>Semester</strong></td><td>' . esc($semester) . '</td></tr>'
|
|
. '<tr><td><strong>School Year</strong></td><td>' . esc($schoolYear) . '</td></tr>'
|
|
. '<tr><td><strong>Reason Type</strong></td><td>' . esc($reasonType ?: '-') . '</td></tr>'
|
|
. '<tr><td><strong>Reason</strong></td><td>' . esc($reasonText) . '</td></tr>'
|
|
. '<tr><td><strong>Dates</strong></td><td>' . esc($dateList ?: '-') . '</td></tr>'
|
|
. '<tr><td><strong>Submitted At</strong></td><td>' . esc($submittedAt) . '</td></tr>'
|
|
. '</table>'
|
|
. '</div>';
|
|
|
|
$linkService = new StaffTimeOffLinkService();
|
|
$token = $linkService->createToken([
|
|
'uid' => $userId,
|
|
'email' => $userEmail,
|
|
'name' => $fullName,
|
|
'role' => $role,
|
|
'dates' => $dateList ?: '-',
|
|
'reason' => $reasonText,
|
|
'reason_type' => $reasonType,
|
|
'submitted_at' => $submittedAt,
|
|
'origin' => 'administrator portal',
|
|
]);
|
|
$notifyUrl = base_url('timeoff/notify/' . rawurlencode($token));
|
|
$body .= '<p style="margin-top:16px;">Click <a href="' . esc($notifyUrl) . '">Send confirmation email to '
|
|
. esc($fullName) . '</a> so the staff member is notified automatically. '
|
|
. 'This link expires in 14 days.</p>';
|
|
|
|
$mailer = \Config\Services::emailService();
|
|
$principalEmail = env('PRINCIPAL_EMAIL') ?: 'principal@alrahmaisgl.org';
|
|
$mailer->send($principalEmail, $subject, $body, 'notifications');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Failed to send TimeOff email (admin): ' . $e->getMessage());
|
|
}
|
|
|
|
return redirect()->to('/administrator/absence')
|
|
->with('status', 'success')
|
|
->with('message', $saved . ' day(s) saved as absent.');
|
|
}
|
|
|