<?php
if(!defined('IN_GATEWEI')) {
	exit('access denied');
}
if($ipost == 'delete'){
	$get_id=intval($get_id);
	$c=$db->result($db->query("select count(*) from g_templates"),0);
	if($c<2){
		gadminmessage("删除失败,系统要求必须保留一个模板");
	}
	$enable=$db->result($db->query("select enable from g_templates where template_id = $get_id limit 1"),0);
	if($enable){
		$enable=$db->result($db->query("select template_id from g_templates order by subtime desc limit 1"),0);
	}
	$db->query("UPDATE g_templates SET enable = 0");
	$db->query("UPDATE g_templates SET enable = 1 WHERE template_id = $enable");
	if($db->query("delete from g_templates where template_id = $get_id")){
		$templatedir=$db->result($db->query("select template_url from g_templates where enable=1 limit 1"),0);
		$db->query("REPLACE INTO g_settings (var, val) VALUES ('templatedir', '$templatedir')");
		w2setting();
		writelog($userid."删除网站模板成功");
		gadminmessage("模板删除成功，请通过ftp或其他远程方式手动删除服务器上的模板包文件","siteadmin.php?iframe=template",1);
	}else{
		gadminmessage("删除失败","siteadmin.php?iframe=template",1);
	}
}
if($ipost == 'savetemplate'){
		if(!empty($new_template_name) && !empty($new_template_copyright)){
		$new_template_name = cutstr(trim(ghtmlspecialchars($new_template_name)), 70 ,'');
		$new_template_url = cutstr(trim(ghtmlspecialchars($new_template_url)), 200 ,'');
		$new_template_copyright = cutstr(trim(ghtmlspecialchars($new_template_copyright)), 70 ,'');
		if(!is_dir($new_template_url) || !file_exists($new_template_url.'message.tpl.php')){
			gadminmessage("添加失败,请先将模板文件包放入服务器中的相应位置");
		}
		$db->query("insert into g_templates (template_id , template_name , template_url , template_copyright , enable , subtime ) VALUES (null, '$new_template_name' , '$new_template_url' , '$new_template_copyright' , 0 , $timestamp)");
		$add_new=true;
		}
	$enable=intval($template_enable);
		if(!$enable){
			if($add_new){
				$enable=$db->insert_id();
			}else{
				$enable=$db->result($db->query("select template_id from g_templates order by subtime desc limit 1"),0);
			}
		}
	if($template_url){
		foreach($template_url as $key => $val){
			$db->query("UPDATE g_templates SET template_url = '$val' WHERE template_id = $key");
		}
	}
	if(!$add_new && $template_enable=='new_enable'){
		gadminmessage("不能设置空模板启动值为真");
	}
		$db->query("UPDATE g_templates SET enable = 0");
		$db->query("UPDATE g_templates SET enable = 1 WHERE template_id = $enable");

		$templatedir=$db->result($db->query("select template_url from g_templates where enable=1 limit 1"),0);
		$db->query("REPLACE INTO g_settings (var, val) VALUES ('templatedir', '$templatedir')");
		w2setting();
		writelog($userid."设置网站模板成功");
		gadminmessage("设置成功","siteadmin.php?iframe=template",1);
}
$q=$db->query("select * from g_templates order by subtime DESC");
?>
<table>
<caption>网站模板设置</caption>
    <tr>
    <td width="20%"><span>可用模板</span></td>
	<td width="37%"><span>模板文件路径</span></td>
	<td width="23%"><span>版权信息</span></td>
	<td width="10%"><span>启用</span></td>
	<td width="10%" align="center"><span>删除</span></td>
  </tr>

 <form id="template_form" method="post">
 <?php while($templateinfo=$db->fetch_array($q)){
	 $enable_checked=$templateinfo['enable']?"checked":"";
	 ?>
  <tr>
    <td><span><?php echo $templateinfo['template_name']?></span></td>
	<td >
	<input type="text" name="template_url[<?php echo $templateinfo['template_id']?>]" value="<?php echo $templateinfo['template_url']?>" class="formtext">
	</td>
	<td ><?php echo $templateinfo['template_copyright']?></td>
	<td ><input type="radio" name="template_enable" value="<?php echo $templateinfo['template_id']?>"  <?php echo $enable_checked?>	></td>
	<td align="center"><span><a href="siteadmin.php?iframe=template&ipost=delete&get_id=<?php echo $templateinfo['template_id']?>"><img src="./images/siteadmin/g_admin_delete.gif"></a></span></td>
  </tr>
  <?php } ?>
     <tr>
          <td colspan="5">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
        <tr>
          <th colspan="5">
	         新增模板
	      </th>
   </tr>
    <tr>
    <td ><span>模板名称</span></td>
	<td ><span>模板文件路径</span></td>
	<td><span>版权信息</span></td>
	<td><span>启用</span></td>
	<td>&nbsp;</td>
  </tr>

      <tr>
    <th ><input type="text" name="new_template_name" class="smalltext"></th>
	<td  ><input type="text" name="new_template_url" value="./templates/your_template_name/" class="formtext"></td>
	<td><input type="text" name="new_template_copyright" class="smalltext">
    </td>
	<td><input type="radio" name="template_enable" value="new_enable"></td>
  </tr>

</form>

     <tr>
          <td colspan="5">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>

</table>
<div id="submitdiv"><input type="button" value="提  交" onclick="subtemplateform()"></div>
</form>

<script language="javascript">
function subtemplateform(){
	$("template_form").action="siteadmin.php?iframe=template&ipost=savetemplate";
	$("template_form").submit();
}

</script>