You are here

search_autocomplete.module in Search Autocomplete 6

File

search_autocomplete.module
View source
<?php

/*
 * Drupal Module: Search Autocomplete
 *
 * @author: Arnaud Bonnevigne <www.bonvga.net/contact>
 */
function search_autocomplete_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/search/search_autocomplete',
      'title' => t("Search Autocomplete"),
      'description' => t('Settings for the search autocomplete module.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'search_autocomplete_admin_settings',
      ),
      'access' => user_access('search autocomplete admin'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'search_autocomplete/autocomplete',
      'callback' => 'search_autocomplete_autocomplete',
      'access' => user_access('search autocomplete access'),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}
function search_autocomplete_perm() {
  return array(
    'search autocomplete admin',
    'search autocomplete access',
  );
}
function search_autocomplete_admin_settings() {
  $form = array();
  $form['search_autocomplete_hook'] = array(
    '#type' => 'fieldset',
    '#title' => t('Apply autocomplete search on fields'),
    '#description' => t('Choose the field to apply search autocomplete'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['search_autocomplete_hook']['search_autocomplete_hook_search'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply autocomplete search on the search form'),
    '#default_value' => variable_get("search_autocomplete_hook_search", 0),
    '#return_value' => 1,
    '#required' => FALSE,
  );
  $form['search_autocomplete_hook']['search_autocomplete_hook_search_box'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply autocomplete search on the search box form'),
    '#default_value' => variable_get("search_autocomplete_hook_search_box", 0),
    '#return_value' => 1,
    '#required' => FALSE,
  );
  $form['search_autocomplete_hook']['search_autocomplete_hook_search_block'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply autocomplete search on the search block form. (need the module <a href="http://drupal.org/project/search_block" target="_blank">Search Block</a>)'),
    '#default_value' => variable_get("search_autocomplete_hook_search_block", 0),
    '#return_value' => 1,
    '#required' => FALSE,
  );
  $form['search_autocomplete_setting'] = array(
    '#type' => 'fieldset',
    '#title' => t('Autocomplete webservice settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['search_autocomplete_setting']['search_autocomplete_method'] = array(
    '#type' => 'radios',
    '#title' => t('Choose a search method'),
    '#default_value' => variable_get('search_autocomplete_method', 1),
    '#options' => array(
      '1' => t('Natural sort (no sort)'),
      '2' => t('Sort keyword alphabetically'),
      '3' => t('Sort by keyword\'s score'),
      '4' => t('Sort by keyword\'s relevance (slow)'),
    ),
    '#description' => t("Caution: Some request method can be very slow !"),
    '#autocomplete_path' => 'search_autocomplete/autocomplete',
    '#required' => TRUE,
  );
  $trigger = array();
  for ($i = 1; $i < 20; $i++) {
    $trigger[$i] = $i . ' ' . t('characters');
  }
  $form['search_autocomplete_setting']['search_autocomplete_trigger'] = array(
    '#type' => 'select',
    '#title' => t('Minimum keyword size that uncouple autocomplete search'),
    '#default_value' => variable_get('search_autocomplete_trigger', variable_get('minimum_word_size', 3)),
    '#options' => $trigger,
    '#multiple' => FALSE,
    '#required' => TRUE,
  );
  $limit = array();
  for ($i = 1; $i < 50; $i++) {
    $limit[$i] = $i . ' ' . t('results');
  }
  $form['search_autocomplete_setting']['search_autocomplete_limit'] = array(
    '#type' => 'select',
    '#title' => t('Limit of the autocomplete search result'),
    '#default_value' => variable_get('search_autocomplete_limit', 15),
    '#options' => $limit,
    '#multiple' => FALSE,
    '#required' => TRUE,
  );
  $form['search_autocomplete_setting_test'] = array(
    '#type' => 'fieldset',
    '#title' => t('Test field'),
    '#description' => t('In this field, you can check the appearence of an autocomplete search field.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['search_autocomplete_setting_test']['search_autocomplete_test'] = array(
    '#type' => 'textfield',
    '#title' => t('Search autocomplete test field'),
    '#size' => 40,
    '#maxlength' => 40,
    '#default_value' => '',
    '#description' => t("Enter a keyword and wait for the result."),
    '#autocomplete_path' => 'search_autocomplete/autocomplete',
    '#required' => FALSE,
  );
  return system_settings_form($form);
}
function search_autocomplete_autocomplete($search_string) {
  $matches = array();
  $query = search_parse_query($search_string);
  if ($query === NULL || strlen($query[1][0]) < variable_get('search_autocomplete_trigger', variable_get('minimum_word_size', 3))) {
    print drupal_to_js($matches);
    exit;
  }
  $string = $query[1][0];
  switch (variable_get('search_autocomplete_method', 1)) {
    case 1:
      $result = db_query_range("SELECT DISTINCT s.word FROM {search_index} s, {node} n WHERE s.type = 'node' AND n.nid = s.sid AND n.status = 1 AND LOWER(s.word) LIKE LOWER('%s%%')", $string, 0, variable_get('search_autocomplete_limit', 15));
      break;
    case 2:
      $result = db_query_range("SELECT DISTINCT i.word FROM {search_index} i, {node} n WHERE i.type = 'node' AND n.nid = i.sid AND n.status = 1 AND LOWER(i.word) LIKE LOWER('%s%%') ORDER BY i.word ASC", $string, 0, variable_get('search_autocomplete_limit', 15));
      break;
    case 3:
      $result = db_query_range("SELECT i.word FROM {search_index} i, {node} n WHERE i.type = 'node' AND n.nid = i.sid AND n.status = 1 AND LOWER(i.word) LIKE LOWER('%s%%') GROUP BY i.word ORDER BY SUM(i.score) DESC", $string, 0, variable_get('search_autocomplete_limit', 15));
      break;
    case 4:
      $result = db_query_range("SELECT i.word FROM {search_index} AS i INNER JOIN {search_total} AS t ON i.word = t.word INNER JOIN {node} AS n ON n.nid = i.sid WHERE i.type = 'node' AND n.status = 1 AND LOWER(i.word) LIKE LOWER('%s%%') GROUP BY i.word ORDER BY SUM(i.score * t.count) DESC", $string, 0, variable_get('search_autocomplete_limit', 15));
      break;
  }
  while ($user = db_fetch_object($result)) {
    $matches[$user->word] = check_plain($user->word);
  }
  print drupal_to_js($matches);
  exit;
}
function search_autocomplete_form_alter($form_id, &$form) {
  if (user_access('search autocomplete access')) {
    if (variable_get("search_autocomplete_hook_search_box", 0) == 1) {
      if ($form_id == "search_theme_form") {
        $form["search_theme_form_keys"]["#autocomplete_path"] = 'search_autocomplete/autocomplete';
      }
    }
    if (variable_get("search_autocomplete_hook_search", 0) == 1) {
      if ($form_id == "search_form" && $form['module']['#value'] == "node") {
        $form["basic"]["inline"]["keys"]["#autocomplete_path"] = 'search_autocomplete/autocomplete';
      }
    }
    if (variable_get("search_autocomplete_hook_search_block", 0) == 1) {
      if ($form_id == "search_block_form") {
        $form["search_block_form_keys"]["#autocomplete_path"] = 'search_autocomplete/autocomplete';
      }
    }
  }
}