You are here

function privatemsg_realname_privatemsg_sql_autocomplete_alter in Privatemsg 6.2

Implements hook_privatemsg_sql_autocomplete_alter().

File

privatemsg_realname/privatemsg_realname.module, line 43

Code

function privatemsg_realname_privatemsg_sql_autocomplete_alter(&$fragments, $search, $names) {

  // Create necessary LEFT JOINs. LEFT because some users might not have a realname for any reason.
  $fragments['inner_join'][] = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';

  // Either select users where the profile field name and value matches or the username.
  // This does replace the default where.
  if (variable_get('privatemsg_realname_search_username', TRUE)) {
    $fragments['where'][1] = "(r.realname LIKE '%s' OR u.name LIKE '%s')";
    array_unshift($fragments['query_args']['where'], $search . '%%');
  }
  else {
    $fragments['where'][1] = "r.realname LIKE '%s'";
  }
  if (!empty($names)) {

    // Exclude already existing realnames, but explicitly allow NULL.
    // r.realname is left joined and cann be NULL => NULL NOT IN (...) => NOT (NULL) => NULL.
    $fragments['where'][] = "r.realname NOT IN (" . db_placeholders($names, 'text') . ") OR r.realname IS NULL";
    $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], array_values($names));
  }
}