You are here

search_autocomplete.module in Search Autocomplete 7.2

File

search_autocomplete.module
View source
<?php

/**
 * @file
 * Search Autocomplete
 * Enables autocomplete functionality on search fields.
 *
 * @authors
 * Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>
 *
 * Sponsored by:
 * www.axiomcafe.fr
 */
include_once 'search_autocomplete.admin.inc';

/**
 * Menu callback; autocomplete handler.
 * Creates suggestions for autocompletion according to settings
 * @param   string    the characters entered in the search boxs
 */
function search_autocomplete_autocomplete($string = '') {
  global $language;
  $matches = array();
  $word_items = array();
  $node_items = array();
  $user_items = array();
  $taxo_items = array();
  $comment_items = array();
  static $max_sug = 0;
  $fid = arg(1);

  // get the form calling
  $words = explode(' ', $string);

  // explode the words entered in the input string
  $word = $words[0];

  // get the first word entered in the search box
  $result = db_query('SELECT min_char FROM {search_autocomplete_forms} f WHERE f.fid = :fid', array(
    ':fid' => $fid,
  ));
  foreach ($result as $match) {
    if (drupal_strlen($word) < $match->min_char) {

      // Check if there if enough letter to start autocompletion.
      drupal_json_output($matches);

      // no matches, return it empty
      return;

      // if there is not enough letters, stop here
    }
  }

  // get every suggestion types associated with the form being autocompleted
  $result = db_query('SELECT * FROM {search_autocomplete_suggestions} s WHERE s.sug_fid = :fid AND s.sug_enabled = :sug_enabled', array(
    ':fid' => $fid,
    'sug_enabled' => 1,
  ));
  foreach ($result as $item) {

    // while there is suggestion types to analyse:
    $query = $item->sug_query;
    $prefix = t($item->sug_prefix);

    // get the prefix for this suggestion type
    $params = array(
      ':like_word' => '%' . $word . '%',
    );
    if (strpos($query, ':curr_lang') !== false) {
      $params[':curr_lang'] = $language->language;
    }
    $result = db_query($query, $params);
    foreach ($result as $obj) {
      $sug_elem = array_shift(array_values(get_object_vars($obj)));
      $sug = html_entity_decode(check_plain($sug_elem), ENT_QUOTES);
      $sug_index = trim($prefix) . ' ' . $sug;
      $matches[trim($sug_index)] = trim($sug);

      // add the suggestion to be returned
    }
  }
  drupal_json_output($matches);

  // Return matches.
}

// search_autocomplete_autocomplete()

/**
 * HOOK OF INIT:
 * add autocomplete.js on everypage
 */
function search_autocomplete_init() {
  global $base_url;

  // checkout if user have authorization to access the autocompleted form
  if (user_access('use Search Autocomplete')) {

    // init:
    $settings = array();

    // checkout if the db exists (it should)
    if (db_table_exists('search_autocomplete_forms')) {

      // get every form to autocomplete
      $result = db_query('SELECT * FROM {search_autocomplete_forms} WHERE enabled=1');

      // build the setting array to transfert to JS
      foreach ($result as $match) {
        $form_id = 'form' . $match->fid;
        drupal_add_js(array(
          'search_autocomplete' => array(
            $form_id => array(
              'selector' => $match->selector,
              'minChars' => $match->min_char,
              'max_sug' => $match->max_sug,
              'url' => $base_url . '/search_autocomplete/' . $match->fid . '/autocomplete',
              'fid' => $match->fid,
            ),
          ),
        ), 'setting');
      }

      // If there is some results: need to include the css and js....
      if ($result) {
        drupal_add_css(drupal_get_path('module', 'search_autocomplete') . '/css/jquery.autocomplete.css');
        drupal_add_js(drupal_get_path('module', 'search_autocomplete') . '/js/jquery.autocomplete.js');
      }
    }
  }
}

// search_autocomplete_init()

Functions

Namesort descending Description
search_autocomplete_autocomplete Menu callback; autocomplete handler. Creates suggestions for autocompletion according to settings
search_autocomplete_init HOOK OF INIT: add autocomplete.js on everypage