You are here

function uc_termsofservice_node_autocomplete in Ubercart Terms of Service 7

Same name and namespace in other branches
  1. 6 uc_termsofservice.module \uc_termsofservice_node_autocomplete()

Autocomplete callback, taken from panels module.

1 string reference to 'uc_termsofservice_node_autocomplete'
uc_termsofservice_menu in ./uc_termsofservice.module
Implements hook_menu().

File

./uc_termsofservice.module, line 387
Ubercart Terms of Service.

Code

function uc_termsofservice_node_autocomplete($string) {

  // If there are node_types passed, we'll use those in a MySQL IN query.
  if ($string != '') {
    $query = db_select('node', 'n')
      ->fields('n', array(
      'nid',
      'title',
    ))
      ->addTag('node_access');
    $query
      ->join('users', 'u', 'u.uid = n.uid');
    $query
      ->fields('u', array(
      'name',
    ));

    // Build query conditions.
    $preg_matches = array();
    $match = preg_match('/\\[nid: (\\d+)\\]/', $string, $preg_matches);
    if (!$match) {
      $match = preg_match('/^nid: (\\d+)/', $string, $preg_matches);
    }
    if ($match) {
      $query
        ->condition('n.nid', $preg_matches[1]);
    }
    else {
      $query
        ->condition('n.title', '%' . db_like($string) . '%', 'LIKE');
    }
    if (!user_access('administer nodes')) {
      $query
        ->condition('n.status', 1);
    }

    // Execute the query.
    $result = $query
      ->execute();
    $matches = array();
    foreach ($result as $node) {
      $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>';
    }

    // Return the results to the form in json.
    drupal_json_output($matches);
  }
}