From 0225c3d953ddc246b69927b6ebfee5302a72d3e7 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Mon, 23 May 2011 01:32:54 -0400
Subject: [PATCH] Issue #193409 by dami, catch, pillarsdotnet: List descendent comments on delete confirm page.

---
 modules/comment/comment.admin.inc |   67 +++++++++++++++++++++++++++++++-----
 1 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/modules/comment/comment.admin.inc b/modules/comment/comment.admin.inc
index 4f3d35071536658cda832478b20f6d99ffe2a5ef..70afb6eee51d31cfd900cf787bb49d93d1255cf9 100644
--- a/modules/comment/comment.admin.inc
+++ b/modules/comment/comment.admin.inc
@@ -200,26 +200,28 @@ function comment_multiple_delete_confirm($form, &$form_state) {
     '#tree' => TRUE,
   );
   // array_filter() returns only elements with actual values.
-  $comment_counter = 0;
+  $comments = array();
   foreach (array_filter($edit['comments']) as $cid => $value) {
     $comment = comment_load($cid);
     if (is_object($comment) && is_numeric($comment->cid)) {
-      $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
-      $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
-      $comment_counter++;
+      $comments[] =  $comment;
+      $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($comment->subject) . '</li>');
     }
   }
   $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
 
-  if (!$comment_counter) {
+  if (empty($comments)) {
     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 confirm_form(
+      $form,
+      t('Are you sure you want to delete these comments?'),
+      'admin/content/comment',
+      _comment_delete_description($comments),
+      t('Delete comments'), t('Cancel')
+    );
   }
 }
 
@@ -261,7 +263,7 @@ function comment_confirm_delete($form, &$form_state, $comment) {
     $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.'),
+    _comment_delete_description(array($comment)),
     t('Delete'),
     t('Cancel'),
     'comment_confirm_delete');
@@ -281,3 +283,48 @@ function comment_confirm_delete_submit($form, &$form_state) {
 
   $form_state['redirect'] = "node/$comment->nid";
 }
+
+/**
+ * Get all replies (including descendants down in the tree) to a comment.
+ *
+ * @param $comment
+ *   The comment object.
+ * @param &$descendants
+ *   A writable variable containing the array of all descendant comments.
+ */
+function _comment_descendants($comment, &$descendants) {
+  $result = db_select('comment', 'c')
+    ->fields('c')
+    ->condition('c.pid', $comment->cid)
+    ->execute();
+  foreach ($result as $child) {
+    $descendants[] = $child;
+    _comment_descendants($child, $descendants);
+  }
+}
+
+function _comment_delete_description(array $comments) {
+  $descendants = array();
+  foreach($comments as $comment) {
+    _comment_descendants($comment, $descendants);
+  }
+  if (empty($descendants)) {
+    $description = t('This action cannot be undone');
+  }
+  else {
+    $description = t('The following replies will also be deleted. This action cannot be undone.');
+    $items = array();
+    foreach ($descendants as $c) {
+      $args = array(
+        '!subject' => l(
+          $c->subject, 'node/'. $comment->nid .'/#comment-'. $c->cid
+        ),
+        '!user' => theme('username', array('account' => user_load($c->uid))),
+      );
+      $items[] = t('!subject by !user', $args);
+    }
+    error_log('items = ' . var_export($items, 1));
+    $description .= theme('item_list', array('items' => $items));
+  }
+  return $description;
+}
-- 
1.7.4.1

