You are here

function biblio_autocomplete in Bibliography Module 6.2

Same name and namespace in other branches
  1. 5 biblio.module \biblio_autocomplete()
  2. 6 biblio.module \biblio_autocomplete()
  3. 7 biblio.module \biblio_autocomplete()
  4. 7.2 biblio.module \biblio_autocomplete()

Parameters

$field:

string $string: (optional)

1 string reference to 'biblio_autocomplete'
biblio_menu in ./biblio.module
Implements hook_menu().

File

./biblio.module, line 391
Main file for Drupal module biblio.

Code

function biblio_autocomplete($field, $string = '') {
  $matches = array();
  if ($field == 'contributor') {
    $sql = "SELECT * FROM {biblio_contributor_data} " . "WHERE LOWER(lastname) LIKE LOWER('%s%%') OR LOWER(firstname) LIKE LOWER('%s%%') " . "ORDER BY lastname ASC ";
    $result = db_query_range($sql, 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', ','));

    // Locate the position of last separator in the string.
    $sep_pos = strrpos($string, $sep);

    // The beginning of the string until the last separator.
    $start = trim(drupal_substr($string, 0, $sep_pos));
    $end_sep = $sep_pos ? $sep_pos + 1 : $sep_pos;

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

      // 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 {
    $sql = "SELECT %s FROM {biblio} WHERE LOWER(%s) LIKE LOWER('%s%%') ORDER BY %s ASC";
    $result = db_query_range($sql, 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;
}