7.x user.module user_search_execute($keys = NULL, $conditions = NULL)

Implements hook_search_execute().

File

modules/user/user.module, line 957
Enables the user registration and login system.

Code

function user_search_execute($keys = NULL, $conditions = NULL) {
  $find = array();

  // Escape for LIKE matching.
  $keys = db_like($keys);

  // Replace wildcards with MySQL/PostgreSQL wildcards.
  $keys = preg_replace('!\\*+!', '%', $keys);
  $query = db_select('users')
    ->extend('PagerDefault');
  $query
    ->fields('users', array(
    'uid',
  ));
  if (user_access('administer users')) {

    // Administrators can also search in the otherwise private email field,
    // and they don't need to be restricted to only active users.
    $query
      ->fields('users', array(
      'mail',
    ));
    $query
      ->condition(db_or()
      ->condition('name', '%' . $keys . '%', 'LIKE')
      ->condition('mail', '%' . $keys . '%', 'LIKE'));
  }
  else {

    // Regular users can only search via usernames, and we do not show them
    // blocked accounts.
    $query
      ->condition('name', '%' . $keys . '%', 'LIKE')
      ->condition('status', 1);
  }
  $uids = $query
    ->limit(15)
    ->execute()
    ->fetchCol();
  $accounts = user_load_multiple($uids);
  $results = array();
  foreach ($accounts as $account) {
    $result = array(
      'title' => format_username($account),
      'link' => url('user/' . $account->uid, array(
        'absolute' => TRUE,
      )),
    );
    if (user_access('administer users')) {
      $result['title'] .= ' (' . $account->mail . ')';
    }
    $results[] = $result;
  }
  return $results;
}