You are here

public static function SuggestionHelper::insert in Autocomplete Search Suggestions 7

Add a suggestion.

Parameters

string $txt: The title to index.

int $src: The bits to OR with the current bitmap.

int $qty: Default quantity.

Return value

int The number of suggestions inserted.

4 calls to SuggestionHelper::insert()
SuggestionHelper::index in src/SuggestionHelper.php
Create a suggestion index from content titles.
suggestion_node_insert in ./suggestion.module
Implements hook_node_insert().
suggestion_surfer_submit in ./suggestion.module
Custom submit function to add surfer suggestions.
_suggestion_admin_submit_keywords in ./suggestion.admin.inc
Process keywords.

File

src/SuggestionHelper.php, line 158
Helper methods for the suggestion module.

Class

SuggestionHelper
Provides helper methods for suggestions.

Code

public static function insert($txt = '', $src = SuggestionStorage::CONTENT_BIT, $qty = NULL) {
  $count = 0;
  $max = variable_get('suggestion_max', 45);
  $txt = self::tokenize($txt, variable_get('suggestion_min', 4));
  if (!$txt) {
    return 0;
  }
  $atoms = self::atomize($txt);
  foreach (array_keys(self::ngrams($atoms)) as $ngram) {
    if (strlen($ngram) > $max) {
      continue;
    }
    $count = str_word_count($ngram);
    $qty = is_numeric($qty) ? $qty + 1 : SuggestionStorage::getNgramQty($ngram) + 1;
    $src = SuggestionStorage::getBitmap($ngram, $src);
    $key = array(
      'ngram' => $ngram,
    );
    $fields = array(
      'atoms' => $count,
      'density' => self::calculateDensity($src, $count, $qty),
      'qty' => $qty,
      'src' => $src,
    );
    SuggestionStorage::mergeSuggestion($key, $fields);
    $count++;
  }
  return $count;
}