function _userreference_potential_references in Content Construction Kit (CCK) 5
Same name and namespace in other branches
- 6.3 modules/userreference/userreference.module \_userreference_potential_references()
- 6 modules/userreference/userreference.module \_userreference_potential_references()
- 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_autocomplete in ./
userreference.module - Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing users
- userreference_field in ./
userreference.module - Implementation of hook_field().
- userreference_widget in ./
userreference.module - Implementation of hook_widget().
- _userreference_filter_handler in ./
userreference.module - Provide a list of users to filter on.
File
- ./
userreference.module, line 316 - Defines a field type for referencing a user from a node.
Code
function _userreference_potential_references($field, $string = '', $uid = NULL) {
$where = array();
$args = array();
$join = array();
if ($string !== '') {
$where[] = "LOWER(name) LIKE LOWER('%s%%')";
$args[] = $string;
}
elseif (!empty($uid) && is_numeric($uid)) {
$where[] = "u.uid = {$uid}";
}
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;
}