Index: modules/project/comment.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project/comment.inc,v
retrieving revision 1.61
diff -u -F^f -r1.61 comment.inc
--- modules/project/comment.inc	31 Mar 2006 15:43:13 -0000	1.61
+++ modules/project/comment.inc	22 Apr 2006 04:17:16 -0000
@@ -6,6 +6,7 @@ function project_comment_page() {
   if (user_access('create project issues')) {
     $edit = (object) $_POST['edit'];
     $node = node_load(array('nid' => arg(3), 'type' => 'project_issue'));
+    $edit->nid = $node->nid;
 
     if ($_POST['op'] == t('Preview') || $_POST['op'] == t('Submit')) {
       $edit = project_comment_validate($edit);
@@ -61,12 +62,15 @@ function project_comment_form(&$edit, &$
   if ($edit->cid) {
     $form['cid']= array('#type' => 'hidden', '#value' => $edit->cid);
   }
+  if ($edit->nid) {
+    $form['nid']= array('#type' => 'hidden', '#value' => $edit->nid);
+  }
   $form['preview'] = array('#type' => 'button', '#value' => t('Preview'));
   if (!form_get_errors()) {
     $form['submit'] = array('#type' => 'button', '#value' => t('Submit'));
   }
   if ($op == t('Preview')) {
-    $form['#after_build'] = 'node_form_add_preview';
+    $form['#after_build'] = 'project_comment_form_add_preview';
   }
   return drupal_get_form('project_comment_form', $form);
 }
@@ -95,6 +99,25 @@ function project_comment_view($node, $ma
   global $user;
   $links = array();
   $rows = array();
+  $result = db_query('SELECT p.*, u.name FROM {project_comments} p INNER JOIN {users} u USING (uid) WHERE p.nid = %d ORDER BY p.created ASC', $node->nid);
+  if (db_num_rows($result)) {
+    $i = 0;
+    while ($comment = db_fetch_object($result)) {
+      $i++;
+      $rows = array_merge($rows, _project_comment_view_one($comment, $i));
+    }
+    $output = '<div class="project">';
+    $output .= theme('table', array(), $rows);
+    $output .= '</div>';
+
+    return theme('box', t('Updates'), $output);
+  }
+}
+
+function _project_comment_view_one($comment, $count) {
+  $comment = (object)$comment;
+  $rows = array();
+  $data = array();
   $fields = array(
     'title' => 'Title',
     'pid' => 'Project',
@@ -106,46 +129,62 @@ function project_comment_view($node, $ma
     'sid' => 'Status'
   );
 
-  $result = db_query('SELECT p.*, u.name FROM {project_comments} p INNER JOIN {users} u USING (uid) WHERE p.nid = %d ORDER BY p.created ASC', $node->nid);
-  if (db_num_rows($result)) {
-    $i = 0;
-    while ($comment = db_fetch_object($result)) {
-      $i++;
-      $data = unserialize($comment->data);
-      $summary = array();
-      foreach ($fields as $field => $text) {
-        if (isset($data['old']->$field) && isset($data['new']->$field)) {
-          $summary[] = array(
-            t($text) .':',
-            project_mail_summary($field, $data['old']->$field),
-            '&raquo; '. project_mail_summary($field, $data['new']->$field)
-          );
-        }
-      }
-
-      if ($comment->file_path && file_exists($comment->file_path)) {
-        $summary[] = array(t('Attachment:'), '<a href="'. file_create_url($comment->file_path). '">'. basename($comment->file_path) .'</a> ('. format_size($comment->file_size) .')');
-      }
-
-      if ($summary || $comment->body) {
-        $rows[] = array(array('class' => 'header', 'data' => t('%count submitted by %user on %date', array('%count' => l("#$i", "node/$node->nid", array ('id' => "comment-$comment->cid", 'name' => "comment-$comment->cid"), NULL, "comment-$comment->cid"), '%user' => theme('username', $comment), '%date' => format_date($comment->created))) . theme('mark', node_mark($comment->nid, $comment->changed))));
-        if ($summary) {
-          $rows[] = array(array('class' => 'summary', 'data' => theme('table', array(), $summary)));
-        }
-        if ($comment->body) {
-          $rows[] = array(array('class' => 'content', 'data' => '<p>'. check_markup($comment->body) .'</p>'));
-        }
-        if ($comment->fid) {
-          $rows[] = array(l(t('download attachment'), "project/comments/download/$comment->cid"));
-        }
+  if ($comment->data) {
+    $data = unserialize($comment->data);
+    // the comment is stored in the DB, so print out what changed
+    $summary = array();
+    foreach ($fields as $field => $text) {
+      if (isset($data['old']->$field) && isset($data['new']->$field)) {
+        $summary[] = array(
+          t($text) .':',
+          project_mail_summary($field, $data['old']->$field),
+          '&raquo; '. project_mail_summary($field, $data['new']->$field)
+        );
       }
     }
-    $output = '<div class="project">';
-    $output .= theme('table', array(), $rows);
-    $output .= '</div>';
+  }
+  elseif ($comment->nid) {
+    // the comment isn't in the DB (we're previewing), but we have the
+    // nid, so we compare what changed against that...
+    $node = node_load($comment->nid);
+    foreach ($fields as $field => $text) {
+      if (isset($node->$field) && isset($comment->$field) && $node->$field != $comment->$field ) {
+        $summary[] = array(
+          t($text) .':',
+          project_mail_summary($field, $node->$field),
+          '&raquo; '. project_mail_summary($field, $comment->$field)
+        );
+      }
+    }
+  }
 
-    return theme('box', t('Updates'), $output);
+  if ($comment->file_path && file_exists($comment->file_path)) {
+    $summary[] = array(t('Attachment:'), '<a href="'. file_create_url($comment->file_path). '">'. basename($comment->file_path) .'</a> ('. format_size($comment->file_size) .')');
   }
+
+  if ($summary || $comment->body) {
+    if ($count) {
+      $rows[] = array(array('class' => 'header', 'data' => t('%count submitted by %user on %date', array('%count' => l("#$count", "node/$node->nid", array ('id' => "comment-$comment->cid", 'name' => "comment-$comment->cid"), NULL, "comment-$comment->cid"), '%user' => theme('username', $comment), '%date' => format_date($comment->created))) . theme('mark', node_mark($comment->nid, $comment->changed))));
+    }
+    if ($summary) {
+      $rows[] = array(array('class' => 'summary', 'data' => theme('table', array(), $summary)));
+    }
+    if ($comment->body) {
+      if ($comment->format) {
+        $format = $comment->format;
+      }
+      elseif (isset($data['format'])) {
+        $format = $data['format'];
+      } else {
+        $format = FILTER_FORMAT_DEFAULT;
+      }
+      $rows[] = array(array('class' => 'content', 'data' => '<p>'. check_markup($comment->body, $format) .'</p>'));
+    }
+    if ($comment->fid) {
+      $rows[] = array(l(t('download attachment'), "project/comments/download/$comment->cid"));
+    }
+  }
+  return $rows;
 }
 
 function project_comment_load($cid) {
@@ -176,6 +215,10 @@ function project_comment_save($edit) {
       }
     }
 
+    if ($edit->format) {
+      $data['format'] = $edit->format;
+    }
+
     watchdog('special', "project: added comment '$edit->title'", l('view', "node/$node->nid"));
     $node->changed = time();
     db_query("INSERT INTO {project_comments} (cid, nid, uid, created, changed, body, data, file_path, file_mime, file_size) VALUES (%d, %d, %d, %d, %d, '%s', '%s', '%s', '%s', %d)", $edit->cid, $edit->nid, $edit->uid, $node->changed, $node->changed, $edit->body, serialize($data), $file->filepath, $file->filemime, $file->filesize);
@@ -190,4 +233,40 @@ function project_comment_save($edit) {
   return $edit->cid;
 }
 
+function project_comment_form_add_preview($form, $edit) {
+  drupal_set_title(t('Preview comment'));
+  $edit = project_comment_validate($edit);
+
+  // Preview the comment with security check.
+  $output = '';
+  if (!form_get_errors()) {
+    $rows = _project_comment_view_one($edit, 0);
+    $output .= theme('table', array(), $rows);
+    $output .= '<br/>';
+  }
+
+  $form['comment_preview'] = array(
+    '#value' => $output,
+    '#weight' => -100,
+    '#prefix' => '<div class="preview">',
+    '#suffix' => '</div>',
+  );
+
+/*
+  // TODO: it'd be nice to put the rest of the node at the bottom of
+  // the form, but i couldn't get this code working to properly view
+  // the node as a project_issue. :(
+
+  // now, put the rest of the node below the preview in the form...
+  $output = '';
+  if ($edit['nid']) {
+    $node = node_load(array('nid' => $edit['nid'], 'type' => 'project_issue'));
+    $output .= node_view($node);
+    $form['comment_preview_below'] = array('#value' => $output, '#weight' => 100);
+  }
+*/
+
+  return $form;
+}
+
 ?>
