<?php
!function_exists('html') && exit('ERR');

if($action=='mod'){
	
	
	//模板1是基本的.模板二是輔助的.實現多樣化效果
	$postdb[tplpart_1code]=StripSlashes($tplpart_1);
	$postdb[tplpart_2code]=StripSlashes($tplpart_2);

	//使用在線編輯器後,去掉多余的網址
	$weburl=preg_replace("/(.*)\/([^\/]+)/is","\\1/",$WEBURL);
	$postdb[tplpart_1code]=str_replace($weburl,"",$postdb[tplpart_1code]);
	$postdb[tplpart_2code]=str_replace($weburl,"",$postdb[tplpart_2code]);
 
	//針對一些自定義的模板做類型判斷
	if(strstr($postdb[tplpart_1code],'$picurl')&&strstr($postdb[tplpart_1code],'$content')){
		$stype="cp";
	}elseif(strstr($postdb[tplpart_1code],'$content')){
		$stype="c";
	}elseif(strstr($postdb[tplpart_1code],'$picurl')){
		$stype="p";
	}

	//選擇顯示兩列以上,這里選擇Table,否則不一定能顯示效果,選擇table指外套一個TABLE,選擇div指不套多余的代碼
	if($colspan>1){
		$DivTpl=0;
	}else{
		$DivTpl=1;
	}

	if($rowspan<1){
		$rowspan=1;
	}
	if($colspan<1){
		$colspan=1;
	}
	$rows=$rowspan*$colspan;
	
	//有輔助模板時,要加多一條,方便獲取其中一條數據
	if($postdb[tplpart_2code]){
		$rows++;
	}
	
	if($yz==1){
		$SQL=" WHERE A.yz=1 ";
	}else{
		$SQL=" WHERE 1 ";
	}
	if($levels==1){
		$SQL.=" AND A.levels=1 ";
	}
	if($fiddb[0]){
		foreach($fiddb AS $key=>$value){
			if(!is_numeric($value)){
				unset($fiddb[$key]);
			}
		}
		$fids=implode(",",$fiddb);
		$fids && $SQL.=" AND FA.fid IN ($fids) ";
	}else{
		showmsg("請選擇一個輔欄目!");
	}
	
	//指定顯示某些模型的內容
	if($amodule!='-1'){
		$SQL.=" AND A.mid='$amodule' ";
	}

	if( $amodule>0 && $article_moduleDB[$amodule][iftable] ){
		//分表處理
		$erp=$article_moduleDB[$amodule][iftable];
	}else{
		$erp='';
	}

	//$stype=="r"幻燈片,$picurl顯示圖片,如果主模板有圖片的話.輔助模板也自動要有圖片
	if($stype=="r"||strstr($postdb[tplpart_1code],'$picurl')){
		$SQL.=" AND A.ispic=1 ";
	}
	
	//特別處理,如果是幻燈片的話,要取消輔助模板
	$stype=="r" && $postdb[tplpart_2code]='';
	
	//有輔助模板的話,要特別處理.如果僅是輔助模板有圖片的話,就要讀多一條數據,否則的話.都是從主模板獲取.
	if(strstr($postdb[tplpart_2code],'$picurl')&&!strstr($postdb[tplpart_1code],'$picurl'))
	{
		if(strstr($postdb[tplpart_2code],'$content')){
			$SQL2=" SELECT A.*,A.aid AS id,R.content FROM {$pre}fu_article FA LEFT JOIN {$pre}article$erp A ON FA.aid=A.aid LEFT JOIN {$pre}reply$erp R ON A.aid=R.aid $SQL AND A.ispic=1 AND R.topic=1 ORDER BY $order $asc LIMIT 1 ";
		}else{
			$SQL2=" SELECT A.*,A.aid AS id FROM {$pre}fu_article FA LEFT JOIN {$pre}article$erp A ON FA.aid=A.aid $SQL AND A.ispic=1 ORDER BY $order $asc LIMIT 1 ";
		}		
	}
	
	//主模板中有圖片
	if( strstr($postdb[tplpart_1code],'$picurl') ){
		$SQLPIC=" AND A.ispic=1 ";
	}
	//主模板,即使有輔助模板,不同時為圖片的話.都從主表讀取,避免多讀一次數據庫
	if( strstr($postdb[tplpart_1code],'$content')||strstr($postdb[tplpart_2code],'$content') ){
		$SQL=" SELECT A.*,A.aid AS id,R.content FROM {$pre}fu_article FA LEFT JOIN {$pre}article$erp A ON FA.aid=A.aid LEFT JOIN {$pre}reply$erp R ON A.aid=R.aid  $SQL $SQLPIC AND R.topic=1 ORDER BY $order $asc LIMIT $rows ";
	}else{
		$SQL=" SELECT A.*,A.aid AS id FROM {$pre}fu_article FA LEFT JOIN {$pre}article$erp A ON FA.aid=A.aid  $SQL $SQLPIC ORDER BY $order $asc LIMIT $rows ";
	}
	
	$postdb[SYS]='artcile';	
	$postdb[RollStyleType]=$RollStyleType;
	
	$postdb[rolltype]=$rolltype;
	$postdb[rolltime]=$rolltime;
	$postdb[roll_height]=$roll_height;
	$postdb[width]=$width;
	$postdb[height]=$height;
	
	$postdb[newhour]=$newhour;
	$postdb[hothits]=$hothits;
	$postdb[amodule]=$amodule;
	$postdb[tplpath]=$tplpath;
	$postdb[DivTpl]=$DivTpl;
	$postdb[fiddb]=$fids;
	$postdb[stype]=$stype;
	$postdb[yz]=$yz;
	$postdb[hidefid]=$hidefid;
	$postdb[timeformat]=$timeformat;
	$postdb[order]=$order;
	$postdb[asc]=$asc;
	$postdb[levels]=$levels;
	$postdb[rowspan]=$rowspan;
	$postdb[sql]=$SQL;			//主模板
	$postdb[sql2]=$SQL2;		//輔助模板
	$postdb[colspan]=$colspan;
	$postdb[content_num]=$content_num;
	$postdb[content_num2]=$content_num2;
	$postdb[titlenum]=$titlenum;
	$postdb[titlenum2]=$titlenum2;
	$postdb[titleflood]=$titleflood;

	$postdb[c_rolltype]=$c_rolltype;
	//print_r($postdb);exit;
	$code=addslashes(serialize($postdb));
	$div_db[div_w]=$div_w;
	$div_db[div_h]=$div_h;
	$div_db[div_bgcolor]=$div_bgcolor;
	$div=addslashes(serialize($div_db));
	$typesystem=1;
	//插入或更新標簽庫
	do_post();

}else{

	$rsdb=get_label();
	$div=unserialize($rsdb[divcode]);
	@extract($div);
	$codedb=unserialize($rsdb[code]);
	@extract($codedb);
	
	if(!isset($levels)){
		$levels="all";
	}
	if(!isset($order)){
		$order="list";
	}
	$titleflood=(int)$titleflood;
	$hide=(int)$rsdb[hide];
	if($rsdb[js_time]){
		$js_ck='checked';
	}

	/*默認值*/
	$yz=='all' || $yz=1;
	$asc || $asc='DESC';
	$titleflood!=1		&& $titleflood=0;
	$timeformat			|| $timeformat="Y-m-d H:i:s";
	$rowspan			|| $rowspan=5;
	$colspan			|| $colspan=1;
	$titlenum			|| $titlenum=20;
	$content_num		|| $content_num=80;
	$div_w				|| $div_w=50;
	$div_h				|| $div_h=30;
	$hide!=1			&& $hide=0;
	$DivTpl!=1			&& $DivTpl=0;
	$stype				|| $stype=4;

	$width				|| $width=250;
	$height				|| $height=187;
	$roll_height		|| $roll_height=50;
	
	$div_width && $div_w=$div_width;
	$div_height && $div_h=$div_height;
	$yzdb[$yz]="checked";
	$ascdb[$asc]="checked";
	$orderdb[$order]=" selected ";
	$levelsdb[$levels]=" checked ";
	$titleflooddb["$titleflood"]="checked";
	$hidedb[$hide]="checked";
	$divtpldb[$DivTpl]="checked";

	$_hidefid[intval($hidefid)]=" checked ";
	$fiddb=explode(",",$codedb[fiddb]);
	
	$c_rolltype || $c_rolltype=0;
	$newhour	|| $newhour=24;
	$hothits	|| $hothits=100;

	$titlenum2			|| $titlenum2=40;
	$content_num2		|| $content_num2=120;
	$rolltime			|| $rolltime=3;

	$_rolltype[$rolltype]=' selected ';

	$c_rolltypedb[$c_rolltype]=" checked ";

 	$select_news=$Guidedb->Checkbox("{$pre}fu_sort",'fiddb[]',$fiddb);
	
	$tplpart_1=str_replace("&nbsp;","&amp;nbsp;",$tplpart_1);
	$tplpart_2=str_replace("&nbsp;","&amp;nbsp;",$tplpart_2);
 
	$getLabelTpl=getLabelTpl($inc,array("common_title","common_pic","common_content","common_fname","common_zh_title","common_zh_pic","common_zh_content"));

	$query = $db->query("SELECT * FROM `{$pre}article_module` WHERE ifclose=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$moduledb[$rs[id]]=$rs[name];
	}
	$mck[$amodule]=' selected ';

	//幻燈片樣式
	$rollpicStyle="<select name='RollStyleType' id='RollStyleType' onChange='rollpictypes(this)'><option value=''>默認</option>";
	$dir=opendir(PHP168_PATH."template/default/rollpic/");
	while($file=readdir($dir)){
		if(eregi("\.htm$",$file)){
			$rollpicStyle.="<option value='$file'>".str_replace(".htm","",$file)."</option>";
		}
	}
	$rollpicStyle.="</select>";

	require("head.php");
	require("template/label/article.htm");
	require("foot.php");
}
?>