You are here

function _nodereference_potential_references_standard in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 modules/nodereference/nodereference.module \_nodereference_potential_references_standard()

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

1 call to _nodereference_potential_references_standard()
_nodereference_potential_references in modules/nodereference/nodereference.module
Fetch an array of all candidate referenced nodes.

File

modules/nodereference/nodereference.module, line 920
Defines a field type for referencing one node from another.

Code

function _nodereference_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[] = "n.type = '%s'";
      $args[] = $related_type;
    }
  }
  $where[] = implode(' OR ', $related_types);
  if (!count($related_types)) {
    return array();
  }
  if ($string !== '') {
    $like = $GLOBALS["db_type"] == 'pgsql' ? "ILIKE" : "LIKE";
    $match_clauses = array(
      'contains' => "{$like} '%%%s%%'",
      'equals' => "= '%s'",
      'starts_with' => "{$like} '%s%%'",
    );
    $where[] = 'n.title ' . (isset($match_clauses[$match]) ? $match_clauses[$match] : $match_clauses['contains']);
    $args[] = $string;
  }
  elseif ($ids) {
    $where[] = 'n.nid IN (' . db_placeholders($ids) . ')';
    $args = array_merge($args, $ids);
  }
  $where_clause = $where ? 'WHERE (' . implode(') AND (', $where) . ')' : '';
  $sql = db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n {$where_clause} ORDER BY n.title, n.type");
  $result = $limit ? db_query_range($sql, $args, 0, $limit) : db_query($sql, $args);
  $references = array();
  while ($node = db_fetch_object($result)) {
    $references[$node->nid] = array(
      'title' => $node->node_title,
      'rendered' => check_plain($node->node_title),
    );
  }
  return $references;
}