You are here

function _userreference_potential_references_standard in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6.2 modules/userreference/userreference.module \_userreference_potential_references_standard()

Helper function for _userreference_potential_references(): referenceable users defined by user role and status

1 call to _userreference_potential_references_standard()
_userreference_potential_references in modules/userreference/userreference.module
Fetch an array of all candidate referenced users.

File

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

Code

function _userreference_potential_references_standard($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
  $where = array();
  $args = array();
  $join = array();
  if ($string !== '') {
    $like = $GLOBALS["db_type"] == 'pgsql' ? "ILIKE" : "LIKE";
    $match_clauses = array(
      'contains' => "{$like} '%%%s%%'",
      'equals' => "= '%s'",
      'starts_with' => "{$like} '%s%%'",
    );
    $where[] = 'u.name ' . (isset($match_clauses[$match]) ? $match_clauses[$match] : $match_clauses['contains']);
    $args[] = $string;
  }
  elseif ($ids) {
    $where[] = 'u.uid IN (' . db_placeholders($ids) . ')';
    $args = array_merge($args, $ids);
  }
  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';
  }
  if (isset($field['referenceable_status']) && is_numeric($field['referenceable_status'])) {
    $where[] = 'u.status = %d';
    $args[] = $field['referenceable_status'];
  }
  $users = array();
  $where_clause = $where ? 'WHERE (' . implode(') AND (', $where) . ')' : '';
  $result = db_query('SELECT u.name, u.uid FROM {users} u ' . implode(' ', $join) . " {$where_clause} ORDER BY u.name ASC", $args);
  while ($user = db_fetch_object($result)) {
    $users[$user->uid] = array(
      'title' => $user->name,
      'rendered' => check_plain($user->name),
    );
  }
  return $users;
}