| 1 |
<?php
|
| 2 |
// $Id: faceted_search_views.module,v 1.21 2009/06/26 21:07:20 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows to use Views to display Faceted Search results.
|
| 7 |
*/
|
| 8 |
|
| 9 |
require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search_ui.inc');
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_faceted_search_ui_style_info().
|
| 13 |
*
|
| 14 |
* Return display styles based on views that use the Faceted Search argument.
|
| 15 |
*/
|
| 16 |
function faceted_search_views_faceted_search_ui_style_info() {
|
| 17 |
$styles = array();
|
| 18 |
foreach (views_get_all_views() as $view) {
|
| 19 |
if ($view->base_table == 'node' && empty($view->disabled) && $view->display['default']->display_options['use_pager']) {
|
| 20 |
$styles[$view->name] = new faceted_search_views_style($view->name);
|
| 21 |
}
|
| 22 |
}
|
| 23 |
return $styles;
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_views_query_alter().
|
| 28 |
*/
|
| 29 |
function faceted_search_views_views_query_alter (&$view, &$query) {
|
| 30 |
if (is_array($view->args[0]) && isset($view->args[0]['faceted_search'])) {
|
| 31 |
$search = $view->args[0]['faceted_search'];
|
| 32 |
|
| 33 |
if ($search->ready()) {
|
| 34 |
if ($search->get_results_count() > 0) {
|
| 35 |
$join = new views_join();
|
| 36 |
$join->construct($search->get_results_table(), 'node', 'nid', 'nid', array(), 'INNER');
|
| 37 |
$query->add_table($search->get_results_table(), NULL, $join);
|
| 38 |
}
|
| 39 |
else {
|
| 40 |
$query->add_where(0, 'FALSE'); // Ensure the view shows no results.
|
| 41 |
}
|
| 42 |
}
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Implementation of hook_views_pre_execute().
|
| 48 |
*/
|
| 49 |
function faceted_search_views_views_pre_execute(&$view) {
|
| 50 |
if (is_array($view->args[0]) && isset($view->args[0]['faceted_search']) && $view->current_display == 'default') {
|
| 51 |
if (empty($view->pager['items_per_page'])) {
|
| 52 |
// Make sure a limit is set.
|
| 53 |
$view->set_items_per_page(variable_get('default_nodes_main', 10));
|
| 54 |
}
|
| 55 |
if (empty($view->pager['use_pager'])) {
|
| 56 |
$view->set_use_pager(TRUE);
|
| 57 |
}
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Provides a view-based display style for search results.
|
| 63 |
*/
|
| 64 |
class faceted_search_views_style extends faceted_search_ui_style {
|
| 65 |
// Name of the view to use with this style.
|
| 66 |
var $_name;
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Constructor.
|
| 70 |
*
|
| 71 |
* @param $view_name
|
| 72 |
* Name of the view to use with this style.
|
| 73 |
*/
|
| 74 |
function faceted_search_views_style($view_name) {
|
| 75 |
$this->_name = $view_name;
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Return the name of this style.
|
| 80 |
*/
|
| 81 |
function get_label() {
|
| 82 |
return t('Views: @view', array('@view' => $this->_name));
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Apply the view's count query as a subquery to filter the search results.
|
| 87 |
*
|
| 88 |
* Note: We use the view's count query for this purpose because we don't need
|
| 89 |
* the sorting and field selection provided by view's full query. The view's
|
| 90 |
* only purpose here is to filter the search results.
|
| 91 |
*/
|
| 92 |
function query_alter(&$query, $search) {
|
| 93 |
$view = views_get_view($this->_name);
|
| 94 |
$view->set_display('default');
|
| 95 |
$view->set_arguments(array('faceted_search' => $search));
|
| 96 |
$view->build('default');
|
| 97 |
$views_query = db_rewrite_sql($view->build_info['count_query'], $view->base_table, $view->base_field, array('view' => &$view));
|
| 98 |
$views_args = $view->build_info['query_args'];
|
| 99 |
$views_replacements = module_invoke_all('views_query_substitutions', $view);
|
| 100 |
$views_query = str_replace(array_keys($views_replacements), $views_replacements, $views_query);
|
| 101 |
if (is_array($views_args)) {
|
| 102 |
foreach ($views_args as $id => $arg) {
|
| 103 |
$views_args[$id] = str_replace(array_keys($views_replacements), $views_replacements, $arg);
|
| 104 |
}
|
| 105 |
}
|
| 106 |
$query->add_subquery('n.nid IN ('. $views_query .')', $views_args);
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Format the search results according to this style's desire.
|
| 111 |
*/
|
| 112 |
function format_results($search) {
|
| 113 |
if ($view = views_get_view($this->_name)) {
|
| 114 |
if ($view->access('default')) {
|
| 115 |
$output = views_embed_view($this->_name, 'default' , array('faceted_search' => $search));
|
| 116 |
$view->destroy();
|
| 117 |
}
|
| 118 |
else {
|
| 119 |
$view->destroy();
|
| 120 |
drupal_access_denied();
|
| 121 |
exit();
|
| 122 |
}
|
| 123 |
}
|
| 124 |
return $output;
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|