<?php if (!defined('VB_ENTRY')) die('Access denied.');

require_once DIR . '/vb/search/searchcontroller.php' ;

class vBCms_Search_SearchController_NewContentNode extends vB_Search_SearchController
{
	const MAX_DAYS = 2;
	/*****
	* This function returns the results set. Note that the user
	*  parameter is used for consistency, but cms content is visible to all.
	* So we don't have
	****/
	private $contenttypeid = false;


	public function get_results($user, $criteria)
	{
		global $vbulletin;
		$db = $vbulletin->db;

		$range_filters = $criteria->get_range_filters();
		$equals_filters = $criteria->get_equals_filters();
		$sort = $criteria->get_sort();
		$direction = strtolower($criteria->get_sort_direction()) == 'desc' ? 'desc' : 'asc';

		$sort_join = "";
		$orderby = "";
		$where = array();

		if ($sort = 'dateline')
		{
			$orderby = 'node.publishdate ' . $direction;
		}
		else if ($sort = 'user')
		{
			$sort_join = "JOIN " . TABLE_PREFIX . "user AS user ON node.userid = user.userid";
			$orderby = "user.username " . $direction . ", node.publishdate DESC";
		}
		else
		{
			$orderby = " node.publishdate DESC";
		}

		$results = array();

		$where[] = " node.publishdate <= " . TIMENOW;
		//get date cut -- but only if we're not using the threadmarking filter
		if (isset($range_filters['datecut']))
		{
			//ignore any upper limit
			$where[] = " node.publishdate >= " . $range_filters['datecut'][0];
		}
		else if (isset($range_filters['dateline']))
		{
			$where[] = " node.publishdate >= " . $range_filters['dateline'][0];
		}
		else if (isset($range_filters['days']))
		{
			$where[] = " node.publishdate >= " . $range_filters['days'][0];
		}
		else
		{
			$where[] = " node.publishdate >= " . TIMENOW - 86400 *
				($vbulletin->GPC_exists['days'] ? $vbulletin->GPC['days'] : self::MAX_DAYS);
		}

		if (isset($equals_filters['userid']))
		{
			$where[] = " node.userid " .
				(is_array($equals_filters['userid'][vB_Search_Core::OP_EQ]) ?
					"in (" . implode(', ', $equals_filters['userid'][vB_Search_Core::OP_EQ])
						. ") " :
					" = " . $equals_filters['userid'][vB_Search_Core::OP_EQ]
				);
		}
		else if ($vbulletin->GPC_exists['userid'])
		{
			$where[] = " node.userid = " . $vbulletin->GPC['userid'];
		}

		if ($keywords = $criteria->get_keywords())
		{
			$searchcore_join = " INNER JOIN " . TABLE_PREFIX . "searchcore AS searchcore
				ON searchcore.primaryid = node.contentid
				AND searchcore.contenttypeid = node.contenttypeid";
			$where[] = " MATCH  (title, keywordtext) against "
				 . $db->escape_string($keywords) . " IN BOOLEAN MODE ";
		}

		if ($this->contenttypeid)
		{
			$where[] = " node.contenttypeid = " . $this->contenttypeid;
		}
		else if (($contenttypeid = $criteria->get_contenttypeid()) and
			($contenttypeid != vB_Types::instance()->getContentTypeID('vBCms_ContentNode')) )
		{
			$where[] = " node.contenttypeid = " . $contenttypeid;
		}

		$entries = $db->query_read_slave($q = "
			SELECT node.nodeid, node.contenttypeid, node.contentid
			FROM " . TABLE_PREFIX . "cms_node as node
			$searchcore_join
			$sort_join
			WHERE
				" . implode(' AND ', $where) . "
			ORDER BY $orderby
			LIMIT " . intval($vbulletin->options['maxresults'])
		);

		while ($entry = $db->fetch_array($entries))
		{
			$results[] = array($entry['contenttypeid'], $entry['contentid'], $entry['nodeid']);
		}

		return $results;
	}
}
/*======================================================================*\
|| ####################################################################

|| # SVN: $Revision: 31978 $
|| ####################################################################
\*======================================================================*/