You are here

function biblio_autocomplete in Bibliography Module 7

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

Page callback for autocompletion. Outputs JSON of autocomplete options.

Parameters

string $field: The field to search.

string $string: The string to search for.

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

File

./biblio.module, line 327
Bibliography Module for Drupal.

Code

function biblio_autocomplete($field = NULL, $string = '') {
  $matches = array();
  switch ($field) {
    case 'contributor':
    case 'a':
      $result = db_query_range("SELECT name FROM {biblio_contributor_data} WHERE LOWER(lastname) LIKE LOWER(:name) OR LOWER(firstname) LIKE LOWER(:firstname) ORDER BY lastname ASC ", 0, 10, array(
        ':name' => $string . '%',
        ':firstname' => $string . '%',
      ));
      foreach ($result as $data) {
        $matches[$data->name] = check_plain($data->name);
      }
      break;
    case 'biblio_keywords':
    case 'k':
      $sep = check_plain(variable_get('biblio_keyword_sep', ','));

      // Find the last separator.
      $sep_pos = strrpos($string, $sep);

      // First part of the string upto the last separator.
      $start = trim(drupal_substr($string, 0, $sep_pos));
      $end_sep = $sep_pos ? $sep_pos + 1 : $sep_pos;

      // Part of the string after the last separator.
      $end = trim(drupal_substr($string, $end_sep)) . '%';
      $result = db_query_range("SELECT * FROM {biblio_keyword_data} WHERE LOWER(word) LIKE LOWER(:end) ORDER BY word ASC ", 0, 10, array(
        ':end' => $end,
      ));
      foreach ($result as $data) {

        // 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;
      }
      break;
    default:
      if (!$field) {
        break;
      }
      $field = drupal_strtolower($field);
      $string = '%' . drupal_strtolower($string) . '%';
      $query = db_select('biblio', 'b')
        ->fields('b', array(
        $field,
      ))
        ->condition($field, $string, 'LIKE')
        ->orderBy($field, 'ASC')
        ->range(0, 10);
      $result = $query
        ->execute();
      foreach ($result as $data) {
        $matches[$data->{$field}] = check_plain($data->{$field});
      }
  }
  print drupal_json_output($matches);
  drupal_exit();
}