$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)); } function normalizeSearchField(?string $v): string { $v = trim((string) $v); return in_array($v, ['all', 'borrower', 'guarantor'], true) ? $v : 'all'; } function archiveMatchesKeyword(array $r, string $q, string $searchField): bool { $q = trim($q); if ($q === '') { return true; } $searchField = normalizeSearchField($searchField); if ($searchField === 'borrower') { $hay = mb_strtolower((string) ($r['borrower'] ?? '')); } elseif ($searchField === 'guarantor') { $hay = mb_strtolower((string) ($r['guarantor'] ?? '')); } else { $hay = mb_strtolower(implode(' ', [ (string) ($r['name'] ?? ''), (string) ($r['loan_institution'] ?? ''), (string) ($r['borrower'] ?? ''), (string) ($r['guarantee_type'] ?? ''), (string) ($r['guarantor'] ?? ''), ])); } return mb_strpos($hay, mb_strtolower($q)) !== false; } function directoryLevel2TimeSortKey(string $name): int { $name = trim($name); if ($name === '') { return 9999; } if (preg_match('/^(\d{4})(?:\b|年)/u', $name, $m)) { return (int) $m[1]; } if (preg_match('/(\d{4})/u', $name, $m)) { return (int) $m[1]; } return 9999; } function directoryPickerItems(array $dirs): array { $byParent = []; foreach ($dirs as $d) { $pid = $d['parent_id'] === null ? 0 : (int) $d['parent_id']; $byParent[$pid][] = $d; } foreach ($byParent as &$children) { if ($children === []) { continue; } if ((int) ($children[0]['level'] ?? 0) === 2) { usort($children, static function (array $a, array $b): int { $ka = directoryLevel2TimeSortKey((string) $a['name']); $kb = directoryLevel2TimeSortKey((string) $b['name']); if ($ka !== $kb) { return $ka <=> $kb; } return strcmp((string) $a['name'], (string) $b['name']); }); } } unset($children); $pathMap = dirPathMap($dirs); $out = []; $walk = static function (int $parent) use (&$walk, $byParent, $pathMap, &$out): void { foreach ($byParent[$parent] ?? [] as $d) { $id = (int) $d['id']; $name = (string) $d['name']; $out[] = [ 'id' => $id, 'name' => $name, 'path' => (string) ($pathMap[$id] ?? $name), 'level' => (int) ($d['level'] ?? 1), 'parent_id' => $parent, ]; $walk($id); } }; $walk(0); return $out; }