better-staridc-MNBT
1<?php
2/**
3 * 管理员端 - 官网新闻管理(列表分页 + 添加/编辑/删除)
4 */
5if (!defined('IN_CRONLITE')) {
6 exit;
7}
8
9// —— 处理 POST(成功后 redirect 到带提示参数的地址,避免刷新重复提交)——
10if ($_SERVER['REQUEST_METHOD'] === 'POST') {
11 $act = $_POST['act'] ?? '';
12
13 if ($act === 'save') {
14 $data = [
15 'id' => (int)($_POST['id'] ?? 0),
16 'title' => $_POST['title'] ?? '',
17 'category' => $_POST['category'] ?? '',
18 'content' => $_POST['content'] ?? '',
19 'status' => $_POST['status'] ?? 'active',
20 'sort' => (int)($_POST['sort'] ?? 50),
21 ];
22 $r = site_news_save($data);
23 if ($r === true) {
24 header('Location: ' . site_admin_url('site_news', 'saved=1'));
25 exit;
26 }
27 $msg = $r;
28 $msg_type = 'danger';
29 $form = $data;
30 $show_modal = true;
31 } elseif ($act === 'delete') {
32 $id = (int)($_POST['id'] ?? 0);
33 $ok = $id > 0 && site_news_delete($id);
34 header('Location: ' . site_admin_url('site_news', ($ok ? 'saved=1&msg=' . urlencode('新闻已删除') : 'msg=' . urlencode('删除失败'))));
35 exit;
36 }
37}
38
39// —— GET 参数处理(redirect 后的提示)——
40$saved = isset($_GET['saved']);
41$msg = $_GET['msg'] ?? ($msg ?? '');
42$msg_type = $saved ? 'success' : ($msg_type ?? 'danger');
43if ($saved && $msg === '') {
44 $msg = '保存成功';
45}
46
47$per = 15;
48$page = max(1, (int)($_GET['page'] ?? 1));
49$pageData = site_news_list($page, $per, '', ''); // 管理端显示全部状态
50$newsList = $pageData['list'];
51$total = $pageData['total'];
52$pages = (int)ceil($total / $per);
53
54// 新闻分类(默认分类 + 数据库中已有的其他分类)
55$news_categories = ['产品发布', '优惠活动', '行业动态', '平台公告'];
56global $DB;
57$cat_rows = $DB->get_all_prepare("SELECT DISTINCT category FROM MN_plugin_site_news WHERE category != '' ORDER BY category ASC") ?: [];
58foreach ($cat_rows as $cr) {
59 if (!in_array($cr['category'], $news_categories, true)) {
60 $news_categories[] = $cr['category'];
61 }
62}
63
64$title = $title ?? '新闻管理';
65mnbt_admin_include('head');
66?>
67<div class="container-fluid p-t-15">
68 <div class="card">
69 <div class="card-header">
70 <h4 style="display:inline-block">新闻管理</h4>
71 <span class="float-right">
72 <button type="button" class="btn btn-primary btn-sm" onclick="openEdit(0)">
73 <i class="mdi mdi-plus"></i> 添加新闻
74 </button>
75 </span>
76 </div>
77 <div class="card-body">
78 <?php if (!empty($msg)): ?>
79 <div class="alert alert-<?= htmlspecialchars($msg_type ?? 'danger', ENT_QUOTES) ?>"><?= htmlspecialchars($msg) ?></div>
80 <?php endif; ?>
81
82 <?php if (empty($newsList)): ?>
83 <p class="text-muted">还没有新闻,点击右上角"添加新闻"发布第一篇。</p>
84 <?php else: ?>
85 <div class="table-responsive">
86 <table class="table table-bordered table-hover">
87 <thead>
88 <tr>
89 <th style="width:60px">ID</th>
90 <th>标题</th>
91 <th style="width:120px">分类</th>
92 <th style="width:80px">浏览</th>
93 <th style="width:80px">状态</th>
94 <th style="width:70px">排序</th>
95 <th style="width:140px">发布时间</th>
96 <th style="width:170px">操作</th>
97 </tr>
98 </thead>
99 <tbody>
100 <?php foreach ($newsList as $n): ?>
101 <tr>
102 <td><?= (int)$n['id'] ?></td>
103 <td><?= htmlspecialchars(mb_substr($n['title'], 0, 40), ENT_QUOTES) ?><?= mb_strlen($n['title']) > 40 ? '…' : '' ?></td>
104 <td><?= htmlspecialchars($n['category'] ?: '—', ENT_QUOTES) ?></td>
105 <td><?= (int)$n['views'] ?></td>
106 <td>
107 <?php if ($n['status'] === 'active'): ?>
108 <span class="badge badge-success">上架</span>
109 <?php else: ?>
110 <span class="badge badge-secondary">下架</span>
111 <?php endif; ?>
112 </td>
113 <td><?= (int)$n['sort'] ?></td>
114 <td><?= htmlspecialchars($n['created_at'], ENT_QUOTES) ?></td>
115 <td>
116 <button type="button" class="btn btn-sm btn-outline-primary" onclick="openEdit(<?= (int)$n['id'] ?>)">编辑</button>
117 <form method="post" style="display:inline-block" onsubmit="var b=this.querySelector('button');b.disabled=true;b.textContent='删除中...';return confirm('确定删除此新闻?')">
118 <input type="hidden" name="act" value="delete">
119 <input type="hidden" name="id" value="<?= (int)$n['id'] ?>">
120 <button type="submit" class="btn btn-sm btn-outline-danger">删除</button>
121 </form>
122 </td>
123 </tr>
124 <?php endforeach; ?>
125 </tbody>
126 </table>
127 </div>
128
129 <?php if ($pages > 1): ?>
130 <nav aria-label="分页">
131 <ul class="pagination pagination-sm justify-content-end" style="margin-bottom:0">
132 <?php for ($i = 1; $i <= $pages; $i++): ?>
133 <li class="page-item<?= $i === $page ? ' active' : '' ?>">
134 <a class="page-link" href="<?= htmlspecialchars(site_admin_url('site_news', 'page=' . $i), ENT_QUOTES) ?>"><?= $i ?></a>
135 </li>
136 <?php endfor; ?>
137 </ul>
138 </nav>
139 <?php endif; ?>
140 <?php endif; ?>
141 </div>
142 </div>
143</div>
144
145<!-- 添加/编辑模态框 -->
146<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-hidden="true">
147 <div class="modal-dialog modal-lg" role="document">
148 <div class="modal-content">
149 <form method="post">
150 <input type="hidden" name="act" value="save">
151 <input type="hidden" name="id" id="f-id" value="0">
152 <div class="modal-header">
153 <h5 class="modal-title" id="modalTitle">添加新闻</h5>
154 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
155 </div>
156 <div class="modal-body">
157 <div class="form-group">
158 <label>新闻标题</label>
159 <input type="text" class="form-control" name="title" id="f-title" required>
160 </div>
161 <div class="form-row">
162 <div class="col-6">
163 <div class="form-group">
164 <label>新闻分类</label>
165 <select class="form-control" name="category" id="f-category">
166 <option value="">未分类</option>
167 <?php foreach ($news_categories as $cat): ?>
168 <option value="<?= htmlspecialchars($cat, ENT_QUOTES) ?>"><?= htmlspecialchars($cat) ?></option>
169 <?php endforeach; ?>
170 </select>
171 </div>
172 </div>
173 <div class="col-3">
174 <div class="form-group">
175 <label>状态</label>
176 <select class="form-control" name="status" id="f-status">
177 <option value="active">上架</option>
178 <option value="inactive">下架</option>
179 </select>
180 </div>
181 </div>
182 <div class="col-3">
183 <div class="form-group">
184 <label>排序(小到大)</label>
185 <input type="number" class="form-control" name="sort" id="f-sort" value="50" min="0">
186 </div>
187 </div>
188 </div>
189 <div class="form-group">
190 <label>正文内容</label>
191 <textarea class="form-control" name="content" id="f-content" rows="10" required></textarea>
192 <small class="form-text text-muted">支持纯文本与 HTML 标签,换行使用 <br> 或空段落。</small>
193 </div>
194 </div>
195 <div class="modal-footer">
196 <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
197 <button type="submit" class="btn btn-primary">保存</button>
198 </div>
199 </form>
200 </div>
201 </div>
202</div>
203
204<script>
205var __siteNews = <?= json_encode($newsList, JSON_UNESCAPED_UNICODE) ?>;
206<?php if (!empty($show_modal)): ?>
207var __siteForm = <?= json_encode($form, JSON_UNESCAPED_UNICODE) ?>;
208<?php endif; ?>
209function openEdit(id) {
210 var el = function (k) { return document.getElementById('f-' + k); };
211 document.getElementById('modalTitle').textContent = id ? '编辑新闻' : '添加新闻';
212 el('id').value = id;
213 if (id > 0) {
214 var n = __siteNews.find(function (x) { return parseInt(x.id, 10) === parseInt(id, 10); });
215 if (n) {
216 el('title').value = n.title || '';
217 el('category').value = n.category || '';
218 el('content').value = n.content || '';
219 el('status').value = n.status || 'active';
220 el('sort').value = n.sort || 50;
221 }
222 } else {
223 el('title').value = '';
224 el('category').value = '';
225 el('content').value = '';
226 el('status').value = 'active';
227 el('sort').value = 50;
228 }
229 <?php if (!empty($show_modal)): ?>
230 // 提交失败回显:用服务端返回的表单数据覆盖字段
231 if (typeof __siteForm !== 'undefined') {
232 el('title').value = __siteForm.title || '';
233 el('category').value = __siteForm.category || '';
234 el('content').value = __siteForm.content || '';
235 el('status').value = __siteForm.status || 'active';
236 el('sort').value = __siteForm.sort || 50;
237 }
238 <?php endif; ?>
239 $('#editModal').modal('show');
240}
241<?php if (!empty($show_modal)): ?>
242 $(function () { openEdit(<?= (int)($form['id'] ?? 0) ?>); });
243<?php endif; ?>
244</script>
245<?php mnbt_admin_include('foot'); ?>