webmaster@1: 'fieldset', '#title' => t('Update options'),
webmaster@1: '#prefix' => '
', '#suffix' => '
'
webmaster@1: );
webmaster@1: $options = array();
webmaster@1: foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
webmaster@1: $options[$key] = $value[0];
webmaster@1: }
webmaster@1: $form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'publish');
webmaster@1: $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
webmaster@1:
webmaster@1: // load the comments that we want to display
webmaster@1: $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
webmaster@1: $form['header'] = array('#type' => 'value', '#value' => array(
webmaster@1: theme('table_select_header_cell'),
webmaster@1: array('data' => t('Subject'), 'field' => 'subject'),
webmaster@1: array('data' => t('Author'), 'field' => 'name'),
webmaster@1: array('data' => t('Posted in'), 'field' => 'node_title'),
webmaster@1: array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
webmaster@1: array('data' => t('Operations'))
webmaster@1: ));
webmaster@1: $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);
webmaster@1:
webmaster@1: // build a table listing the appropriate comments
webmaster@1: $destination = drupal_get_destination();
webmaster@1: while ($comment = db_fetch_object($result)) {
webmaster@1: $comments[$comment->cid] = '';
webmaster@1: $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
webmaster@7: $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)));
webmaster@1: $form['username'][$comment->cid] = array('#value' => theme('username', $comment));
webmaster@1: $form['node_title'][$comment->cid] = array('#value' => l($comment->node_title, 'node/'. $comment->nid));
webmaster@1: $form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small'));
webmaster@1: $form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array('query' => $destination)));
webmaster@1: }
webmaster@1: $form['comments'] = array('#type' => 'checkboxes', '#options' => isset($comments) ? $comments: array());
webmaster@1: $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
webmaster@1: return $form;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Validate comment_admin_overview form submissions.
webmaster@1: *
webmaster@1: * We can't execute any 'Update options' if no comments were selected.
webmaster@1: */
webmaster@1: function comment_admin_overview_validate($form, &$form_state) {
webmaster@1: $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
webmaster@1: if (count($form_state['values']['comments']) == 0) {
webmaster@1: form_set_error('', t('Please select one or more comments to perform the update on.'));
webmaster@1: drupal_goto('admin/content/comment');
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process comment_admin_overview form submissions.
webmaster@1: *
webmaster@1: * Execute the chosen 'Update option' on the selected comments, such as
webmaster@1: * publishing, unpublishing or deleting.
webmaster@1: */
webmaster@1: function comment_admin_overview_submit($form, &$form_state) {
webmaster@1: $operations = comment_operations();
webmaster@1: if ($operations[$form_state['values']['operation']][1]) {
webmaster@1: // extract the appropriate database query operation
webmaster@1: $query = $operations[$form_state['values']['operation']][1];
webmaster@1: foreach ($form_state['values']['comments'] as $cid => $value) {
webmaster@1: if ($value) {
webmaster@1: // perform the update action, then refresh node statistics
webmaster@1: db_query($query, $cid);
webmaster@1: $comment = _comment_load($cid);
webmaster@1: _comment_update_node_statistics($comment->nid);
webmaster@1: // Allow modules to respond to the updating of a comment.
webmaster@1: comment_invoke_comment($comment, $form_state['values']['operation']);
webmaster@1: // Add an entry to the watchdog log.
webmaster@1: watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)));
webmaster@1: }
webmaster@1: }
webmaster@1: cache_clear_all();
webmaster@1: drupal_set_message(t('The update has been performed.'));
webmaster@1: $form_state['redirect'] = 'admin/content/comment';
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme the comment admin form.
webmaster@1: *
webmaster@1: * @param $form
webmaster@1: * An associative array containing the structure of the form.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_admin_overview($form) {
webmaster@1: $output = drupal_render($form['options']);
webmaster@1: if (isset($form['subject']) && is_array($form['subject'])) {
webmaster@1: foreach (element_children($form['subject']) as $key) {
webmaster@1: $row = array();
webmaster@1: $row[] = drupal_render($form['comments'][$key]);
webmaster@1: $row[] = drupal_render($form['subject'][$key]);
webmaster@1: $row[] = drupal_render($form['username'][$key]);
webmaster@1: $row[] = drupal_render($form['node_title'][$key]);
webmaster@1: $row[] = drupal_render($form['timestamp'][$key]);
webmaster@1: $row[] = drupal_render($form['operations'][$key]);
webmaster@1: $rows[] = $row;
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
webmaster@1: }
webmaster@1:
webmaster@1: $output .= theme('table', $form['header']['#value'], $rows);
webmaster@1: if ($form['pager']['#value']) {
webmaster@1: $output .= drupal_render($form['pager']);
webmaster@1: }
webmaster@1:
webmaster@1: $output .= drupal_render($form);
webmaster@1:
webmaster@1: return $output;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * List the selected comments and verify that the admin really wants to delete
webmaster@1: * them.
webmaster@1: *
webmaster@1: * @param $form_state
webmaster@1: * An associative array containing the current state of the form.
webmaster@1: * @return
webmaster@1: * TRUE if the comments should be deleted, FALSE otherwise.
webmaster@1: * @ingroup forms
webmaster@1: * @see comment_multiple_delete_confirm_submit()
webmaster@1: */
webmaster@1: function comment_multiple_delete_confirm(&$form_state) {
webmaster@1: $edit = $form_state['post'];
webmaster@1:
webmaster@1: $form['comments'] = array('#prefix' => '', '#tree' => TRUE);
webmaster@1: // array_filter() returns only elements with actual values
webmaster@1: $comment_counter = 0;
webmaster@1: foreach (array_filter($edit['comments']) as $cid => $value) {
webmaster@1: $comment = _comment_load($cid);
webmaster@1: if (is_object($comment) && is_numeric($comment->cid)) {
webmaster@1: $subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid));
webmaster@1: $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '', '#suffix' => check_plain($subject) .'');
webmaster@1: $comment_counter++;
webmaster@1: }
webmaster@1: }
webmaster@1: $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
webmaster@1:
webmaster@1: if (!$comment_counter) {
webmaster@1: drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.'));
webmaster@1: drupal_goto('admin/content/comment');
webmaster@1: }
webmaster@1: else {
webmaster@1: return confirm_form($form,
webmaster@1: t('Are you sure you want to delete these comments and all their children?'),
webmaster@1: 'admin/content/comment', t('This action cannot be undone.'),
webmaster@1: t('Delete comments'), t('Cancel'));
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process comment_multiple_delete_confirm form submissions.
webmaster@1: *
webmaster@1: * Perform the actual comment deletion.
webmaster@1: */
webmaster@1: function comment_multiple_delete_confirm_submit($form, &$form_state) {
webmaster@1: if ($form_state['values']['confirm']) {
webmaster@1: foreach ($form_state['values']['comments'] as $cid => $value) {
webmaster@1: $comment = _comment_load($cid);
webmaster@1: _comment_delete_thread($comment);
webmaster@1: _comment_update_node_statistics($comment->nid);
webmaster@1: }
webmaster@1: cache_clear_all();
webmaster@1: drupal_set_message(t('The comments have been deleted.'));
webmaster@1: }
webmaster@1: $form_state['redirect'] = 'admin/content/comment';
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Menu callback; delete a comment.
webmaster@1: *
webmaster@1: * @param $cid
webmaster@1: * The comment do be deleted.
webmaster@1: */
webmaster@1: function comment_delete($cid = NULL) {
webmaster@1: $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));
webmaster@1: $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
webmaster@1:
webmaster@1: $output = '';
webmaster@1:
webmaster@1: if (is_object($comment) && is_numeric($comment->cid)) {
webmaster@1: $output = drupal_get_form('comment_confirm_delete', $comment);
webmaster@1: }
webmaster@1: else {
webmaster@1: drupal_set_message(t('The comment no longer exists.'));
webmaster@1: }
webmaster@1:
webmaster@1: return $output;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Form builder; Builds the confirmation form for deleting a single comment.
webmaster@1: *
webmaster@1: * @ingroup forms
webmaster@1: * @see comment_confirm_delete_submit()
webmaster@1: */
webmaster@1: function comment_confirm_delete(&$form_state, $comment) {
webmaster@1: $form = array();
webmaster@1: $form['#comment'] = $comment;
webmaster@1: return confirm_form(
webmaster@1: $form,
webmaster@1: t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
webmaster@1: 'node/'. $comment->nid,
webmaster@1: t('Any replies to this comment will be lost. This action cannot be undone.'),
webmaster@1: t('Delete'),
webmaster@1: t('Cancel'),
webmaster@1: 'comment_confirm_delete');
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process comment_confirm_delete form submissions.
webmaster@1: */
webmaster@1: function comment_confirm_delete_submit($form, &$form_state) {
webmaster@1: drupal_set_message(t('The comment and all its replies have been deleted.'));
webmaster@1:
webmaster@1: $comment = $form['#comment'];
webmaster@1:
webmaster@1: // Delete comment and its replies.
webmaster@1: _comment_delete_thread($comment);
webmaster@1:
webmaster@1: _comment_update_node_statistics($comment->nid);
webmaster@1:
webmaster@1: // Clear the cache so an anonymous user sees that his comment was deleted.
webmaster@1: cache_clear_all();
webmaster@1:
webmaster@1: $form_state['redirect'] = "node/$comment->nid";
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Perform the actual deletion of a comment and all its replies.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * An associative array describing the comment to be deleted.
webmaster@1: */
webmaster@1: function _comment_delete_thread($comment) {
webmaster@1: if (!is_object($comment) || !is_numeric($comment->cid)) {
webmaster@7: watchdog('content', 'Cannot delete non-existent comment.', array(), WATCHDOG_WARNING);
webmaster@1: return;
webmaster@1: }
webmaster@1:
webmaster@1: // Delete the comment:
webmaster@1: db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid);
webmaster@1: watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject));
webmaster@1:
webmaster@1: comment_invoke_comment($comment, 'delete');
webmaster@1:
webmaster@1: // Delete the comment's replies
webmaster@1: $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);
webmaster@1: while ($comment = db_fetch_object($result)) {
webmaster@1: $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
webmaster@1: _comment_delete_thread($comment);
webmaster@1: }
webmaster@1: }