Index: modules/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment.module,v
retrieving revision 1.384
diff -u -F^f -r1.384 comment.module
--- modules/comment.module	20 Oct 2005 09:27:36 -0000	1.384
+++ modules/comment.module	30 Oct 2005 11:02:18 -0000
@@ -348,53 +348,43 @@ function comment_reply($nid, $pid = NULL
   $node = node_load($nid);
   menu_set_location(array(array('path' => "node/$nid", 'title' => $node->title), array('path' => "comment/reply/$nid")));
 
-  $output = '';
+  $op = isset($_POST['op']) ? $_POST['op'] : '';
 
-  // are we posting or previewing a reply?
-  if ($_POST['op'] == t('Post comment')) {
-    $edit = $_POST['edit'];
-    $edit = comment_validate($edit);
-    drupal_set_title(t('Post comment'));
-    if (!$cid = comment_save($edit)) {
-      // comment could not be posted. show edit form with errors
-      return comment_preview($edit);
-    }
-    else {
-      drupal_goto("node/$nid#comment-$cid");
-    }
-  }
-  else if ($_POST['op'] == t('Preview comment')) {
-    $edit = $_POST['edit'];
-    $edit = comment_validate($edit);
-    drupal_set_title(t('Preview comment'));
-    return comment_preview($edit);
-  }
+  $output = '';
 
-  // or are we merely showing the form?
   if (user_access('access comments')) {
-
-    // if this is a reply to another comment, show that comment first
-    // else, we'll just show the user the node they're commenting on.
-    if ($pid) {
-      $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $pid, COMMENT_PUBLISHED));
-      $comment = drupal_unpack($comment);
-      $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
-      $output .= theme('comment_view', $comment);
-    }
-    else if (user_access('access content')) {
-      $output .= node_view($node);
-      $pid = 0;
-    }
-
-    // should we show the reply box?
-    if (node_comment_mode($nid) != 2) {
-      drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
-    }
-    else if (user_access('post comments')) {
-      $output .= comment_form(array('pid' => $pid, 'nid' => $nid), t('Reply'));
+    if ($op == t('Preview comment')) {
+      if (user_access('post comments')) {
+        $output .= comment_form(array('pid' => $pid, 'nid' => $nid), NULL);
+      }
+      else {
+        drupal_set_message(t('You are not authorized to post comments.'), 'error');
+      }
     }
     else {
-      drupal_set_message(t('You are not authorized to post comments.'), 'error');
+      // if this is a reply to another comment, show that comment first
+      // else, we'll just show the user the node they're commenting on.
+      if ($pid) {
+        $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $pid, COMMENT_PUBLISHED));
+        $comment = drupal_unpack($comment);
+        $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
+        $output .= theme('comment_view', $comment);
+      }
+      else if (user_access('access content')) {
+        $output .= node_view($node);
+        $pid = 0;
+      }
+
+      // should we show the reply box?
+      if (node_comment_mode($nid) != 2) {
+        drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
+      }
+      else if (user_access('post comments')) {
+        $output .= comment_form(array('pid' => $pid, 'nid' => $nid), t('Reply'));
+      }
+      else {
+        drupal_set_message(t('You are not authorized to post comments.'), 'error');
+      }
     }
   }
   else {
@@ -521,8 +511,11 @@ function comment_preview($edit) {
   if (!form_get_errors()) {
     $output .= theme('comment_view', $comment);
   }
+/*
+  TODO: this results in the form into a infinite loop
   $output .= comment_form($edit, t('Reply'));
 
+  TODO: this should sit under the textarea in the preview
   if ($edit['pid']) {
     $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
     $comment = drupal_unpack($comment);
@@ -533,7 +526,7 @@ function comment_preview($edit) {
     $output .= node_view(node_load($edit['nid']));
     $edit['pid'] = 0;
   }
-
+*/
   return $output;
 }
 
@@ -1062,6 +1055,8 @@ function comment_num_new($nid, $timestam
 function comment_form($edit, $title = NULL) {
   global $user;
 
+  $op = isset($_POST['op']) ? $_POST['op'] : '';
+
   if ($user->uid) {
     if ($edit['cid'] && user_access('administer comments')) {
       if ($edit['author']) {
@@ -1128,10 +1123,10 @@ function comment_form($edit, $title = NU
 
   $form = array_merge($form, filter_form($node->format));
 
-  $form['cid'] = array('#type' => 'hidden', '#value' => $edit['cid']);
-  $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
-  $form['nid'] = array('#type' => 'hidden', '#value' => $edit['nid']);
-  $form['uid'] = array('#type' => 'hidden', '#value' => $edit['uid']);
+  $form['cid'] = array('#type' => 'value', '#value' => $edit['cid']);
+  $form['pid'] = array('#type' => 'value', '#value' => $edit['pid']);
+  $form['nid'] = array('#type' => 'value', '#value' => $edit['nid']);
+  $form['uid'] = array('#type' => 'value', '#value' => $edit['uid']);
 
   $form['preview'] = array('#type' => 'submit', '#value' => t('Preview comment'), '#weight' => 19);
   $form['#token'] = 'comment' . $edit['nid'] . $edit['pid'];
@@ -1139,14 +1134,21 @@ function comment_form($edit, $title = NU
   // Only show post button if preview is optional or if we are in preview mode.
   // We show the post button in preview mode even if there are form errors so that
   // optional form elements (e.g., captcha) can be updated in preview mode.
-  if ((variable_get('comment_preview', 1) == 0) || ($_POST['op'] == t('Preview comment')) || ($_POST['op'] == t('Post comment'))) {
+  if ((variable_get('comment_preview', 1) == 0) || ($op == t('Preview comment')) || ($op == t('Post comment'))) {
     $form['submit'] = array('#type' => 'submit', '#value' => t('Post comment'), '#weight' => 20);
   }
 
+  if ($op == t('Preview comment')) {
+    $form['#post_process'] = 'comment_form_add_preview';
+  }
+
   if ($_REQUEST['destination']) {
     $form['#attributes']['destination'] = $_REQUEST['destination'];
   }
-  $form['#action'] = url('comment/reply/'. $edit['nid']);
+
+  if (empty($edit['cid']) && empty($edit['pid'])) {
+    $form['#action'] = url('comment/reply/'. $edit['nid']);
+  }
 
   // Graft in extra form additions
   $form = array_merge($form, comment_invoke_comment($form, 'form'));
@@ -1154,23 +1156,54 @@ function comment_form($edit, $title = NU
   return theme('box', $title, drupal_get_form('comment_form', $form));
 }
 
+function comment_form_add_preview($form_id, $form, $edit) {
+  $form['comment_preview'] = array('#value' => comment_preview(comment_validate($edit)), '#weight' => -100);
+  return $form;
+}
+
+function comment_form_validate($form_id, $edit) {
+  comment_validate($edit);
+}
+
+function comment_form_execute($form_id, $edit) {
+
+  $op = isset($_POST['op']) ? $_POST['op'] : '';
+  $nid = $edit['nid'];
+  
+  // are we posting or previewing a reply?
+  if ($op == t('Post comment')) {
+    drupal_set_title(t('Post comment'));
+    if ($cid = comment_save($edit)) {
+      drupal_goto("node/$nid#comment-$cid");
+    }
+  }
+  else if ($_POST['op'] == t('Preview comment')) {
+    drupal_set_title(t('Preview comment'));
+  }
+}
+
 /*
 ** Renderer or visualization functions this can be optionally
 ** overridden by themes.
 */
 
 function theme_comment_form($form) {
-  return form_render($form);
+  $output = '';
+  if (isset($form['comment_preview'])) {
+    $output .= form_render($form['comment_preview']);
+  }
+  $output .= form_render($form);
+  return $output;
 }
 
-function theme_comment_preview($comment, $links = '', $visible = 1) {
+function theme_comment_preview($comment, $links = array(), $visible = 1) {
   $output = '<div class="preview">';
   $output .= theme('comment_view', $comment, $links, $visible);
   $output .= '</div>';
   return $output;
 };
 
-function theme_comment_view($comment, $links = '', $visible = 1) {
+function theme_comment_view($comment, $links = array(), $visible = 1) {
 
   // Emit selectors:
   $output = '';
@@ -1225,7 +1258,7 @@ function theme_comment_controls($form) {
   return theme('box', t('Comment viewing options'), $output);
 }
 
-function theme_comment($comment, $links = 0) {
+function theme_comment($comment, $links = array()) {
   $output  = '<div class="comment">';
   $output .= '<div class="subject">'. l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid") . ' ' . theme('mark', $comment->new) ."</div>\n";
   $output .= '<div class="credit">'. t('by %a on %b', array('%a' => theme('username', $comment), '%b' => format_date($comment->timestamp))) ."</div>\n";
