--- C:/Documents and Settings/cskene/Desktop/content_taxonomy/content_taxonomy.module	Wed Jan 30 03:55:30 2008
+++ C:/Documents and Settings/cskene/Desktop/content_taxonomy/content_taxonomy-nuclear.module	Tue May 06 12:32:17 2008
@@ -109,7 +109,10 @@
         }
       }
       else {
-        $additions[$field['field_name']][$field['tid']] = content_taxonomy_terms_by_field($node, $field['vid'], $field['tid'], $field['depth']);
+        $terms = content_taxonomy_terms_by_field($node, $field['vid'], $field['tid'], $field['depth']);
+		if (is_array($terms)) {
+          $additions[$field['field_name']][$field['tid']] = $terms;
+        }
       }
       
       $widget_type = $field['widget']['type'];
@@ -121,24 +124,20 @@
       break;
       
     case 'submit': 
-      global $content_taxonomy_array_cleared;
-      if (!is_array($content_taxonomy_array_cleared) || !$content_taxonomy_array_cleared[$node->nid]) {
-        unset($node->taxonomy);
-        $content_taxonomy_array_cleared[$node->nid] = true;
-      }
-  
       if (isset($field['save']) && $field['save'] != 'cck_table') {
         if (is_array($node_field['tids'])) {
           foreach ($node_field['tids'] as $tid) {
             if ($tid) {
-              $node->taxonomy[$field['vid']][$tid] = $tid;
+              $handle = $tid;
+							$node->taxonomy[$field['vid']][$handle] = $handle;
             }
           }
         }
         elseif (is_array($node_field) && $field['save'] == 'both') {
-          foreach ($node_field as $tid => $value) {
+          foreach ($node_field as $tid) {
             if ($tid) {
-              $node->taxonomy[$field['vid']][$tid] = $tid;
+              $handle = $tid['value'];
+							$node->taxonomy[$field['vid']][$handle] = $handle;
             }
           }
         }        
@@ -212,18 +211,18 @@
  *  @param Integer depth of hierarchy to load
  */
 function content_taxonomy_terms_by_field($node, $vid, $parent = NULL, $depth) {
-  if (is_numeric($parent) && $depth == 1) {
-    $result = db_query("SELECT n.tid FROM {term_hierarchy} h, {term_node} n WHERE
-      n.nid = %d AND n.tid = h.tid AND h.parent = %d", $node->nid, $parent);
-    
-    while ($data = db_fetch_array($result)) {
-      $term = taxonomy_get_term($data["tid"]);
-      $additions[$term->tid] = $term;
-    }
-    return $additions;
+  static $terms;
+
+  if (!isset($terms[$node->nid][$vid])) {
+    $terms[$node->nid][$vid] = taxonomy_node_get_terms_by_vocabulary($node->nid, $vid);
+  }
+  if ($parent > 0 && $depth == 1) {
+    $tree = taxonomy_get_tree($vid, $parent, -1, $depth);
+    $tids = array_flip(array_map(create_function('$leaf', 'return $leaf->tid;'), $tree));
+    return array_intersect_key($terms[$node->nid][$vid], $tids);
   }
   else { 
-    return taxonomy_node_get_terms_by_vocabulary($node->nid, $vid);
+		return $terms[$node->nid][$vid];
   }
 }
 
@@ -233,18 +232,48 @@
  * hides the taxonomy form if there exists content taxonomy fields
  */
 function content_taxonomy_form_alter($form_id, &$form) {
-  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {   
+  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {  
     $info = _content_type_info();
     $content_type = $info['content types'][$form['type']['#value']];
+
     foreach ($content_type['fields'] as $field_name => $field) {
       if ($field['type'] == 'content_taxonomy') {
+        if (($field['tid'] == 0) && (array_key_exists($field['vid'], $form['taxonomy']))) {
+          unset($form['taxonomy'][$field['vid']]);
+        }
+        else {
+          if (empty($field['depth'])) {
+            $tree = taxonomy_get_tree($field['vid'], $field['tid']);
+          }
+          else {
+            $tree = taxonomy_get_tree($field['vid'], $field['tid'], -1, $field['depth']);
+          }
+          if (is_array($form['taxonomy'][$field['vid']]['#options'])) {
+            $tids = array_map(create_function('$leaf', 'return $leaf->tid;'), $tree);
+            $form_tids = array_map(create_function('$element', 'return key((array)$element->option);'), $form['taxonomy'][$field['vid']]['#options']);
+            foreach ($form['taxonomy'][$field['vid']]['#options'] as $key => $element) {
+              if (is_array($element->option) && key($element->option)) {
+                $tid = key($element->option);
+                if (in_array($tid, $tids)) {
+                  unset($form['taxonomy'][$field['vid']]['#options'][$key]);
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+
+    if (is_array($form['taxonomy'])) {
+      $element_keys = array_keys($form['taxonomy']);
+      $vids = array_keys(taxonomy_get_vocabularies($form['type']['#value']));
+      if (count(array_intersect($vids, $element_keys)) == 0) {
         unset($form['taxonomy']);
-        break;
       }
     }
+
   }
 }
-
 
 /**
  * Implementation of hook_token_list().
