| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function top_searches_menu($may_cache) {
|
| 8 |
$items = array();
|
| 9 |
if ($may_cache) {
|
| 10 |
$items[] = array(
|
| 11 |
'path' => 'admin/settings/top_searches',
|
| 12 |
'access' => user_access('administer blocks'),
|
| 13 |
'title' => t('Top Searches'),
|
| 14 |
'callback' => drupal_get_form,
|
| 15 |
'callback arguments' => array('top_searches_admin_form'),
|
| 16 |
'description' => t('General settings for the Top Searches module.'),
|
| 17 |
);
|
| 18 |
$items[] = array(
|
| 19 |
'path' => 'admin/settings/top_searches/clear',
|
| 20 |
'access' => user_access('administer blocks'),
|
| 21 |
'title' => t('Top Searches'),
|
| 22 |
'type' => MENU_CALLBACK,
|
| 23 |
'callback' => 'top_searches_form_clear',
|
| 24 |
'description' => t('General settings for the Top Searches module.'),
|
| 25 |
);
|
| 26 |
}
|
| 27 |
return $items;
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Admin UI. Allow to limit number of items in the results list and allow to clear the results
|
| 32 |
*/
|
| 33 |
function top_searches_admin_form() {
|
| 34 |
$form = array();
|
| 35 |
$op = isset($_POST['op']) ? $_POST['op'] : '';
|
| 36 |
|
| 37 |
// In case we need to clear the DB table, redirect:
|
| 38 |
if ($op == t('Reset search counters')) {
|
| 39 |
drupal_goto('admin/settings/top_searches/clear');
|
| 40 |
}
|
| 41 |
|
| 42 |
$form['top_searches']['top_searches_block_items'] = array(
|
| 43 |
'#type' => 'textfield',
|
| 44 |
'#maxlength' => 2,
|
| 45 |
'#size' => 2,
|
| 46 |
'#title' => t('Maximum number of items to show in Top searches block'),
|
| 47 |
'#default_value' => variable_get('top_searches_block_items', 50),
|
| 48 |
);
|
| 49 |
|
| 50 |
$form['clear_searches'] = array(
|
| 51 |
'#type' => 'button',
|
| 52 |
'#value' => t('Reset search counters'),
|
| 53 |
'#description' => top_searches_count_rows() . ' values'
|
| 54 |
);
|
| 55 |
|
| 56 |
return system_settings_form($form);
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Clears the Top Searches table
|
| 61 |
*/
|
| 62 |
function top_searches_form_clear() {
|
| 63 |
// We first set the message, so we have the right number of rows
|
| 64 |
drupal_set_message(t("The Top Searches counters were reset. @number records were deleted", array('@number' => top_searches_count_rows())));
|
| 65 |
db_query("TRUNCATE {top_searches}");
|
| 66 |
drupal_goto('admin/settings/top_searches');
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Implementation of hook_block().
|
| 71 |
*/
|
| 72 |
function top_searches_block($op = 'list', $delta = 0, $edit = array()) {
|
| 73 |
switch ($op) {
|
| 74 |
case 'list':
|
| 75 |
$blocks[0]['info'] = t('Top Searches');
|
| 76 |
return $blocks;
|
| 77 |
case 'view':
|
| 78 |
switch ($delta) {
|
| 79 |
case 0:
|
| 80 |
$top_searches = top_searches_collect_results();
|
| 81 |
if (count($top_searches)) {
|
| 82 |
$block['subject'] = t("Top Searches");
|
| 83 |
$block['content'] = theme('top_searches_block', $top_searches);
|
| 84 |
}
|
| 85 |
break;
|
| 86 |
}
|
| 87 |
return $block;
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
function top_searches_form_alter($form_id, &$form) {
|
| 92 |
// Don't count admin searches (like admin/user/search)
|
| 93 |
if (arg(0) == 'admin') return;
|
| 94 |
|
| 95 |
// Only count node searches
|
| 96 |
// TODO: allow different blocks for different types of seraches
|
| 97 |
if ($form['module']['#value'] != 'node') return;
|
| 98 |
|
| 99 |
if ($form_id == 'search_form' || $form_id == 'search_block_form') {
|
| 100 |
$form['#submit'] = array_merge($form['#submit'], array('top_searches_catch_search_keys' => array()));
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
function top_searches_catch_search_keys($form_id, $form_values) {
|
| 105 |
switch ($form_id) {
|
| 106 |
case "search_block_form":
|
| 107 |
$keys = $form_values['search_block_form_keys'];
|
| 108 |
break;
|
| 109 |
case "search_form":
|
| 110 |
$keys = $form_values['keys'];
|
| 111 |
break;
|
| 112 |
}
|
| 113 |
|
| 114 |
// Beautify the search phrase
|
| 115 |
$keys = preg_replace("/[' ']{2,}/", ' ', ucwords(strtolower(trim($keys))));
|
| 116 |
|
| 117 |
// Search the DB for existing keys:
|
| 118 |
$results_qid = db_result(db_query("SELECT qid FROM {top_searches} WHERE q = '%s'", $keys));
|
| 119 |
if ($results_qid) {
|
| 120 |
db_query("UPDATE {top_searches} SET counter = (counter + 1) WHERE qid = %d", $results_qid);
|
| 121 |
}
|
| 122 |
else {
|
| 123 |
db_query("INSERT INTO {top_searches} (q, counter) VALUES ('%s', %d)", $keys, 1);
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
function top_searches_collect_results() {
|
| 128 |
$result = db_query("SELECT q, counter FROM {top_searches} ORDER by counter DESC LIMIT %d", variable_get('top_searches_block_items', 50));
|
| 129 |
$top_searches = array();
|
| 130 |
|
| 131 |
if (db_num_rows($result)) {
|
| 132 |
while ($row = db_fetch_object($result)) {
|
| 133 |
$top_searches[] = $row;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
return $top_searches;
|
| 137 |
}
|
| 138 |
|
| 139 |
function top_searches_count_rows() {
|
| 140 |
$result = db_result(db_query("SELECT COUNT(*) FROM {top_searches}"));
|
| 141 |
return $result ? $result : '0';
|
| 142 |
}
|
| 143 |
|
| 144 |
function theme_top_searches_block($top_searches) {
|
| 145 |
$output = '';
|
| 146 |
foreach ($top_searches as $q) {
|
| 147 |
// With counters:
|
| 148 |
//$items[] = l($q->q, "search/node/". $q->q) . ' (' . $q->counter . ')';
|
| 149 |
// Without counters:
|
| 150 |
$items[] = l($q->q, "search/node/". $q->q);
|
| 151 |
}
|
| 152 |
return theme('item_list', $items);
|
| 153 |
}
|