You are here

function library_autocomplete in Library 7

Same name and namespace in other branches
  1. 5.2 library.admin.inc \library_autocomplete()
  2. 6.2 library.admin.inc \library_autocomplete()
  3. 6 library.admin.inc \library_autocomplete()

Autocomplete call for library items.

Retrieve a pipe delimited string of autocomplete suggestions for library items in JSON encoding for the ajax call.

1 string reference to 'library_autocomplete'
library_menu in ./library.module
Implements hook_menu().

File

./library.admin.inc, line 443
Administrative settings for the library module

Code

function library_autocomplete($string) {
  $matches = array();
  if (variable_get('library_item_barcodes', LIBRARY_NO_BARCODES) == LIBRARY_BARCODES) {
    $select = db_select('library', 'l')
      ->fields('l', array(
      'id',
      'barcode',
    ))
      ->condition('l.barcode', '', '<>')
      ->condition('l.barcode', db_like(check_plain($string)) . '%', 'LIKE');
    $select
      ->join('node', 'n', 'n.nid = l.nid');
    $select
      ->fields('n', array(
      'title',
    ))
      ->condition('n.status', 1)
      ->orderBy('l.barcode')
      ->range(0, 10);
    $results = $select
      ->execute()
      ->fetchAll();
    foreach ($results as $item) {
      $matches[$item->barcode . ' [title:' . $item->title . '] [id:' . $item->id . ']'] = check_plain($item->barcode);
    }
  }
  else {
    $select = db_select('library', 'l')
      ->fields('l', array(
      'id',
    ));
    $select
      ->join('node', 'n', 'n.nid = l.nid');
    $select
      ->fields('n', array(
      'title',
    ))
      ->condition('n.status', 1)
      ->condition('n.title', db_like(check_plain($string)) . '%', 'LIKE')
      ->orderBy('n.title')
      ->distinct()
      ->range(0, 10);
    $results = $select
      ->execute()
      ->fetchAllAssoc('nid');
    foreach ($results as $item) {
      $my_key = $item->title . ' [id:' . $item->id . ']';
      $matches[$my_key] = check_plain($my_key);
    }
  }
  print drupal_json_encode($matches);
  exit;
}