101 lines
2.1 KiB
PHP
Executable File
101 lines
2.1 KiB
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
function e(string $v): string
|
|
{
|
|
return htmlspecialchars($v, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
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));
|
|
}
|