plugin('ChangeLog');
/**
* 把表单里的"日期"和"时间"两个输入拼成 event_date
*
* 日期输入框给 Y-m-d,时间输入框给 H:i(:s),拼成 Y-m-d H:i:s;
* 时间没填时只传日期,由 Action 按 00:00:00 处理。
* 同时兼容直接提交 2026-10-27T11:07:16 这种 datetime 字符串的情况。
*/
if (!function_exists('changelogJoinDateTime')) {
function changelogJoinDateTime($date, $time = '')
{
$date = trim((string) $date);
$time = trim((string) $time);
if ($date === '') {
return '';
}
if (strpos($date, ' ') !== false || strpos($date, 'T') !== false) {
return str_replace('T', ' ', $date);
}
return $time === '' ? $date : $date . ' ' . $time;
}
}
/**
* 后台列表的日期时间显示:日期后空格显示时分秒
*
* 老数据只存了日期(时间为 00:00:00),这类记录只显示日期。
*/
if (!function_exists('changelogFormatDateTime')) {
function changelogFormatDateTime($value, $dateFormat = 'Y-m-d')
{
$timestamp = strtotime((string) $value);
if ($timestamp === false) {
return '';
}
$date = date($dateFormat, $timestamp);
$time = date('H:i:s', $timestamp);
return $time === '00:00:00' ? $date : $date . ' ' . $time;
}
}
/**
* 后台列表的搜索框(2.0.10)
*
* 有记录时放在表头工具栏的正中间(「已选择 N 条」与「删除选中」之间);
* 搜索无结果时整个表单不渲染,所以再单独输出一份($standalone = true),
* 让用户能直接改关键词,而不是只能点「查看全部记录」。
*
* 注意:这个 input 落在 mainForm 内部,用 form="cl-no-form"(指向不存在的 id)
* 让它脱离表单——否则按回车会提交 mainForm,勾选了记录时会误触发批量删除。
*
* @param string $keyword 当前搜索词
* @param string $panelPath 本面板地址(不含 query)
* @param bool $standalone 是否用于无结果时单独一行
* @return string
*/
if (!function_exists('changelogSearchBox')) {
function changelogSearchBox($keyword, $panelPath, $standalone = false)
{
$kw = htmlspecialchars((string) $keyword, ENT_QUOTES, 'UTF-8');
$path = htmlspecialchars((string) $panelPath, ENT_QUOTES, 'UTF-8');
$idSuffix = $standalone ? '-empty' : '';
$clear = '';
if ($keyword !== '') {
$clear = '清除';
}
return '
'
. ''
. ''
. $clear
. '
';
}
}
// 引入Action类
require_once __DIR__ . '/Action.php';
$action = new ChangeLog_Action();
// 获取当前页码
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$perPage = isset($config->perPage) ? intval($config->perPage) : 10;
if ($perPage < 1) {
$perPage = 10;
}
// 搜索关键词(2.0.10:后台列表按「内容」过滤,跨页生效)
// 长度限一下,避免超长串拼进 LIKE;% 和 _ 不转义,理由见 Action::getEntries()
$keyword = isset($_GET['q']) ? trim((string) $_GET['q']) : '';
if (mb_strlen($keyword, 'UTF-8') > 50) {
$keyword = mb_substr($keyword, 0, 50, 'UTF-8');
}
// 拼在分页 / 重定向链接后面的参数(带 & 前缀,空搜索时不加)
$keywordParam = $keyword === '' ? '' : '&q=' . urlencode($keyword);
// 本面板自己的地址:后台目录可能被改过(/admin/ 或别的),统一从当前请求取路径
$panelPath = strtok((string) $_SERVER['REQUEST_URI'], '?');
if (!$panelPath) {
$panelPath = (string) $_SERVER['PHP_SELF'];
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 处理新增 - 关键:处理完后不重定向,刷新当前页面
// 在manage-panel.php中修改发布处理部分(第29-38行):
// 处理新增
if (!empty($_POST['content'])) {
try {
$action->addEntry(array(
'content' => $_POST['content'],
'event_date' => changelogJoinDateTime(
isset($_POST['event_date']) ? $_POST['event_date'] : '',
isset($_POST['event_time']) ? $_POST['event_time'] : ''
),
'post_cids' => isset($_POST['post_cids']) ? $_POST['post_cids'] : ''
));
$successMsg = '记录添加成功!';
// 添加滚动到列表的锚点(新记录排在最前,固定回到第 1 页)
header('Location: ' . $_SERVER['PHP_SELF'] . '?panel=ChangeLog/manage-panel.php&page=1' . $keywordParam . '#cl-list-anchor');
exit;
} catch (Exception $e) {
// 走到这里是插入前的校验失败或插入本身失败,数据并没有写入,
// 因此必须按"错误"提示,不能再显示成"已添加但有警告"
$errorMsg = htmlspecialchars($e->getMessage());
}
}
// 处理批量删除
if (!empty($_POST['delete_ids']) && is_array($_POST['delete_ids'])) {
$action->deleteEntries($_POST['delete_ids']);
$successMsg = '删除成功!';
// 添加滚动到列表的锚点
header('Location: ' . $_SERVER['PHP_SELF'] . '?panel=ChangeLog/manage-panel.php&page=' . $page . $keywordParam . '#cl-list-anchor');
exit;
}
// 处理编辑
if (!empty($_POST['edit_id'])) {
$action->updateEntry(array(
'edit_id' => $_POST['edit_id'],
'edit_content' => $_POST['edit_content'],
'edit_event_date' => changelogJoinDateTime(
isset($_POST['edit_event_date']) ? $_POST['edit_event_date'] : '',
isset($_POST['edit_event_time']) ? $_POST['edit_event_time'] : ''
),
'edit_post_cids' => isset($_POST['edit_post_cids']) ? $_POST['edit_post_cids'] : ''
));
$successMsg = '记录更新成功!';
// 添加滚动到列表的锚点
header('Location: ' . $_SERVER['PHP_SELF'] . '?panel=ChangeLog/manage-panel.php&page=' . $page . $keywordParam . '#cl-list-anchor');
exit;
}
// 处理导出
if (isset($_POST['export'])) {
$exportContent = $action->exportData();
$filename = 'changelog_' . date('Ymd_His') . '.txt';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
}
// 处理导入
if (isset($_POST['import']) && !empty($_FILES['import_file']['tmp_name'])) {
$fileContent = file_get_contents($_FILES['import_file']['tmp_name']);
$result = $action->importData($fileContent);
if ($result['imported'] > 0) {
$successMsg = '导入成功!成功导入 ' . $result['imported'] . ' 条记录';
if ($result['failed'] > 0) {
$successMsg .= ',失败 ' . $result['failed'] . ' 条记录';
// 显示失败原因(限制显示前10条)
if (!empty($result['fail_reasons'])) {
$errorDetails = '失败原因:
';
$displayCount = min(10, count($result['fail_reasons']));
for ($i = 0; $i < $displayCount; $i++) {
$errorDetails .= htmlspecialchars($result['fail_reasons'][$i]) . '
';
}
if (count($result['fail_reasons']) > 10) {
$errorDetails .= '...还有 ' . (count($result['fail_reasons']) - 10) . ' 条失败记录';
}
$errorMsg = $errorDetails;
}
}
} else {
$errorMsg = '导入失败!未找到可导入的记录或格式不正确';
if (!empty($result['fail_reasons'])) {
$errorMsg .= '
失败原因:
';
$displayCount = min(10, count($result['fail_reasons']));
for ($i = 0; $i < $displayCount; $i++) {
$errorMsg .= htmlspecialchars($result['fail_reasons'][$i]) . '
';
}
}
}
}
}
// 获取记录
// 先数总数再查列表:搜索/删除后页码可能超界(比如停在第 5 页时搜到只有 1 页的关键词),
// 这里把页码夹回有效范围,避免看到「明明有记录却显示空列表」。
$total = $action->getTotalCount($keyword); // 搜索时为匹配条数,供分页用
$totalAll = $action->getTotalCount(); // 全库总数,统计卡片用(搜索时不该跟着变小)
$totalPages = max(1, (int) ceil($total / $perPage));
if ($page < 1) {
$page = 1;
} elseif ($page > $totalPages) {
$page = $totalPages;
}
$entries = $action->getEntries($page, $perPage, $keyword);
?>