You are here

function _userreference_potential_references in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 5 userreference.module \_userreference_potential_references()
  2. 6.3 modules/userreference/userreference.module \_userreference_potential_references()
  3. 6.2 modules/userreference/userreference.module \_userreference_potential_references()

Fetch an array of all candidate referenced users, for use in presenting the selection form to the user.

4 calls to _userreference_potential_references()
userreference_allowed_values in modules/userreference/userreference.module
Implementation of hook_allowed_values().
userreference_autocomplete in modules/userreference/userreference.module
Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing users
userreference_field in modules/userreference/userreference.module
Implementation of hook_field().
_userreference_filter_handler in modules/userreference/userreference.module
Provide a list of users to filter on.

File

modules/userreference/userreference.module, line 451
Defines a field type for referencing a user from a node.

Code

function _userreference_potential_references($field, $string = '') {
  $where = array();
  $args = array();
  $join = array();
  if (!empty($string)) {
    $where[] = "LOWER(name) LIKE LOWER('%s%%')";
    $args[] = $string;
  }
  else {
    $where[] = "u.uid > 0";
  }
  $roles = array();
  if (isset($field['referenceable_roles']) && is_array($field['referenceable_roles'])) {

    // keep only selected checkboxes
    $roles = array_filter($field['referenceable_roles']);

    // filter invalid values that seems to get through sometimes ??
    $roles = array_intersect(array_keys(user_roles(1)), $roles);
  }
  if (!empty($roles) && !in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
    $where[] = "r.rid IN (" . implode($roles, ',') . ")";
    $join[] = 'LEFT JOIN {users_roles} r ON u.uid = r.uid';
  }
  $status = array();
  if (isset($field['referenceable_status']) && is_array($field['referenceable_status'])) {

    // keep only selected checkboxes
    $status = array_filter($field['referenceable_status']);
  }
  if (!empty($status)) {

    // Limit query if only one status should be referenced.
    if (count($status) == 1) {
      $where[] = "u.status = " . array_pop($status);
    }
  }
  $users = array();
  $result = db_query('SELECT u.name, u.uid FROM {users} u ' . implode(' ', $join) . ' WHERE ' . implode(' AND ', $where) . ' ORDER BY u.name ASC', $args);
  while ($user = db_fetch_object($result)) {
    $users[$user->uid] = $user->name;
  }
  return $users;
}