diff U3 D:/DATA/Tom/MyData/TFSoft/projekty-02-rozpracovane/UEB/015-web-PB-PS/Drupal/autocomplete_deluxe-orig/autocomplete_deluxe.module S:/xampp/htdocs/drupal_ueb_bp/sites/all/modules/autocomplete_deluxe/autocomplete_deluxe.module
--- D:/DATA/Tom/MyData/TFSoft/projekty-02-rozpracovane/UEB/015-web-PB-PS/Drupal/autocomplete_deluxe-orig/autocomplete_deluxe.module	Mon Aug 08 10:38:33 2011
+++ S:/xampp/htdocs/drupal_ueb_bp/sites/all/modules/autocomplete_deluxe/autocomplete_deluxe.module	Fri Mar 23 08:33:41 2012
@@ -39,13 +39,16 @@
  */
 function taxonomy_autocomplete_deluxe($field_name, $tags_typed = '') {
   $field = field_info_field($field_name);
-
+  // TODO: TFs mod: load instance
+  $instance = field_info_instance('node', $field['field_name'], $field['bundles']['node'][0]);
+  // END: TFs mod
+  
   // The user enters a comma-separated list of tags. We only autocomplete the last tag.
   $tags_typed = drupal_explode_tags($tags_typed);
   $tag_last = drupal_strtolower(array_pop($tags_typed));
 
   $matches = array();
-
+    
   // Part of the criteria for the query come from the field's own settings.
   $vids = array();
   $vocabularies = taxonomy_vocabulary_get_names();
@@ -58,7 +61,21 @@
   $query->addTag('term_access');
   $query->addTag('autocomplete_deluxe_taxonomy_autocomplete');
   $query->addMetadata('field_name', $field_name);
-
+  // TODO: TFs mod: limit query to child terms
+  $term_parent = $instance['widget']['settings']['parent_id']; // tid 70 "species"
+  if (!empty($term_parent) && $term_parent > 0) { 
+    $query->join('taxonomy_term_hierarchy', 'h', 't.tid = h.tid'); //JOIN
+    $query->condition('h.parent', $term_parent); //WHERE
+  }
+  // TODO: TFs mod: remove child terms (including parent)  
+  $term_parent_not = $instance['widget']['settings']['parent_id_not']; // tid 70 "species"
+  if (!empty($term_parent_not) && $term_parent_not > 0) { 
+    $query->join('taxonomy_term_hierarchy', 'h', 't.tid = h.tid'); //JOIN
+    $query->condition('h.parent', $term_parent_not, '<>'); //WHERE
+    $query->condition('t.tid', $term_parent_not, '<>'); //WHERE
+  }     
+  // END: TFs mod
+  
   if ($tag_last != '') {
     // Do not select already entered terms.
     if (!empty($tags_typed)) {
@@ -125,18 +142,34 @@
   $form['size'] = array(
     '#type' => 'textfield',
     '#title' => t('Size of textfield'),
-    '#default_value' => isset($settings['size']) ? $settings['size'] : 6,
+    '#default_value' => isset($settings['size']) ? $settings['size'] : 60,
     '#element_validate' => array('_element_validate_integer_positive'),
     '#required' => TRUE,
   );
-
+  // TODO: TFs mod: Extend form - PARENT settings
+  $form['parent_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Limit TERMS to this PARENT ID (0 = disabled'),    
+    '#default_value' => isset($settings['parent_id']) ? $settings['parent_id'] : 0,
+    '#element_validate' => array('_element_validate_integer'),
+    '#required' => FALSE,  
+  );
+  $form['parent_id_not'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Remove all TERMS with this PARENT ID (0 = disabled)'),    
+    '#default_value' => isset($settings['parent_id_not']) ? $settings['parent_id_not'] : 0,
+    '#element_validate' => array('_element_validate_integer'),
+    '#required' => FALSE,  
+  );  
+  // END: TFs mod
+    
   return $form;
 }
 
 /**
  * Implodes the tags from the taxonomy module.
  *
- * This function is essentially the same as axonomy_implode_tags, with the
+ * This function is essentially the same as taxonomy_implode_tags, with the
  * difference, that it uses only a comma instead of a comma and a space to
  * implode the tags. It will help keep problems with delimiters to a minimum.
  */
@@ -184,7 +217,9 @@
       foreach ($items as $item) {
         $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
       }
-      $element['#element_validate'] = array('taxonomy_autocomplete_validate');
+      // TODO: TFs mod: Change validation (adding new term with parent)
+      //$element['#element_validate'] = array('taxonomy_autocomplete_validate');
+      $element['#element_validate'] = array('autocomplete_deluxe_autocomplete_validate');
       if (isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms']) && $instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'deny') {
         $options = autocomplete_deluxe_allowed_terms($field);
         $default_values = array();
@@ -284,6 +319,7 @@
         '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : '',
         '#prefix' => '<div class="autocomplete-deluxe-value-container">',
         '#suffix' => '</div>',
+        '#maxlength' => NULL,
       );
       $element['textfield']['#attributes']['style'] = array('display:none');
     }
@@ -398,3 +434,51 @@
   );
   return $items;
 }
+
+/**
+ * TODO: TFs mod: new validate function - create new term in parent-term hierarchy.
+ */
+function autocomplete_deluxe_autocomplete_validate($element, &$form_state) {
+  // Autocomplete widgets do not send their tids in the form, so we must detect
+  // them here and process them independently.
+  $value = array();
+  if ($tags = $element['#value']) {
+    // Collect candidate vocabularies.
+    $field = field_widget_field($element, $form_state);
+    $vocabularies = array();
+    foreach ($field['settings']['allowed_values'] as $tree) {
+      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
+        $vocabularies[$vocabulary->vid] = $vocabulary;
+      }
+    }
+
+    // Read widget field settings - parent ID
+    $parent_limitation = $form_state['field'][$element['#field_name']]['und']['instance']['widget']['settings']['parent_id']; 
+    // Translate term names into actual terms.
+    $typed_terms = drupal_explode_tags($tags);
+    foreach ($typed_terms as $typed_term) {
+      // See if the term exists in the chosen vocabulary and return the tid;
+      // otherwise, create a new 'autocreate' term for insert/update.
+      if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) {
+        $term = array_pop($possibilities);
+      }
+      else {
+        $vocabulary = reset($vocabularies);
+        $term = array(
+          'tid' => 'autocreate',
+          'vid' => $vocabulary->vid,
+          'name' => $typed_term,
+          'vocabulary_machine_name' => $vocabulary->machine_name,
+        );
+        if (!empty($parent_limitation) && $parent_limitation) {
+          $term += array(
+            'parent' => $parent_limitation,
+          );
+        }        
+      }
+      $value[] = (array)$term;
+    }
+  }
+
+  form_set_value($element, $value, $form_state);
+}
\ No newline at end of file
