You are here

function content_taxonomy_autocomplete_load in Content Taxonomy 5

Same name and namespace in other branches
  1. 6.2 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_load()
  2. 6 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_load()

Retrieve a pipe delimited string of autocomplete suggestions

Parameters

String Fieldname:

Integer TID of a parent (optional):

BOOLEAN whether a multiple field or not:

STRING typed input:

1 string reference to 'content_taxonomy_autocomplete_load'
content_taxonomy_autocomplete_menu in ./content_taxonomy_autocomplete.module
Implementation of hook_menu

File

./content_taxonomy_autocomplete.module, line 157
Defines a widget type for content_taxonomy with autocomplete

Code

function content_taxonomy_autocomplete_load($field_name, $multiple = TRUE, $string = '') {

  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  // This regexp allows the following types of user input:
  // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
  $content_type_info = _content_type_info();
  $vid = $content_type_info['fields'][$field_name]['vid'];
  if ($content_type_info['fields'][$field_name]['tid']) {
    $tid = $content_type_info['fields'][$field_name]['tid'];
  }
  elseif ($content_type_info['fields'][$field_name]['parent']) {
    $tid = $content_type_info['fields'][$field_name]['parent'];
  }
  $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
  preg_match_all($regexp, $string, $matches);
  $array = $matches[1];

  // Fetch last tag
  $last_string = trim(array_pop($array));
  if ($last_string != '') {
    if ($tid) {
      $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t \n        LEFT JOIN {term_synonym} s ON t.tid = s.tid\n        INNER JOIN {term_hierarchy} h ON  t.tid = h.tid\n        WHERE h.parent = %d \n        AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'), $tid, $last_string, $last_string, 0, 10);
    }
    else {
      $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t \n        LEFT JOIN {term_synonym} s ON t.tid = s.tid\n        WHERE t.vid = %d \n        AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'), $vid, $last_string, $last_string, 0, 10);
    }
    if ($multiple) {
      $prefix = count($array) ? implode(',', $array) . ', ' : '';
    }
    else {
      $prefix = '';
    }
    $matches = array();
    while ($tag = db_fetch_object($result)) {
      $n = $tag->name;

      // Commas and quotes in terms are special cases, so encode 'em.
      if (preg_match('/,/', $tag->name) || preg_match('/"/', $tag->name)) {
        $n = '"' . preg_replace('/"/', '""', $tag->name) . '"';
      }
      $matches[$prefix . $n] = check_plain($tag->name);
    }
    print drupal_to_js($matches);
    exit;
  }
}