View source
<?php
function top_searches_theme() {
return array(
'top_searches_block' => array(
'file' => 'top_searches.module',
'variables' => array(
'top_searches' => array(),
),
),
);
}
function top_searches_menu() {
$items = array();
$items['admin/config/search/top_searches'] = array(
'access arguments' => array(
'administer blocks',
),
'title' => 'Top Searches',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'top_searches_admin_form',
),
'description' => 'General settings for the Top Searches module.',
'file' => 'top_searches.admin.inc',
);
$items['admin/config/search/top_searches/clear'] = array(
'access arguments' => array(
'administer blocks',
),
'title' => 'Top Searches',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'top_searches_form_clear_confirm',
),
'description' => 'General settings for the Top Searches module.',
'file' => 'top_searches.admin.inc',
);
return $items;
}
function top_searches_block_info() {
$blocks[0]['info'] = t('Top Searches');
return $blocks;
}
function top_searches_block_view($delta = 0) {
switch ($delta) {
case 0:
$block['subject'] = "";
$top_searches = top_searches_collect_results();
if (count($top_searches)) {
$variable = array();
$block['subject'] = t("Top Searches");
foreach ($top_searches as $searchq) {
$variable['top_searches'][] = array(
'ts_q' => $searchq->q,
'ts_counter' => $searchq->counter,
);
}
$block['content'] = theme('top_searches_block', $variable);
}
break;
}
return $block;
}
function top_searches_form_alter(&$form, &$form_state, $form_id) {
if (arg(0) == 'admin') {
return;
}
if (isset($form['module']['#value']) && $form['module']['#value'] != 'node') {
return;
}
if (in_array($form_id, array(
'search_form',
'search_block_form',
'search_theme_form',
))) {
$form['#submit'][] = 'top_searches_catch_search_keys';
}
}
function top_searches_catch_search_keys($form, $form_state) {
switch ($form['form_id']['#value']) {
case "search_block_form":
$keys = $form_state['values']['search_block_form'];
break;
case "search_theme_form":
$keys = $form_state['values']['search_theme_form'];
break;
case "search_form":
$keys = $form_state['values']['keys'];
break;
}
if ($keys) {
$valid = _top_searches_valid_phrase($keys);
if ($valid) {
top_searches_top_searches($keys);
}
}
}
function _top_searches_valid_phrase($keys) {
$min = variable_get('minimum_word_size', 3);
$words = explode(' ', $keys);
$pass = FALSE;
foreach ($words as $word) {
_search_index_truncate($word);
if (drupal_strlen($word) >= $min) {
$pass = TRUE;
break;
}
}
return $pass;
}
function top_searches_top_searches($keys = NULL) {
if (!is_string($keys)) {
return;
}
else {
top_searches_process_keys($keys);
}
}
function top_searches_process_keys($keys) {
$keys = preg_replace("/[' ']{2,}/", ' ', ucwords(strtolower(trim($keys))));
$results_qid = db_query("SELECT qid FROM {top_searches} WHERE q = :phrase", array(
':phrase' => $keys,
))
->fetchField();
if ($results_qid) {
db_query("UPDATE {top_searches} SET counter = (counter + 1) WHERE qid = {$results_qid}");
}
else {
db_insert('top_searches')
->fields(array(
'q' => $keys,
'counter' => 1,
))
->execute();
}
}
function top_searches_collect_results() {
$limit = variable_get('top_searches_block_items', 50);
$result = db_query("SELECT q, counter FROM {top_searches} ORDER by counter DESC LIMIT {$limit} ");
$top_searches = array();
while ($row = $result
->fetchObject()) {
$top_searches[] = $row;
}
return $top_searches;
}
function top_searches_count_rows() {
$result = db_query("SELECT COUNT(*) FROM {top_searches}")
->fetchField();
return $result ? $result : '0';
}
function theme_top_searches_block($variables) {
$show_counters = variable_get('top_searches_show_counters', 0);
foreach ($variables['top_searches'] as $value) {
$items['items'][] = l($value['ts_q'], 'search/node/' . $value['ts_q']) . ($show_counters ? ' (' . $value['ts_counter'] . ')' : '');
}
return theme('item_list', $items);
}