Files
Collection/Manage.php

2171 lines
74 KiB
PHP
Raw Permalink Normal View History

2026-02-23 17:19:04 +08:00
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
// 定义获取排序方式文本的函数
function getSortOrderText($sortOrder, $collectionType) {
$sortText = '';
switch($sortOrder) {
case 'created_desc':
if ($collectionType == 'comment') {
$sortText = '评论时间倒序';
} elseif ($collectionType == 'user') {
$sortText = '注册时间倒序';
} else {
$sortText = '发布时间倒序';
}
break;
case 'created_asc':
if ($collectionType == 'comment') {
$sortText = '评论时间正序';
} elseif ($collectionType == 'user') {
$sortText = '注册时间正序';
} else {
$sortText = '发布时间正序';
}
break;
case 'title_asc':
$sortText = '标题正序';
break;
case 'title_desc':
$sortText = '标题倒序';
break;
case 'name_asc':
$sortText = '名称正序';
break;
case 'name_desc':
$sortText = '名称倒序';
break;
case 'comments_desc':
$sortText = '评论数倒序';
break;
case 'comments_asc':
$sortText = '评论数正序';
break;
case 'custom':
$sortText = '自定义';
break;
default:
$sortText = $sortOrder;
}
return $sortText;
}
include 'header.php';
include 'menu.php';
include 'common-js.php';
include 'table-js.php';
// 获取插件选项
$options = Typecho_Widget::widget('Widget_Options');
$pluginOptions = $options->plugin('Collection');
// 检查是否有搜索参数
$searchKeyword = isset($_GET['search']) ? trim($_GET['search']) : '';
$searchResults = array();
$searchPerformed = false;
$currentPage = isset($_GET['page']) ? intval($_GET['page']) : 1;
// 尝试获取合集数据
try {
$collections = Collection_Plugin::getAllCollections();
} catch (Exception $e) {
$collections = array();
}
?>
<style>
/* 内联样式补充 */
.typecho-table-wrap{padding:20px 0 0 !important;}
.collection-management {
padding: 20px;
}
.collection-list {
margin-top: 20px;
}
.typecho-list-table th, .typecho-list-table td {
vertical-align: middle !important;
}
.related-items-cell {
max-width: 200px;
position: relative;
}
.item-id-tag {
display: inline-block;
background: #e8f4fd;
color: #2c6aa0;
padding: 2px 6px;
border-radius: 3px;
font-size: 11px;
margin: 2px;
border: 1px solid #c2e0ff;
cursor: pointer;
position: relative;
transition: background-color 0.2s;
}
.item-id-tag:hover {
background: #d0e8ff;
}
.item-id-tag-tooltip {
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.9);
color: white;
padding: 8px 12px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
z-index: 1000;
display: none;
margin-bottom: 5px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
min-width: 180px;
text-align: center;
max-width: 300px;
word-wrap: break-word;
white-space: normal;
opacity: 0;
transition: opacity 0.2s, visibility 0.2s;
pointer-events: none;
visibility: hidden;
}
.item-id-tag-tooltip.show {
display: block;
opacity: 1;
visibility: visible;
}
.item-id-tag-tooltip::after {
content: '';
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
border-width: 5px;
border-style: solid;
border-color: rgba(0, 0, 0, 0.9) transparent transparent transparent;
}
.item-id-tag-tooltip-title {
font-weight: 600;
margin-bottom: 3px;
color: #fff;
}
.item-id-tag-tooltip-date {
font-size: 11px;
color: #ccc;
margin-bottom: 5px;
}
.item-id-tag-tooltip-content {
font-size: 11px;
color: #ddd;
margin-top: 5px;
max-height: 100px;
overflow-y: auto;
}
.action-cell {
white-space: nowrap;
}
.action-link {
color: #467b96;
text-decoration: none;
margin-right: 10px;
font-size: 13px;
cursor: pointer;
background: none;
border: none;
padding: 0;
}
.action-link:hover {
text-decoration: underline;
}
.action-link.delete {
color: #dc3545;
}
/* 模态框样式 - 修复居中问题 */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: none;
z-index: 10000;
align-items: center;
justify-content: center;
padding: 20px;
overflow-y: auto;
}
.modal-container {
background: white;
border-radius: 8px;
width: 90%;
max-width: 600px;
max-height: 85vh;
overflow: hidden;
display: none;
animation: modalFadeIn 0.3s ease;
margin: auto;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}
@keyframes modalFadeIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.modal-header {
padding: 20px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
background: #f8f9fa;
position: sticky;
top: 0;
z-index: 10;
}
.modal-title {
margin: 0;
font-size: 18px;
color: #333;
}
.modal-close {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #666;
line-height: 1;
padding: 0;
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
}
.modal-close:hover {
color: #333;
background: rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.modal-body {
padding: 20px;
overflow-y: auto;
max-height: calc(85vh - 140px);
}
.modal-footer {
padding: 15px 20px;
border-top: 1px solid #eee;
display: flex;
justify-content: flex-end;
gap: 10px;
background: #f8f9fa;
position: sticky;
bottom: 0;
z-index: 10;
}
.typecho-page-title{display:none!important;}
/* 按钮样式 */
.btn {
padding: 4px 16px;
border: 1px solid #ddd;
border-radius: 4px;
background: white;
color: #333;
cursor: pointer;
font-size: 14px;
transition: all 0.3s;
}
.btn-s {height:20px!important;}
.btn:hover {
background: #f5f5f5;
border-color: #ccc;
}
.btn.primary {
background: #467b96;
color: white;
border-color: #467b96;
}
.btn.primary:hover {
background: #3a6a83;
border-color: #3a6a83;
}
/* 表单样式 */
.form-group {
margin-bottom: 20px;
}
.form-label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #333;
font-size: 14px;
}
.form-label.required::after {
content: "*";
color: #dc3545;
margin-left: 4px;
}
.form-input {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
transition: border-color 0.3s;
}
.form-input:focus {
outline: none;
border-color: #467b96;
box-shadow: 0 0 0 3px rgba(70, 123, 150, 0.1);
}
.form-textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
min-height: 100px;
resize: vertical;
transition: border-color 0.3s;
}
.form-textarea:focus {
outline: none;
border-color: #467b96;
box-shadow: 0 0 0 3px rgba(70, 123, 150, 0.1);
}
.form-help {
color: #666;
font-size: 12px;
margin-top: 4px;
}
/* 合集类型选项 */
.collection-type-options {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
margin-top: 5px;
}
.collection-type-option {
padding: 10px;
border: 2px solid #ddd;
border-radius: 4px;
text-align: center;
cursor: pointer;
background: white;
transition: all 0.3s;
font-size: 13px;
}
.collection-type-option:hover {
border-color: #467b96;
}
.collection-type-option.active {
border-color: #467b96;
background: #e8f4fd;
color: #467b96;
}
/* 排序选项 */
.sort-options {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
margin-top: 5px;
}
.sort-option {
padding: 8px 10px;
border: 2px solid #ddd;
border-radius: 4px;
text-align: center;
cursor: pointer;
background: white;
transition: all 0.3s;
font-size: 12px;
line-height: 1.3;
}
.sort-option:hover {
border-color: #467b96;
}
.sort-option.active {
border-color: #467b96;
background: #e8f4fd;
color: #467b96;
}
/* 关联内容区域 */
.related-section {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
border: 1px solid #e9ecef;
}
.related-input-group {
display: flex;
gap: 10px;
margin-bottom: 15px;
}
.related-input {
flex: 1;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
.related-button {
padding: 8px 20px;
background: #467b96;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
white-space: nowrap;
}
.related-button:hover {
background: #3a6a83;
}
.items-list {
max-height: 200px;
overflow-y: auto;
border: 1px solid #e9ecef;
border-radius: 4px;
background: white;
min-height: 50px;
}
.item-item {
padding: 10px 15px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: flex-start;
}
.item-item:last-child {
border-bottom: none;
}
.item-info {
flex: 1;
min-width: 0;
margin-right: 10px;
}
.item-title {
font-weight: 500;
color: #333;
margin-bottom: 2px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 13px;
}
.item-author {
color: #666;
font-size: 11px;
margin-bottom: 2px;
}
.item-content {
color: #888;
font-size: 11px;
max-height: 60px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
margin: 3px 0;
}
.item-id {
color: #999;
font-size: 10px;
}
.item-remove {
color: #dc3545;
background: none;
border: 1px solid #dc3545;
cursor: pointer;
font-size: 12px;
padding: 4px 8px;
border-radius: 3px;
transition: background-color 0.2s;
flex-shrink: 0;
margin-top: 5px;
}
.item-remove:hover {
background: #f8d7da;
}
/* 消息提示 */
.alert {
padding: 12px 15px;
border-radius: 4px;
margin-bottom: 15px;
display: none;
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 99999;
max-width: 90%;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.alert.success {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
}
.alert.error {
background: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
}
.alert.warning {
background: #fff3cd;
border: 1px solid #ffeaa7;
color: #856404;
}
/* 加载动画 */
.loading-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.8);
display: none;
align-items: center;
justify-content: center;
z-index: 99999;
}
.loading-spinner {
width: 40px;
height: 40px;
border: 3px solid #f3f3f3;
border-top: 3px solid #467b96;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* 操作栏布局修复 */
.typecho-list-operate {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.typecho-list-operate .operate {
display: flex;
align-items: center;
gap: 10px;
flex: 1;
}
.typecho-list-operate .btn-group {
margin-bottom: 0;
}
.typecho-list-operate .dropdown-menu {
margin-top: 0;
}
/* 类型筛选和搜索框容器 */
.filter-search-container {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
justify-content: flex-start;
flex: 1;
}
/* 类型筛选 */
.type-filter {
display: inline-flex;
align-items: center;
gap: 5px;
margin-left: 0;
}
.type-filter select {
padding: 6px 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 13px;
background: white;
min-width: 100px;
}
/* 搜索框 */
.search-container {
display: flex;
gap: 10px;
align-items: center;
margin-left: 0;
}
.search-input {
padding: 6px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 13px;
width: 200px;
min-width: 150px;
}
.search-button {
padding: 6px 15px;
background: #467b96;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
white-space: nowrap;
}
.search-button:hover {
background: #3a6a83;
}
.search-clear {
padding: 6px 15px;
background: #6c757d;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
text-decoration: none;
white-space: nowrap;
}
.search-clear:hover {
background: #5a6268;
text-decoration: none;
color: white;
}
/* 新增按钮 */
.add-btn-container {
flex-shrink: 0;
}
/* 选中项下拉菜单样式修复 */
.btn-drop {
margin: 0 !important;
}
.dropdown-toggle.btn-s {
padding: 4px 8px !important;
line-height: 1.5 !important;
height: auto !important;
margin: 0 !important;
}
/* 合集类型标签 */
.collection-type-tag {
display: inline-block;
padding: 2px 6px;
border-radius: 3px;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
}
.collection-type-tag.article {
background: #e8f4fd;
color: #2c6aa0;
}
.collection-type-tag.comment {
background: #f0f7e6;
color: #5b8c00;
}
.collection-type-tag.user {
background: #f9f0ff;
color: #8a2be2;
}
/* 响应式设计 */
@media (max-width: 992px) {
.collection-type-options {
grid-template-columns: repeat(2, 1fr);
}
.sort-options {
grid-template-columns: repeat(2, 1fr);
}
.related-input-group {
flex-direction: column;
}
.modal-container {
width: 95%;
margin: 10px;
}
.typecho-list-operate {
flex-direction: column;
align-items: stretch;
}
.filter-search-container {
flex-direction: column;
align-items: stretch;
}
.search-input {
width: 100%;
}
.type-filter {
width: 100%;
}
.type-filter select {
width: 100%;
}
}
@media (max-width: 768px) {
.collection-type-options {
grid-template-columns: 1fr;
}
.sort-options {
grid-template-columns: 1fr;
}
.search-container {
flex-direction: column;
width: 100%;
}
.search-input {
width: 100%;
}
}
@media (max-width: 480px) {
.item-list-info {
flex-direction: column;
align-items: flex-start;
}
.item-list-date {
margin-left: 0;
margin-top: 5px;
}
.collection-card {
padding: 15px;
}
}
</style>
<div class="main">
<div class="body container">
<?php include 'page-title.php'; ?>
<div class="row typecho-page-main" role="main">
<div class="col-mb-12 typecho-list">
<div class="typecho-list-operate clearfix">
<form method="get" class="operate-form-left">
<div class="operate">
<label><i class="sr-only"><?php _e('全选'); ?></i><input type="checkbox" class="typecho-table-select-all"/></label>
<div class="btn-group btn-drop">
<button class="btn dropdown-toggle btn-s" type="button"><i
class="sr-only"><?php _e('操作'); ?></i><?php _e('选中项'); ?> <i
class="i-caret-down"></i></button>
<ul class="dropdown-menu">
<li>
<a href="javascript:void(0);" id="batch-delete-btn"><?php _e('删除'); ?></a>
</li>
</ul>
</div>
</div>
</form>
<!-- 类型筛选和搜索框 -->
<div class="filter-search-container">
<!-- 类型筛选 -->
<div class="type-filter">
<select id="collection-type-filter">
<option value="">全部类型</option>
<option value="article">文章集</option>
<option value="comment">评论集</option>
<option value="user">用户集</option>
</select>
</div>
<!-- 搜索框 -->
<div class="search-container <?php echo $searchKeyword ? 'has-search' : ''; ?>">
<form method="get" action="" style="display: flex; align-items: center; gap: 10px;">
<input type="text" name="search" class="search-input"
placeholder="<?php _e('搜索合集名称...'); ?>"
value="<?php echo htmlspecialchars($searchKeyword); ?>"
autocomplete="off">
<input type="hidden" name="panel" value="Collection/Manage.php">
<button type="submit" class="search-button"><?php _e('搜索'); ?></button>
<?php if ($searchKeyword): ?>
<a href="<?php echo \Typecho\Common::url('extending.php?panel=Collection/Manage.php', $options->adminUrl); ?>"
class="search-clear"><?php _e('清除'); ?></a>
<?php endif; ?>
</form>
</div>
</div>
<!-- 新增按钮 -->
<div class="add-btn-container">
<a class="btn primary btn-s btn-operate" href="javascript:void(0);" id="add-collection-btn"><?php _e('新增合集'); ?></a>
</div>
</div><!-- end .typecho-list-operate -->
<?php if ($searchKeyword): ?>
<div class="alert info">
<p><?php _e('搜索关键词:'); ?><strong>"<?php echo htmlspecialchars($searchKeyword); ?>"</strong></p>
<a href="<?php echo \Typecho\Common::url('extending.php?panel=Collection/Manage.php', $options->adminUrl); ?>">
<?php _e('返回全部列表'); ?>
</a>
</div>
<?php endif; ?>
<form method="post" name="manage_collections" class="operate-form">
<div class="typecho-table-wrap">
<table class="typecho-list-table">
<colgroup>
<col width="3%" class="kit-hidden-mb"/>
<col width="5%"/>
<col width="10%"/>
<col width="10%"/>
<col width="25%"/>
<col width="25%"/>
<col width="12%"/>
<col width="10%"/>
</colgroup>
<thead>
<tr>
<th class="kit-hidden-mb"></th>
<th><?php _e('ID'); ?></th>
<th><?php _e('类型'); ?></th>
<th><?php _e('名称'); ?></th>
<th><?php _e('简介'); ?></th>
<th><?php _e('关联内容'); ?></th>
<th><?php _e('排序方式'); ?></th>
<th><?php _e('操作'); ?></th>
</tr>
</thead>
<tbody id="collection-list">
<?php
// 过滤搜索结果
$filteredCollections = $collections;
if ($searchKeyword) {
$filteredCollections = array_filter($collections, function($collection) use ($searchKeyword) {
return stripos($collection['name'], $searchKeyword) !== false ||
stripos($collection['description'], $searchKeyword) !== false;
});
$searchPerformed = true;
}
// 分页
$pageSize = 10;
$total = count($filteredCollections);
$totalPages = ceil($total / $pageSize);
$offset = ($currentPage - 1) * $pageSize;
$pagedCollections = array_slice($filteredCollections, $offset, $pageSize);
if (empty($pagedCollections)) {
if ($searchKeyword) {
echo '<tr><td colspan="8" style="text-align: center;"><h6 class="typecho-list-table-title">' . sprintf(_t('没有找到与"%s"相关的合集'), htmlspecialchars($searchKeyword)) . '</h6></td></tr>';
} else {
echo '<tr><td colspan="8" style="text-align: center;"><h6 class="typecho-list-table-title">' . _t('没有任何合集') . '</h6></td></tr>';
}
} else {
foreach ($pagedCollections as $collection) {
echo '<tr id="collection-' . $collection['id'] . '">';
echo '<td class="kit-hidden-mb"><input type="checkbox" value="' . $collection['id'] . '" name="collection[]"/></td>';
echo '<td>' . $collection['id'] . '</td>';
// 合集类型
echo '<td>';
$typeText = '';
$typeClass = '';
if ($collection['collection_type'] == 'article') {
$typeText = '文章';
$typeClass = 'article';
} else if ($collection['collection_type'] == 'comment') {
$typeText = '评论';
$typeClass = 'comment';
} else if ($collection['collection_type'] == 'user') {
$typeText = '用户';
$typeClass = 'user';
} else {
$typeText = $collection['collection_type'];
}
echo '<span class="collection-type-tag ' . $typeClass . '">' . $typeText . '</span>';
echo '</td>';
echo '<td>' . htmlspecialchars($collection['name'] ?: '-') . '</td>';
echo '<td>' . htmlspecialchars($collection['description'] ? substr($collection['description'], 0, 100) . (strlen($collection['description']) > 100 ? '...' : '') : '-') . '</td>';
// 关联内容显示
echo '<td class="related-items-cell">';
if (!empty($collection['related_items'])) {
$relatedItems = explode(',', $collection['related_items']);
$displayCount = 0;
foreach ($relatedItems as $itemId) {
$itemId = trim($itemId);
if ($itemId) {
$dataType = $collection['collection_type'];
echo '<span class="item-id-tag" data-id="' . $itemId . '" data-type="' . $dataType . '">' . $itemId . '</span>';
$displayCount++;
if ($displayCount >= 3) {
echo '...';
break;
}
}
}
} else {
echo '-';
}
echo '</td>';
// 排序方式显示
echo '<td>';
$sortText = getSortOrderText($collection['sort_order'], $collection['collection_type']);
echo $sortText;
echo '</td>';
echo '<td class="action-cell">';
echo '<a href="javascript:void(0);" class="action-link edit-collection" data-id="' . $collection['id'] . '">' . _t('编辑') . '</a>';
echo '<span></span>';
echo '<a href="javascript:void(0);" class="action-link delete delete-collection" data-id="' . $collection['id'] . '">' . _t('删除') . '</a>';
echo '</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
</form><!-- end .operate-form -->
<?php if ($totalPages > 1): ?>
<div class="typecho-list-operate clearfix">
<ul class="typecho-pager">
<li>
<?php if ($currentPage > 1): ?>
<a href="<?php echo \Typecho\Common::url('extending.php?panel=Collection/Manage.php&page=' . ($currentPage - 1) . ($searchKeyword ? '&search=' . urlencode($searchKeyword) : ''), $options->adminUrl); ?>">
<?php _e('上一页'); ?>
</a>
<?php else: ?>
<span class="current"><?php _e('上一页'); ?></span>
<?php endif; ?>
</li>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<li>
<?php if ($i === $currentPage): ?>
<span class="current"><?php echo $i; ?></span>
<?php else: ?>
<a href="<?php echo \Typecho\Common::url('extending.php?panel=Collection/Manage.php&page=' . $i . ($searchKeyword ? '&search=' . urlencode($searchKeyword) : ''), $options->adminUrl); ?>">
<?php echo $i; ?>
</a>
<?php endif; ?>
</li>
<?php endfor; ?>
<li>
<?php if ($currentPage < $totalPages): ?>
<a href="<?php echo \Typecho\Common::url('extending.php?panel=Collection/Manage.php&page=' . ($currentPage + 1) . ($searchKeyword ? '&search=' . urlencode($searchKeyword) : ''), $options->adminUrl); ?>">
<?php _e('下一页'); ?></a>
<?php else: ?>
<span class="current"><?php _e('下一页'); ?></span>
<?php endif; ?>
</li>
</ul>
</div>
<?php endif; ?>
</div><!-- end .typecho-list -->
</div><!-- end .typecho-page-main -->
</div>
</div>
<!-- 模态框 -->
<div class="modal-overlay" id="modal-overlay">
<div class="modal-container" id="modal-container">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">新增合集</h3>
<button type="button" class="modal-close" id="modal-close">&times;</button>
</div>
<div class="modal-body">
<form id="collection-form" method="post" enctype="multipart/form-data">
<!-- 合集类型 -->
<div class="form-group">
<label class="form-label required">合集类型</label>
<div class="collection-type-options" id="collection-type-options">
<button type="button" class="collection-type-option" data-value="article">文章集</button>
<button type="button" class="collection-type-option" data-value="comment">评论集</button>
<button type="button" class="collection-type-option" data-value="user">用户集</button>
</div>
<input type="hidden" id="collection-type" name="collection_type" value="article">
</div>
<!-- 基本设置 -->
<div class="form-group">
<label class="form-label required" for="collection-name">合集名称</label>
<input type="text" id="collection-name" name="name" class="form-input"
placeholder="例如:技术教程系列" required>
</div>
<div class="form-group">
<label class="form-label" for="collection-description">合集简介</label>
<textarea id="collection-description" name="description" class="form-textarea"
placeholder="关于这个合集的简要描述..."></textarea>
</div>
<!-- 排序设置 -->
<div class="form-group">
<label class="form-label">内容排序方式</label>
<div class="sort-options" id="sort-options">
<!-- 通用选项 -->
<button type="button" class="sort-option" data-value="custom">自定义顺序</button>
<button type="button" class="sort-option" data-value="title_asc">标题正序</button>
<button type="button" class="sort-option" data-value="title_desc">标题倒序</button>
<!-- 文章特定选项 -->
<button type="button" class="sort-option article-only" data-value="created_desc">发布时间倒序</button>
<button type="button" class="sort-option article-only" data-value="created_asc">发布时间正序</button>
<!-- 评论特定选项 -->
<button type="button" class="sort-option comment-only" data-value="created_desc">评论时间倒序</button>
<button type="button" class="sort-option comment-only" data-value="created_asc">评论时间正序</button>
<!-- 用户特定选项 -->
<button type="button" class="sort-option user-only" data-value="created_desc">注册时间倒序</button>
<button type="button" class="sort-option user-only" data-value="created_asc">注册时间正序</button>
<button type="button" class="sort-option user-only" data-value="name_asc">名称正序</button>
<button type="button" class="sort-option user-only" data-value="name_desc">名称倒序</button>
<button type="button" class="sort-option user-only" data-value="comments_desc">评论数倒序</button>
<button type="button" class="sort-option user-only" data-value="comments_asc">评论数正序</button>
</div>
<input type="hidden" id="collection-sort-order" name="sort_order" value="created_desc">
</div>
<!-- 关联内容 -->
<div class="related-section">
<label class="form-label">关联内容</label>
<div class="form-help" id="related-help">
多个文章ID用逗号分隔1,2,3。在输入框中直接编辑CID列表然后点击保存。
</div>
<div class="related-input-group">
<input type="text" id="related-items-input"
class="related-input"
placeholder="输入文章CID多个用逗号分隔1,2,3">
<button type="button" id="save-related-items-btn" class="related-button">
保存
</button>
</div>
<div class="items-list" id="related-items-list">
<div class="item-item empty">
<div class="item-info">
<div class="item-title">暂无关联内容</div>
</div>
</div>
</div>
<input type="hidden" id="related_items" name="related_items" value="">
</div>
<input type="hidden" id="collection-id" name="id" value="">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn" id="modal-cancel">取消</button>
<button type="button" class="btn primary" id="modal-save">保存合集</button>
</div>
</div>
</div>
<!-- 消息提示 -->
<div class="alert" id="message-alert" style="display: none;"></div>
<!-- 加载动画 -->
<div class="loading-overlay" id="loading-overlay">
<div class="loading-spinner"></div>
</div>
<script>
// 设置插件配置 - 使用正确的URL
var CollectionConfig = {
actionUrl: '<?php echo Typecho_Common::url('/action/collection', $options->index); ?>'
};
// 使用立即执行函数确保代码安全执行
(function($) {
'use strict';
// 全局变量
var relatedItems = [];
var isEditMode = false;
var itemTooltips = {}; // 存储内容提示信息
var hoverTimeout = null; // 悬停超时控制
// 初始化函数
function initCollection() {
console.log('Collection插件管理页面初始化');
console.log('Action URL:', CollectionConfig.actionUrl);
// 绑定事件
$('#add-collection-btn').on('click', function(e) {
e.preventDefault();
console.log('点击新增合集按钮');
showCollectionModal();
});
$('#modal-save').on('click', function(e) {
e.preventDefault();
console.log('点击保存按钮');
saveCollection();
});
$('#modal-close, #modal-cancel').on('click', function(e) {
e.preventDefault();
hideModal();
});
$('#modal-overlay').on('click', function(e) {
if (e.target === this) {
hideModal();
}
});
$('#batch-delete-btn').on('click', function(e) {
e.preventDefault();
console.log('点击批量删除按钮');
var selectedIds = [];
$('input[name="collection[]"]:checked').each(function() {
selectedIds.push($(this).val());
});
if (selectedIds.length === 0) {
showMessage('请选择要删除的合集', 'warning');
return;
}
if (confirm('你确认要删除选中的 ' + selectedIds.length + ' 个合集吗?')) {
showLoading();
// 批量删除逻辑收集所有选中的ID一次性发送到服务器
$.ajax({
url: CollectionConfig.actionUrl,
type: 'POST',
data: {
do: 'batchDelete',
collection: selectedIds
},
dataType: 'json',
success: function(response) {
hideLoading();
console.log('批量删除响应:', response);
if (response.success) {
showMessage('成功删除 ' + selectedIds.length + ' 个合集', 'success');
setTimeout(function() {
window.location.reload();
}, 1500);
} else {
showMessage(response.message, 'error');
}
},
error: function(xhr, status, error) {
hideLoading();
console.error('批量删除请求失败:', status, error);
showMessage('批量删除失败:' + (error || '网络错误'), 'error');
}
});
}
});
// 保存关联内容按钮点击事件
$('#save-related-items-btn').on('click', function(e) {
e.preventDefault();
saveRelatedItems();
});
// 关联内容输入框回车事件
$('#related-items-input').on('keypress', function(e) {
if (e.which === 13) {
e.preventDefault();
saveRelatedItems();
}
});
// 使用更可靠的绑定方式绑定编辑和删除按钮
bindEditDeleteEvents();
// 监听表格变化,为新增的按钮绑定事件
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
bindEditDeleteEvents();
initItemIdTooltips();
}
});
});
var tbody = document.getElementById('collection-list');
if (tbody) {
observer.observe(tbody, { childList: true, subtree: true });
}
initItemIdTooltips();
initSortOptions();
initCollectionTypeOptions();
// 测试API连接
testApiConnection();
// 初始化表格选中功能
initTableSelection();
// 类型筛选
$('#collection-type-filter').on('change', function() {
filterCollectionsByType($(this).val());
});
}
// 绑定编辑和删除按钮事件 - 参考可靠方式
function bindEditDeleteEvents() {
console.log('绑定编辑和删除按钮事件');
// 移除旧的绑定,防止重复绑定
$('.edit-collection').off('click.edit').on('click.edit', function(e) {
e.preventDefault();
e.stopPropagation();
var collectionId = $(this).data('id');
console.log('点击编辑按钮ID:', collectionId);
editCollection(collectionId);
});
$('.delete-collection').off('click.delete').on('click.delete', function(e) {
e.preventDefault();
e.stopPropagation();
var collectionId = $(this).data('id');
console.log('点击删除按钮ID:', collectionId);
deleteCollection(collectionId);
});
}
// 初始化内容ID标签的悬停提示优化版本
function initItemIdTooltips() {
$(document).on('mouseenter', '.item-id-tag', function(e) {
var $tag = $(this);
var itemId = $tag.data('id');
var itemType = $tag.data('type');
if (!itemId || !itemType) return;
// 清除之前的超时
if (hoverTimeout) {
clearTimeout(hoverTimeout);
}
// 设置新的超时
hoverTimeout = setTimeout(function() {
var cacheKey = itemType + '_' + itemId;
// 如果已经有缓存的数据,直接显示
if (itemTooltips[cacheKey]) {
showItemTooltip($tag, itemTooltips[cacheKey], itemType);
return;
}
// 否则获取内容信息
var apiDo = '';
var paramName = '';
if (itemType === 'comment') {
apiDo = 'getCommentInfo';
paramName = 'comment_coid';
} else if (itemType === 'user') {
apiDo = 'getUserInfo';
paramName = 'user_uid';
} else {
apiDo = 'getArticleInfo';
paramName = 'article_cid';
}
$.ajax({
url: CollectionConfig.actionUrl,
type: 'GET',
data: {do: apiDo, [paramName]: itemId},
dataType: 'json',
success: function(response) {
if (response.success && response.data) {
var itemInfo = {
title: response.data.title || response.data.author || response.data.name || '无标题',
created: response.data.created ? new Date(response.data.created * 1000).toLocaleDateString() : '未知日期',
content: response.data.text || response.data.content || response.data.url || '无内容',
link: response.data.link || response.data.url || ''
};
// 缓存数据
itemTooltips[cacheKey] = itemInfo;
// 检查鼠标是否还在元素上
if ($tag.is(':hover')) {
showItemTooltip($tag, itemInfo, itemType);
}
}
}
});
}, 200); // 200ms延迟避免频繁触发
});
$(document).on('mouseleave', '.item-id-tag', function() {
// 清除悬停超时
if (hoverTimeout) {
clearTimeout(hoverTimeout);
hoverTimeout = null;
}
// 立即隐藏提示
var $tag = $(this);
hideItemTooltip($tag);
});
}
// 显示内容提示(优化版本)
function showItemTooltip($tag, itemInfo, itemType) {
// 移除旧的提示
$tag.find('.item-id-tag-tooltip').remove();
// 创建新的提示
var tooltipHtml = '<div class="item-id-tag-tooltip">';
if (itemType === 'comment') {
tooltipHtml += '<div class="item-id-tag-tooltip-title">' + //= '<div class="item-id-tag-tooltip-title">评论作者:' +
(itemInfo.title.length > 30 ? itemInfo.title.substring(0, 30) + '...' : itemInfo.title) +
'</div>';
} else if (itemType === 'user') {
tooltipHtml += '<div class="item-id-tag-tooltip-title">' + //= '<div class="item-id-tag-tooltip-title">用户:' +
(itemInfo.title.length > 30 ? itemInfo.title.substring(0, 30) + '...' : itemInfo.title) +
'</div>';
} else {
tooltipHtml += '<div class="item-id-tag-tooltip-title">' +
(itemInfo.title.length > 30 ? itemInfo.title.substring(0, 30) + '...' : itemInfo.title) +
'</div>';
}
if (itemInfo.created) {
var dateLabel = itemType === 'comment' ? '' : //'comment' ? '评论时间' :
itemType === 'user' ? '' : '';// 'user' ? '注册时间' : '发布时间';
tooltipHtml += '<div class="item-id-tag-tooltip-date">' + dateLabel + '' + itemInfo.created + '</div>';
}//'<div class="item-id-tag-tooltip-date">' + dateLabel + '' + itemInfo.created + '</div>'
// 显示部分内容
if (itemInfo.content && itemType !== 'user') {
var content = itemInfo.content.replace(/<[^>]*>/g, '');
if (content.length > 100) {
content =''; //默认content.substring(0, 100) + '...';
}
tooltipHtml += '<div class="item-id-tag-tooltip-content">' + content + '</div>';
} else if (itemType === 'user' && itemInfo.content) {
tooltipHtml += '<div class="item-id-tag-tooltip-content">网站:' + itemInfo.content + '</div>';
}
tooltipHtml += '</div>';
$tag.append(tooltipHtml);
var $tooltip = $tag.find('.item-id-tag-tooltip');
$tooltip.addClass('show');
}
// 隐藏内容提示(优化版本)
function hideItemTooltip($tag) {
var $tooltip = $tag.find('.item-id-tag-tooltip');
if ($tooltip.length) {
$tooltip.removeClass('show');
// 短暂延迟后移除元素,确保动画完成
setTimeout(function() {
if (!$tag.is(':hover')) {
$tooltip.remove();
}
}, 100);
}
}
// 初始化表格选中功能
function initTableSelection() {
// 全选/取消全选
$('.typecho-table-select-all').on('change', function() {
var isChecked = $(this).prop('checked');
$('input[name="collection[]"]').prop('checked', isChecked).trigger('change');
});
// 单个选择时更新全选框状态
$(document).on('change', 'input[name="collection[]"]', function() {
var total = $('input[name="collection[]"]').length;
var checked = $('input[name="collection[]"]:checked').length;
$('.typecho-table-select-all').prop('checked', total === checked);
});
}
// 类型筛选
function filterCollectionsByType(type) {
if (!type) {
$('tbody tr').show();
return;
}
$('tbody tr').each(function() {
var rowType = $(this).find('.collection-type-tag').hasClass('article') ? 'article' :
$(this).find('.collection-type-tag').hasClass('comment') ? 'comment' :
$(this).find('.collection-type-tag').hasClass('user') ? 'user' : '';
if (rowType === type) {
$(this).show();
} else {
$(this).hide();
}
});
}
// 测试API连接
function testApiConnection() {
$.ajax({
url: CollectionConfig.actionUrl,
type: 'GET',
data: {do: 'test'},
dataType: 'json',
success: function(response) {
console.log('API连接测试成功:', response);
},
error: function(xhr, status, error) {
console.error('API连接测试失败:', status, error);
console.log('响应内容:', xhr.responseText);
}
});
}
// 初始化合集类型选项
function initCollectionTypeOptions() {
$('.collection-type-option').on('click', function() {
var typeValue = $(this).data('value');
$('.collection-type-option').removeClass('active');
$(this).addClass('active');
$('#collection-type').val(typeValue);
// 更新排序选项显示
updateSortOptionsVisibility(typeValue);
// 更新帮助文本和输入框提示
var helpText = $('#related-help');
var inputPlaceholder = $('#related-items-input');
if (typeValue === 'comment') {
helpText.text('多个评论ID用逗号分隔1,2,3。在输入框中直接编辑COID列表然后点击保存。');
inputPlaceholder.attr('placeholder', '输入评论COID多个用逗号分隔1,2,3');
// 设置评论默认排序
$('.sort-option').removeClass('active');
$('.sort-option[data-value="created_desc"].comment-only').addClass('active');
$('#collection-sort-order').val('created_desc');
} else if (typeValue === 'user') {
helpText.text('多个用户ID用逗号分隔1,2,3。在输入框中直接编辑UID列表然后点击保存。');
inputPlaceholder.attr('placeholder', '输入用户UID多个用逗号分隔1,2,3');
// 设置用户默认排序
$('.sort-option').removeClass('active');
$('.sort-option[data-value="created_desc"].user-only').addClass('active');
$('#collection-sort-order').val('created_desc');
} else {
helpText.text('多个文章ID用逗号分隔1,2,3。在输入框中直接编辑CID列表然后点击保存。');
inputPlaceholder.attr('placeholder', '输入文章CID多个用逗号分隔1,2,3');
// 设置文章默认排序
$('.sort-option').removeClass('active');
$('.sort-option[data-value="created_desc"].article-only').addClass('active');
$('#collection-sort-order').val('created_desc');
}
// 清空现有关联内容
relatedItems = [];
$('#related_items').val('');
renderRelatedItemsList();
});
}
// 更新排序选项可见性
function updateSortOptionsVisibility(collectionType) {
// 隐藏所有特定类型的选项
$('.sort-option').hide();
// 显示通用选项
$('.sort-option[data-value="custom"]').show();
$('.sort-option[data-value="title_asc"]').show();
$('.sort-option[data-value="title_desc"]').show();
// 显示特定类型的选项
if (collectionType === 'article') {
$('.sort-option.article-only').show();
// 标题排序对文章有效
$('.sort-option[data-value="title_asc"]').show();
$('.sort-option[data-value="title_desc"]').show();
} else if (collectionType === 'comment') {
$('.sort-option.comment-only').show();
// 评论不需要标题排序,隐藏它们
$('.sort-option[data-value="title_asc"]').hide();
$('.sort-option[data-value="title_desc"]').hide();
} else if (collectionType === 'user') {
$('.sort-option.user-only').show();
// 用户使用名称排序
$('.sort-option[data-value="title_asc"]').hide();
$('.sort-option[data-value="title_desc"]').hide();
}
}
// 初始化排序选项
function initSortOptions() {
// 先设置所有排序选项不可见
updateSortOptionsVisibility('article');
// 绑定点击事件
$(document).on('click', '.sort-option:visible', function() {
var sortValue = $(this).data('value');
$('.sort-option').removeClass('active');
$(this).addClass('active');
$('#collection-sort-order').val(sortValue);
});
}
// 保存关联内容
function saveRelatedItems() {
var itemsInput = $('#related-items-input');
var inputValue = itemsInput.val().trim();
var collectionType = $('#collection-type').val();
if (!inputValue) {
// 清空关联内容
relatedItems = [];
$('#related_items').val('');
renderRelatedItemsList();
showMessage('已清空关联内容', 'success');
return;
}
// 解析逗号分隔的ID列表
var idArray = inputValue.split(',').map(function(id) {
return id.trim();
}).filter(function(id) {
return id !== '';
});
// 验证ID格式
var validIds = [];
var invalidIds = [];
idArray.forEach(function(id) {
if (!isNaN(id) && id !== '') {
validIds.push(id);
} else if (id !== '') {
invalidIds.push(id);
}
});
if (validIds.length === 0 && invalidIds.length > 0) {
showMessage('请输入有效的ID数字', 'error');
return;
}
if (invalidIds.length > 0) {
showMessage('以下ID格式无效已忽略' + invalidIds.join(', '), 'warning');
}
// 去重
var uniqueIds = [];
validIds.forEach(function(id) {
if (uniqueIds.indexOf(id) === -1) {
uniqueIds.push(id);
}
});
// 验证内容是否存在
showLoading();
// 批量验证内容存在性
var promises = uniqueIds.map(function(id) {
return new Promise(function(resolve) {
var apiDo = '';
var paramName = '';
if (collectionType === 'comment') {
apiDo = 'getCommentInfo';
paramName = 'comment_coid';
} else if (collectionType === 'user') {
apiDo = 'getUserInfo';
paramName = 'user_uid';
} else {
apiDo = 'getArticleInfo';
paramName = 'article_cid';
}
$.ajax({
url: CollectionConfig.actionUrl,
type: 'GET',
data: {do: apiDo, [paramName]: id},
dataType: 'json',
success: function(response) {
resolve({
id: id,
success: response.success,
data: response.data,
message: response.message
});
},
error: function() {
resolve({
id: id,
success: false,
error: '请求失败'
});
}
});
});
});
Promise.all(promises).then(function(results) {
hideLoading();
var validItems = [];
var invalidItems = [];
results.forEach(function(result) {
if (result.success && result.data) {
if (collectionType === 'comment') {
if (result.data.author) {
validItems.push(result.id);
} else {
invalidItems.push(result.id);
}
} else if (collectionType === 'user') {
if (result.data.name) {
validItems.push(result.id);
} else {
invalidItems.push(result.id);
}
} else {
if (result.data.title) {
validItems.push(result.id);
} else {
invalidItems.push(result.id);
}
}
} else {
invalidItems.push(result.id);
}
});
// 更新关联内容
relatedItems = validItems;
$('#related_items').val(relatedItems.join(','));
// 更新输入框显示有效的ID
itemsInput.val(validItems.join(','));
// 渲染内容列表
renderRelatedItemsList();
// 显示结果消息
if (validItems.length > 0) {
var typeText = collectionType === 'comment' ? '评论' :
collectionType === 'user' ? '用户' : '文章';
var message = '成功保存 ' + validItems.length + ' 个' + typeText;
if (invalidItems.length > 0) {
message += '以下ID无效或内容不存在' + invalidItems.join(', ');
showMessage(message, 'warning');
} else {
showMessage(message, 'success');
}
} else {
var typeText = collectionType === 'comment' ? '评论' :
collectionType === 'user' ? '用户' : '文章';
showMessage('所有ID都无效或' + typeText + '不存在', 'error');
}
}).catch(function(error) {
hideLoading();
console.error('验证内容信息失败:', error);
showMessage('验证内容信息失败,请检查网络连接', 'error');
});
}
// 移除单个关联内容
function removeRelatedItem(id) {
console.log('正在移除关联内容ID:', id);
var index = relatedItems.indexOf(id);
if (index !== -1) {
relatedItems.splice(index, 1);
$('#related_items').val(relatedItems.join(','));
// 更新输入框
$('#related-items-input').val(relatedItems.join(','));
renderRelatedItemsList();
showMessage('已移除内容 ID: ' + id, 'success');
}
}
// 渲染关联内容列表
function renderRelatedItemsList() {
var listContainer = $('#related-items-list');
var collectionType = $('#collection-type').val();
if (relatedItems.length === 0) {
listContainer.html('<div class="item-item empty"><div class="item-info"><div class="item-title">暂无关联内容</div></div></div>');
return;
}
var html = '';
relatedItems.forEach(function(id) {
html += '<div class="item-item" id="item-item-' + id + '">';
html += '<div class="item-info">';
html += '<div class="item-title">正在获取内容信息...</div>';
html += '<div class="item-id">ID: ' + id + '</div>';
html += '</div>';
html += '<!--<button type="button" class="item-remove" data-id="' + id + '">移除</button>-->';
html += '</div>';
});
listContainer.html(html);
// 绑定移除按钮事件
$('.item-remove').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var id = $(this).data('id');
removeRelatedItem(id);
});
// 异步获取内容信息
relatedItems.forEach(function(id) {
var apiDo = '';
var paramName = '';
if (collectionType === 'comment') {
apiDo = 'getCommentInfo';
paramName = 'comment_coid';
} else if (collectionType === 'user') {
apiDo = 'getUserInfo';
paramName = 'user_uid';
} else {
apiDo = 'getArticleInfo';
paramName = 'article_cid';
}
$.ajax({
url: CollectionConfig.actionUrl,
type: 'GET',
data: {do: apiDo, [paramName]: id},
dataType: 'json',
success: function(response) {
var item = $('#item-item-' + id);
if (item.length && response && response.success && response.data) {
if (collectionType === 'comment') {
var author = response.data.author || '匿名';
var content = response.data.text || response.data.content || '';
// 清理HTML标签
content = content.replace(/<[^>]*>/g, '');
if (content.length > 50) {
content = content.substring(0, 50) + '...';
}
item.find('.item-title').text('评论者:' + author);
item.find('.item-author').remove();
if (content) {
if (!item.find('.item-content').length) {
item.find('.item-info').append('<div class="item-content">' + content + '</div>');
} else {
item.find('.item-content').text(content);
}
}
} else if (collectionType === 'user') {
var name = response.data.name || '匿名用户';
var url = response.data.url || '';
var commentCount = response.data.commentCount || 0;
var recentActivity = response.data.recentActivity || '';
item.find('.item-title').text('用户:' + name);
item.find('.item-author').remove();
var userInfo = '';
if (url) {
userInfo += '网站:' + url;
}
if (commentCount > 0) {
if (userInfo) userInfo += ' | ';
userInfo += '评论数:' + commentCount;
}
if (recentActivity) {
if (userInfo) userInfo += ' | ';
userInfo += '最近活跃:' + recentActivity;
}
if (userInfo) {
if (!item.find('.item-content').length) {
item.find('.item-info').append('<div class="item-content">' + userInfo + '</div>');
} else {
item.find('.item-content').text(userInfo);
}
}
} else {
var title = response.data.title || '无标题';
// 截断过长的标题
if (title.length > 30) {
title = title.substring(0, 30) + '...';
}
item.find('.item-title').text(title);
}
item.find('.item-id').text('ID: ' + id);
} else if (item.length) {
item.find('.item-title').text('内容不存在');
item.find('.item-id').text('ID: ' + id);
}
},
error: function() {
var item = $('#item-item-' + id);
if (item.length) {
item.find('.item-title').text('获取失败');
item.find('.item-id').text('ID: ' + id);
}
}
});
});
}
// 显示模态框
function showModal() {
$('#modal-overlay').fadeIn(200, function() {
$('#modal-container').fadeIn(200);
// 添加ESC键关闭功能
$(document).on('keydown.modal', function(e) {
if (e.keyCode === 27) { // ESC键
hideModal();
}
});
});
}
// 隐藏模态框
function hideModal() {
$('#modal-container').fadeOut(200, function() {
$('#modal-overlay').fadeOut(200);
// 移除ESC键事件监听
$(document).off('keydown.modal');
});
}
// 显示加载动画
function showLoading() {
$('#loading-overlay').fadeIn(200);
}
// 隐藏加载动画
function hideLoading() {
$('#loading-overlay').fadeOut(200);
}
// 显示消息
function showMessage(message, type) {
var alert = $('#message-alert');
alert.removeClass('success error warning info').addClass(type);
alert.html(message).fadeIn(300);
setTimeout(function() {
alert.fadeOut(300);
}, 3000);
}
// 显示合集模态框
function showCollectionModal(collection) {
// 重置表单
$('#collection-form')[0].reset();
$('#collection-id').val('');
$('#collection-sort-order').val('created_desc');
$('#related_items').val('');
$('#collection-type').val('article');
// 重置UI状态
relatedItems = [];
// 重置合集类型选项
$('.collection-type-option').removeClass('active');
$('.collection-type-option[data-value="article"]').addClass('active');
// 更新排序选项显示
updateSortOptionsVisibility('article');
// 重置排序选项
$('.sort-option').removeClass('active');
$('.sort-option[data-value="created_desc"].article-only').addClass('active');
// 更新帮助文本
$('#related-help').text('多个文章ID用逗号分隔1,2,3。在输入框中直接编辑CID列表然后点击保存。');
$('#related-items-input').attr('placeholder', '输入文章CID多个用逗号分隔1,2,3');
if (collection) {
isEditMode = true;
$('#modal-title').text('编辑合集');
$('#collection-id').val(collection.id);
$('#collection-name').val(collection.name || '');
$('#collection-description').val(collection.description || '');
// 设置合集类型
if (collection.collection_type) {
$('#collection-type').val(collection.collection_type);
$('.collection-type-option').removeClass('active');
$('.collection-type-option[data-value="' + collection.collection_type + '"]').addClass('active');
// 更新排序选项显示
updateSortOptionsVisibility(collection.collection_type);
// 更新帮助文本
if (collection.collection_type === 'comment') {
$('#related-help').text('多个评论ID用逗号分隔1,2,3。在输入框中直接编辑COID列表然后点击保存。');
$('#related-items-input').attr('placeholder', '输入评论COID多个用逗号分隔1,2,3');
} else if (collection.collection_type === 'user') {
$('#related-help').text('多个用户ID用逗号分隔1,2,3。在输入框中直接编辑UID列表然后点击保存。');
$('#related-items-input').attr('placeholder', '输入用户UID多个用逗号分隔1,2,3');
}
}
if (collection.sort_order) {
$('#collection-sort-order').val(collection.sort_order);
$('.sort-option').removeClass('active');
$('.sort-option[data-value="' + collection.sort_order + '"]:visible').addClass('active');
}
if (collection.related_items) {
relatedItems = collection.related_items.split(',').map(function(itemId) {
return itemId.trim();
}).filter(function(itemId) {
return itemId !== '';
});
$('#related_items').val(relatedItems.join(','));
// 设置输入框的值
$('#related-items-input').val(relatedItems.join(','));
renderRelatedItemsList();
} else {
$('#related-items-input').val('');
renderRelatedItemsList();
}
} else {
isEditMode = false;
$('#modal-title').text('新增合集');
$('#related-items-input').val('');
renderRelatedItemsList();
}
showModal();
setTimeout(function() {
$('#collection-name').focus();
}, 300);
}
function editCollection(id) {
console.log('编辑合集请求ID:', id);
showLoading();
$.ajax({
url: CollectionConfig.actionUrl,
type: 'GET',
data: {do: 'get', id: id},
dataType: 'json',
success: function(response) {
hideLoading();
console.log('编辑合集响应:', response);
if (response.success) {
showCollectionModal(response.data);
} else {
showMessage(response.message || '获取合集信息失败', 'error');
}
},
error: function(xhr, status, error) {
hideLoading();
console.error('编辑合集错误:', status, error);
console.log('响应内容:', xhr.responseText);
showMessage('获取合集信息失败: ' + error, 'error');
}
});
}
function saveCollection() {
var id = $('#collection-id').val();
// 验证表单
var name = $('#collection-name').val().trim();
if (!name) {
showMessage('请填写合集名称', 'warning');
$('#collection-name').focus();
return;
}
// 准备表单数据
var formData = {
do: isEditMode ? 'update' : 'add',
name: name,
description: $('#collection-description').val().trim(),
sort_order: $('#collection-sort-order').val(),
related_items: $('#related_items').val(),
collection_type: $('#collection-type').val()
};
if (isEditMode) {
formData.id = id;
}
console.log('保存合集数据:', formData);
showLoading();
$.ajax({
url: CollectionConfig.actionUrl,
type: 'POST',
data: formData,
dataType: 'json',
success: function(response) {
hideLoading();
console.log('保存合集响应:', response);
if (response.success) {
showMessage(response.message, 'success');
hideModal();
setTimeout(function() {
window.location.reload();
}, 1500);
} else {
showMessage(response.message || '保存失败', 'error');
}
},
error: function(xhr, status, error) {
hideLoading();
console.error('保存合集错误:', status, error);
console.log('响应内容:', xhr.responseText);
var errorMsg = '操作失败:';
if (xhr.responseText && xhr.responseText.includes('<!DOCTYPE')) {
errorMsg += '服务器返回了HTML页面而不是JSON响应请检查action.php是否正确处理请求';
} else {
errorMsg += error;
}
showMessage(errorMsg, 'error');
}
});
}
function deleteCollection(id) {
console.log('删除合集ID:', id);
if (confirm('你确认要删除这个合集吗?')) {
showLoading();
$.ajax({
url: CollectionConfig.actionUrl,
type: 'POST',
data: {do: 'delete', id: id},
dataType: 'json',
success: function(response) {
hideLoading();
console.log('删除合集响应:', response);
if (response.success) {
showMessage(response.message, 'success');
setTimeout(function() {
window.location.reload();
}, 1500);
} else {
showMessage(response.message || '删除失败', 'error');
}
},
error: function(xhr, status, error) {
hideLoading();
console.error('删除合集错误:', error);
console.log('响应内容:', xhr.responseText);
showMessage('删除失败: ' + error, 'error');
}
});
}
}
$(document).ready(function() {
initCollection();
});
})(jQuery);
</script>
<?php
include 'footer.php';
?>