You are here

function panels_node_autocomplete in Panels 5.2

Same name and namespace in other branches
  1. 5 panels.module \panels_node_autocomplete()
  2. 6.2 includes/callbacks.inc \panels_node_autocomplete()

Helper function for autocompletion of node titles. This is mostly stolen from clipper.

1 string reference to 'panels_node_autocomplete'
panels_menu in ./panels.module
Implementation of hook_menu

File

./panels.module, line 1343
panels.module Core API for Panels. Provides display editing and rendering capabilities.

Code

function panels_node_autocomplete($string) {

  // TODO: Compare this to the nodequeue version, see which is better.
  // TODO: The nodequeue version is totally better. Steal it.
  // if there are node_types passed, we'll use those in a MySQL IN query.
  if ($string != '') {
    $preg_matches = array();
    $match = preg_match('/\\[nid: (\\d+)\\]/', $string, $preg_matches);
    if (!$match) {
      $match = preg_match('/^nid: (\\d+)/', $string, $preg_matches);
    }
    if ($match) {
      $arg = $preg_matches[1];
      $where = "n.nid = %d";
    }
    else {
      $arg = $string;
      $where = "LOWER(title) LIKE LOWER('%%%s%%')";
    }
    $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.name FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE {$where}"), $arg, 0, 10);
    $matches = array();
    while ($node = db_fetch_object($result)) {
      $name = empty($node->name) ? variable_get('anonymous', t('Anonymous')) : check_plain($node->name);
      $matches[$node->title . " [nid: {$node->nid}]"] = '<span class="autocomplete_title">' . check_plain($node->title) . '</span> <span class="autocomplete_user">(' . t('by @user', array(
        '@user' => $name,
      )) . ')</span>';
    }
    drupal_set_header('Content-Type: text/javascript; charset=utf-8');
    print drupal_to_js($matches);
  }
}