You are here

function nodequeue_api_autocomplete in Nodequeue 6.2

Same name and namespace in other branches
  1. 5.2 nodequeue.module \nodequeue_api_autocomplete()
  2. 7.3 nodequeue.module \nodequeue_api_autocomplete()
  3. 7.2 nodequeue.module \nodequeue_api_autocomplete()

Fetch a list of nodes available to a given subqueue for autocomplete.

Parameters

$queue: The queue that owns the subqueue

$subqueue: The subqueue

$string: The string being matched.

Return value

An keyed array $nid => $title

1 call to nodequeue_api_autocomplete()
_nodequeue_autocomplete in includes/nodequeue.admin.inc

File

./nodequeue.module, line 1585
Maintains queues of nodes in arbitrary order.

Code

function nodequeue_api_autocomplete($queue, $subqueue, $string) {
  $matches = array();
  if (empty($string)) {
    return $matches;
  }
  $where_args = array();
  global $user;
  if (!user_access('administer nodes', $user)) {
    $where = '(n.status = 1 || n.uid = %d) AND ';
    $where_args = array(
      $user->uid,
    );
  }
  $where .= "n.type IN (" . db_placeholders($queue->types, 'varchar') . ')';
  $where_args = array_merge($where_args, $queue->types);

  // Run a match to see if they're specifying by nid.
  $preg_matches = array();
  $match = preg_match('/\\[nid: (\\d+)\\]/', $string, $preg_matches);
  if (!$match) {
    $match = preg_match('/^nid: (\\d+)/', $string, $preg_matches);
  }
  if ($match) {

    // If it found a nid via specification, reduce our resultset to just that nid.
    $where .= " AND n.nid = %d";
    array_push($where_args, $preg_matches[1]);
  }
  else {

    // Build the constant parts of the query.
    $where .= " AND LOWER(n.title) LIKE LOWER('%%%s%%')";
    array_push($where_args, $string);
  }

  // Call to the API.
  $function = $queue->owner . "_nodequeue_autocomplete";
  if (function_exists($function)) {
    return $function($queue, $subqueue, $string, $where, $where_args);
  }
  else {

    // Disable language selection temporarily, enable it again later.
    if (module_exists('i18n')) {
      i18n_selection_mode('off');
    }
    $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.tnid FROM {node} n WHERE " . $where), $where_args, 0, variable_get('nodequeue_autocomplete_limit', 10));
    if (module_exists('i18n')) {
      i18n_selection_mode('reset');
    }
    while ($node = db_fetch_object($result)) {
      $id = nodequeue_get_content_id($queue, $node);
      $matches[$id] = check_plain($node->title) . " [nid: {$id}]";
    }
  }
  return $matches;
}