You are here

function _user_reference_potential_references_standard in References 7.2

Helper function for user_reference_potential_references().

List of referenceable users defined by user role and status.

1 call to _user_reference_potential_references_standard()
user_reference_potential_references in user_reference/user_reference.module
Retrieves an array of candidate referenceable users.

File

user_reference/user_reference.module, line 889
Defines a field type for referencing a user from a node.

Code

function _user_reference_potential_references_standard($field, $options) {
  $filter_roles = array();

  // Avoid useless work.
  if (is_array($field['settings']['referenceable_roles'])) {
    $filter_roles = array_filter($field['settings']['referenceable_roles']);
  }

  // $field['settings']['referenceable_status'] may be an int/boolean on D6
  // upgraded sites.
  $filter_status = array();
  if (is_array($field['settings']['referenceable_status'])) {

    // Selects only items in array that are not empty (true, 1, string, etc.)
    $filter_status = array_filter($field['settings']['referenceable_status']);
  }
  if (!count($filter_status) && !count($filter_roles)) {
    return array();
  }
  $query = db_select('users', 'u')
    ->fields('u')
    ->addMetaData('id', ' _user_reference_potential_references_standard')
    ->addMetaData('field', $field)
    ->addMetaData('options', $options);

  // Enable this filter only if any statuses checked (and not both).
  if (count($filter_status) == 1) {
    $query
      ->condition('u.status', array_keys($filter_status), 'IN');
  }

  // Skip filter when "authenticated user" choosen.
  if ($filter_roles && !isset($filter_roles[DRUPAL_AUTHENTICATED_RID])) {
    $query
      ->join('users_roles', 'r', 'u.uid = r.uid');
    $query
      ->condition('r.rid', array_keys($filter_roles), 'IN');
  }
  if ($options['string'] !== '') {
    switch ($options['match']) {
      case 'contains':
        $query
          ->condition('u.name', '%' . $options['string'] . '%', 'LIKE');
        break;
      case 'starts_with':
        $query
          ->condition('u.name', $options['string'] . '%', 'LIKE');
        break;
      case 'equals':

      // No match type or incorrect match type: use "=".
      default:
        $query
          ->condition('u.name', $options['string'], '=');
        break;
    }
  }
  if ($options['ids']) {
    $query
      ->condition('u.uid', $options['ids'], 'IN');
  }

  // Explicitly exclude the anonymous user.
  $query
    ->condition('u.uid', 0, '<>');
  if ($options['limit']) {
    $query
      ->range(0, $options['limit']);
  }
  $query
    ->orderBy('u.name');
  $result = $query
    ->execute()
    ->fetchAll();
  $references = array();
  foreach ($result as $account) {
    $references[$account->uid] = array(
      'title' => $account->name,
      'rendered' => check_plain(format_username($account)),
    );
  }
  return $references;
}