|
1 <?php |
|
2 // $Id: comment.admin.inc,v 1.4.2.2 2008/05/19 07:27:35 goba Exp $ |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * Admin page callbacks for the comment module. |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Menu callback; present an administrative comment listing. |
|
11 */ |
|
12 function comment_admin($type = 'new') { |
|
13 $edit = $_POST; |
|
14 |
|
15 if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) { |
|
16 return drupal_get_form('comment_multiple_delete_confirm'); |
|
17 } |
|
18 else { |
|
19 return drupal_get_form('comment_admin_overview', $type, arg(4)); |
|
20 } |
|
21 } |
|
22 |
|
23 /** |
|
24 * Form builder; Builds the comment overview form for the admin. |
|
25 * |
|
26 * @param $type |
|
27 * Not used. |
|
28 * @param $arg |
|
29 * Current path's fourth component deciding the form type (Published comments/Approval queue) |
|
30 * @return |
|
31 * The form structure. |
|
32 * @ingroup forms |
|
33 * @see comment_admin_overview_validate() |
|
34 * @see comment_admin_overview_submit() |
|
35 * @see theme_comment_admin_overview() |
|
36 */ |
|
37 function comment_admin_overview($type = 'new', $arg) { |
|
38 // build an 'Update options' form |
|
39 $form['options'] = array( |
|
40 '#type' => 'fieldset', '#title' => t('Update options'), |
|
41 '#prefix' => '<div class="container-inline">', '#suffix' => '</div>' |
|
42 ); |
|
43 $options = array(); |
|
44 foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) { |
|
45 $options[$key] = $value[0]; |
|
46 } |
|
47 $form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'publish'); |
|
48 $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update')); |
|
49 |
|
50 // load the comments that we want to display |
|
51 $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED; |
|
52 $form['header'] = array('#type' => 'value', '#value' => array( |
|
53 theme('table_select_header_cell'), |
|
54 array('data' => t('Subject'), 'field' => 'subject'), |
|
55 array('data' => t('Author'), 'field' => 'name'), |
|
56 array('data' => t('Posted in'), 'field' => 'node_title'), |
|
57 array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'), |
|
58 array('data' => t('Operations')) |
|
59 )); |
|
60 $result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid, n.title as node_title FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid INNER JOIN {node} n ON n.nid = c.nid WHERE c.status = %d'. tablesort_sql($form['header']['#value']), 50, 0, NULL, $status); |
|
61 |
|
62 // build a table listing the appropriate comments |
|
63 $destination = drupal_get_destination(); |
|
64 while ($comment = db_fetch_object($result)) { |
|
65 $comments[$comment->cid] = ''; |
|
66 $comment->name = $comment->uid ? $comment->registered_name : $comment->name; |
|
67 $form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid, array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'fragment' => 'comment-'. $comment->cid))); |
|
68 $form['username'][$comment->cid] = array('#value' => theme('username', $comment)); |
|
69 $form['node_title'][$comment->cid] = array('#value' => l($comment->node_title, 'node/'. $comment->nid)); |
|
70 $form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small')); |
|
71 $form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array('query' => $destination))); |
|
72 } |
|
73 $form['comments'] = array('#type' => 'checkboxes', '#options' => isset($comments) ? $comments: array()); |
|
74 $form['pager'] = array('#value' => theme('pager', NULL, 50, 0)); |
|
75 return $form; |
|
76 } |
|
77 |
|
78 /** |
|
79 * Validate comment_admin_overview form submissions. |
|
80 * |
|
81 * We can't execute any 'Update options' if no comments were selected. |
|
82 */ |
|
83 function comment_admin_overview_validate($form, &$form_state) { |
|
84 $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0)); |
|
85 if (count($form_state['values']['comments']) == 0) { |
|
86 form_set_error('', t('Please select one or more comments to perform the update on.')); |
|
87 drupal_goto('admin/content/comment'); |
|
88 } |
|
89 } |
|
90 |
|
91 /** |
|
92 * Process comment_admin_overview form submissions. |
|
93 * |
|
94 * Execute the chosen 'Update option' on the selected comments, such as |
|
95 * publishing, unpublishing or deleting. |
|
96 */ |
|
97 function comment_admin_overview_submit($form, &$form_state) { |
|
98 $operations = comment_operations(); |
|
99 if ($operations[$form_state['values']['operation']][1]) { |
|
100 // extract the appropriate database query operation |
|
101 $query = $operations[$form_state['values']['operation']][1]; |
|
102 foreach ($form_state['values']['comments'] as $cid => $value) { |
|
103 if ($value) { |
|
104 // perform the update action, then refresh node statistics |
|
105 db_query($query, $cid); |
|
106 $comment = _comment_load($cid); |
|
107 _comment_update_node_statistics($comment->nid); |
|
108 // Allow modules to respond to the updating of a comment. |
|
109 comment_invoke_comment($comment, $form_state['values']['operation']); |
|
110 // Add an entry to the watchdog log. |
|
111 watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid))); |
|
112 } |
|
113 } |
|
114 cache_clear_all(); |
|
115 drupal_set_message(t('The update has been performed.')); |
|
116 $form_state['redirect'] = 'admin/content/comment'; |
|
117 } |
|
118 } |
|
119 |
|
120 /** |
|
121 * Theme the comment admin form. |
|
122 * |
|
123 * @param $form |
|
124 * An associative array containing the structure of the form. |
|
125 * @ingroup themeable |
|
126 */ |
|
127 function theme_comment_admin_overview($form) { |
|
128 $output = drupal_render($form['options']); |
|
129 if (isset($form['subject']) && is_array($form['subject'])) { |
|
130 foreach (element_children($form['subject']) as $key) { |
|
131 $row = array(); |
|
132 $row[] = drupal_render($form['comments'][$key]); |
|
133 $row[] = drupal_render($form['subject'][$key]); |
|
134 $row[] = drupal_render($form['username'][$key]); |
|
135 $row[] = drupal_render($form['node_title'][$key]); |
|
136 $row[] = drupal_render($form['timestamp'][$key]); |
|
137 $row[] = drupal_render($form['operations'][$key]); |
|
138 $rows[] = $row; |
|
139 } |
|
140 } |
|
141 else { |
|
142 $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6')); |
|
143 } |
|
144 |
|
145 $output .= theme('table', $form['header']['#value'], $rows); |
|
146 if ($form['pager']['#value']) { |
|
147 $output .= drupal_render($form['pager']); |
|
148 } |
|
149 |
|
150 $output .= drupal_render($form); |
|
151 |
|
152 return $output; |
|
153 } |
|
154 |
|
155 /** |
|
156 * List the selected comments and verify that the admin really wants to delete |
|
157 * them. |
|
158 * |
|
159 * @param $form_state |
|
160 * An associative array containing the current state of the form. |
|
161 * @return |
|
162 * TRUE if the comments should be deleted, FALSE otherwise. |
|
163 * @ingroup forms |
|
164 * @see comment_multiple_delete_confirm_submit() |
|
165 */ |
|
166 function comment_multiple_delete_confirm(&$form_state) { |
|
167 $edit = $form_state['post']; |
|
168 |
|
169 $form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); |
|
170 // array_filter() returns only elements with actual values |
|
171 $comment_counter = 0; |
|
172 foreach (array_filter($edit['comments']) as $cid => $value) { |
|
173 $comment = _comment_load($cid); |
|
174 if (is_object($comment) && is_numeric($comment->cid)) { |
|
175 $subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid)); |
|
176 $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) .'</li>'); |
|
177 $comment_counter++; |
|
178 } |
|
179 } |
|
180 $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); |
|
181 |
|
182 if (!$comment_counter) { |
|
183 drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.')); |
|
184 drupal_goto('admin/content/comment'); |
|
185 } |
|
186 else { |
|
187 return confirm_form($form, |
|
188 t('Are you sure you want to delete these comments and all their children?'), |
|
189 'admin/content/comment', t('This action cannot be undone.'), |
|
190 t('Delete comments'), t('Cancel')); |
|
191 } |
|
192 } |
|
193 |
|
194 /** |
|
195 * Process comment_multiple_delete_confirm form submissions. |
|
196 * |
|
197 * Perform the actual comment deletion. |
|
198 */ |
|
199 function comment_multiple_delete_confirm_submit($form, &$form_state) { |
|
200 if ($form_state['values']['confirm']) { |
|
201 foreach ($form_state['values']['comments'] as $cid => $value) { |
|
202 $comment = _comment_load($cid); |
|
203 _comment_delete_thread($comment); |
|
204 _comment_update_node_statistics($comment->nid); |
|
205 } |
|
206 cache_clear_all(); |
|
207 drupal_set_message(t('The comments have been deleted.')); |
|
208 } |
|
209 $form_state['redirect'] = 'admin/content/comment'; |
|
210 } |
|
211 |
|
212 /** |
|
213 * Menu callback; delete a comment. |
|
214 * |
|
215 * @param $cid |
|
216 * The comment do be deleted. |
|
217 */ |
|
218 function comment_delete($cid = NULL) { |
|
219 $comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', $cid)); |
|
220 $comment->name = $comment->uid ? $comment->registered_name : $comment->name; |
|
221 |
|
222 $output = ''; |
|
223 |
|
224 if (is_object($comment) && is_numeric($comment->cid)) { |
|
225 $output = drupal_get_form('comment_confirm_delete', $comment); |
|
226 } |
|
227 else { |
|
228 drupal_set_message(t('The comment no longer exists.')); |
|
229 } |
|
230 |
|
231 return $output; |
|
232 } |
|
233 |
|
234 /** |
|
235 * Form builder; Builds the confirmation form for deleting a single comment. |
|
236 * |
|
237 * @ingroup forms |
|
238 * @see comment_confirm_delete_submit() |
|
239 */ |
|
240 function comment_confirm_delete(&$form_state, $comment) { |
|
241 $form = array(); |
|
242 $form['#comment'] = $comment; |
|
243 return confirm_form( |
|
244 $form, |
|
245 t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)), |
|
246 'node/'. $comment->nid, |
|
247 t('Any replies to this comment will be lost. This action cannot be undone.'), |
|
248 t('Delete'), |
|
249 t('Cancel'), |
|
250 'comment_confirm_delete'); |
|
251 } |
|
252 |
|
253 /** |
|
254 * Process comment_confirm_delete form submissions. |
|
255 */ |
|
256 function comment_confirm_delete_submit($form, &$form_state) { |
|
257 drupal_set_message(t('The comment and all its replies have been deleted.')); |
|
258 |
|
259 $comment = $form['#comment']; |
|
260 |
|
261 // Delete comment and its replies. |
|
262 _comment_delete_thread($comment); |
|
263 |
|
264 _comment_update_node_statistics($comment->nid); |
|
265 |
|
266 // Clear the cache so an anonymous user sees that his comment was deleted. |
|
267 cache_clear_all(); |
|
268 |
|
269 $form_state['redirect'] = "node/$comment->nid"; |
|
270 } |
|
271 |
|
272 /** |
|
273 * Perform the actual deletion of a comment and all its replies. |
|
274 * |
|
275 * @param $comment |
|
276 * An associative array describing the comment to be deleted. |
|
277 */ |
|
278 function _comment_delete_thread($comment) { |
|
279 if (!is_object($comment) || !is_numeric($comment->cid)) { |
|
280 watchdog('content', 'Cannot delete non-existent comment.', array(), WATCHDOG_WARNING); |
|
281 return; |
|
282 } |
|
283 |
|
284 // Delete the comment: |
|
285 db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid); |
|
286 watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject)); |
|
287 |
|
288 comment_invoke_comment($comment, 'delete'); |
|
289 |
|
290 // Delete the comment's replies |
|
291 $result = db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE pid = %d', $comment->cid); |
|
292 while ($comment = db_fetch_object($result)) { |
|
293 $comment->name = $comment->uid ? $comment->registered_name : $comment->name; |
|
294 _comment_delete_thread($comment); |
|
295 } |
|
296 } |