Index: content_taxonomy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/content_taxonomy/content_taxonomy.module,v
retrieving revision 1.2.2.15
diff -u -p -r1.2.2.15 content_taxonomy.module
--- content_taxonomy.module	29 Jan 2008 17:55:29 -0000	1.2.2.15
+++ content_taxonomy.module	6 Mar 2008 08:05:58 -0000
@@ -121,12 +142,6 @@ function content_taxonomy_field($op, &$n
       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) {
@@ -136,7 +151,7 @@ function content_taxonomy_field($op, &$n
           }
         }
         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;
             }
@@ -212,18 +219,18 @@ function content_taxonomy_field_formatte
  *  @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];
   }
 }
 
@@ -238,8 +245,23 @@ function content_taxonomy_form_alter($fo
     $content_type = $info['content types'][$form['type']['#value']];
     foreach ($content_type['fields'] as $field_name => $field) {
       if ($field['type'] == 'content_taxonomy') {
-        unset($form['taxonomy']);
-        break;
+        if (($field['tid'] == 0) && (array_key_exists($field['vid'], $form['taxonomy']))) {
+          unset($form['taxonomy'][$field['vid']]);
+        }
+        else {
+          $tree = taxonomy_get_tree($field['vid'], $field['tid'], -1, $field['depth']);
+          foreach ($tree as $term) {
+            $field_tids[] = $term->tid;
+          }
+          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, $field_tids)) {
+                unset($form['taxonomy'][$field['vid']]['#options'][$key]);
+              }
+            }
+          }
+        }
       }
     }
   }
