--- cckasetracker/cckasetracker.module	2009-06-22 15:47:11.000000000 +0200
+++ cckasetracker.module	2009-07-27 00:33:15.000000000 +0200
@@ -19,7 +23,6 @@ function cckasetracker_help($path, $arg)
     case 'admin/help#cckasetracker':
       $output .= '<p>'. t('This module enables teams to track outstanding cases which need resolution.') .'</p>';
       $output .= '<p>'. t('As it is dependent on CCK (i.e. content module), it allows customizing your CCKase tracker options; Views, Token, Rules modules integration; etc.') .'</p>';
-      break;
   }
   return $output;
 }
@@ -55,6 +58,9 @@ function cckasetracker_menu() {
  * Implementation of hook_nodeapi().
  */
 function cckasetracker_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+  if (!cckasetracker_is_case_type($node->type)) {
+    return;
+  }
   switch ($op) {
     case 'delete':
       // Delete all the case comments.
@@ -78,8 +84,14 @@ function cckasetracker_comment(&$comment
       break;
 
     case 'view':
-      $status = cckasetracker_comment_show_status_change($comment->cid, $comment->nid);
-      $comment->comment = $status . $comment->comment;
+      $node = node_load($comment->nid);
+      if (!variable_get('cckasetracker_general_output', TRUE)) {
+        break;
+      }
+      if (cckasetracker_is_case_type($node->type)) {
+        $status = cckasetracker_comment_show_status_change($comment->cid, $comment->nid);
+        $comment->comment = $status . $comment->comment;
+      }
       break;
   }
 }
@@ -117,9 +129,12 @@ function cckasetracker_form_node_type_fo
  * Implementation of hook_form_FORM-ID_alter().
  */
 function cckasetracker_form_comment_form_alter(&$form, &$form_state) {
+  if(!variable_get('cckasetracker_general_input', TRUE)) {
+    return;
+  }
   $node = node_load($form['nid']['#value']);
   $type_name = $node->type;
-  if (variable_get('cckasetracker_content_type_'. $type_name, array())) {
+  if (variable_get('cckasetracker_type_usage_'. $type_name, 'omitted') == 'case') {
     $item = menu_get_item();
     // Alter the comment only if this is node view or comment creation.
     if ($item['path'] == 'comment/reply/%' || $item['path'] == 'node/%') {
@@ -133,6 +148,7 @@ function cckasetracker_form_comment_form
         '#weight'      => -5,
       );
       $form['cckasetracker']['cckasetracker_'. $type_name] = cckasetracker_prepare_case_comment();
+      // TODO hide cckasetracker if no visible field
 
       // Add own submit handler.
       $form['#submit'][] = 'cckasetracker_case_comment_submit';
@@ -154,10 +170,10 @@ function cckasetracker_prepare_case_comm
     // Maybe we are in comment/reply/nid, so load the correct node object.
     $node = menu_get_object('node', 2);
   }
-  $node_type = $node->type;
+  $type_name = $node->type;
   $form_state = array('storage' => NULL, 'submitted' => FALSE);
   // Embed the node-form.
-  $form = subform_element_create($node_type .'_node_form', array($node), $form_state, TRUE, TRUE);
+  $form = subform_element_create($type_name .'_node_form', array($node), $form_state, TRUE, TRUE);
 
   // Unset form elements that are not needed.
   $remove = array(
@@ -166,15 +182,19 @@ function cckasetracker_prepare_case_comm
     'revision_information',
     'comment_settings',
     'buttons',
+    // TODO remove these by config?
+    'notifications', // notifications.module - disable notifications
+    'path', // pathauto.module - node autopath alias creation
   );
   foreach ($remove as $element) {
     unset($form['#form'][$element]);
   }
-  $fields = variable_get('cckasetracker_content_type_'. $node_type, array());
+  // we need all fields here, especially inactivated ones
+  $fields = variable_get('cckasetracker_content_type_'. $type_name, array());
   foreach ($fields as $field => $value) {
     // Hide the field it it is not a CCKase tracker field, or if user doesn't
     // have edit permissions.
-    if (!$value['cckasetracker']) {
+    if (!$value) {
       // FIXME: should be somekind of find_element().
       // so it can check nested elements
       $form['#form'][$field]['#access'] = FALSE;
@@ -195,13 +215,14 @@ function cckasetracker_case_comment_subm
   $nodes['old'] = cckasetracker_strip_node($node);
   $status_change = FALSE;
 
-  $node_type = $node->type;
-  $fields = variable_get('cckasetracker_content_type_'. $node_type, array());
+  $type_name = $node->type;
+  $safe = str_replace(array('][', '_', ' '), '-', $type_name);
+  $fields = variable_get('cckasetracker_content_type_'. $type_name, array());
   foreach ($fields as $field => $value) {
-    if ($value['cckasetracker']) {
+    if ($value) {
       // Check if the CCKase tracker field changed.
-      if ($node->$field != $form_state['subform'][$node_type .'-node-form']['state']['values'][$field]) {
-        $node->$field = $form_state['subform'][$node_type .'-node-form']['state']['values'][$field];
+      if ($node->$field != $form_state['subform'][$safe .'-node-form']['state']['values'][$field]) {
+        $node->$field = $form_state['subform'][$safe .'-node-form']['state']['values'][$field];
         $status_change = TRUE;
       }
     }
@@ -387,14 +408,18 @@ function cckasetracker_get_content_field
  *   A themed table with all the diffs.
  */
 function cckasetracker_comment_show_status_change($cid, $nid) {
+  $output = '';
   if ($comment = cckasetracker_get_comment($cid)) {
     $nodes = unserialize($comment['nodes']);
     $old_node = $nodes['old'];
     $node = $nodes['new'];
-
     $cols = _diff_default_cols();
     $header = array();
     $rows = _diff_body_rows($old_node, $node, array('cckasetracker' => TRUE, 'type' => $node->type));
+    // remove empty diffs
+    if($rows[1][0]['data']=='' && $rows[1][1]['data']=='') {
+      return;
+    }
     $output = theme('diff_table', $header, $rows, array('class' => 'cckasetracker_diff'), NULL, $cols);
   }
   return $output;
@@ -406,14 +431,22 @@ function cckasetracker_comment_show_stat
  * @return
  *   An array with the content type name as the key, and the usage as the value.
  */
-function cckasetracker_get_types() {
-  $return = array();
+function cckasetracker_get_types($reset = FALSE) {
+  static $_tracker_types = NULL;
+  if($reset) {
+    $_tracker_types = NULL;
+  }
+  if(is_array($_tracker_types)) {
+    return $_tracker_types;
+  }
+
+  $_tracker_types = array();
   foreach (node_get_types() as $type_name => $values) {
   if (cckasetracker_is_case_type($type_name)) {
-      $return[$type_name] = 'case';
+      $_tracker_types[$type_name] = 'case';
     }
   }
-  return $return;
+  return $_tracker_types;
 }
 /**
  * Define the different content types recognized by CCKase tracker.
@@ -442,9 +475,9 @@ function cckasetracker_types_map() {
 function cckasetracker_strip_node($node) {
   $return = new stdClass;
   $type_name = $node->type;
-  $fields = variable_get('cckasetracker_content_type_'. $type_name, array());
-
-  foreach ($fields as $field => $value) {
+  $fields = variable_get('cckasetracker_content_type_'. $type_name .'_active', array());
+  
+  foreach ($fields as $field) {
     // Create a new object with the CCKase tracker fields.
     $return->{$field} = $node->{$field};
   }
