--- /Users/nvahalik/Downloads/casetracker/casetracker.module	2010-10-29 13:35:35.000000000 -0500
+++ /Users/nvahalik/Sites/drupal-6.19/sites/all/modules/casetracker/casetracker.module	2010-11-03 16:51:46.000000000 -0500
@@ -245,9 +241,17 @@ function casetracker_comment(&$comment, 
 
     // Populate old state values from node
     $old = $node->casetracker;
+    $old->title = $node->title;
     $old->cid = $comment['cid'];
     $old->state = 0;
 
+    // We have to do this first because updating the node causes all of the
+    // casetracker values to be reset to $old.
+    if ($old->title != $new->title) {
+      $node->title = $new->title;
+      node_save($node);
+    }
+
     drupal_write_record('casetracker_case', $new, array('nid', 'vid'));
   }
 
@@ -261,8 +265,54 @@ function casetracker_comment(&$comment, 
       drupal_write_record('casetracker_comment_status', $new, array('cid', 'state'));
       break;
     case 'delete':
-      // @todo theoretically, if you delete a comment, we should reset all the values
-      // to what they were before the comment was submitted. this doesn't happen yet.
+      /**
+       * Handle deleting of comments.  There are a few scenarios to consider when deleting
+       * comments:
+       * 
+       * 1. The deleted comment was the only comment on the ticket.  Therefore the old
+       * values stored in the casetracker_comment_status table should be applied.
+       * 
+       * 2. The deleted comment was *not* the newest comment in the system and therefore
+       * no changes need to be made to the casetracker_comment_status table.
+       * 
+       * 3. The deleted comment was the newest comment in the system.  However, because
+       * we don't know if a comment before it was deleted, we should attempt to grab the
+       * next "freshest" comment's changes and use those when reverting the case tracker
+       * data.
+       * 
+       * At this point, the comment has already been deleted out of the comments table,
+       * so we really have to go off of cid.  It should be pretty safe to bet that the
+       * cids will be sequential. :)
+       * 
+       * @todo (maybe?) Suppose a comment is made (#1), changing the status, and then another comment
+       * (#2) subsequently does the same.  If comment #1 gets deleted, and then comment
+       * #2 gets deleted, there is no record of what it was, so it will be reverted to
+       * #1.  But #1 won't exist so it will be all weird.  Basically, #1 above applies
+       * but it isn't an accurate picture of the state of the case.  But who would do that
+       * anyway?
+       */
+      $comments_by_cid = db_query('SELECT cid FROM {comments} WHERE nid = %d ORDER BY cid DESC', $node->nid);
+      $newest_comment = db_result($comments_by_cid);
+      if ($comment->cid > $newest_comment) {
+        $deleted_comment = db_fetch_object(db_query('SELECT * FROM {casetracker_comment_status} WHERE state = 1 AND cid = %d', $comment->cid));
+        if ($newest_comment != FALSE) {
+          $replacement_comment = db_fetch_object(db_query('SELECT * FROM {casetracker_comment_status} WHERE state = 1 AND cid = %d', $newest_comment));
+        }
+        else { // there is no other comment, use stored values.
+          $replacement_comment = db_fetch_object(db_query('SELECT * FROM {casetracker_comment_status} WHERE state = 0 AND cid = %d', $comment->cid));
+        }
+
+        // Revert the title if it was also updated.
+        if ($deleted_comment->title != $replacement_comment->title) {
+          $node->title = $replacement_comment->title;
+          node_save($node);
+        }
+        // Update the casetracker_case table with the old values.  We need to stick the nid and vid 
+        // values in here because the record will not have it.
+        $replacement_comment->nid = $node->nid;
+        $replacement_comment->vid = $node->vid;
+        drupal_write_record('casetracker_case', $replacement_comment, array('nid', 'vid'));
+      }
       db_query("DELETE FROM {casetracker_comment_status} WHERE cid = %d", $comment->cid);
       break;
     case 'view':
@@ -272,6 +322,7 @@ function casetracker_comment(&$comment, 
         $case_data['new']->assign_to = casetracker_get_uid($case_data['new']->assign_to);
         $case = node_load($comment->nid);
         $case_data['old'] = drupal_clone($case->casetracker);
+        $case_data['old']->title = $node->title;
       }
       else {
         $results = db_query("SELECT * FROM {casetracker_comment_status} WHERE cid = %d", $comment->cid);
@@ -287,8 +338,11 @@ function casetracker_comment(&$comment, 
 
 /**
  * Implementation of hook_form_alter().
- */
+ */ 
 function casetracker_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'comment_form') { // interferes with comment form alter
+    return;
+  }
   if (!empty($form['#node'])) {
     $node = $form['#node'];
 
@@ -320,6 +374,16 @@ function casetracker_form_comment_form_a
 
     // add case options to the comment form.
     casetracker_case_form_common($form);
+    // We only want this on the comment form...
+    $form['casetracker']['title'] = array(
+      '#type'              => 'textfield',
+      '#title'             => t('Title'),
+      '#required'          => TRUE,
+      '#weight'            => -10,
+      '#default_value'     => isset($node->title) ? $node->title : NULL,	 
+      '#prefix'            => '<div id="comment-case-title">',  
+      '#suffix'            => '</div>', // escapes the inlining.  
+    );
 
     // necessary for our casetracker_comment() callback.
     $form['revision_id'] = array('#type' => 'hidden', '#value' => $node->vid);
@@ -803,7 +865,7 @@ function theme_casetracker_case_form_com
   drupal_add_css(drupal_get_path('module', 'casetracker') .'/casetracker.css');
   $output = '';
   $output .= drupal_render($form['pid']);
-  $output .= drupal_render($form['case_title']);
+  $output .= drupal_render($form['title']);
 
   if ($form['assign_to']['#type'] == 'radios') {
     if ($form['assign_to']['#access']) {
@@ -824,7 +886,7 @@ function theme_casetracker_case_form_com
 
   $row = array();
   foreach (element_children($form) as $id) {
-    if (!in_array($id, array('pid', 'case_title', 'assign_to'))) {
+    if (!in_array($id, array('pid', 'title', 'assign_to'))) {
       $row[] = drupal_render($form[$id]);
     }
   }
