| 1 |
<?php
|
| 2 |
// $Id: drupalorg_search.module,v 1.8 2009/04/10 19:29:49 sagannotcarl Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Drupal.org search customizations.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* The TID of the "Modules" term.
|
| 11 |
*/
|
| 12 |
define('DRUPALORG_MODULE_TID', 14);
|
| 13 |
|
| 14 |
/**
|
| 15 |
* The TID of the "Themes" term.
|
| 16 |
*/
|
| 17 |
define('DRUPALORG_THEME_TID', 15);
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Return the Mark Boulton "filter by..." facet (TM) of a given node.
|
| 21 |
*/
|
| 22 |
function drupalorg_node_meta_type($node) {
|
| 23 |
if ($node->type == 'project_project') {
|
| 24 |
if (isset($node->taxonomy[DRUPALORG_MODULE_TID])) {
|
| 25 |
return 'module';
|
| 26 |
}
|
| 27 |
else if (isset($node->taxonomy[DRUPALORG_THEME_TID])) {
|
| 28 |
return 'theme';
|
| 29 |
}
|
| 30 |
}
|
| 31 |
else if ($node->type == 'book') {
|
| 32 |
return 'documentation';
|
| 33 |
}
|
| 34 |
else if ($node->type == 'project_issue' || $node->type == 'forum') {
|
| 35 |
return 'forum-issues';
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Define Mark Boulton's "filter by..." entries.
|
| 41 |
*/
|
| 42 |
function drupalorg_meta_types() {
|
| 43 |
return array(
|
| 44 |
'module_theme' => t('Modules and Themes'),
|
| 45 |
'documentation' => t('Documentation'),
|
| 46 |
'api' => t('API'),
|
| 47 |
'forum-issues' => t('Forum & Issues'),
|
| 48 |
);
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_apachesolr_update_index().
|
| 53 |
*
|
| 54 |
* This adds our specific facets to the Apachesolr search index.
|
| 55 |
*/
|
| 56 |
function drupalorg_search_apachesolr_update_index(&$document, $node) {
|
| 57 |
if ($type = drupalorg_node_meta_type($node)) {
|
| 58 |
$document->ss_meta_type = $type;
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Implementation of hook_apachesolr_facets().
|
| 64 |
*
|
| 65 |
* Returns an array keyed by block delta.
|
| 66 |
*/
|
| 67 |
function drupalorg_search_apachesolr_facets() {
|
| 68 |
$facets = array();
|
| 69 |
|
| 70 |
$facets['meta_type'] = array(
|
| 71 |
'info' => t('Drupalorg Search: Filter by meta type'),
|
| 72 |
'facet_field' => '{!ex=meta_type}ss_meta_type',
|
| 73 |
'exclude_filter' => '',
|
| 74 |
);
|
| 75 |
|
| 76 |
return $facets;
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Implementation of hook_apachesolr_modify_query().
|
| 81 |
*
|
| 82 |
* Exclude the meta_type filter from faceting.
|
| 83 |
*/
|
| 84 |
function drupalorg_search_apachesolr_modify_query(&$query, &$params) {
|
| 85 |
// TODO: some pending improvements to Apachesolr may simplify this.
|
| 86 |
foreach ($query->get_fields() as $field) {
|
| 87 |
if ($field['#name'] == 'ss_meta_type') {
|
| 88 |
$query->remove_field($field['#name'], $field['#value']);
|
| 89 |
$query->add_field('{!tag=meta_type}' . $field['#name'], $field['#value']);
|
| 90 |
}
|
| 91 |
}
|
| 92 |
foreach ($params['facet.field'] as &$field) {
|
| 93 |
if ($field == 'ss_meta_type') {
|
| 94 |
$field = '{!ex=meta_type}' . $field;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Implementation of hook_block().
|
| 101 |
*/
|
| 102 |
function drupalorg_search_block($op = 'list', $delta = 0, $edit = array()) {
|
| 103 |
if ($op == 'list') {
|
| 104 |
$blocks = array();
|
| 105 |
|
| 106 |
$blocks['search_box'] = array(
|
| 107 |
'info' => t('Drupalorg Search: Search box'),
|
| 108 |
'cache' => BLOCK_CACHE_PER_PAGE,
|
| 109 |
);
|
| 110 |
|
| 111 |
// Add the facet blocks.
|
| 112 |
$enabled_facets = apachesolr_get_enabled_facets('drupalorg_search');
|
| 113 |
$facets = drupalorg_search_apachesolr_facets();
|
| 114 |
foreach ($enabled_facets as $delta => $facet_field) {
|
| 115 |
$blocks[$delta] = $facets[$delta] + array('cache' => BLOCK_CACHE_PER_PAGE);
|
| 116 |
}
|
| 117 |
return $blocks;
|
| 118 |
}
|
| 119 |
else if ($op == 'view' && $delta == 'search_box') {
|
| 120 |
if (apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) {
|
| 121 |
$querystring = $query->get_query_basic();
|
| 122 |
}
|
| 123 |
else {
|
| 124 |
$querystring = '';
|
| 125 |
}
|
| 126 |
|
| 127 |
// We need to special case POST requests, because the search module doesn't
|
| 128 |
// trigger search on POST requests.
|
| 129 |
if (!empty($querystring) || (arg(0) == 'search' && $_SERVER['REQUEST_METHOD'] == 'POST')) {
|
| 130 |
return array(
|
| 131 |
'subject' => t('Search again'),
|
| 132 |
'content' => drupal_get_form('drupalorg_search_block_form', $querystring),
|
| 133 |
);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
else if ($op == 'view' && $delta == 'meta_type') {
|
| 137 |
if (apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) {
|
| 138 |
$entries = array();
|
| 139 |
$total = 0;
|
| 140 |
$has_active = FALSE;
|
| 141 |
foreach (drupalorg_meta_types() as $name => $description) {
|
| 142 |
if (!empty($response->facet_counts->facet_fields->ss_meta_type->$name)) {
|
| 143 |
$count = $response->facet_counts->facet_fields->ss_meta_type->$name;
|
| 144 |
$total += $count;
|
| 145 |
|
| 146 |
$new_query = clone $query;
|
| 147 |
$active = $query->has_field('ss_meta_type', $name);
|
| 148 |
$has_active |= $active;
|
| 149 |
$new_query->remove_field('{!tag=meta_type}ss_meta_type');
|
| 150 |
$new_query->add_field('ss_meta_type', $name);
|
| 151 |
|
| 152 |
$path = 'search/' . arg(1) . '/' . $new_query->get_query_basic();
|
| 153 |
$querystring = $new_query->get_url_querystring();
|
| 154 |
|
| 155 |
$entries[] = l(t('@facet (@count)', array('@facet' => $description, '@count' => $count)), $path, array('query' => $querystring, 'html' => TRUE, 'attributes' => $active ? array('class' => 'selected') : array()));
|
| 156 |
}
|
| 157 |
}
|
| 158 |
if (count($entries)) {
|
| 159 |
// Add the "All" entry.
|
| 160 |
$new_query = clone $query;
|
| 161 |
$new_query->remove_field('{!tag=meta_type}ss_meta_type');
|
| 162 |
$path = 'search/' . arg(1) . '/' . $new_query->get_query_basic();
|
| 163 |
$querystring = $new_query->get_url_querystring();
|
| 164 |
array_unshift($entries, l(t('All (@count)', array('@count' => $total)), $path, array('query' => $querystring, 'html' => TRUE, 'attributes' => !$has_active ? array('class' => 'selected') : array())));
|
| 165 |
|
| 166 |
return array(
|
| 167 |
'subject' => t('or filter by...'),
|
| 168 |
'content' => theme('item_list', $entries),
|
| 169 |
);
|
| 170 |
}
|
| 171 |
}
|
| 172 |
}
|
| 173 |
}
|
| 174 |
|
| 175 |
/**
|
| 176 |
* Implementation of hook_form_search_form_alter().
|
| 177 |
*/
|
| 178 |
function drupalorg_search_form_search_form_alter(&$form, $form_state) {
|
| 179 |
if (apachesolr_has_searched() && ($response = apachesolr_static_response_cache()) && ($query = apachesolr_current_query())) {
|
| 180 |
$form['#access'] = FALSE;
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Implementation of hook_form_search_theme_form_alter().
|
| 186 |
*/
|
| 187 |
function drupalorg_search_form_search_theme_form_alter(&$form, $form_state) {
|
| 188 |
$form['search_theme_form']['#title'] = t('Search Drupal.org');
|
| 189 |
$form['advanced'] = array(
|
| 190 |
'#type' => 'fieldset',
|
| 191 |
'#title' => t('Refine your search'),
|
| 192 |
'#collapsible' => !drupal_is_front_page(),
|
| 193 |
'#collapsed' => !drupal_is_front_page(),
|
| 194 |
);
|
| 195 |
$form['advanced']['meta_type'] = array(
|
| 196 |
'#type' => 'radios',
|
| 197 |
'#options' => drupalorg_meta_types(),
|
| 198 |
);
|
| 199 |
$form['#submit'][] = 'drupalorg_search_search_theme_form_submit';
|
| 200 |
}
|
| 201 |
|
| 202 |
function drupalorg_search_search_theme_form_submit($form, &$form_state) {
|
| 203 |
if (!is_array($form_state['redirect'])) {
|
| 204 |
$form_state['redirect'] = array($form_state['redirect']);
|
| 205 |
}
|
| 206 |
if (!empty($form_state['values']['meta_type'])) {
|
| 207 |
$form_state['redirect'][1]['ss_meta_type'] = $form_state['values']['meta_type'];
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Form callback; display a search form in a block.
|
| 213 |
*/
|
| 214 |
function drupalorg_search_block_form(&$form_state, $querystring) {
|
| 215 |
$form['search-wrapper'] = array(
|
| 216 |
'#prefix' => '<div class="container-inline">',
|
| 217 |
'#suffix' => '</div>',
|
| 218 |
);
|
| 219 |
$form['search-wrapper']['query'] = array(
|
| 220 |
'#type' => 'textfield',
|
| 221 |
'#default_value' => $querystring,
|
| 222 |
'#size' => 10,
|
| 223 |
);
|
| 224 |
$form['search-wrapper']['submit'] = array(
|
| 225 |
'#type' => 'submit',
|
| 226 |
'#value' => t('Submit'),
|
| 227 |
);
|
| 228 |
return $form;
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Submit callback; redirect the user to the search result page.
|
| 233 |
*/
|
| 234 |
function drupalorg_search_block_form_submit($form, &$form_state) {
|
| 235 |
$query = $_GET;
|
| 236 |
unset($query['q']);
|
| 237 |
$form_state['redirect'] = array('search/' . arg(1) . '/' . $form_state['values']['query'], $query);
|
| 238 |
}
|