<?php
require_once("inc/common.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
$searchLimitFlag = 0;$searchtime = 5; //searchLimitFlag为显示搜索控制标识(0不开启,1开启),searchtime为搜索间隔秒数
$searchword = getForm("searchword","both");$searchType = getForm("searchtype","both");$page = getForm("page","both");
if (empty($searchType)) { 
	$searchType=-1;
} else {
	if (is_numeric($searchType)){ $searchType=intval($searchType); } else { echoSaveStr("safe");exit;}
}
if (empty($page)) {
	$page=1;
} else { 
	if (is_numeric($page)){ $page=intval($page); } else { echoSaveStr("safe");exit;}
}
if (strlen($searchword)>20) $searchword = (msubstr($searchword,20));
echoSearchPage();

function echoSearchPage() { 
	global $page,$t1,$searchLimitFlag,$searchtime,$searchType,$searchword;
	if (empty($searchword)){ echoMsgAndGo('请输入搜索关键字',$searchtime);}
	if ($searchLimitFlag == 1) checkSearchTimes($searchtime);
	$searchTemplatePath = DJCMS_TEMPLATE."search.html";
	$pSize = getPageSize(loadFile($searchTemplatePath),"search") ; if (empty($pSize)) $pSize=12;
	switch ($searchType){
		case -1 : $whereStr=" WHERE m_recycle=0 AND (m_name LIKE '%".$searchword."%' OR m_actor LIKE '%".$searchword."%' OR  m_director LIKE '%".$searchword."%')";break;
		case 0 : $whereStr=" WHERE m_recycle=0 AND m_name LIKE '%".$searchword."%'";break;
		case 1 : $whereStr=" WHERE m_recycle=0 AND (m_actor LIKE '%".$searchword."%' OR m_director LIKE '%".$searchword."%')";break;
		case 2 : $whereStr=" WHERE m_recycle=0 AND m_publisharea LIKE '%".$searchword."%'";break;
		case 3 : $whereStr=" WHERE m_recycle=0 AND m_publishyear LIKE '%".$searchword."%'";break;
		case 4 : $whereStr=" WHERE m_recycle=0 AND m_letter='".strtoupper($searchword)."'";break;
		case 5 : $whereStr=" WHERE m_recycle=0 AND m_lang LIKE '%".$searchword."%'";break;
	}
	if ("".$searchword=="") $whereStr=" WHERE 1=2";
	$recordcount = DB::result_first("SELECT count(*) FROM ".DB::table('data')." ".$whereStr);
	$cacheName="parse_search_";
	if (cacheStart == 1) {
		if (djcms::$content = memory('get', $cacheName)) {
        } else {
            parseSearchPart($searchTemplatePath);
            memory('set', $cacheName, djcms::$content);
        }
	} else {
	    parseSearchPart($searchTemplatePath);
	}
	$content = replaceStr(djcms::$content,"{djcms:searchword}",$searchword);
	djcms::$content = replaceStr($content,"{djcms:searchnum}",$recordcount);
	djcms::parsePageList("",$page,"search");
	djcms::$content = replaceCurrentTypeId(djcms::$content);
	djcms::parseIf();
	echo replaceStr(djcms::$content,"{djcms:runinfo}",getRunTime($t1)) ;
}

function parseSearchPart($x) {
	djcms::$content=loadFile($x);
	djcms::parseTopAndFoot();
	djcms::parseSelf();
	djcms::parseGlobal();
	djcms::parseMenuList("");
	djcms::parseNewsList();
	djcms::parseAreaList();
	djcms::parseVideoList();
	djcms::parseTopicList();
}

function checkSearchTimes($searchtime) {
	global $searchword,$searchtime;
	if (!empty($searchword)) {
		if (cookie::get("djcms_search")=="ok") echoMsgAndGo('搜索限制为'.$searchtime.'秒一次',$searchtime);
		cookie::set("djcms_search", "ok", $searchtime);
	}
}
?>