<?php

/**
 *--------------------------------------
 * 搜索
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-11-13
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class SearchCtrlr extends IndexCtrlr
{
	public function search_do()
	{
		$_o_i = M('Option')->get_option('interaction');

		/* 搜索开关检查 */
		if(!$_o_i['search_switch'])
		{
			$this->error(L('SEARCH_IS_OFF'), AServer::get_preUrl());
		}

		/* 操作间隔太短 */
		if(!I('search', $_o_i['search_interval']))
		{
			$this->error(L('_TRY_LATER_'), __APP__);
		}

		$keyword = AFilter::keyword(ARequest::get('keyword'));
		if(1 > AString::mstrlen($keyword))
		{
			$this->error(L('KEYWORD_TOO_SHORT'), AServer::get_preUrl());
		}
		$this->assign('keyword', $keyword);

		/* 定义当前位置 */
		$this->assign('POSITION', L('SEARCH').' <strong>'.$keyword.'</strong>');

		/* 分解关键词 */
		$keywordSub = explode(' ', $keyword, 3);
		$likeStr = '';
		foreach($keywordSub as $k => $v)
		{
			if(empty($v))
			{
				continue;
			}
			if(0 == $k)
			{
				$likeStr .= 'LIKE \'%'.$v.'%\'';
			}
			else
			{
				$likeStr .= ' OR __ARCHIVE__.a_title LIKE \'%'.$v.'%\'';
			}
		}

		$where = array();
		$where['__ARCHIVE__.a_status'] = array('EQ', 1);
		$where['__ARCHIVE__.a_title'] = array('EXP', $likeStr);
		$order = '`a_rank` DESC, `a_edit_time` DESC';

		/* 获取分页信息 */
		$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
		$rowsNum = M('Archive')->where($where)->count();
		$p = new APage($rowsNum, 10, Url::U('home@search/search_do?keyword='.$keyword.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_AL = M('Archive')->get_archiveList($where, $order, $limit);
		foreach($_AL as $k => $v)
		{
			foreach($keywordSub as $kw)
			{
				$_AL[$k]['a_title'] = str_replace($kw, '<font color="#f00">'.$kw.'</font>', $_AL[$k]['a_title']);
				$_AL[$k]['a_description'] = str_replace($kw, '<font color="#f00">'.$kw.'</font>', $_AL[$k]['a_description']);
			}
		}

		$this->assign('_L', $_AL);

		I('search');
		$this->display('home/search');
	}

}

?>