<?php 
class Search_Controller extends Front_Controller {
	function __construct(){
		parent::__construct();
		$this->html		=	new Html_Model();
		$this->searchModel	=	new Search_Model();

	}
	
	function index($keywords=false,$page=1){
		if($this->input->post()&&!$keywords){
			$condition				=	$this->input->post("search");
		}
		else{
			$condition				=	array("title"=>rawurldecode($keywords));		

		}
		//过滤搜索条件为空的字符串
		foreach ($condition as $name=>$value){
			if($value!='')	$search[$name]	=	$value;
		}
		/**
		 * $search				=	关键字：title
		 */
		$GLOBALS['search']	=	$search;
		$title				=	$search['title'];
		$searesu			=	$this->searchModel->keywordsSearch($title);
		if($searesu->count()<=0){
			$searchKeywords	=	array(
				"keywords"	=>	$title,
				"time"		=>	time(),
				"times"		=>	1
			);
			$this->searchModel->addSearch($searchKeywords);
		}
		else{
			$times		=	$searesu->current()->times;
			$updattimes	=	array(
			"times"	=>	$times+1
			);
			$this->searchModel->updateKeywordsTimes($title,$updattimes);
		}
		$this->lmtpl->defineload(SITE_SEARCH_TPL);
		$GLOBALS['sum']						=	$this->searchModel->search($search)->count();
		$GLOBALS['page']					=	$page;
		$GLOBALS['pagesize']				=	$this->lmtpl->pagesize;
		$GLOBALS['dir']						=	BASEURI.'search/index';
		$GLOBALS['uri_segment']				=	rawurlencode($search['title']);
		$GLOBALS['is_html']					=	false;
		$this->lmtpl->content->title		=	$title;
		$this->lmtpl->content->keywords		=	"";
		$this->lmtpl->content->description	=	"";
		$this->lmtpl->render(SITE_SEARCH_TPL);
	}
}