<?php
/**
 * @file
 * Allows to use Views to display Faceted Search results.
 */

	require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search_ui.inc');

	function faceted_search_views_views_api()
	{  
		return array(
    		'api' => 2,
    		'path' => drupal_get_path('module', 'faceted_search_views'),
  		);
	}
	
	/**
	 * Implementation of hook_faceted_search_ui_style_info().
	 *
	 * Return display styles based on views that use the Faceted Search argument.
	 */
	function faceted_search_views_faceted_search_ui_style_info() 
	{
		$styles = array();
		$results = db_query("SELECT name FROM {views_view} ORDER BY name");
		while ($view = db_fetch_object($results)) 
		{
			$styles[$view->name] = new faceted_search_views_style($view->name);
		}
		return $styles;
	}
	
	function faceted_search_views_views_query_alter (&$view, &$query)
	{   
		if( isset($view->args[0]['id']) && $view->args[0]['id'] == 'facet' )
		{    
	        $search = $view->args[0]['search'];
	        
	        $join = new views_join();
	        $join->construct( $search->get_results_table(), 'node', 'nid', 'nid', array(), 'INNER' );
	        
	        if( $search->ready() )
	        {
	        	if ($search->get_results_count() > 0)
	        	{
	        		$query->add_table($search->get_results_table(), NULL, $join);        		
	        	}
	        	else 
	        	{
	        		$query->add_where(0,'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->display['default']->display_options['items_per_page'] > 0) {
      return $view->display['default']->display_options['items_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.
   */
  
  //@todo implement query_alter in the faceted_search
/*
  function query_alter(&$query, $search) {
    $view = $this->get_view();
    
    $queries = views_build_view('queries', $view, array($search->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) {  	
    return views_embed_view( $this->_name, 'default' , array( 'search' => $search, 'id' => 'facet' ));
  }
}

