comment.admin.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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);
  16. }
  17. }
  18. /**
  19. * Form builder for the comment overview administration form.
  20. *
  21. * @param $arg
  22. * Current path's fourth component: the type of overview form ('approval' or
  23. * 'new').
  24. *
  25. * @ingroup forms
  26. * @see comment_admin_overview_validate()
  27. * @see comment_admin_overview_submit()
  28. * @see theme_comment_admin_overview()
  29. */
  30. function comment_admin_overview($form, &$form_state, $arg) {
  31. // Build an 'Update options' form.
  32. $form['options'] = array(
  33. '#type' => 'fieldset',
  34. '#title' => t('Update options'),
  35. '#attributes' => array('class' => array('container-inline')),
  36. );
  37. if ($arg == 'approval') {
  38. $options['publish'] = t('Publish the selected comments');
  39. }
  40. else {
  41. $options['unpublish'] = t('Unpublish the selected comments');
  42. }
  43. $options['delete'] = t('Delete the selected comments');
  44. $form['options']['operation'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Operation'),
  47. '#title_display' => 'invisible',
  48. '#options' => $options,
  49. '#default_value' => 'publish',
  50. );
  51. $form['options']['submit'] = array(
  52. '#type' => 'submit',
  53. '#value' => t('Update'),
  54. );
  55. // Load the comments that need to be displayed.
  56. $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
  57. $header = array(
  58. 'subject' => array('data' => t('Subject'), 'field' => 'subject'),
  59. 'author' => array('data' => t('Author'), 'field' => 'name'),
  60. 'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title'),
  61. 'changed' => array('data' => t('Updated'), 'field' => 'c.changed', 'sort' => 'desc'),
  62. 'operations' => array('data' => t('Operations')),
  63. );
  64. $query = db_select('comment', 'c')->extend('PagerDefault')->extend('TableSort');
  65. $query->join('node', 'n', 'n.nid = c.nid');
  66. $query->addField('n', 'title', 'node_title');
  67. $query->addTag('node_access');
  68. $result = $query
  69. ->fields('c', array('cid', 'subject', 'name', 'changed'))
  70. ->condition('c.status', $status)
  71. ->limit(50)
  72. ->orderByHeader($header)
  73. ->execute();
  74. $cids = array();
  75. // We collect a sorted list of node_titles during the query to attach to the
  76. // comments later.
  77. foreach ($result as $row) {
  78. $cids[] = $row->cid;
  79. $node_titles[] = $row->node_title;
  80. }
  81. $comments = comment_load_multiple($cids);
  82. // Build a table listing the appropriate comments.
  83. $options = array();
  84. $destination = drupal_get_destination();
  85. foreach ($comments as $comment) {
  86. // Remove the first node title from the node_titles array and attach to
  87. // the comment.
  88. $comment->node_title = array_shift($node_titles);
  89. $comment_body = field_get_items('comment', $comment, 'comment_body');
  90. $options[$comment->cid] = array(
  91. 'subject' => array(
  92. 'data' => array(
  93. '#type' => 'link',
  94. '#title' => $comment->subject,
  95. '#href' => 'comment/' . $comment->cid,
  96. '#options' => array('attributes' => array('title' => truncate_utf8($comment_body[0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid),
  97. ),
  98. ),
  99. 'author' => theme('username', array('account' => $comment)),
  100. 'posted_in' => array(
  101. 'data' => array(
  102. '#type' => 'link',
  103. '#title' => $comment->node_title,
  104. '#href' => 'node/' . $comment->nid,
  105. ),
  106. ),
  107. 'changed' => format_date($comment->changed, 'short'),
  108. 'operations' => array(
  109. 'data' => array(
  110. '#type' => 'link',
  111. '#title' => t('edit'),
  112. '#href' => 'comment/' . $comment->cid . '/edit',
  113. '#options' => array('query' => $destination),
  114. ),
  115. ),
  116. );
  117. }
  118. $form['comments'] = array(
  119. '#type' => 'tableselect',
  120. '#header' => $header,
  121. '#options' => $options,
  122. '#empty' => t('No comments available.'),
  123. );
  124. $form['pager'] = array('#theme' => 'pager');
  125. return $form;
  126. }
  127. /**
  128. * Validate comment_admin_overview form submissions.
  129. */
  130. function comment_admin_overview_validate($form, &$form_state) {
  131. $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
  132. // We can't execute any 'Update options' if no comments were selected.
  133. if (count($form_state['values']['comments']) == 0) {
  134. form_set_error('', t('Select one or more comments to perform the update on.'));
  135. }
  136. }
  137. /**
  138. * Process comment_admin_overview form submissions.
  139. *
  140. * Execute the chosen 'Update option' on the selected comments, such as
  141. * publishing, unpublishing or deleting.
  142. */
  143. function comment_admin_overview_submit($form, &$form_state) {
  144. $operation = $form_state['values']['operation'];
  145. $cids = $form_state['values']['comments'];
  146. if ($operation == 'delete') {
  147. comment_delete_multiple($cids);
  148. }
  149. else {
  150. foreach ($cids as $cid => $value) {
  151. $comment = comment_load($value);
  152. if ($operation == 'unpublish') {
  153. $comment->status = COMMENT_NOT_PUBLISHED;
  154. }
  155. elseif ($operation == 'publish') {
  156. $comment->status = COMMENT_PUBLISHED;
  157. }
  158. comment_save($comment);
  159. }
  160. }
  161. drupal_set_message(t('The update has been performed.'));
  162. $form_state['redirect'] = 'admin/content/comment';
  163. cache_clear_all();
  164. }
  165. /**
  166. * List the selected comments and verify that the admin wants to delete them.
  167. *
  168. * @param $form_state
  169. * An associative array containing the current state of the form.
  170. * @return
  171. * TRUE if the comments should be deleted, FALSE otherwise.
  172. * @ingroup forms
  173. * @see comment_multiple_delete_confirm_submit()
  174. */
  175. function comment_multiple_delete_confirm($form, &$form_state) {
  176. $edit = $form_state['input'];
  177. $form['comments'] = array(
  178. '#prefix' => '<ul>',
  179. '#suffix' => '</ul>',
  180. '#tree' => TRUE,
  181. );
  182. // array_filter() returns only elements with actual values.
  183. $comment_counter = 0;
  184. foreach (array_filter($edit['comments']) as $cid => $value) {
  185. $comment = comment_load($cid);
  186. if (is_object($comment) && is_numeric($comment->cid)) {
  187. $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
  188. $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
  189. $comment_counter++;
  190. }
  191. }
  192. $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
  193. if (!$comment_counter) {
  194. drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
  195. drupal_goto('admin/content/comment');
  196. }
  197. else {
  198. return confirm_form($form,
  199. t('Are you sure you want to delete these comments and all their children?'),
  200. 'admin/content/comment', t('This action cannot be undone.'),
  201. t('Delete comments'), t('Cancel'));
  202. }
  203. }
  204. /**
  205. * Process comment_multiple_delete_confirm form submissions.
  206. */
  207. function comment_multiple_delete_confirm_submit($form, &$form_state) {
  208. if ($form_state['values']['confirm']) {
  209. comment_delete_multiple(array_keys($form_state['values']['comments']));
  210. cache_clear_all();
  211. $count = count($form_state['values']['comments']);
  212. watchdog('content', 'Deleted @count comments.', array('@count' => $count));
  213. drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
  214. }
  215. $form_state['redirect'] = 'admin/content/comment';
  216. }
  217. /**
  218. * Page callback for comment deletions.
  219. */
  220. function comment_confirm_delete_page($cid) {
  221. if ($comment = comment_load($cid)) {
  222. return drupal_get_form('comment_confirm_delete', $comment);
  223. }
  224. return MENU_NOT_FOUND;
  225. }
  226. /**
  227. * Form builder; Builds the confirmation form for deleting a single comment.
  228. *
  229. * @ingroup forms
  230. * @see comment_confirm_delete_submit()
  231. */
  232. function comment_confirm_delete($form, &$form_state, $comment) {
  233. $form['#comment'] = $comment;
  234. // Always provide entity id in the same form key as in the entity edit form.
  235. $form['cid'] = array('#type' => 'value', '#value' => $comment->cid);
  236. return confirm_form(
  237. $form,
  238. t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
  239. 'node/' . $comment->nid,
  240. t('Any replies to this comment will be lost. This action cannot be undone.'),
  241. t('Delete'),
  242. t('Cancel'),
  243. 'comment_confirm_delete');
  244. }
  245. /**
  246. * Process comment_confirm_delete form submissions.
  247. */
  248. function comment_confirm_delete_submit($form, &$form_state) {
  249. $comment = $form['#comment'];
  250. // Delete the comment and its replies.
  251. comment_delete($comment->cid);
  252. drupal_set_message(t('The comment and all its replies have been deleted.'));
  253. watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
  254. // Clear the cache so an anonymous user sees that his comment was deleted.
  255. cache_clear_all();
  256. $form_state['redirect'] = "node/$comment->nid";
  257. }