You are here

function apachesolr_autocomplete_callback in Apache Solr Autocomplete 7

Same name and namespace in other branches
  1. 6 apachesolr_autocomplete.module \apachesolr_autocomplete_callback()
  2. 7.2 apachesolr_autocomplete.module \apachesolr_autocomplete_callback()

Callback for url apachesolr_autocomplete/autocomplete.

Parameters

$context_id: A string identifying the form. For example: apachesolr_search_page:core_search

1 string reference to 'apachesolr_autocomplete_callback'
apachesolr_autocomplete_menu in ./apachesolr_autocomplete.module
Implementation of hook_menu().

File

./apachesolr_autocomplete.module, line 131
Alters search forms to suggest terms using Apache Solr using AJAX. Thanks to: robertDouglass who contributed some of the code. sch4lly for contributing to D7 version

Code

function apachesolr_autocomplete_callback($context_id) {

  // Get search page.
  if (strpos($context_id, 'apachesolr_search_page:') === 0) {
    $search_page_id = substr($context_id, 23);
  }
  else {
    $search_page_id = 'core_search';
  }
  if ($search_page_id && function_exists('apachesolr_search_page_load')) {
    $search_page = apachesolr_search_page_load($search_page_id);
    if (!$search_page) {
      $search_page = 0;
    }
  }
  else {
    $search_page = 0;
  }

  // TODO: Get more user entry, like state of 'retain current filters' and current filters, etc.
  // Get terms
  if (apachesolr_autocomplete_variable_get_widget() == 'custom') {

    // Keys for custom widget come from $_GET.
    $keys = isset($_GET['query']) ? $_GET['query'] : '';
  }
  elseif (apachesolr_autocomplete_variable_get_widget() == 'jqueryui') {

    // Keys for custom widget come from $_GET.
    $keys = isset($_GET['term']) ? $_GET['term'] : '';
  }
  elseif (apachesolr_autocomplete_variable_get_widget() == 'drupal') {

    // Keys for custom widget come from $_GET.
    $keys = arg(2);
  }
  if (!$keys) {

    // Exit quickly when there are no keys.
    drupal_json_output(array());
    exit;
  }
  $suggestions = array();
  $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_word_completion($keys, 5, $search_page));
  if (apachesolr_autocomplete_variable_get_suggest_keywords() || apachesolr_autocomplete_variable_get_suggest_spellcheck()) {
    $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_additional_term($keys, 5, $search_page));
  }

  // Allow other modules to alter the suggestions.
  drupal_alter("apachesolr_autocomplete_suggestions", $suggestions, $keys);
  $result = array();
  $show_counts = apachesolr_autocomplete_variable_get_counts();
  if (apachesolr_autocomplete_variable_get_widget() == 'custom') {

    // Place suggestions into new array for returning as JSON.
    foreach ($suggestions as $key => $suggestion) {
      $display = theme($suggestion['theme'], array(
        'suggestion' => $suggestion,
        'show_counts' => $show_counts,
      ));
      $result[] = array(
        "key" => substr($key, 1),
        "display" => $display,
      );
    }
  }
  elseif (apachesolr_autocomplete_variable_get_widget() == 'jqueryui') {
    foreach ($suggestions as $key => $suggestion) {
      $display = theme($suggestion['theme'], array(
        'suggestion' => $suggestion,
        'show_counts' => $show_counts,
      ));
      $result[] = array(
        "label" => '<a>' . $display . '</a>',
        "value" => $suggestion['suggestion'],
      );
    }
    if (count($result) == 0) {
      $result[] = array(
        "label" => '<span class="apachesolr_autocomplete message">' . t('No suggestions.') . '</span>',
      );
    }
  }
  elseif (apachesolr_autocomplete_variable_get_widget() == 'drupal') {
    foreach ($suggestions as $key => $suggestion) {
      $display = theme($suggestion['theme'], array(
        'suggestion' => $suggestion,
        'show_counts' => $show_counts,
      ));
      $result[substr($key, 1)] = $display;
    }
  }

  // Add caching headers.
  // The cache lifetime will be:
  //  * apachesolr_autocomplete_cache_maximum_age if it's set and >0
  //  * else, use page_cache_maximum_age.
  $cache_max_age = variable_get('apachesolr_autocomplete_cache_maximum_age', -1);
  if ($cache_max_age == -1) {
    $cache_max_age = variable_get('page_cache_maximum_age', 0);
  }
  if ($cache_max_age > 0) {

    // Only add the caching headers if necessary.
    drupal_add_http_header('Cache-Control', 'public, max-age=' . $cache_max_age);
  }

  // Send the output.
  drupal_json_output($result);
  exit;
}