comment.admin.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $options[$comment->cid] = array(
  90. 'subject' => array(
  91. 'data' => array(
  92. '#type' => 'link',
  93. '#title' => $comment->subject,
  94. '#href' => 'comment/' . $comment->cid,
  95. '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body[LANGUAGE_NONE][0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid),
  96. ),
  97. ),
  98. 'author' => theme('username', array('account' => $comment)),
  99. 'posted_in' => array(
  100. 'data' => array(
  101. '#type' => 'link',
  102. '#title' => $comment->node_title,
  103. '#href' => 'node/' . $comment->nid,
  104. ),
  105. ),
  106. 'changed' => format_date($comment->changed, 'short'),
  107. 'operations' => array(
  108. 'data' => array(
  109. '#type' => 'link',
  110. '#title' => t('edit'),
  111. '#href' => 'comment/' . $comment->cid . '/edit',
  112. '#options' => array('query' => $destination),
  113. ),
  114. ),
  115. );
  116. }
  117. $form['comments'] = array(
  118. '#type' => 'tableselect',
  119. '#header' => $header,
  120. '#options' => $options,
  121. '#empty' => t('No comments available.'),
  122. );
  123. $form['pager'] = array('#theme' => 'pager');
  124. return $form;
  125. }
  126. /**
  127. * Validate comment_admin_overview form submissions.
  128. */
  129. function comment_admin_overview_validate($form, &$form_state) {
  130. $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
  131. // We can't execute any 'Update options' if no comments were selected.
  132. if (count($form_state['values']['comments']) == 0) {
  133. form_set_error('', t('Select one or more comments to perform the update on.'));
  134. }
  135. }
  136. /**
  137. * Process comment_admin_overview form submissions.
  138. *
  139. * Execute the chosen 'Update option' on the selected comments, such as
  140. * publishing, unpublishing or deleting.
  141. */
  142. function comment_admin_overview_submit($form, &$form_state) {
  143. $operation = $form_state['values']['operation'];
  144. $cids = $form_state['values']['comments'];
  145. if ($operation == 'delete') {
  146. comment_delete_multiple($cids);
  147. }
  148. else {
  149. foreach ($cids as $cid => $value) {
  150. $comment = comment_load($value);
  151. if ($operation == 'unpublish') {
  152. $comment->status = COMMENT_NOT_PUBLISHED;
  153. }
  154. elseif ($operation == 'publish') {
  155. $comment->status = COMMENT_PUBLISHED;
  156. }
  157. comment_save($comment);
  158. }
  159. }
  160. drupal_set_message(t('The update has been performed.'));
  161. $form_state['redirect'] = 'admin/content/comment';
  162. cache_clear_all();
  163. }
  164. /**
  165. * List the selected comments and verify that the admin wants to delete them.
  166. *
  167. * @param $form_state
  168. * An associative array containing the current state of the form.
  169. * @return
  170. * TRUE if the comments should be deleted, FALSE otherwise.
  171. * @ingroup forms
  172. * @see comment_multiple_delete_confirm_submit()
  173. */
  174. function comment_multiple_delete_confirm($form, &$form_state) {
  175. $edit = $form_state['input'];
  176. $form['comments'] = array(
  177. '#prefix' => '<ul>',
  178. '#suffix' => '</ul>',
  179. '#tree' => TRUE,
  180. );
  181. // array_filter() returns only elements with actual values.
  182. $comment_counter = 0;
  183. foreach (array_filter($edit['comments']) as $cid => $value) {
  184. $comment = comment_load($cid);
  185. if (is_object($comment) && is_numeric($comment->cid)) {
  186. $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
  187. $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
  188. $comment_counter++;
  189. }
  190. }
  191. $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
  192. if (!$comment_counter) {
  193. drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
  194. drupal_goto('admin/content/comment');
  195. }
  196. else {
  197. return confirm_form($form,
  198. t('Are you sure you want to delete these comments and all their children?'),
  199. 'admin/content/comment', t('This action cannot be undone.'),
  200. t('Delete comments'), t('Cancel'));
  201. }
  202. }
  203. /**
  204. * Process comment_multiple_delete_confirm form submissions.
  205. */
  206. function comment_multiple_delete_confirm_submit($form, &$form_state) {
  207. if ($form_state['values']['confirm']) {
  208. comment_delete_multiple(array_keys($form_state['values']['comments']));
  209. cache_clear_all();
  210. $count = count($form_state['values']['comments']);
  211. watchdog('content', 'Deleted @count comments.', array('@count' => $count));
  212. drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
  213. }
  214. $form_state['redirect'] = 'admin/content/comment';
  215. }
  216. /**
  217. * Page callback for comment deletions.
  218. */
  219. function comment_confirm_delete_page($cid) {
  220. if ($comment = comment_load($cid)) {
  221. return drupal_get_form('comment_confirm_delete', $comment);
  222. }
  223. return MENU_NOT_FOUND;
  224. }
  225. /**
  226. * Form builder; Builds the confirmation form for deleting a single comment.
  227. *
  228. * @ingroup forms
  229. * @see comment_confirm_delete_submit()
  230. */
  231. function comment_confirm_delete($form, &$form_state, $comment) {
  232. $form['#comment'] = $comment;
  233. // Always provide entity id in the same form key as in the entity edit form.
  234. $form['cid'] = array('#type' => 'value', '#value' => $comment->cid);
  235. return confirm_form(
  236. $form,
  237. t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
  238. 'node/' . $comment->nid,
  239. t('Any replies to this comment will be lost. This action cannot be undone.'),
  240. t('Delete'),
  241. t('Cancel'),
  242. 'comment_confirm_delete');
  243. }
  244. /**
  245. * Process comment_confirm_delete form submissions.
  246. */
  247. function comment_confirm_delete_submit($form, &$form_state) {
  248. $comment = $form['#comment'];
  249. // Delete the comment and its replies.
  250. comment_delete($comment->cid);
  251. drupal_set_message(t('The comment and all its replies have been deleted.'));
  252. watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
  253. // Clear the cache so an anonymous user sees that his comment was deleted.
  254. cache_clear_all();
  255. $form_state['redirect'] = "node/$comment->nid";
  256. }