<?php

// TODO: replace this controller with page one
class vB5_Frontend_Controller_Search extends vB5_Frontend_Controller
{

	function __construct()
	{
		parent::__construct();
	}

	function actionIndex()
	{
		$api = Api_InterfaceAbstract::instance();
		$router = vB5_ApplicationAbstract::instance()->getRouter();
		$arguments = $router->getArguments();
		$userAction = $router->getUserAction();

		if (!empty($userAction))
		{
			$api->callApi('wol', 'register', array($userAction['action'], $userAction['params']));
		}

		$pageid = (int) (isset($arguments['pageid']) ? $arguments['pageid'] : $arguments['contentid']);

		$page = $api->callApi('page', 'fetchPageById', array($pageid, $arguments));
		if (!$page)
		{
			// @todo This needs to output a user-friendly "page not found" page
			throw new Exception('Could not find page.');
		}
		$serverData = array_merge($_GET, $_POST);
		$page['title'] = 'Advanced Search';
		$page['url'] = vB5_Template_Runtime::buildUrl('advanced_search');
		$page['crumbs'] = $router->getBreadcrumbs();

		if(!empty($serverData['cookie']))
		{
			$page['searchJSON'] = '{"specific":['.$_COOKIE[$serverData['cookie']].']}';
		}

		if(!empty($serverData['searchJSON']))
		{
			$decoded = json_decode($serverData['searchJSON'],true);
			if (!empty($decoded))
			{
				$page['searchJSON'] = json_encode($decoded);
			}
			//$page['searchJSONStructure'] = json_decode($page['searchJSON'],true);
		}
		elseif (!empty($serverData['r']))
		{
			$page['resultId'] = $serverData['r'];
			if(!empty($serverData['p']) && is_numeric($serverData['p'])){
				$page['currentPage'] = intval($serverData['p']);
			}
		}
		elseif (!empty($serverData['e']))
		{
			$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
			if (strlen($path) AND $path{0} == '/')
			{
				$path = substr($path, 1);
			}
			$route = $api->callApi('route', 'getRoute', array('pathInfo' => 'advanced_search', 'queryString' => $_SERVER['QUERY_STRING']));
			$page = $api->callApi('page', 'fetchPageById', array($route['arguments']['pageid']));
//			$info = $api->callApi('search', 'getInfo', array($serverData['e']));
			$page['resultId'] = $serverData['e'];
// 			$page['searchJSON'] = $info['searchJSON'];
// 			$page['searchJSONStructure'] = $info['searchJSONStructure'];
// 			$page['error'] = $info['searchJSONStructure']['error'];
		}
		$page['ignore_np_notices'] = isset($_COOKIE[vB5_Config::instance()->cookie_prefix . 'np_notices_displayed']) ? explode(',', $_COOKIE[vB5_Config::instance()->cookie_prefix . 'np_notices_displayed']) : array();
		$templater = new vB5_Template($page['screenlayouttemplate']);
		$templater->register('page', $page);
		echo $templater->render();
	}


	function index()
	{
		return $this->actionIndex();
	}

	function actionResult()
	{
		$serverData = array_merge($_GET, $_POST);
		$api = Api_InterfaceAbstract::instance();
		$router = vB5_ApplicationAbstract::instance()->getRouter();
		$arguments = $router->getArguments();
		$userAction = $router->getUserAction();

		if (!empty($userAction))
		{
			$api->callApi('wol', 'register', array($userAction['action'], $userAction['params']));
		}

		// if Human verification is required, and we don't have 'q' set in serverData (means the user is using
		//   the quick search box), we redirect user to advanced search page with HV
		$requirehv = $api->callApi('hv', 'fetchRequireHvcheck', array('search'));
		if (!empty($serverData['AdvSearch']) OR ($requirehv AND isset($serverData['q'])))
		{
			$adv_search = $api->callApi('route', 'getRoute', array('pathInfo' => 'advanced_search', 'queryString' =>''), true);
			$arguments = $adv_search['arguments'];
		}
		elseif ($requirehv)
		{
			// Advanced search form submitted
			if (empty($serverData['humanverify']))
			{
				$serverData['humanverify'] = array();
			}
			$return = $api->callApi('hv', 'verifyToken', array($serverData['humanverify'], 'search'));

			if ($return !== true)
			{
				$adv_search = $api->callApi('route', 'getRoute', array('pathInfo' => 'advanced_search', 'queryString' =>''), true);
				$arguments = $adv_search['arguments'];
				$error = $return['errors'][0][0];
			}
		}

		$pageid = (int) (isset($arguments['pageid']) ? $arguments['pageid'] : $arguments['contentid']);

		$page = $api->callApi('page', 'fetchPageById', array($pageid, $arguments));
		if (!$page)
		{
			echo 'Could not find page.';
			exit;
		}
		$page['crumbs'] = array(
			0 => array(
				'title' => 'Advanced Search',
				'url' => vB5_Template_Runtime::buildUrl('advanced_search', array(), array(), array('noBaseUrl' => true))
			),
			1 => array(
				'title' => 'Search Results',
				'url' => ''
			)
		);
		if(!empty($serverData['searchJSON']))
		{
			if (is_string($serverData['searchJSON']))
			{
				$serverData['searchJSON'] = json_decode($serverData['searchJSON'], true);
			}
			if (!empty($serverData['searchJSON']))
			{
				if (!empty($serverData['searchJSON']['keywords']))
				{
					$serverData['searchJSON']['keywords'] = str_replace(array('"', '\\'), '', $serverData['searchJSON']['keywords']);
					$serverData['searchJSON']['keywords'] = filter_var($serverData['searchJSON']['keywords'], FILTER_SANITIZE_STRING);
				}
				$serverData['searchJSON'] = json_encode($serverData['searchJSON']);
			}
			else
			{
				$serverData['searchJSON'] = '';
			}
			$page['searchJSON'] = $serverData['searchJSON'];
			$page['url'] = vB5_Template_Runtime::buildUrl('search', array(),array('searchJSON' => $serverData['searchJSON']));
			//$page['searchJSONStructure'] = json_decode($page['searchJSON'],true);
			$page['crumbs'][0]['url'] = vB5_Template_Runtime::buildUrl('advanced_search', array(),array('searchJSON' => $page['searchJSON']), array('noBaseUrl' => true));
		}
		elseif (!empty($serverData['q']))
		{
			$serverData['q'] = str_replace(array('"', '\\'), '', $serverData['q']);
			$serverData['q'] = filter_var($serverData['q'], FILTER_SANITIZE_STRING);
			$page['searchJSON'] = '{"keywords":"' . $serverData['q'] . '","sort":"title"}';
			$page['url'] = vB5_Template_Runtime::buildUrl('search', array(),array('q' => $serverData['q']));
			//$page['searchJSONStructure'] = json_decode($page['searchJSON'],true);
			$page['searchStr'] = $serverData['q'];
			$page['crumbs'][0]['url'] = vB5_Template_Runtime::buildUrl('advanced_search', array(''),array('searchJSON' => $page['searchJSON']), array('noBaseUrl' => true));
		}
		elseif (!empty($serverData['r']))
		{
			$page['crumbs'][0]['title'] = $page['title'] = 'Search results';
			$page['url'] = vB5_Template_Runtime::buildUrl('search', array(),array('r' => $serverData['r']));
			$page['resultId'] = $serverData['r'];
			if(!empty($serverData['p']) && is_numeric($serverData['p'])){
				$page['currentPage'] = intval($serverData['p']);
			}
			$page['crumbs'][0]['url'] = vB5_Template_Runtime::buildUrl('advanced_search', array(),array('r' => $serverData['r']), array('noBaseUrl' => true));
		}
		else
		{
			return $this->index();
		}
		$page['ignore_np_notices'] = isset($_COOKIE[vB5_Config::instance()->cookie_prefix . 'np_notices_displayed']) ? explode(',', $_COOKIE[vB5_Config::instance()->cookie_prefix . 'np_notices_displayed']) : array();
		$page['charset'] = vB5_String::getTempCharset();
		if (!empty($error))
		{
			$page['error'] = $error;
		}
		$templater = new vB5_Template($page['screenlayouttemplate']);
		$templater->register('page', $page);
		echo $templater->render();
	}

	function results()
	{
		return $this->actionResult();
	}


	public function actionFetchTagCloud()
	{
		$taglevels = 5;
		$limit = 20;
		$type = 'search';
		$serverData = array_merge($_GET, $_POST);
		$type = empty($serverData['type']) ? 'search' : $serverData['type'];
		$taglevels = empty($serverData['taglevels']) ? 5 : $serverData['taglevels'];
		$limit = empty($serverData['limit']) ? 20 : $serverData['limit'];

		$tags = vB_Api::instanceInternal('Tags')->fetchTagsForCloud($taglevels, $limit, $type);
		$templater = new vB5_Template('tag_cloud');
		$templater->register('tags', $tags);
		$templater->register('noformat', $serverData['noformat']);
		$this->sendAsJson($templater->render());
	}
/*	protected $template;

	public function __construct($template)
	{
		parent::__construct();

		$this->template = $template;
	}

	public function actionIndex()
	{

		$serverData = array_merge($_GET, $_POST);
		$searchAPI = vB_Api_Search::instanceInternal();
		$searchJSON = '{"keywords":"' . $serverData['q'] . '"}';
		$nodes = $searchAPI->getInitialResults($searchJSON, 1, 100);
		$page = array(
			'title' => 'Search results for '.$serverData['q'],
			'metakeywords' => '',
			'metadescription' => '',
			'screenlayouttemplate' => 'screenlayout_1',
			'pageid' => 0,
			'pagetemplateid' => 2, // hard-coded for now. @todo fix this
			'nodes' => $nodes
			//'channelid' => false,
			//'parentchannelid' => false,
		);

		$templater = new vB5_Template($page['screenlayouttemplate']);
		$templater->register('page', $page);
		echo $templater->render();

	}*/

}