View source
<?php
function search_api_stats_block_block_info() {
$blocks = array();
foreach (search_api_index_load_multiple(FALSE) as $index) {
$blocks[$index->server . '__' . $index->machine_name] = array(
'info' => t('Search API stats block: @identifier', array(
'@identifier' => $index->server . ' - ' . $index->machine_name,
)),
'cache' => DRUPAL_CACHE_GLOBAL,
);
}
return $blocks;
}
function search_api_stats_block_block_view($delta = '') {
$block = array();
list($s_name, $i_name) = explode('__', $delta);
if (!empty($s_name) && !empty($i_name)) {
$block = array(
'subject' => t('Top search phrases'),
'content' => _search_api_stats_block_show_keywords($s_name, $i_name),
);
}
return $block;
}
function search_api_stats_block_block_configure($delta = '') {
$form = array();
$config = variable_get('search_api_stats_block_config', array());
list($s_name, $i_name) = explode('__', $delta);
if (!empty($s_name) && !empty($i_name)) {
$form['search_api_stats_block_num_phrases'] = array(
'#type' => 'select',
'#title' => t('Number of top search phrases to display'),
'#default_value' => empty($config[$s_name][$i_name]['num_phrases']) ? 8 : $config[$s_name][$i_name]['num_phrases'],
'#options' => drupal_map_assoc(array(
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
25,
30,
)),
);
$form['search_api_stats_block_path'] = array(
'#type' => 'textfield',
'#title' => t('Path of search page'),
'#default_value' => empty($config[$s_name][$i_name]['path']) ? '' : $config[$s_name][$i_name]['path'],
'#required' => TRUE,
);
$form['search_api_stats_block_param_name'] = array(
'#type' => 'textfield',
'#title' => t('Parameter name for the search phrase'),
'#default_value' => empty($config[$s_name][$i_name]['param_name']) ? 'keywords' : $config[$s_name][$i_name]['param_name'],
'#required' => TRUE,
);
}
return $form;
}
function search_api_stats_block_block_save($delta = '', $edit = array()) {
$config = variable_get('search_api_stats_block_config', array());
list($s_name, $i_name) = explode('__', $delta);
if (!empty($s_name) && !empty($i_name)) {
$config[$s_name][$i_name]['num_phrases'] = (int) $edit['search_api_stats_block_num_phrases'];
$config[$s_name][$i_name]['path'] = $edit['search_api_stats_block_path'];
$config[$s_name][$i_name]['param_name'] = $edit['search_api_stats_block_param_name'];
}
variable_set('search_api_stats_block_config', $config);
}
function search_api_stats_block_theme($existing, $type, $theme, $path) {
return array(
'search_api_stats_block' => array(
'variables' => array(
'phrases' => NULL,
'path' => NULL,
'param_name' => NULL,
),
'file' => 'search_api_stats_block.theme.inc',
),
);
}
function _search_api_stats_block_show_keywords($s_name, $i_name) {
$config = variable_get('search_api_stats_block_config', array());
$phrases = array();
$path = empty($config[$s_name][$i_name]['path']) ? '' : $config[$s_name][$i_name]['path'];
$param_name = empty($config[$s_name][$i_name]['param_name']) ? 'keywords' : $config[$s_name][$i_name]['param_name'];
$result = db_query_range("SELECT *, count(*) as num FROM {search_api_stats} WHERE s_name=:s_name AND i_name=:i_name AND keywords <> '' GROUP BY keywords ORDER BY num DESC", 0, 8, array(
':s_name' => $s_name,
':i_name' => $i_name,
));
foreach ($result as $phrase) {
$phrases[$phrase->keywords] = (array) $result;
}
return theme('search_api_stats_block', array(
'phrases' => $phrases,
'path' => $path,
'param_name' => $param_name,
));
}