You are here

function biblio_autocomplete in Bibliography Module 7.2

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 biblio.module \biblio_autocomplete()

Page callback for biblio/autocomplete in hook_menu().

Parameters

string $field:

string $string:

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

File

./biblio.module, line 326

Code

function biblio_autocomplete($field, $string = '') {
  $matches = array();
  switch ($field) {
    case 'contributor':
    case 'a':

      // @todo: get this working on first OR last name
      $result = db_query_range("SELECT biblio_contributor_name_value FROM {field_data_biblio_contributor_name} WHERE LOWER(biblio_contributor_name_value) LIKE LOWER(:name) OR LOWER(biblio_contributor_name_value) LIKE LOWER(:firstname) ORDER BY biblio_contributor_name_value ASC ", 0, 10, array(
        ':name' => $string . '%',
        ':firstname' => $string . '%',
      ));
      foreach ($result as $data) {
        $matches[$data->biblio_contributor_name_value] = check_plain($data->biblio_contributor_name_value);
      }
      break;
    case 'biblio_keywords':
    case 'k':
      $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(: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:
      $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_encode($matches);
  exit;
}