You are here

function search_autocomplete_autocomplete in Search Autocomplete 7.2

Same name and namespace in other branches
  1. 5 search_autocomplete.module \search_autocomplete_autocomplete()
  2. 6.4 search_autocomplete.autocomplete.inc \search_autocomplete_autocomplete()
  3. 6 search_autocomplete.module \search_autocomplete_autocomplete()
  4. 6.2 search_autocomplete.module \search_autocomplete_autocomplete()
  5. 7.4 search_autocomplete.autocomplete.inc \search_autocomplete_autocomplete()

Menu callback; autocomplete handler. Creates suggestions for autocompletion according to settings

Parameters

string the characters entered in the search boxs:

1 string reference to 'search_autocomplete_autocomplete'
search_autocomplete_menu in ./search_autocomplete.admin.inc
Implementation of hook_menu(). Create an administration page to access admin form

File

./search_autocomplete.module, line 23

Code

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.
}