suggestion.module in Autocomplete Search Suggestions 7
Same filename and directory in other branches
Autocomplete suggestions.
File
suggestion.moduleView source
<?php
/**
* @file
* Autocomplete suggestions.
*/
/**
* AJAX search autocomplete callback.
*
* @param string $txt
* The search string.
*/
function suggestion_autocomplete($txt = '') {
$limit = variable_get('suggestion_limit', 20);
$min = variable_get('suggestion_atoms_min', 3);
$txt = preg_replace(array(
'/^[^a-z]+/',
'/[^a-z]+$/',
), array(
'',
' ',
), strtolower($txt));
if (strlen($txt) < variable_get('suggestion_min', 4)) {
print drupal_json_output(array(
'' => '',
));
drupal_exit();
}
$count = str_word_count($txt);
$atoms = $count < $min ? $min + 2 : $count + 2;
$ngram = db_like($txt) . '%';
$suggestions = SuggestionStorage::getAutocomplete($ngram, $atoms, $limit);
if (count($suggestions) < $limit) {
$suggestions += SuggestionStorage::getAutoComplete('%' . $ngram, $atoms, $limit - count($suggestions));
}
print count($suggestions) ? drupal_json_output($suggestions) : drupal_json_output(array(
'' => '',
));
drupal_exit();
}
/**
* Access callback value.
*
* @return bool
* Always TRUE.
*/
function suggestion_autocomplete_access_callback() {
return TRUE;
}
/**
* Implements hook_cron().
*/
function suggestion_cron() {
if (!variable_get('suggestion_synced', TRUE)) {
SuggestionHelper::index();
}
}
/**
* Implements hook_form_alter().
*/
function suggestion_form_alter(&$form, &$state, $form_id) {
$autocomplete = variable_get('suggestion_autocomplete', array());
if (!empty($autocomplete[$form_id])) {
return SuggestionHelper::alterElement($form, $autocomplete[$form_id]);
}
}
/**
* Implements hook_help().
*/
function suggestion_help($path, $arg) {
switch ($path) {
case 'admin/help#suggestion':
$txt = file_get_contents(drupal_get_path('module', 'suggestion') . '/README.txt');
return str_replace("\n", "\n<br />", check_plain($txt));
}
}
/**
* Drupal AJAX callback form for the "Index Suggestions" block.
*/
function suggestion_index_block_ajax_callback(&$form, &$state) {
if (!empty($state['values']['flush'])) {
SuggestionStorage::truncateSuggestion();
}
SuggestionHelper::index();
return array(
'#markup' => '<div id="suggestion-index-feedback"><p>' . t('Suggestions Indexed') . '</p></div>',
);
}
/**
* Implements hook_menu().
*/
function suggestion_menu() {
return array(
'admin/config/suggestion' => array(
'title' => 'Suggestion Configuration',
'description' => 'Suggestion auto-complete settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'suggestion_admin_settings_form',
),
'access arguments' => array(
'administer suggestion',
),
'file' => 'suggestion.admin.inc',
'type' => MENU_NORMAL_ITEM,
),
'admin/config/suggestion/edit/%' => array(
'title' => 'Edit Suggestion',
'description' => 'Suggestions editing.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'suggestion_admin_edit_form',
4,
),
'access arguments' => array(
'administer suggestion',
),
'file' => 'suggestion.admin.inc',
'type' => MENU_CALLBACK,
),
'admin/config/suggestion/index' => array(
'title' => 'Indexing',
'description' => 'Suggestions indexing options.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'suggestion_admin_index_form',
),
'access arguments' => array(
'administer suggestion',
),
'file' => 'suggestion.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 10,
),
'admin/config/suggestion/search' => array(
'title' => 'Search',
'description' => 'Search and update suggestions.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'suggestion_admin_search_form',
4,
),
'access arguments' => array(
'administer suggestion',
),
'file' => 'suggestion.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
),
'admin/config/suggestion/settings' => array(
'title' => 'Configuration',
'description' => 'Suggestion auto-complete settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'suggestion_admin_settings_form',
),
'access arguments' => array(
'administer suggestion',
),
'file' => 'suggestion.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
),
'suggestion/autocomplete' => array(
'page callback' => 'suggestion_autocomplete',
'access callback' => 'suggestion_autocomplete_access_callback',
'type' => MENU_CALLBACK,
'delivery callback' => 'ajax_callback',
),
);
}
/**
* Implements hook_node_insert().
*/
function suggestion_node_insert($node) {
$types = SuggestionHelper::types();
if (!empty($node->status) && !empty($node->type) && in_array($node->type, $types)) {
SuggestionHelper::insert($node->title, SuggestionStorage::CONTENT_BIT);
}
}
/**
* Implements hook_permission().
*/
function suggestion_permission() {
return array(
'administer suggestion' => array(
'title' => t('Administer the Suggestion Module'),
'description' => t('Perform administration tasks for the suggestion module.'),
),
);
}
/**
* Custom submit function to add surfer suggestions.
*/
function suggestion_surfer_submit($form, &$state) {
$field_name = variable_get('suggestion_field_name', '');
if (empty($state['values'][$field_name])) {
return;
}
$txt = SuggestionHelper::tokenize($state['values'][$field_name], variable_get('suggestion_min', 4));
if (!$txt) {
return;
}
$atoms = SuggestionHelper::atomize($txt);
$count = count($atoms);
if ($count < variable_get('suggestion_atoms_min', 3) || $count > variable_get('suggestion_atoms_max', 6)) {
return;
}
$ngram = implode(' ', $atoms);
$qty = SuggestionStorage::getNgramQty($ngram);
if ($qty) {
$src = SuggestionStorage::getBitmap($ngram, SuggestionStorage::SURFER_BIT);
$key = array(
'ngram' => $ngram,
);
$fields = array(
'atoms' => $count,
'density' => SuggestionHelper::calculateDensity($src, $count, $qty + 1),
'qty' => $qty + 1,
'src' => $src,
);
SuggestionStorage::mergeSuggestion($key, $fields);
return;
}
$score = SuggestionStorage::getScore($atoms);
if ($score >= SuggestionHelper::MIN_SCORE) {
SuggestionHelper::insert($ngram, SuggestionStorage::SURFER_BIT, $score);
}
}
Functions
Name | Description |
---|---|
suggestion_autocomplete | AJAX search autocomplete callback. |
suggestion_autocomplete_access_callback | Access callback value. |
suggestion_cron | Implements hook_cron(). |
suggestion_form_alter | Implements hook_form_alter(). |
suggestion_help | Implements hook_help(). |
suggestion_index_block_ajax_callback | Drupal AJAX callback form for the "Index Suggestions" block. |
suggestion_menu | Implements hook_menu(). |
suggestion_node_insert | Implements hook_node_insert(). |
suggestion_permission | Implements hook_permission(). |
suggestion_surfer_submit | Custom submit function to add surfer suggestions. |