<?php	
define('IN_OUN', true);
include_once( "includes/command.php");
if($Aconf['priveMessage'] != '')
{
   echo showMessage($Aconf['priveMessage']);
   exit;
}

$db_table = $pre."sysconfig";
$template_file = ($_REQUEST['template_file'])?$_REQUEST['template_file']:'home';
//系统定义标签名,是否显示，记录数，分类，自定义标签名
if($_POST['action'] == 'save')
{
		$str = 'one|';
        foreach( $_POST[one] as $k => $v ) {
		  $str .= $k.':'.$v[0].','.$v[1].','.$v[2].','.$v[3].';';
	    }
        $str .= ']';
		$str .= 'two|';
        foreach( $_POST[two] as $k => $v ) {
				$str .= $k.':'.$v[0].','.$v[1].','.$v[2].','.$v[3].';';
		}
        $str .= ']';
		$str .= 'three|';
 
        foreach( $_POST[three] as $kx => $vx ) {
		   if($kx == 'acids_s'){ 
			    $n = 1;
				foreach( $vx as $k => $v) {
					if($v>0 && $_POST[three][acids_n][$k] > 0){
						$str .='acids'.$n.':';
						$acid =  str_replace(',','~',$_POST[three][acids_id][$k]);
						$str .=$v.','.$_POST[three][acids_n][$k].','.$acid.','.$_POST[three][acids_t][$k].";";
						$n ++ ;
					}
				}
		   }
		   break;
		}
        $sql = "UPDATE " . $db_table . " SET ".$_POST['template_file']."='$str' WHERE  scid=".$_SESSION[$un_domain_id];
        $oPub->query($sql);
		$strMessage = '提交成功！';

}


/* 可以修改的模板 */
$Atemplate_files = array(
    'home'          => '首页     -- index.html',
);

/* 得到选择列表 */
$template_fileopt = '<SELECT NAME="template_file" id="template_file_id" onchange="selecttemplate(this.options[this.options.selectedIndex].value)">';
$template_fileopt .= '<OPTION VALUE="">模板文件选择</OPTION>';
foreach( $Atemplate_files as $k => $v ) {
	$tmp = ($_REQUEST['template_file'] == $k)?'selected':'';
    $template_fileopt .= '<OPTION VALUE="'.$k.'" '.$tmp.'>'.$v.'</OPTION>';
}
$template_fileopt .= '</SELECT>';

$strTitle = '';
if($template_file == 'home') {
	$strTitle = '首页';    
}
if($template_file == 'articles') {
   $strTitle  = '文章列表';
} 


$sql = "SELECT ".$template_file." FROM ".$db_table." WHERE scid='".$_SESSION[$un_domain_id]."'";
$Arow = $oPub->getRow($sql);
if($Arow )
{
	/* 基本配置信息 */
	$n = 0;
	$Asets = explode("]",$Arow[$template_file]);
    foreach ($Asets as $v)
    {
		$At = array();
		$At = explode("|",$v);

		if($At[0] == 'one') {
		   $Atv = explode(";",$At[1]);
		   foreach($Atv as $val) {
				$Atmp = explode(":",$val);
				if($Atmp[0]) { 
				   $Aitem[one][$Atmp[0]] = explode(",",$Atmp[1]); 
				}
			}
		}

		if($At[0] == 'two') {
			$Atv = explode(";",$At[1]);
			foreach($Atv as $val) {
			$Atmp = explode(":",$val);
				if($Atmp[0]) {
				   $Aitem[two][$Atmp[0]] = explode(",",$Atmp[1]);  
				}
			}
		}

		if($At[0] == 'three') {
			$Atv = explode(";",$At[1]);
			foreach($Atv as $val) {
			$Atmp = explode(":",$val);
				if($Atmp[0]) {
				   $Aitem[three][$Atmp[0]] = explode(",",$Atmp[1]);  
				}
			}
		}
	} 
} 

if(!$Aitem && $template_file){
	$Aitem[one][notices]          = 1;
	$Aitem[one][descs]            = 1;
	$Aitem[one][articat]          = 1;
	$Aitem[one][productcat]       = 1;
	$Aitem[one][vote]             = 1; 

	$Aitem[two][articles]         = array(1,5);
	$Aitem[two][articles_top]     = array(0,6);
	$Aitem[two][articles_focus]   = array(0,20); 
	$Aitem[two][articles_trundle] = array(0,10);
	$Aitem[two][articles_ifpic]   = array(0,20);
	$Aitem[two][articles_comms]   = array(0,20);

	$Aitem[two][products]         = array(0,6);  
	$Aitem[two][products_top]     = array(0,20); 
	$Aitem[two][probrand]         = array(0,20); 
	$Aitem[two][pravail]          = array(0,20); 
 
	$Aitem[two][votes]            = array(1,6);
	$Aitem[two][links]            = array(1,10); 
}

/* 得到公共选项 */

$strItem = '';
$n = 0;
if($Aitem){
	foreach( $Aitem[one] as $k => $v ) 
	{
		$tmpstr = ($n % 2 == 0)?"even":"odd";
		$strItem .= '<TR class="'.$tmpstr.'">';

		$strItem .= '<TD align="left">';
		$strItem .= $Aitname[$k];
		$strItem .= '</TD><TD align="left">';
		$strItem .= '<SELECT NAME="one['.$k.'][0]">';
		$selected_1 = ($v[0])?'SELECTED':''; 
		$selected_0 = (!$v[0])?'SELECTED':'';
		$strItem .= '<OPTION VALUE="1" '.$selected_1.'>是</OPTION>';
		$strItem .= '<OPTION VALUE="0" '.$selected_0.'> 否</OPTION>';
		$strItem .= '</SELECT>';
		$strItem .= '</TD><TD align="left"><INPUT TYPE="hidden" NAME="one['.$k.'][1]" value="" size="8"></TD>';
		$strItem .= '<TD align="left"><INPUT TYPE="hidden" NAME="one['.$k.'][2]" value=""></TD>';
		$strItem .= '<TD align="left">';
		$strItem .= '<INPUT TYPE="text" NAME="one['.$k.'][3]" value="'.($v[3]?$v[3]:$Aitname[$k]).'" size="20">'; 
		$strItem .= '</TD>';
		$strItem .= '<TD align="left">';
		$strItem .= '{$home.'.$k.'}';
		$strItem .= '</TD>';
		$strItem .= '</TR>';
		$n ++;
	}
 
	foreach( $Aitem[two] as $k => $v ) {
		$tmpstr = ($n % 2 == 0)?"even":"odd";
		$strItem .= '<TR class="'.$tmpstr.'">';

		$strItem .= '<TD align="left">';
		$strItem .= $Aitname[$k];
		$strItem .= '</TD><TD align="left">';
		$strItem .= '<SELECT NAME="two['.$k.'][0]">';
		$selected_1 = ($v[0])?'SELECTED':''; 
		$selected_0 = (!$v[0])?'SELECTED':'';
		$strItem .= '<OPTION VALUE="1" '.$selected_1.'>是</OPTION>';
		$strItem .= '<OPTION VALUE="0" '.$selected_0.'> 否</OPTION>';
		$strItem .= '</SELECT>';
		$strItem .= '</TD><TD align="left">';
		$strItem .= '<INPUT TYPE="text" NAME="two['.$k.'][1]" value="'.$v[1].'" size="5">';
		$strItem .= '</TD>';
		$strItem .= '<TD align="left"><INPUT TYPE="hidden" NAME="two['.$k.'][2]" value="" size="8"></TD>';
		$strItem .= '<TD align="left">';
		$strItem .= '<INPUT TYPE="text" NAME="two['.$k.'][3]" value="'.($v[3]?$v[3]:$Aitname[$k]).'" size="20">'; 
		$strItem .= '</TD>';
		$strItem .= '<TD align="left">';
		$strItem .= '{$home.'.$k.'}';
		$strItem .= '</TD>';
		$strItem .= '</TR>';
		$n ++;
	}

	if($Aitem[three])
	foreach( $Aitem[three] as $k => $v ) {
		$tmpstr = ($n % 2 == 0)?"even":"odd";
		$strItem .= '<TR class="'.$tmpstr.'">';
		
		$strItem .= '<TD align="left">';
		$strItem .= $Aitname["acids"];
		$strItem .= '</TD><TD align="left">';
		$strItem .= '<SELECT NAME="three[acids_s]['.$n.']">';
		$selected_1 = ($v[0])?'SELECTED':''; 
		$selected_0 = (!$v[0])?'SELECTED':'';
		$strItem .= '<OPTION VALUE="1" '.$selected_1.'>是</OPTION>';
		$strItem .= '<OPTION VALUE="0" '.$selected_0.'> 否</OPTION>';
		$strItem .= '</SELECT>';
		$strItem .= '</TD><TD align="left">';
		$strItem .= '<INPUT TYPE="text" NAME="three[acids_n]['.$n.']" value="'.$v[1].'" size="5">';
		$strItem .= '</TD>';
		$strItem .= '<TD align="left"><INPUT TYPE="text" NAME="three[acids_id]['.$n.']" value="'.(str_replace('~',',',$v[2])).'" style="width:200px"></TD>';
		$strItem .= '<TD align="left">';
		$strItem .= '<INPUT TYPE="text" NAME="three[acids_t]['.$n.']" value="'.($v[3]?$v[3]:$Aitname[$k]).'" size="20">'; 
		$strItem .= '</TD>';
		$strItem .= '<TD align="left">';
		$strItem .= '{$home.'.$k.'}';
		$strItem .= '</TD>';
		$strItem .= '</TR>';
		$n ++;
	}
}
//新闻一级分类
$sql = "SELECT acid,name FROM ".$pre."articat where fid = 0 AND domain_id=".$_SESSION[$un_domain_id]." ORDER BY acid ASC";
$AnormAll = $oPub->select($sql);
$strAcid ='';
while( @list( $key, $value ) = @each( $AnormAll) ) {
	   $n ++;
       $strAcid .= '<b>'.$value["name"]."</b>( ".$value["acid"]." ) &nbsp;&nbsp;&nbsp;" ; 
}  
?>
<?php
include_once( "header.php");
if ($strMessage != '') {
	echo  '<SCRIPT language="javascript"> alert( "'.$strMessage.'");</script>';
}
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="button">
<tr>
  <td align="left">
	<?php echo ($strTitle)?'>>'.$strTitle:'';?>    
    <?php echo $template_fileopt;?>
 </td>
</tr>
</table>
<form action="" method="post" name="theForm">
<TABLE width="100%" border=0> 
  <TR class="odd">
     <TD><B>项目</B></TD>
	 <TD><B>显示</B></TD>
	 <TD><B>记录数</B></TD>
	 <TD><B>分类</B></TD>
	 <TD><B>在前台显示的标签标签名</B></TD>
	 <TD><B>模版代码调用检索(完善中...)</B></TD>
  </TR> 
  <?PHP echo $strItem;?>
</TABLE>
<DIV class=content id=tabbar-div>
<TABLE width="100%" border=0 id=gallery-table> 
  <TR>
	<TD>
		<A onclick=addImg(this) href="javascript:;">[+]</A> 
		新闻子类调用: 
		<B>显示</B><SELECT NAME="three[acids_s][]"> 
			 <OPTION VALUE="1" >是</OPTION> 
			 <OPTION VALUE="0" > 否</OPTION> 
			 </SELECT> 
		<B>记录数</B><INPUT TYPE="text" NAME=three[acids_n][] value="" size="5"> 
		<B>新闻分类ID号</B><INPUT TYPE="text" NAME=three[acids_id][] value="" style="width:200px;"> 
		<B>在前台显示的标签标签名</B><INPUT TYPE="text" NAME=three[acids_t][] value="" size="10">
	</TD> 
  </TR> 
</TABLE>
<span style="margin-left: 356px;font-size: 9px">注：多个ID号，请用","逗号分割。</span>
<br/><b>新闻顶级分类(ID号):</b><?php echo $strAcid;?>
</DIV>
<TABLE width="100%" border=0> 
  <TR class="odd" >         
      <TD width="13%" align="left" colspan="6">
		 <div style="clear:left">
		    <input type="submit" name="Submit" value="确定保存" style="background-color: #FFCC66"/>
			<INPUT TYPE="hidden" name="template_file" value="<?php echo $template_file;?>">
			<INPUT TYPE="hidden" name="action" value="save">
		 </div>
      </TD>
   </TR> 
</TABLE>
</FORM>
<SCRIPT src="js/tab.js" type="text/javascript"></SCRIPT>
<SCRIPT src="js/utils.js" type="text/javascript"></SCRIPT>	
<SCRIPT src="../js/ajax.js" type="text/javascript"></SCRIPT>
<!--以下的两个script为添加的ajax上传-->
<SCRIPT language=JavaScript> 
  function addImg(obj)
  {
      var src  = obj.parentNode.parentNode;
      var idx  = rowindex(src);
      var tbl  = document.getElementById('gallery-table');
      var row  = tbl.insertRow(idx + 1);
      var cell = row.insertCell(-1);
      cell.innerHTML = src.cells[0].innerHTML.replace(/(.*)(addImg)(.*)(\[)(\+)/i, "$1removeImg$3$4-");
  } 
  /**
   * ~{I>3}M<F,IO4+~}
   */
  function removeImg(obj)
  {
      var row = rowindex(obj.parentNode.parentNode);
      var tbl = document.getElementById('gallery-table');

      tbl.deleteRow(row);
  } 
  /**
   * ~{I>3}M<F,~}
   */

  function  selecttemplate(a)
  {
     location="<?php echo $PHP_SELF;?>?template_file=" + a;   
  }
</SCRIPT>
<?php
include_once( "footer.php");
?>