--- content_taxonomy_autocomplete.module	2009-08-19 11:42:30.000000000 +0200
+++ content_taxonomy_autocomplete.module.2	2009-12-05 17:13:13.166007062 +0100
@@ -75,7 +75,20 @@ function content_taxonomy_autocomplete_w
         '#default_value' => (isset($widget['extra_parent']) && is_numeric($widget['extra_parent'])) ? $widget['extra_parent'] : 0,
         '#description' => t('This setting is only relevant if you have selected "Allow and insert new terms by the user into the vocabulary". If you select any term here, new terms will get children of the selected one, otherwise new terms get children of the parent term (root, if no parent selected) selected in the global settings.'),
       );
-      
+      $form['autocomplete']['newparent_php_code_fieldset'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Advanced PHP code'),
+        '#collapsible' => TRUE,  
+        '#collapsed' => empty($widget['newparent_php_code']) ,
+      );
+      $form['autocomplete']['newparent_php_code_fieldset']['newparent_php_code'] = array(
+        '#title' => t('PHP Code for selecting the parent for new terms'),
+        '#type' => 'textarea',
+        '#default_value' => !empty($widget['newparent_php_code']) ? $widget['newparent_php_code'] : '',
+        '#rows' => 3,
+        '#description' => t('Advanced usage only: PHP code that returns the parent term ID. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the ID returned by this code  will override the selected parent above.'),
+      );
+
       $form['autocomplete']['maxlength'] = array(
         '#type' => 'textfield', 
         '#title' => t('Maximum length of autocomplete'), 
@@ -95,7 +108,7 @@ function content_taxonomy_autocomplete_w
       return $form;
 
     case 'save':
-      return array('new_terms', 'extra_parent', 'maxlength', 'active_tags');
+      return array('new_terms', 'extra_parent', 'maxlength', 'active_tags', 'newparent_php_code');
   }
 }
 
@@ -225,7 +238,7 @@ function content_taxonomy_autocomplete_v
     form_set_error($field['field_name'] .'][value', t('You can provide only one value'));
     return;
   }
-  else if (($field['multiple'] >= 2) && (count(content_taxonomy_autocomplete_split_tags($value, $field['vid'])) > $field['multiple'])) {
+  elseif (($field['multiple'] >= 2) && (count(content_taxonomy_autocomplete_split_tags($value, $field['vid'])) > $field['multiple'])) {
     form_set_error($field['field_name'] .'][value', t('%name: this field cannot hold more than @count values.', array('%name' => t($field['widget']['label']), '@count' => $field['multiple'])));
   }
   if ($field['widget']['new_terms'] == 'deny') {
@@ -247,17 +260,20 @@ function content_taxonomy_autocomplete_f
   $existing_tids = is_array($extracted_ids['existing_tids']) ? $extracted_ids['existing_tids'] : array();
   $new_tids = array();
   if (is_array($extracted_ids['non_existing_terms'])) {
-    if ($field['widget']['extra_parent']) {
-      $new_tids = content_taxonomy_autocomplete_insert_tags($extracted_ids['non_existing_terms'], $field['widget']['extra_parent']);
-    } 
-    else {
-      $new_tids = content_taxonomy_autocomplete_insert_tags($extracted_ids['non_existing_terms'], content_taxonomy_field_get_parent($field));          
-    }
+    $new_tids = content_taxonomy_autocomplete_insert_tags($extracted_ids['non_existing_terms'], content_taxonomy_field_get_newparent($field)); 
   }
   return content_transpose_array_rows_cols(array($element['#columns'][0] => array_merge($existing_tids, $new_tids)));
-
 }
 
+function content_taxonomy_field_get_newparent($field) {
+  if (!empty($field['newparent_php_code'])) {
+    return eval($field['newparent_php_code']);
+  }
+  elseif ($field['widget']['extra_parent']) {
+    return $field['widget']['extra_parent'];
+  }
+  return content_taxonomy_field_get_parent($field);
+}
 
 /**
  * Retrieve a pipe delimited string of autocomplete suggestions
@@ -294,7 +310,7 @@ function content_taxonomy_autocomplete_l
         INNER JOIN {term_hierarchy} h ON  t.tid = h.tid
         WHERE h.parent = %d 
         AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'),
-        $tid,$last_string,$last_string,0,10);
+        $tid, $last_string, $last_string, 0, 10);
     }
     else {
       $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t 
@@ -334,7 +350,9 @@ function content_taxonomy_autocomplete_t
     // or includes a comma or quote character), we remove the escape
     // formatting so to save the term into the DB as the user intends.
     $typed_term = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term)));
-    if ($typed_term == "") { continue; }
+    if ($typed_term == "") { 
+      continue; 
+    }
     
     // See if the term exists in the chosen vocabulary
     // and return the tid, otherwise, add a new record.
