You are here

function realname_privatemsg_autocomplete in Real Name 6

Same name and namespace in other branches
  1. 5 realname.module \realname_privatemsg_autocomplete()

Intercept Privatemsg autocomplete results for usernames.

1 string reference to 'realname_privatemsg_autocomplete'
realname_menu in ./realname.module
Implements hook_menu().

File

./realname.module, line 684

Code

function realname_privatemsg_autocomplete($string) {
  $names = explode(',', $string);
  $names = array_map('trim', $names);
  $search = array_pop($names);
  if ($search != '') {
    $sql = 'SELECT u.uid, u.name, r.realname FROM {users} u INNER JOIN {realname} r USING(uid) ';
    $sql .= ' WHERE u.status <> 0 AND LOWER(r.realname) LIKE LOWER(\'%%%s%%\') AND ';
    $sql .= variable_get('privatemsg_default_allow', 1) ? '(u.data NOT LIKE \'%%:16:"privatemsg_allow";i:0%%\' OR u.data IS NULL)' : 'u.data LIKE \'%%:16:"privatemsg_allow";i:1%%\'';
    $sql .= ' ORDER BY r.realname ASC';
    $result = db_query_range($sql, $search, 0, 10);
    $prefix = count($names) ? implode(', ', $names) . ', ' : '';
    $matches = array();
    while ($user = db_fetch_object($result)) {
      $account = user_load(array(
        'uid' => $user->uid,
      ));
      $matches[$prefix . $user->name] = check_plain($user->realname);
      if ($user->realname != $user->name) {
        $matches[$prefix . $user->name] .= ' (' . check_plain($user->name) . ')';
      }
    }
    drupal_json($matches);
  }
  else {
    drupal_json(array());

    // prevent Drupal autocomplete error message
  }
}