View source
<?php
function top_searches_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/top_searches',
'access' => user_access('administer blocks'),
'title' => t('Top Searches'),
'callback' => drupal_get_form,
'callback arguments' => array(
'top_searches_admin_form',
),
'description' => t('General settings for the Top Searches module.'),
);
$items[] = array(
'path' => 'admin/settings/top_searches/clear',
'access' => user_access('administer blocks'),
'title' => t('Top Searches'),
'type' => MENU_CALLBACK,
'callback' => 'top_searches_form_clear',
'description' => t('General settings for the Top Searches module.'),
);
}
return $items;
}
function top_searches_admin_form() {
$form = array();
$op = isset($_POST['op']) ? $_POST['op'] : '';
if ($op == t('Reset search counters')) {
drupal_goto('admin/settings/top_searches/clear');
}
$form['top_searches']['top_searches_block_items'] = array(
'#type' => 'textfield',
'#maxlength' => 2,
'#size' => 2,
'#title' => t('Maximum number of items to show in Top searches block'),
'#default_value' => variable_get('top_searches_block_items', 50),
);
$form['clear_searches'] = array(
'#type' => 'button',
'#value' => t('Reset search counters'),
'#description' => top_searches_count_rows() . ' values',
);
return system_settings_form($form);
}
function top_searches_form_clear() {
drupal_set_message(t("The Top Searches counters were reset. @number records were deleted", array(
'@number' => top_searches_count_rows(),
)));
db_query("TRUNCATE {top_searches}");
drupal_goto('admin/settings/top_searches');
}
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_id, &$form) {
if ($form_id == 'search_form' || $form_id == 'search_block_form') {
$form['#submit'] = array_merge($form['#submit'], array(
'top_searches_catch_search_keys' => array(),
));
}
}
function top_searches_catch_search_keys($form_id, $form_values) {
switch ($form_id) {
case "search_block_form":
$keys = $form_values['search_block_form_keys'];
break;
case "search_form":
$keys = $form_values['keys'];
break;
}
$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();
if (db_num_rows($result)) {
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) {
$output = '';
foreach ($top_searches as $q) {
$items[] = l($q->q, "search/node/" . $q->q);
}
return theme('item_list', $items);
}