You are here

function nodequeue_api_autocomplete in Nodequeue 7.3

Same name and namespace in other branches
  1. 5.2 nodequeue.module \nodequeue_api_autocomplete()
  2. 6.2 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 1739
Maintains queues of nodes in arbitrary order.

Code

function nodequeue_api_autocomplete($queue, $subqueue, $string) {
  $matches = array();
  if (empty($string)) {
    return $matches;
  }
  $query = db_select('node', 'n')
    ->addTag('node_access')
    ->fields('n', array(
    'nid',
    'tnid',
    'title',
  ))
    ->range(0, variable_get('nodequeue_autocomplete_limit', 10));
  if (!empty($queue->types)) {
    $query
      ->condition('n.type', $queue->types, 'IN');
  }
  $where_args = array();
  global $user;
  if (!user_access('administer nodes', $user)) {
    $query
      ->condition(db_or()
      ->condition('n.status', 1)
      ->condition('n.uid', $user->uid));
  }

  // 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.
    $query
      ->condition('n.nid', $preg_matches[1]);
  }
  else {

    // Build the constant parts of the query.
    $query
      ->where('LOWER(n.title) LIKE LOWER(:string)', array(
      ':string' => '%' . db_like($string) . '%',
    ));
  }

  // Call to the API.
  $function = $queue->owner . "_nodequeue_autocomplete";
  if (function_exists($function)) {
    return $function($queue, $subqueue, $string, $where, $where_args);
  }
  else {
    $query
      ->addTag('i18n_select');
    $result = $query
      ->execute();
    foreach ($result as $node) {
      $id = nodequeue_get_content_id($queue, $node);
      $matches[$node->nid] = check_plain($node->title) . " [nid: {$id}]";
    }
  }
  return $matches;
}