196 lines
5.0 KiB
PHP
Executable File
196 lines
5.0 KiB
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
function e(string $v): string
|
|
{
|
|
return htmlspecialchars($v, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
/** 静态资源引用统一加版本号(文件 mtime):文件一变 URL 即变,老用户浏览器无需清缓存 */
|
|
function assetUrl(string $path): string
|
|
{
|
|
$file = __DIR__ . '/../public/' . ltrim($path, '/');
|
|
$v = is_file($file) ? filemtime($file) : 0;
|
|
return $path . '?v=' . $v;
|
|
}
|
|
|
|
function now(): string
|
|
{
|
|
return date('Y-m-d H:i:s');
|
|
}
|
|
|
|
function flash(string $type, string $msg): void
|
|
{
|
|
$_SESSION['flash'] = ['type' => $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;
|
|
}
|