You are here

function privatemsg_user_name_autocomplete in Privatemsg 6

Return autocomplete results for usernames.

Prevents usernames from being used and/or suggested twice.

2 string references to 'privatemsg_user_name_autocomplete'
privatemsg_filter_menu in privatemsg_filter/privatemsg_filter.module
Implements hook_menu().
privatemsg_menu in ./privatemsg.module
Implements hook_menu().

File

./privatemsg.module, line 1381
Allows users to send private messages to other users.

Code

function privatemsg_user_name_autocomplete($string) {
  $names = array();

  // 1: Parse $string and build list of valid user names.
  $fragments = explode(',', $string);
  foreach ($fragments as $index => $name) {
    if ($name = trim($name)) {
      $names[$name] = $name;
    }
  }

  // By using user_validate_user we can ensure that names included in $names are at least logisticaly possible.
  // 2: Find the next user name suggestion.
  $fragment = array_pop($names);
  $matches = array();
  if (!empty($fragment)) {
    $query = _privatemsg_assemble_query('autocomplete', $fragment, $names);
    $result = db_query_range($query['query'], $fragment, 0, 10);
    $prefix = count($names) ? implode(", ", $names) . ", " : '';

    // 3: Build proper suggestions and print.
    while ($user = db_fetch_object($result)) {
      $matches[$prefix . $user->name . ", "] = $user->name;
    }
  }

  // convert to object to prevent drupal bug, see http://drupal.org/node/175361
  drupal_json((object) $matches);
}