You are here

function biblio_autocomplete in Bibliography Module 6

Same name and namespace in other branches
  1. 5 biblio.module \biblio_autocomplete()
  2. 6.2 biblio.module \biblio_autocomplete()
  3. 7 biblio.module \biblio_autocomplete()
  4. 7.2 biblio.module \biblio_autocomplete()
1 string reference to 'biblio_autocomplete'
biblio_menu in ./biblio.module
Implementation of hook_menu().

File

./biblio.module, line 301

Code

function biblio_autocomplete($field, $string = '') {
  $matches = array();
  if ($field == 'contributor') {
    $result = db_query_range("SELECT * FROM {biblio_contributor_data} WHERE LOWER(lastname) LIKE LOWER('%s%%') OR LOWER(firstname) LIKE LOWER('%s%%') ORDER BY lastname ASC ", array(
      $string,
      $string,
    ), 0, 10);
    while ($data = db_fetch_object($result)) {
      $matches[$data->name] = check_plain($data->name);
    }
  }
  elseif ($field == 'biblio_keywords') {
    $sep = check_plain(variable_get('biblio_keyword_sep', ','));
    $sep_pos = strrpos($string, $sep);

    //find the last separator
    $start = trim(drupal_substr($string, 0, $sep_pos));

    // first part of the string upto the last separator
    $end_sep = $sep_pos ? $sep_pos + 1 : $sep_pos;
    $end = trim(drupal_substr($string, $end_sep));

    // part of the string after the last separator
    $result = db_query_range("SELECT * FROM {biblio_keyword_data} WHERE LOWER(word) LIKE LOWER('%s%%') ORDER BY word ASC ", array(
      $end,
    ), 0, 10);
    while ($data = db_fetch_object($result)) {

      // now glue the word found onto the end of the original string...
      $keywords = $sep_pos ? $start . ', ' . check_plain($data->word) : check_plain($data->word);
      $matches[$keywords] = $keywords;
    }
  }
  else {
    $result = db_query_range("SELECT %s FROM {biblio} WHERE LOWER(%s) LIKE LOWER('%s%%') ORDER BY %s ASC", array(
      $field,
      $field,
      $string,
      $field,
    ), 0, 10);
    while ($data = db_fetch_object($result)) {
      $matches[$data->{$field}] = check_plain($data->{$field});
    }
  }
  print drupal_to_js($matches);
  exit;
}