You are here

function search_autocomplete_autocomplete in Search Autocomplete 6.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. 7.4 search_autocomplete.autocomplete.inc \search_autocomplete_autocomplete()
  5. 7.2 search_autocomplete.module \search_autocomplete_autocomplete()

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

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 22

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
  $query = search_parse_query($string);

  // Allow Drupal to parse the search query.
  $word = $query[1][0];

  // get the first word entered in the search box
  $result = db_fetch_array(db_query('SELECT min_char FROM {search_autocomplete_forms} f WHERE f.fid = %d', $fid));
  if (drupal_strlen($word) < $result['min_char']) {

    // Check if there if enough letter to start autocompletion.
    drupal_json($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
  $results = db_query('SELECT * FROM {search_autocomplete_suggestions} s WHERE s.sug_fid = %d AND s.sug_enabled = 1', $fid);
  while ($item = db_fetch_array($results)) {

    // while there is suggestion types to analyse:
    $query = $item['sug_query'];

    // get the SQL query for this suggestion type
    $prefix = t($item['sug_prefix']);

    // get the prefix for this suggestion type
    $params = array(
      ':like_word' => "'%s'",
      ':curr_lang' => $language->language,
    );
    $query = strtr($query, $params);
    $suggestions = db_query($query, '%' . $word . '%');
    while ($obj = db_fetch_object($suggestions)) {
      $sug_obj_vals = array_values(get_object_vars($obj));
      $sug_elem = array_shift($sug_obj_vals);
      $sug_link_vals = array_values(get_object_vars($obj));
      $sug_link = array_pop($sug_link_vals);
      $sug = html_entity_decode(check_plain($sug_elem), ENT_QUOTES);
      $sug_url = html_entity_decode(check_plain($sug_link), ENT_QUOTES);
      $sug_pref = trim($prefix) . ' ' . $sug;
      $matches[trim($sug_pref)] = url(trim($sug_url));

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

  // Return matches.
}