<?php
(!defined('M_COM') || !defined('M_ADMIN')) && exit('No Permission');
aheader();
load_cache('cotypes,channels,currencys,permissions');
load_cache('catalogs,mtpls,cnodes',$sid);
cache_merge($channels,'channels',$sid);
include_once M_ROOT."./include/parse.fun.php";
include_once M_ROOT."./include/notice.cls.php";
if(!submitcheck('bstatic')){
	tabheader('生成静态页面','static',"?entry=static_cp$param_suffix");
	$ptypearr = array('index' => '首页','cnodes' =>'栏目页','archives' => '文档页');
	$staticarr = array('0' => '被动生成','1' =>'主动生成',);
	trbasic('选择页面类型','',makecheckbox('ptypes[]',$ptypearr),'');
	trbasic('选择生成方式','',makeradio('staticmode',$staticarr),'','被动生成速度非常快，但生成后需要页面被浏览一次才有更新效果。<br>主动生成速度相对较慢，但生成之后马上有更新效果。');
	trbasic('限最近更新的多少篇文档','arcnums','','text','不限请留空，主动生成文档页时设置有效');
	tabfooter('bstatic');
}else{
	if(empty($ptypes)) amessage('请选择需要生成静态的页面类型！',M_REFERER);
	if(in_array('index',$ptypes)){
		if(empty($staticmode)){
			if($sid){
				$db->query("UPDATE {$tblprefix}subsites SET ineedstatic=LEAST(ineedstatic,$timestamp) WHERE sid='$sid'");
			}else $db->query("UPDATE {$tblprefix}mconfigs SET value=LEAST(value,$timestamp) WHERE varname='ineedstatic'");
		}else{
			include_once M_ROOT."./include/cn_static.fun.php";
			index_static('',1);
		}
	}
	if(in_array('cnodes',$ptypes)){
		if(empty($staticmode)){
			$db->query("UPDATE {$tblprefix}cnodes SET lneedstatic=LEAST(lneedstatic,$timestamp) WHERE sid='$sid'");
		}else{
			include_once M_ROOT."./include/cn_static.fun.php";
			$query = $db->query("SELECT ename FROM {$tblprefix}cnodes WHERE sid='$sid' ORDER BY cnid");
			while($item = $db->fetch_array($query)){
				list_static($item['ename'],0,1);
			}
		}
	}
	if(in_array('archives',$ptypes)){
		if(!$staticmode){//被动生成静态
			foreach($ptypes as $k) $sqlstr .= (!$sqlstr ? '' : ',')."s.needstatic".($k ? $k : '')."=LEAST(s.needstatic".($k ? $k : '').",$timestamp+$staticdelay*60)";
			$sqlstr && $db->query("UPDATE {$tblprefix}archives a LEFT JOIN {$tblprefix}archives_sub s ON (s.aid=a.aid) SET s.needstatic=LEAST(s.needstatic,$timestamp) WHERE a.sid='$sid' AND chid != '5'");
		}else{
			$arcnums = empty($arcnums) ? 5000 : min(500,max(1,intval($arcnums)));
			include_once M_ROOT."./include/archive.cls.php";
			include_once M_ROOT."./include/arc_static.fun.php";
			$query = $db->query("SELECT aid FROM {$tblprefix}archives WHERE sid='$sid' AND chid NOT IN ('5','6') ORDER BY updatedate DESC LIMIT 0,$arcnums");
			while($item = $db->fetch_array($query)){
				arc_static($item['aid'],'',0);
			}
		}
	
	}
	amessage('静态生成完毕!',"?entry=static_cp$param_suffix");
}
?>
