* @package Drupal * @subpackage modules */ /** * Port to Drupal 6.x by Kenneth Lett */ // require the config require_once 'osu_search.config.inc'; // changed to new Drupal 6 menu API function osu_search_menu() { $items = array(); $items['search'] = array( 'title' => 'Search', 'description' => t('Search site content.'), 'page callback' => 'osu_search_main', 'access callback' => TRUE); $items['admin/settings/osu_search'] = array( 'title' => 'OSU Search', 'description' => t('Settings for the OSU Search module.'), 'page callback' => 'osu_search_settings', 'access callback' => 'user_access', 'access arguments' => array('administer site configuration')); return $items; } function osu_search_main() { $output = ''; $args = func_get_args(); list($term, $page_num, $per_page, $where) = $args; $term = urldecode(urldecode($term)); $output .= drupal_get_form('osu_search_form', $term, $where); // if there's a search term, do the thing if ($term) { global $osu_search_xml_source; global $osu_search_xsl_file; $page_num = (empty($page_num) || $page_num < 0) ? 1 : (int) $page_num; $per_page = (empty($per_page)) ? 10 : (int) $per_page; if (in_array($per_page, array(10, 20, 30, 50))) { $per_page = 10; } $start = $page_num * $per_page - $per_page; $base_web_path = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].base_path(); preg_match_all('(\w+)', $base_web_path, $matches); $matches = $matches[0]; foreach ($matches as $k => $v) { $matches[$k] = str_replace('/', '\/', '('.$v.')'); }; $query_params = '&hitsPerPage='.$per_page.'&hitsPerSite=0&start='.$start; $xml_data = file_get_contents($osu_search_xml_source.'?output=xml&query='.urlencode($term.' site:'.$base_web_path).$query_params); preg_match('/(\d+)<\/hits>/', $xml_data, $total); $total = (int) $total[1]; $max_pages = ceil($total / $per_page); if ($max_pages > 0 && $page_num > $max_pages) { drupal_goto('search/'.urlencode(urlencode($term)).'/'.$max_pages.'/'.$per_page.'/'.$where); exit; } $xml_data = preg_replace($matches, '', $xml_data, 1); $xsl_info = file_get_contents(dirname(__FILE__).'/'.$osu_search_xsl_file); $output .= osu_search_xml2html($xml_data, $xsl_info); // pagination!! if ($max_pages > 1) { $term = urlencode(urlencode($term)); $output .= '
'."\n" .(($page_num > 1) ? '' : '') .'← Previous' .(($page_num > 1) ? '' : '') .((($page_num - 5) > 1) ? ''."\n" : ''); $begin_page_num = $page_num - 5; $begin_page_num = ($begin_page_num < 1) ? 1 : $begin_page_num; $end_page_num = $page_num + 5; $pages_count = $end_page_num - $begin_page_num; $end_page_num = ($pages_count < 11) ? ($begin_page_num + 10) : $end_page_num; if ($end_page_num > $max_pages) { $end_page_num = $max_pages; $begin_page_num = $max_pages - 10; $begin_page_num = ($begin_page_num < 1) ? 1 : $begin_page_num; } for ($i = $begin_page_num; $i <= $end_page_num; $i++) { $page_url = (url('search/'.$term.'/'.($i).'/'.$per_page.'/'.$where)); $output .= ''.$i.''."\n"; } $output .= ((($page_num + 5) < $max_pages) ? ''."\n" : '') .(($page_num < $max_pages) ? '' : '') .'Next →' .(($page_num < $max_pages) ? '' : '') .'
'."\n"; } } return $output; } function osu_search_form(&$form_state, $term, $where=1) { $form = array(); drupal_add_css('sites/all/modules/osu_search/osu_search.css'); drupal_add_js('misc/jquery.js'); drupal_add_js('sites/all/modules/osu_search/osu_search.js'); $displayTerm = $term; if(strpos($displayTerm,'site:')){ $displayTerm = substr($displayTerm,0,strpos($displayTerm,'site:')); } $form['#action'] = url('search'); $form['term'] = array( '#type' => 'textfield', '#default_value' => $displayTerm ); $choices = array('1' => t('This Site')); if (($osu_search_other_url = variable_get('osu_search_other_url', '')) != '') { $osu_search_other_title = variable_get('osu_search_other_title', ''); $choices['2'] = (empty($osu_search_other_title)) ? $osu_search_other_url : $osu_search_other_title; } $choices['3'] = 'OSU'; $form['where'] = array( '#type' => 'radios', '#default_value' => $where, '#options' => $choices ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Search' ); return $form; } // new form api, now uses $form_state function osu_search_form_submit($form, &$form_state) { global $osu_search_url; switch ($form_state['values']['where']) { case '3': drupal_goto($osu_search_url, 'query='.urlencode($form_state['values']['term'])); exit; case '2': if (($osu_search_other_url = variable_get('osu_search_other_url', '')) != '') { if(variable_get('osu_search_other_results_page', 1) == 1){ drupal_goto('search/'.urlencode(urlencode($form_state['values']['term'] . ' site:'.$osu_search_other_url)).'/1/10/2');//stay within current site } else { drupal_goto($osu_search_url, 'query='.urlencode($form_state['values']['term'].' site:'.$osu_search_other_url));//go to university search site } exit; } case '1': default: // I'm encoding twice because Drupal is so damn nice that // it would automatically decode first and then explode on '/'. // In other words, I need it if someone searches for 'foobar/1/10'... drupal_goto('search/'.urlencode(urlencode($form_state['values']['term'])).'/1/10/1'); exit; } } // new form api, now uses $form_state function osu_search_form_validate($form, &$form_state) { if (trim($form_state['values']['term']) === '') { form_set_error('term', t('Please enter what you are looking for…')); } } function osu_search_settings() { $output = '

Set up an extra site as an option on the search form.

' .drupal_get_form('osu_search_settings_form'); return $output; } // new form api, now uses $form_state function osu_search_settings_form(&$form_state) { $form = array(); $form['osu_search_other_title'] = array( '#type' => 'textfield', '#title' => t('Site Title'), '#default_value' => variable_get('osu_search_other_title', '') ); $form['osu_search_other_url'] = array( '#type' => 'textfield', '#title' => t('Site URL'), '#default_value' => variable_get('osu_search_other_url', '') ); $form['osu_search_other_results_page'] = array( '#type' => 'checkbox', '#title' => t('Show search results on this site (if left unchecked results will be shown on the OSU search page).'), '#default_value' => variable_get('osu_search_other_results_page', 1) ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Save' ); return $form; } function osu_search_settings_form_submit($form, &$form_state) { variable_set('osu_search_other_title', $form_state['values']['osu_search_other_title']); variable_set('osu_search_other_url', $form_state['values']['osu_search_other_url']); variable_set('osu_search_other_results_page', $form_state['values']['osu_search_other_results_page']);//determines whether to display search results on current site or university search } /** * Converts a pure xml/xslt file to plain html (so its compatible with older browsers) * Stolen from OSU Search. * PHP5 ONLY! */ function osu_search_xml2html($xmldata, $xsldata) { $xml = @DOMDocument::loadXML($xmldata); $xsl = @DOMDocument::loadXML($xsldata); if (!$xml || !$xsl) { return t('The search engine is having a bit of trouble right now.'); } $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); $doc = $proc->transformToDoc($xml); if ($doc === false) { return t('Error processing search results'); } return $doc->saveHTML(); } function osu_search_get_template_form(&$vars) { $search_form = drupal_get_form('osu_search_template_form'); $vars['osu_search_form'] = $search_form; $image_submit_search_form = drupal_get_form('osu_search_image_submit_template_form'); $vars['osu_search_image_submit_form'] = $image_submit_search_form; } function osu_search_template_form(&$form_state) { $form = array(); $form['#action'] = url('search'); $form['search-query'] = array( '#id' => 'osu-search-query', '#type' => 'textfield', '#size' => '' ); $form['search-scope'] = array( '#id' => 'osu-search-scope', '#type' => 'checkbox', '#title' => 'Search all of OSU.', ); $form['submit'] = array( '#id' => 'osu-search-submit', '#type' => 'submit', '#value' => 'Search' ); return $form; } function osu_search_image_submit_template_form(&$form_state) { $transparent_image = path_to_theme().'/images/blank.gif'; //must be set or IE shows broken image icon $form = array(); $form['#action'] = url('search'); $form['search-query'] = array( '#id' => 'osu-search-query', '#type' => 'textfield', '#size' => '' ); $form['search-scope'] = array( '#id' => 'osu-search-scope', '#type' => 'checkbox', '#title' => 'Search all of OSU.', ); $form['submit'] = array( '#id' => 'osu-search-submit', '#type' => 'image_button', '#src' => $transparent_image, '#value' => ' ' ); return $form; } // new form api, now uses $form_state function osu_search_template_form_submit($form, &$form_state) { $search_query = trim($form_state['values']['search-query']); if (empty($search_query)) { return false; } $form_state['values']['term'] = $form_state['values']['search-query']; if ($form_state['values']['search-scope'] == '1') { $form_state['values']['where'] = '3'; } else { $form_state['values']['where'] = '1'; } osu_search_form_submit('osu_search_form', &$form_state); } /** * Implements hook_nodeapi(). * * @access public * @param $node "The node the action is being performed on." * @param $op The kind of action. * @see http://api.drupal.org/api/5/function/hook_nodeapi * @return void */ function osu_search_nodeapi(&$node, $op, $a3, $a4) { if ($op == 'validate') { $path = strtolower(trim($node->path)); if ($path === 'search') { form_set_error( 'path', t('You cannot use the URL path \'search\'. It has been reserved for the OSU Search module.')); } } }