add badge print role
This commit is contained in:
@@ -122,6 +122,10 @@ class BadgesController extends PrintablesBaseController
|
||||
$roleResolved = $postedRole !== null ? $postedRole : $resolveRole($info);
|
||||
$roleResolved = $formatRole((string)$roleResolved);
|
||||
$info['role_resolved'] = $roleResolved;
|
||||
if ($postedRole !== null && trim((string)$postedRole) !== '') {
|
||||
$info['roles_raw'] = $roleResolved;
|
||||
$info['role_name_raw'] = $roleResolved;
|
||||
}
|
||||
|
||||
// Prefer posted class name if provided (keeps what the user saw)
|
||||
if (!empty($classesMap[$userId])) {
|
||||
|
||||
@@ -7,10 +7,17 @@ $formatRole = function (?string $role): string {
|
||||
$role = str_replace(['-', '_'], ' ', $role);
|
||||
$role = preg_replace('/\s+/', ' ', trim($role));
|
||||
if ($role === '') return '';
|
||||
$special = [
|
||||
'of' => 'of',
|
||||
'head' => 'Head',
|
||||
];
|
||||
$out = [];
|
||||
foreach (explode(' ', $role) as $w) {
|
||||
if ($w === '') continue;
|
||||
if (preg_match('/^[A-Za-z]{1,3}$/', $w)) {
|
||||
$lower = strtolower($w);
|
||||
if (isset($special[$lower])) {
|
||||
$out[] = $special[$lower];
|
||||
} elseif (preg_match('/^[A-Za-z]{1,3}$/', $w)) {
|
||||
$out[] = strtoupper($w); // TA, PTA, HR, KG...
|
||||
} else {
|
||||
$out[] = ucfirst(strtolower($w)); // Teacher, Assistant, Admin...
|
||||
@@ -59,14 +66,23 @@ $formatRole = function (?string $role): string {
|
||||
<?php $order = 1; ?>
|
||||
<?php foreach ($users as $user): ?>
|
||||
<?php
|
||||
// Pick a representative role (first of CSV or role_name/active_role)
|
||||
$roleRaw = $user['active_role'] ?? $user['role_name'] ?? ($user['roles'] ?? '');
|
||||
if (strpos((string)$roleRaw, ',') !== false) {
|
||||
$parts = array_filter(array_map('trim', explode(',', (string)$roleRaw)));
|
||||
$roleRaw = $parts[0] ?? '';
|
||||
$rolesSource = $user['roles_raw'] ?? ($user['roles'] ?? ($user['role_name_raw'] ?? ($user['role_name'] ?? '')));
|
||||
$roleOptions = [];
|
||||
foreach (array_filter(array_map('trim', explode(',', (string)$rolesSource)), 'strlen') as $roleOptionRaw) {
|
||||
$roleOptionLabel = $formatRole($roleOptionRaw);
|
||||
if ($roleOptionLabel !== '' && !in_array($roleOptionLabel, $roleOptions, true)) {
|
||||
$roleOptions[] = $roleOptionLabel;
|
||||
}
|
||||
}
|
||||
//$roleLabel = $roleRaw !== '' ? $formatRole($roleRaw) : '-';
|
||||
$roleLabel = $user['role_name'] ?? ($user['roles'] ?? '-'); // both are pre-formatted by $formatRole
|
||||
|
||||
if (empty($roleOptions)) {
|
||||
$fallbackRole = $formatRole((string)($user['role_name_raw'] ?? ($user['role_name'] ?? '')));
|
||||
if ($fallbackRole !== '') {
|
||||
$roleOptions[] = $fallbackRole;
|
||||
}
|
||||
}
|
||||
|
||||
$roleLabel = $roleOptions[0] ?? '-';
|
||||
|
||||
// Normalize user id key for checkbox
|
||||
$uid = $user['user_id'] ?? $user['id'] ?? ($user['users.id'] ?? null);
|
||||
@@ -81,7 +97,17 @@ $formatRole = function (?string $role): string {
|
||||
<td style="text-align: center;"><?= esc($order++) ?></td>
|
||||
<td><?= esc($user['firstname'] ?? '') ?></td>
|
||||
<td><?= esc($user['lastname'] ?? '') ?></td>
|
||||
<td><?= esc($roleLabel) ?></td>
|
||||
<td>
|
||||
<?php if (count($roleOptions) > 1): ?>
|
||||
<select class="form-select form-select-sm role-select" aria-label="Badge role for <?= esc(trim(($user['firstname'] ?? '') . ' ' . ($user['lastname'] ?? ''))) ?>">
|
||||
<?php foreach ($roleOptions as $roleOption): ?>
|
||||
<option value="<?= esc($roleOption) ?>"><?= esc($roleOption) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php else: ?>
|
||||
<?= esc($roleLabel) ?>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?= $className !== '' ? esc($className) : '-' ?></td>
|
||||
<td><span class="badge bg-secondary prints-badge" data-user-id="<?= esc($uid ?? '') ?>">—</span></td>
|
||||
<td>
|
||||
@@ -235,11 +261,20 @@ $formatRole = function (?string $role): string {
|
||||
// Delegate checkbox change handling once at the table level (works across redraws)
|
||||
document.getElementById('staffTable').addEventListener('change', function (e) {
|
||||
const target = e.target;
|
||||
if (!target || !target.classList.contains('user-checkbox')) return;
|
||||
if (!target) return;
|
||||
const tr = target.closest('tr[data-user-id]');
|
||||
const userId = tr ? tr.getAttribute('data-user-id') : null;
|
||||
if (!userId) return;
|
||||
if (target.checked) selected.add(userId); else selected.delete(userId);
|
||||
|
||||
if (target.classList.contains('role-select')) {
|
||||
rowMeta[userId] = rowMeta[userId] || { role: '', className: '' };
|
||||
rowMeta[userId].role = target.value || '';
|
||||
} else if (target.classList.contains('user-checkbox')) {
|
||||
if (target.checked) selected.add(userId); else selected.delete(userId);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
syncHiddenInputs();
|
||||
syncPageCheckboxes();
|
||||
// If user interacts, cancel any pending auto-clear to avoid wiping new selections
|
||||
@@ -341,4 +376,4 @@ $formatRole = function (?string $role): string {
|
||||
window.addEventListener('focus', fetchPrintStatus);
|
||||
});
|
||||
</script>
|
||||
<?= $this->endSection() ?>
|
||||
<?= $this->endSection() ?>
|
||||
|
||||
Reference in New Issue
Block a user