You are here

function views_ajax_autocomplete_user in Views (for Drupal 7) 6.3

Same name and namespace in other branches
  1. 8.3 includes/ajax.inc \views_ajax_autocomplete_user()
  2. 6.2 includes/ajax.inc \views_ajax_autocomplete_user()
  3. 7.3 includes/ajax.inc \views_ajax_autocomplete_user()

Page callback for views user autocomplete

Related topics

1 string reference to 'views_ajax_autocomplete_user'
views_menu in ./views.module
Implementation of hook_menu().

File

includes/ajax.inc, line 160
Handles the server side AJAX interactions of Views.

Code

function views_ajax_autocomplete_user($string = '') {

  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  $array = drupal_explode_tags($string);

  // Fetch last tag
  $last_string = trim(array_pop($array));
  $matches = array();
  if ($last_string != '') {
    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
    if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
      $matches[$prefix . 'Anonymous'] = 'Anonymous';
    }
    $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $last_string, 0, 10);
    while ($account = db_fetch_object($result)) {
      $n = $account->name;

      // Commas and quotes in terms are special cases, so encode 'em.
      if (strpos($account->name, ',') !== FALSE || strpos($account->name, '"') !== FALSE) {
        $n = '"' . str_replace('"', '""', $account->name) . '"';
      }
      $matches[$prefix . $n] = check_plain($account->name);
    }
  }
  drupal_json($matches);
}