You are here

function _rolereference_potential_references_standard in Role Reference 6

Helper function for _nodereference_potential_references(): referenceable nodes defined by content types.

1 call to _rolereference_potential_references_standard()
_rolereference_potential_references in ./rolereference.module
Fetch an array of all candidate referenced roles.

File

./rolereference.module, line 788
Defines a field type for referencing a role. Based almost entirely on nodereference and userreference modules.

Code

function _rolereference_potential_references_standard($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
  $related_types = array();
  $where = array();
  $args = array();
  if (is_array($field['referenceable_types'])) {
    foreach (array_filter($field['referenceable_types']) as $related_type) {
      $related_types[] = "r.rid = %d";
      $args[] = $related_type;
    }
  }

  // Default to all roles if no roles selected.
  if (!count($related_types)) {
    $roles = db_query('SELECT rid, name FROM {role} ORDER BY name');
    while ($role = db_fetch_object($roles)) {
      $related_types[] = "r.rid = %d";
      $args[] = $role->rid;
    }
  }
  $where[] = implode(' OR ', $related_types);
  if ($string !== '') {
    $match_operators = array(
      'contains' => "LIKE '%%%s%%'",
      'equals' => "= '%s'",
      'starts_with' => "LIKE '%s%%'",
    );
    $where[] = 'r.name ' . (isset($match_operators[$match]) ? $match_operators[$match] : $match_operators['contains']);
    $args[] = $string;
  }
  elseif ($ids) {
    $where[] = 'r.rid IN (' . db_placeholders($ids) . ')';
    $args = array_merge($args, $ids);
  }
  $references = array();
  $where_clause = $where ? 'WHERE (' . implode(') AND (', $where) . ')' : '';
  $result = db_query('SELECT r.rid, r.name FROM {role} r ' . " {$where_clause} ORDER BY r.name ASC", $args);
  while ($reference = db_fetch_object($result)) {
    $references[$reference->rid] = array(
      'title' => $reference->name,
      'rendered' => check_plain($reference->name),
    );
  }
  return $references;
}