Bento box search with multiple targets
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.8 KiB

12 years ago
<?php
/**
* @file
* Implementation of Roblib search for searching several targets.
*/
/**
* Implements hook_boot().
* @global type $conf
*/
function roblib_search_cufts_boot() {
/* below is an example from islandora_solr module for internationalization
* global $conf;
// Allow i18n, by using multilingual variables.
if (module_exists('i18n')) {
$vars = array(
'islandora_solr_facets',
'islandora_solr_result_fields',
'islandora_solr_searchterms'
);
if (isset($conf['i18n_variables']) && is_array($conf['i18n_variables'])) {
$conf['i18n_variables'] = array_merge($vars, $conf['i18n_variables']);
}
else {
$conf['i18n_variables'] = $vars;
}
} */
}
/**
* Implements hook_menu().
*/
function roblib_search_cufts_menu() {
$items = array();
$items['admin/roblib_search/cufts_search'] = array(
'title' => 'Cufts Search Target configuration',
'description' => 'Configuration for the Roblib evergreen search target',
'page callback' => 'drupal_get_form',
'page arguments' => array('roblib_search_cufts_config_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['roblib_search/cufts/ajax/%'] = array(
'title' => 'cufts ajax',
'page callback' => 'roblib_search_cufts_ajax',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
'access arguments' => array('search roblib cufts'),
);
return $items;
}
function roblib_search_cufts_config_form($form, &$form_state) {
$form['roblib_search_cufts_url'] = array(
'#type' => 'textfield',
'#title' => t('Cufts url'),
'#default_value' => variable_get('roblib_search_cufts_url', 'http://cufts2.lib.sfu.ca/CJDB/PCU/browse/show?'),
'#description' => t('The base Cufts URL, for example http://cufts2.lib.sfu.ca/CJDB/PCU/browse/show?'),
'#required' => TRUE,
);
$form['roblib_search_cufts_search_suffix'] = array(
'#type' => 'textfield',
'#title' => t('Cufts search suffix'),
'#default_value' => variable_get('roblib_search_cufts_search_suffix', 'browse_field=title&search_type=startswith&format=json&search_terms='),
'#description' => t('The suffix will be appended to the base url for searches, for example browse_field=title&search_type=startswith&format=json&search_terms='),
'#required' => TRUE,
);
$form['roblib_search_cufts_num_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results to return'),
'#default_value' => variable_get('roblib_search_cufts_num_results', '5'),
'#description' => t('The number of results to display in the Bento box'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* Implements hook_permission().
*/
function roblib_search_cufts_permission() {
return array(
'search roblib cufts' => array(
'title' => t('Search the cufts target'),
'description' => t('Search oblib cufts target. This permission exposes the search blocks and allows you to see search results.'),
),
'administer roblib search cufts' => array(
'title' => t('Administer Roblib Search Cufts'),
'description' => t('Administer settings for the Roblib cufts search client.'),
),
);
}
/**
* Implements hook_theme().
*/
function roblib_search_cufts_theme() {
// set path
$path = drupal_get_path('module', 'roblib_search_cufts');
$file = 'theme.inc';
return array(
// results page
'roblib_search_cufts' => array(
'path' => $path . '/theme',
'file' => $file,
'template' => 'roblib-search-cufts',
'variables' => array('results'=>NULL)
12 years ago
)
);
}
function roblib_search_cufts_block_info() {
$blocks['roblib_search_cufts_results'] = array(
// info: The name of the block.
'info' => t('Cufts Search Results block'),
// Block caching options (per role, per user, etc.)
'cache' => DRUPAL_CACHE_PER_ROLE, // default
);
return $blocks;
}
function roblib_search_cufts_block_view($delta = '') {
//The $delta parameter tells us which block is being requested.
switch ($delta) {
case 'roblib_search_cufts_results':
// The subject is displayed at the top of the block. Note that it
// should be passed through t() for translation. The title configured
// for the block using Drupal UI supercedes this one.
$block['subject'] = t('Cufts Results');
// The content of the block is typically generated by calling a custom
// function.
// $block['content'] = roblib_search_cufts_get_results();
$block['content'] = theme('roblib_search_cufts', array('results' => NULL)); //we will get the results via javascript
break;
}
return $block;
}
function roblib_search_cufts_ajax($query) {
print roblib_search_cufts_get_results($query);
exit();
}
/**
*
* @param string $query
* @return string
* json string
*/
function roblib_search_cufts_get_results($query = NULL) {
drupal_add_css(drupal_get_path('module', 'roblib_search_cufts') . '/css/roblib_search_cufts.theme.css');
// Url parameters.
if (!isset($query)) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
return '';
}
}
//http://cufts2.lib.sfu.ca/CJDB/PCU/browse/show?browse_field=title&search_type=startswith&format=json&search_terms=dog&submit=Search
$url = variable_get('roblib_search_cufts_url', 'http://cufts2.lib.sfu.ca/CJDB/PCU/browse/show?');
$url_suffix = variable_get('roblib_search_cufts_search_suffix', 'browse_field=title&search_type=startswith&format=json&search_terms=');
$number_of_records = variable_get('roblib_search_cufts_num_results', '5');
$search_url = $url . $url_suffix . $query . '&submit=Search';
12 years ago
//get the total number of result
12 years ago
$results = drupal_http_request($search_url);
if ($results->code == '200') {
$output = $results->data;
}
else {
$output = $results->status_message;
}
$start_index = strpos($output,'"total_count":')+ strlen('"total_count":');
$end_index = strlen($output)-1;
$length=$end_index-$start_index;
$num_of_result = substr($output,$start_index,$length);
$search_url = $search_url . '&per_page='.$number_of_records;
$results = drupal_http_request($search_url);
if ($results->code == '200') {
$output = $results->data;
}
else {
$output = $results->status_message;
}
$output = str_replace('"total_count":', '"numOfResults":'.$num_of_result.',"total_count":',$output);
return $output;
12 years ago
}
/**
* Implements hook_help().
*
* @param type $path
* @param type $arg
* @return type
*/
function roblib_search_cufts_help($path, $arg) {
switch ($path) {
case 'admin/help#roblib_search_cufts':
return t(
'<p>
provides a target for the Roblib search module. This target uses javascript
to render the results.
12 years ago
</p>'
);
}
}