You are here

function webform_autocomplete_js in Webform Autocomplete 7.2

Same name and namespace in other branches
  1. 7 autocomplete.inc \webform_autocomplete_js()

Autocomplete callback.

1 string reference to 'webform_autocomplete_js'
webform_autocomplete_menu in ./webform_autocomplete.module
Implements hook_menu().

File

./autocomplete.inc, line 273
Autocomplete component.

Code

function webform_autocomplete_js($node, $cid, $str = '') {
  if ($str !== '') {
    $component = $node->webform['components'][$cid];
    $result_count = (int) $component['extra']['autocomplete_result_count'];
    $match_rule = $component['extra']['autocomplete_match_rule'];
    $results = array();
    $count = 0;

    // Search from prepulated options
    if (!empty($component['extra']['autocomplete_items'])) {
      $options = preg_split('/\\r\\n|\\r|\\n/', $component['extra']['autocomplete_items']);
      foreach ($options as $val) {
        $is_match = FALSE;
        if ($match_rule == WEBFORM_AUTOCOMPLETE_MATCH_BEGIN) {
          if (stripos($val, $str, 0) === 0) {
            $is_match = TRUE;
          }
        }
        elseif ($match_rule == WEBFORM_AUTOCOMPLETE_MATCH_CONTAINS) {
          if (stripos($val, $str) !== FALSE) {
            $is_match = TRUE;
          }
        }
        else {
          if (strpos($val, $str) + strlen($str) == strlen(trim($val))) {
            $is_match = TRUE;
          }
        }
        if ($is_match) {
          $results[$val] = $val;

          // Limit to 20 results
          if (++$count >= $result_count) {
            break;
          }
        }
      }
    }

    // Search from existing submissions
    // Only fire the query if we have fewer than $result_count results already
    if (!empty($component['extra']['autocomplete_existing']) && $count < $result_count) {
      $str_like = db_like($str);
      if ($match_rule == WEBFORM_AUTOCOMPLETE_MATCH_CONTAINS || $match_rule == WEBFORM_AUTOCOMPLETE_MATCH_END) {
        $str_like = '%' . $str_like;
      }
      if ($match_rule == WEBFORM_AUTOCOMPLETE_MATCH_BEGIN || $match_rule == WEBFORM_AUTOCOMPLETE_MATCH_CONTAINS) {
        $str_like = $str_like . '%';
      }
      $db = db_query("SELECT DISTINCT data\n        FROM {webform_submitted_data}\n        WHERE nid = :nid AND cid = :cid AND data LIKE :str LIMIT " . ($result_count - $count), array(
        ':nid' => $node->nid,
        ':cid' => $cid,
        ':str' => $str_like,
      ));
      foreach ($db as $row) {
        $results[$row->data] = $row->data;
      }
    }

    // Search in taxonomy term names.
    if (!empty($component['extra']['autocomplete_taxonomy']) && $count < $result_count && module_exists('taxonomy')) {
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'taxonomy_term')
        ->entityCondition('bundle', $component['extra']['autocomplete_taxonomy'])
        ->propertyOrderBy('name', 'ASC')
        ->range(0, $result_count - $count);
      switch ($component['extra']['autocomplete_match_rule']) {
        case WEBFORM_AUTOCOMPLETE_MATCH_BEGIN:
          $query
            ->propertyCondition('name', db_like($str) . '%', 'like');
          break;
        case WEBFORM_AUTOCOMPLETE_MATCH_END:
          $query
            ->propertyCondition('name', '%' . db_like($str), 'like');
          break;
        default:
          $query
            ->propertyCondition('name', '%' . db_like($str) . '%', 'like');
      }
      $result = $query
        ->execute();
      if (isset($result['taxonomy_term'])) {
        foreach (taxonomy_term_load_multiple(array_keys($result['taxonomy_term'])) as $term) {
          $results[$term->name] = $term->name;
        }
      }
    }

    // Sort all results together
    natcasesort($results);
    drupal_alter('webform_autocomplete_options', $results, $node, $cid, $str);
    drupal_json_output($results);
  }
}