You are here

function library_patron_autocomplete in Library 6.2

Same name and namespace in other branches
  1. 7 library.module \library_patron_autocomplete()

Retrieve a pipe delimited string of autocomplete suggestions for existing patron.

1 string reference to 'library_patron_autocomplete'
library_menu in ./library.module
Implementation of hook_menu().

File

./library.module, line 1529

Code

function library_patron_autocomplete($string) {
  $matches = array();
  $result = NULL;
  if (module_exists('profile')) {
    $profile_fields = db_query("SELECT fid, name FROM {profile_fields} WHERE type='textfield'");
    $fids = array();
    while ($field = db_fetch_object($profile_fields)) {
      if (variable_get('library_profile_' . $field->name, 0) == 1) {
        $fids[] = $field->fid;

        /*
        if (!empty($fids)) {
          $fids .= "OR fid='" . $field->fid . "'";
        }
        else {
          $fids = "fid='" . $field->fid . "'";
        }
        */
      }
    }
    if (!empty($fids)) {
      $fid_string = implode(',', $fids);
      if (!empty($fid_string)) {
        $result = db_query_range("SELECT u.uid, CONCAT(pv.value,' (', u.name, ')') as name FROM {users} u JOIN (SELECT uid, GROUP_CONCAT(value SEPARATOR ', ' ) as value FROM {profile_values} WHERE fid IN (%s) GROUP BY (uid)) AS pv ON u.uid = pv.uid WHERE (LOWER(u.name) LIKE LOWER('%s%%') or LOWER(pv.value) LIKE LOWER('%%%s%%'))", $fid_string, check_plain($string), check_plain($string), 0, 10);
      }
    }
  }
  if (empty($result)) {
    $result = db_query_range("SELECT uid, name FROM {users} WHERE LOWER(name) LIKE LOWER('%%%s%%')", check_plain($string), 0, 10);
  }
  while ($patron = db_fetch_object($result)) {
    $matches[$patron->name . ' [uid:' . $patron->uid . ']'] = check_plain($patron->name);
  }
  print drupal_to_js($matches);
  exit;
}