View source
<?php
function top_searches_theme() {
return array(
'top_searches_block' => array(
'file' => 'top_searches.module',
'arguments' => array(
'top_searches' => array(),
),
),
);
}
function top_searches_menu() {
$items = array();
$items['admin/settings/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/settings/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($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Top Searches');
return $blocks;
case 'view':
switch ($delta) {
case 0:
$top_searches = top_searches_collect_results();
if (count($top_searches)) {
$block['subject'] = t("Top Searches");
$block['content'] = theme('top_searches_block', $top_searches);
}
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_result(db_query("SELECT qid FROM {top_searches} WHERE q = '%s'", $keys));
if ($results_qid) {
db_query("UPDATE {top_searches} SET counter = (counter + 1) WHERE qid = %d", $results_qid);
}
else {
db_query("INSERT INTO {top_searches} (q, counter) VALUES ('%s', %d)", $keys, 1);
}
}
function top_searches_collect_results() {
$result = db_query("SELECT q, counter FROM {top_searches} ORDER by counter DESC LIMIT %d", variable_get('top_searches_block_items', 50));
$top_searches = array();
while ($row = db_fetch_object($result)) {
$top_searches[] = $row;
}
return $top_searches;
}
function top_searches_count_rows() {
$result = db_result(db_query("SELECT COUNT(*) FROM {top_searches}"));
return $result ? $result : '0';
}
function theme_top_searches_block($top_searches) {
$show_counters = variable_get('top_searches_show_counters', 0);
$output = '';
foreach ($top_searches as $q) {
$items[] = l($q->q, "search/node/" . $q->q) . ($show_counters ? ' (' . $q->counter . ')' : '');
}
return theme('item_list', $items);
}