From 38a1300df2005f9f054f19d8d2bd41518a8e7ad8 Mon Sep 17 00:00:00 2001 From: Steven Wittens Date: Fri, 22 Jun 2007 05:44:21 +0000 Subject: #147723: Deletion API (by hunmonk). Woop woop. --- modules/comment/comment.module | 137 ++++++++++++++++++++++++----------------- 1 file changed, 82 insertions(+), 55 deletions(-) (limited to 'modules/comment/comment.module') diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 1c9002dca..bb1dee560 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -232,7 +232,8 @@ function comment_menu() { $items['comment/delete'] = array( 'title' => 'Delete comment', - 'page callback' => 'comment_delete', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('comment_delete', 2), 'access arguments' => array('administer comments'), 'type' => MENU_CALLBACK, ); @@ -474,8 +475,8 @@ function comment_nodeapi(&$node, $op, $arg = 0) { break; case 'delete': - db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid); - db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {comments} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid); break; case 'update index': @@ -1093,50 +1094,44 @@ function comment_render($node, $cid = 0) { /** * Menu callback; delete a comment. */ -function comment_delete($cid = NULL) { +function comment_delete(&$form_state, $cid = NULL) { $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)); $comment->name = $comment->uid ? $comment->registered_name : $comment->name; - $output = ''; - if (is_object($comment) && is_numeric($comment->cid)) { - $output = drupal_get_form('comment_confirm_delete', $comment); + drupal_delete_initiate('comment', $comment->cid); + drupal_delete_add_callback( + array( + 'comment_delete_post' => array($comment), + // Clear the cache so an anonymous poster can see the node being deleted. + 'cache_clear_all' => array(), + ) + ); + + // Delete comment and its replies. + _comment_delete_thread($comment); + + return drupal_delete_confirm( + array( + 'question' => t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)), + 'path' => 'node/'. $comment->nid, + 'description' => t('Any replies to this comment will be lost. This action cannot be undone.'), + 'destination' => 'node/'. $comment->nid, + ) + ); } else { - drupal_set_message(t('The comment no longer exists.')); + drupal_set_message(t('The comment no longer exists.'), 'error'); + drupal_goto(''); } - - return $output; -} - -function comment_confirm_delete(&$form_state, $comment) { - $form = array(); - $form['#comment'] = $comment; - return confirm_form( - $form, - t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)), - 'node/'. $comment->nid, - t('Any replies to this comment will be lost. This action cannot be undone.'), - t('Delete'), - t('Cancel'), - 'comment_confirm_delete'); } -function comment_confirm_delete_submit($form, &$form_state) { - drupal_set_message(t('The comment and all its replies have been deleted.')); +function comment_delete_post($comment) { - $comment = $form['#comment']; - - // Delete comment and its replies. - _comment_delete_thread($comment); + drupal_set_message(t('The comment %subject and all its replies have been deleted.', array('%subject' => $comment->subject))); + watchdog('content', 'Comment: deleted %subject and all its replies.', array('%subject' => $comment->subject)); _comment_update_node_statistics($comment->nid); - - // Clear the cache so an anonymous user sees that his comment was deleted. - cache_clear_all(); - - $form_state['redirect'] = "node/$comment->nid"; - return; } /** @@ -1291,45 +1286,78 @@ function theme_comment_admin_overview($form) { function comment_multiple_delete_confirm(&$form_state) { $edit = $form_state['post']; - $form['comments'] = array('#prefix' => '', '#tree' => TRUE); + $form['comments']['#tree'] = TRUE; + $form['prefix'] = array('#value' => ''); + $form['operation'] = array( + '#type' => 'hidden', + '#value' => 'delete' + ); + + // New package for the main admin callback. + drupal_delete_initiate('comment', 'admin'); + + // Add the main callback for the mass deletion last. + drupal_delete_add_callback(array('comment_multiple_delete_confirm_post' => array())); if (!$comment_counter) { drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.')); drupal_goto('admin/content/comment'); } else { - return confirm_form($form, - t('Are you sure you want to delete these comments and all their children?'), - 'admin/content/comment', t('This action cannot be undone.'), - t('Delete comments'), t('Cancel')); + return drupal_delete_confirm( + array( + 'form' => $form, + 'question' => t('Are you sure you want to delete these comments and all their children?'), + 'path' => 'admin/content/comment', + 'yes' => t('Delete all'), + 'destination' => 'admin/content/comment', + ) + ); } } /** - * Perform the actual comment deletion. + * Post deletion callback for multiple comment deletion. */ -function comment_multiple_delete_confirm_submit($form, &$form_state) { - if ($form_state['values']['confirm']) { - foreach ($form_state['values']['comments'] as $cid => $value) { - $comment = _comment_load($cid); - _comment_delete_thread($comment); - _comment_update_node_statistics($comment->nid); - } +function comment_multiple_delete_confirm_post() { cache_clear_all(); drupal_set_message(t('The comments have been deleted.')); - } - drupal_goto('admin/content/comment'); } /** @@ -1892,8 +1920,7 @@ function _comment_delete_thread($comment) { } // Delete the comment: - db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid); - watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject)); + drupal_delete_add_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid); comment_invoke_comment($comment, 'delete'); -- cgit v1.2.3