You are here

function nodequeue_api_autocomplete in Nodequeue 5.2

Same name and namespace in other branches
  1. 6.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

3 calls to nodequeue_api_autocomplete()
nodequeue_arrange_subqueue_form_validate in ./nodequeue.module
Validate handler for nodequeue_arrange_subqueue_form
_nodequeue_ajax_add in ./nodequeue.module
_nodequeue_autocomplete in ./nodequeue.module

File

./nodequeue.module, line 2362

Code

function nodequeue_api_autocomplete($queue, $subqueue, $string) {
  $matches = array();
  if (empty($string)) {
    return $matches;
  }
  global $user;
  if (!user_access('administer nodes', $user)) {
    $where = 'n.status = 1 AND ';
  }
  $where .= "n.type IN (" . implode(', ', array_fill(0, count($queue->types), "'%s'")) . ')';
  $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 {
    $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE {$where}"), $where_args, 0, 10);
    while ($node = db_fetch_object($result)) {
      $matches[$node->nid] = check_plain($node->title) . " [nid: {$node->nid}]";
    }
  }
  return $matches;
}