You are here

function masquerade_autocomplete_multiple in Masquerade 7

Same name and namespace in other branches
  1. 6 masquerade.module \masquerade_autocomplete_multiple()

Returns JS array for Masquerade autocomplete fields.

Supports multiple entries separated by a comma.

Parameters

$string: The string of autocmplete value submitted by the user.

$add_anonymous: Flag to include Anonymous user into result.

1 string reference to 'masquerade_autocomplete_multiple'
masquerade_menu in ./masquerade.module
Implements hook_menu().

File

./masquerade.module, line 748
The masquerade module allows administrators to masquerade as other user.

Code

function masquerade_autocomplete_multiple($string, $add_anonymous = TRUE) {
  $matches = array();

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

  // Fetch last string.
  $last_string = drupal_strtolower(array_pop($users_typed));
  if ($last_string) {
    $prefix = count($users_typed) ? implode(', ', $users_typed) . ', ' : '';
    if ($add_anonymous) {

      // Anonymous user goes first to be visible for user.
      $anonymous = variable_get('anonymous', t('Anonymous'));
      if (stripos($anonymous, $last_string) === 0) {
        $matches[$prefix . $anonymous] = $anonymous;
      }
    }

    // Other suggestions.
    $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE :string", 0, 10, array(
      ':string' => $last_string . '%',
    ));
    foreach ($result as $user) {
      $matches[$prefix . $user->name] = check_plain($user->name);
    }

    // Remove existing tags.
    $matches = array_diff($matches, $users_typed);

    // @todo Check compatibility for D7.
    if (module_exists('alt_login')) {
      $result = db_query_range("SELECT u.alt_login AS alt_login FROM {alt_login} u WHERE LOWER(u.alt_login) LIKE LOWER(:string)", 0, 10, array(
        ':string' => $last_string . '%',
      ));
      foreach ($result as $user) {
        $matches[$user->alt_login] = check_plain($user->alt_login);
      }
    }
  }
  if (module_exists('devel')) {
    $GLOBALS['devel_shutdown'] = FALSE;
  }
  drupal_json_output($matches);
}