$type, 'msg' => $msg]; } function getFlash(): ?array { if (!isset($_SESSION['flash'])) { return null; } $f = $_SESSION['flash']; unset($_SESSION['flash']); return $f; } function normalizeFileName(string $name): string { $name = strtolower($name); $name = preg_replace('/[^a-z0-9\.]+/', '_', $name); return trim((string)$name, '_'); } function archiveStatus(?string $endDate): string { if (!$endDate) { return '待更新'; } $today = date('Y-m-d'); if ($endDate >= $today) { return '履行中'; } return '待更新'; } function canPreview(string $ext): bool { return in_array(strtolower($ext), ['pdf', 'jpg', 'jpeg', 'png', 'gif'], true); } function amount(?float $v): string { if ($v === null) { return ''; } return number_format($v, 2, '.', ''); } function dirPathMap(array $dirs): array { $map = []; foreach ($dirs as $d) { $map[(int)$d['id']] = $d; } $path = []; foreach ($dirs as $d) { $id = (int)$d['id']; $parts = [$d['name']]; $p = $d['parent_id']; while ($p && isset($map[(int)$p])) { $parts[] = $map[(int)$p]['name']; $p = $map[(int)$p]['parent_id']; } $path[$id] = implode(' / ', array_reverse($parts)); } return $path; } function collectSubDirIds(array $dirs, int $rootId): array { $children = []; foreach ($dirs as $d) { $pid = $d['parent_id'] === null ? null : (int)$d['parent_id']; if ($pid !== null) { $children[$pid][] = (int)$d['id']; } } $result = [$rootId]; $queue = [$rootId]; while ($queue) { $cur = array_shift($queue); foreach ($children[$cur] ?? [] as $cid) { $result[] = $cid; $queue[] = $cid; } } return array_values(array_unique($result)); }