📄 正在查看:admin/control/comment_control.class.php
大小:6,065 字节 · 修改:2014-01-24 01:15:24 · 行数:208
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class comment_control extends admin_control {
10 // 评论管理
11 public function index() {
12 // hook admin_comment_control_index_before.php
13
14 $mid = max(2, (int)R('mid'));
15 $table = $this->models->get_table($mid);
16
17 // 模型名称
18 $mod_name = $this->models->get_name();
19 if(isset($mod_name[1])) unset($mod_name[1]);
20 $this->assign('mid', $mid);
21 $this->assign('mod_name', $mod_name);
22
23 $this->cms_content_comment->table = 'cms_'.$table.'_comment';
24
25 // 初始分页
26 $pagenum = 20;
27 $total = $this->cms_content_comment->count();
28 $maxpage = max(1, ceil($total/$pagenum));
29 $page = min($maxpage, max(1, intval(R('page'))));
30 $pages = pages($page, $maxpage, 'index.php?u=comment-index-mid-'.$mid.'-page-{page}');
31 $this->assign('pages', $pages);
32 $this->assign('total', $total);
33
34 // 获取评论列表
35 $id_arr = array();
36 $comment_arr = $this->cms_content_comment->list_arr(array(), -1, ($page-1)*$pagenum, $pagenum, $total);
37 foreach($comment_arr as &$v) {
38 $this->cms_content_comment->format($v, 'Y-m-d H:i:s', 0);
39 $id_arr[] = $v['id'];
40 }
41
42 $id_arr = array_unique($id_arr);
43 $this->cms_content->table = 'cms_'.$table;
44 $key_pre = 'cms_'.$table.'-id-';
45 $tmp = $this->cms_content->mget($id_arr);
46 foreach($comment_arr as &$v) {
47 $row = $tmp[$key_pre.$v['id']];
48 $v['title'] = $row['title'];
49 $v['url'] = $this->cms_content->content_url($row['cid'], $row['id'], $row['alias'], $row['dateline']);
50 }
51 $this->assign('comment_arr', $comment_arr);
52
53 // hook admin_comment_control_index_after.php
54
55 $this->display();
56 }
57
58 // 单条内容评论管理
59 public function content() {
60 // hook admin_comment_control_content_before.php
61
62 $id = (int) R('id');
63 $mid = max(2, (int)R('mid'));
64 $table = $this->models->get_table($mid);
65
66 // 模型名称
67 $mod_name = $this->models->get_name();
68 if(isset($mod_name[1])) unset($mod_name[1]);
69 $this->assign('mid', $mid);
70 $this->assign('mod_name', $mod_name);
71
72 // 读取内容
73 $this->cms_content->table = 'cms_'.$table;
74 $row = $this->cms_content->read($id);
75
76 // 初始化标题、位置
77 $this->_pkey = 'content';
78 $this->_title = '评论管理';
79 $this->_place = '内容 &#187; 评论管理 &#187; '.$row['title'];
80
81 // 初始分页
82 $pagenum = 20;
83 $total = $row['comments'];
84 $maxpage = max(1, ceil($total/$pagenum));
85 $page = min($maxpage, max(1, intval(R('page'))));
86 $pages = pages($page, $maxpage, 'index.php?u=comment-content-mid-'.$mid.'-id-'.$id.'-page-{page}');
87 $this->assign('pages', $pages);
88 $this->assign('total', $total);
89
90 // 获取评论列表
91 $this->cms_content_comment->table = 'cms_'.$table.'_comment';
92 $comment_arr = $this->cms_content_comment->list_arr(array('id' => $id), -1, ($page-1)*$pagenum, $pagenum, $total);
93 foreach($comment_arr as &$v) {
94 $this->cms_content_comment->format($v, 'Y-m-d H:i:s', 0);
95 $v['title'] = $row['title'];
96 $v['url'] = $this->cms_content->content_url($row['cid'], $row['id'], $row['alias'], $row['dateline']);
97 }
98 $this->assign('comment_arr', $comment_arr);
99
100 // hook admin_comment_control_content_after.php
101
102 $this->display('comment_index.htm');
103 }
104
105 // 读取一条评论
106 public function get_json() {
107 // hook admin_comment_control_get_json_before.php
108
109 $mid = max(2, (int)R('mid', 'P'));
110 $table = $this->models->get_table($mid);
111
112 $commentid = (int) R('commentid', 'P');
113
114 $this->cms_content_comment->table = 'cms_'.$table.'_comment';
115 $data = $this->cms_content_comment->read($commentid);
116
117 // hook admin_comment_control_get_json_after.php
118
119 echo json_encode($data);
120 exit;
121 }
122
123 // 编辑评论
124 public function edit() {
125 // hook admin_comment_control_edit_before.php
126
127 $mid = max(2, (int)R('mid', 'P'));
128 $table = $this->models->get_table($mid);
129
130 $commentid = (int) R('commentid', 'P');
131 $author = htmlspecialchars(trim(R('author', 'P')));
132 $content = htmlspecialchars(trim(R('content', 'P')));
133
134 empty($commentid) && E(1, '评论ID不能为空!');
135 empty($author) && E(1, '昵称不能为空!');
136 strlen($author)>20 && E(1, '昵称太长了!');
137 empty($content) && E(1, '评论内容不能为空!');
138 strlen($content)>3000 && E(1, '评论内容太长了!');
139
140 $this->cms_content_comment->table = 'cms_'.$table.'_comment';
141 $data = $this->cms_content_comment->read($commentid);
142
143 $data['author'] = $author;
144 $data['content'] = $content;
145
146 // hook admin_comment_control_edit_after.php
147
148 if($this->cms_content_comment->update($data)) {
149 E(0, '编辑成功!');
150 }else{
151 E(1, '编辑失败!');
152 }
153 }
154
155 // 删除评论
156 public function del() {
157 // hook admin_comment_control_del_before.php
158
159 $mid = max(2, (int)R('mid', 'P'));
160 $table = $this->models->get_table($mid);
161
162 $id = (int) R('id', 'P');
163 $commentid = (int) R('commentid', 'P');
164
165 empty($id) && E(1, '内容ID不能为空!');
166 empty($commentid) && E(1, '评论ID不能为空!');
167
168 // hook admin_comment_control_del_after.php
169
170 $err = $this->cms_content_comment->xdelete($table, $id, $commentid);
171 if($err) {
172 E(1, $err);
173 }else{
174 E(0, '删除成功!');
175 }
176 }
177
178 // 批量删除评论
179 public function batch_del() {
180 // hook admin_comment_control_batch_del_before.php
181
182 $mid = max(2, (int)R('mid', 'P'));
183 $table = $this->models->get_table($mid);
184
185 $id_arr = R('id_arr', 'P');
186
187 if(!empty($id_arr) && is_array($id_arr)) {
188 $err_num = 0;
189 foreach($id_arr as $arr) {
190 $id = $arr[0];
191 $commentid = $arr[1];
192 $err = $this->cms_content_comment->xdelete($table, $id, $commentid);
193 if($err) $err_num++;
194 }
195
196 if($err_num) {
197 E(1, $err_num.' 条评论删除失败!');
198 }else{
199 E(0, '删除成功!');
200 }
201 }else{
202 E(1, '参数不能为空!');
203 }
204 }
205
206 // hook admin_comment_control_after.php
207}
208