name] = new faceted_search_views_style($view->name); } // Load default views. views_load_cache(); $default_views = _views_get_default_views(); foreach ($default_views as $view) { foreach ($view->argument as $argument) { if ($argument['type'] == 'faceted_search_results') { $styles[$view->name] = new faceted_search_views_style($view->name); } } } return $styles; } /** * Implementation of hook_views_arguments(). */ function faceted_search_views_views_arguments() { $arguments['faceted_search_results'] = array( 'name' => t('Faceted Search: Environment ID'), 'help' => t("Filter by the current results of a Faceted Search environment. The option field allows you to sort the results by relevance score. If a sort option is selected, it is applied before all other sort criteria of the view. The higher the score, the more relevant the item."), 'handler' => 'faceted_search_views_results_argument_handler', 'option' => array( '#type' => 'select', '#options' => array( 'none' => t('No sort'), 'ASC' => t('Sort by score - Ascending'), 'DESC' => t('Sort by score - Descending'), ), '#default_value' => 0, ), // Note: Sort options are provided here, because a sort handler would not // have access to the environment id argument, and thus would not know the // name of the results table to get its score field (unless ugly hacks were // done). ); return $arguments; } /** * An argument handler that updates the specified query to filter with a Faceted * Search environment's current results. */ function faceted_search_views_results_argument_handler($op, &$query, $a1, $a2 = NULL) { global $_faceted_search; if ($op == 'filter' && isset($_faceted_search[$a2])) { if ($_faceted_search[$a2]->ready()) { // Thanks to the above condition, this handler has no effect when there is // no current search results. This avoids a race condition between Views // and Faceted Search; Faceted Search may use this query in a subquery to // build its results, and Views may call this handler afterwards to filter // by Faceted Search's results. if ($_faceted_search[$a2]->get_results_count() > 0) { // Join results table. $join = array( 'type' => 'inner', 'left' => array('table' => 'node', 'field' => 'nid'), 'right' => array('field' => 'nid'), ); $query->add_table($_faceted_search[$a2]->get_results_table(), FALSE, 1, $join); // Apply sort option. if ($a1['options'] != 'none') { $query->add_orderby($_faceted_search[$a2]->get_results_table(), 'score', $a1['options']); } } else { $query->add_where('FALSE'); // Ensure the view shows no results. } } } } /** * Provides a view-based display style for search results. */ class faceted_search_views_style extends faceted_search_ui_style { // Name of the view to use with this style. var $_name; // View to use with this style (lazy loaded). var $_view; /** * Constructor. * * @param $view_name * Name of the view to use with this style. */ function faceted_search_views_style($view_name) { $this->_name = $view_name; } /** * Return the view to use with this style. */ function get_view() { if (!isset($this->_view)) { $this->_view = views_get_view($this->_name); } return $this->_view; } /** * Return the name of this style. */ function get_label() { $view = $this->get_view(); return t('Views: @view', array('@view' => $view->name)); } /** * Return the number of nodes per page. */ function get_limit() { $view = $this->get_view(); if ($view->nodes_per_page > 0) { return $view->nodes_per_page; } // The pager is mandatory for search results, thus a valid limit required. return variable_get('default_nodes_main', 10); } /** * Apply the view's query as a subquery to filter the search results. * * Note: We use the view's count query for this purpose because the sorting * and field selection provided by its main query is not wanted here. The * view's only purpose here is to filter the search results. */ function query_alter(&$query, $search) { $view = $this->get_view(); $queries = views_build_view('queries', $view, array($search->get_env_id())); // Make sure the view's query selects node.id rather than a count. This // assumes that the view is counting node.id. $views_query = preg_replace('!count\(((DISTINCT\()?node.nid\)?)\)!', '$1', $queries['countquery']); $views_query = db_rewrite_sql($views_query, 'node'); $query->add_subquery('n.nid IN ('. $views_query .')'); } /** * Format the search results according to this style's desire. */ function format_results($search) { $view = $this->get_view(); return views_build_view('embed', $view, array($search->get_env_id()), TRUE, $this->get_limit()); } }