comment.admin.inc

  1. 7.x drupal-7.x/modules/comment/comment.admin.inc
  2. 6.x drupal-6.x/modules/comment/comment.admin.inc

Admin page callbacks for the comment module.

File

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