<?php
include("qs.php");
require( '../data/config.php');
$conn=mysql_connect($db_host,$db_user,$db_pass);
mysql_query("set names 'utf8'");
mysql_select_db($db_name);
if (PHP_VERSION >= '5.1' && !empty($timezone))
{
    date_default_timezone_set($timezone);
}
$action=_get('action');
if($action=="")
$action=_post('action');
$id=_get('id');
if($id=="")
$id=_post('id');
$qs=explode(",",$_SESSION["tm_qs"]);
if($qs[0]==0)
   {
   echo "<script>location.href='main.php';</script>";
   exit();
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META http-equiv="Pragma" content="no-cache" />
<LINK href="css/general.css" rel="stylesheet" type="text/css" />
<LINK href="css/main.css" rel="stylesheet" type="text/css" />
</HEAD><BODY>
<?php 
$nowurl="http://".$_SERVER['SERVER_NAME'].request_uri();
$nowurl=ereg_replace("admin/","",$nowurl);
$date=date('Y-m-d');
 if($action=="del")
 {
 $rs=mysql_fetch_object(mysql_query("select * from tm_art_class where art_id='$id'"));
 if(!$rs)
 {
 echo "<script>location.href='class.php';</script>";
 exit();
 }
//如果自定义导航存在，则删除自定义导航对应的栏目
if($rs->art_show==1)
    mysql_query("delete from tm_mune where url='".$nowurl."artlist.php?id=".$id."'");

 mysql_query("delete from tm_art_class where art_id='$id'");
 mysql_query("delete from tm_art where art_class='$id'");
 
 echo "<script>location.href='class.php';</script>";
 exit();
 }
elseif($action=="add")
{
$title=_post('title');
$show1=_post('show1');
$filename=_post('filename');
$dir = "../";
$handle=opendir($dir);
while ($file = readdir($handle)) 
{
if($file<>"." and $file<>".." and is_dir( $dir ."/".$file ))
	   {
	   if($file==$filename)
	      {
	      echo "<script>alert('".$filename."文件名称已存在!');history.go(-1);</script>";
	      exit();
		  }
	   }
} 
$des=_post('des');
if(mysql_fetch_object(mysql_query("select * from tm_art_class where filename='$filename'")))
  {
  echo "<script>alert('存在相同文件名!');history.go(-1);</script>";
  exit();
  }
$rs=mysql_query("insert into tm_art_class(art_name,art_show,filename,des) values('$title','$show1','$filename','$des')");
//如果分类写入成功，则判断是否在导航中显示，显示则写入自定义导航的数据表中
if($rs)
   {
   if($show1==1)
      {
      $inurl=$nowurl."artlist.php?id=".mysql_insert_id();
	  mysql_query("insert into tm_mune(title,url) values('$title','$inurl')");
	  }
   }
echo "<script>location.href='class.php';</script>";
exit();
}
elseif($action=="edit")
{
$title=_post('title');
$show1=_post('show1');
$filename=_post('filename');
//取得原目录名称
$rs=mysql_fetch_object(mysql_query("select * from tm_art_class where art_id='$id'"));
$yfilename=$rs->filename;
$dir = "../";
$handle=opendir($dir);
while ($file = readdir($handle)) 
{
if($file<>"." and $file<>".." and is_dir( $dir ."/".$file ))
	   {
	   if($file==$filename and $file<>$yfilename)
	      {
	      echo "<script>alert('".$filename."文件名称已存在!');history.go(-1);</script>";
	      exit();
		  }
	   }
} 
if(mysql_fetch_object(mysql_query("select * from tm_art_class where filename='$filename' and art_id<>'$id'")))
  {
  echo "<script>alert('存在相同文件名!');history.go(-1);</script>";
  exit();
  }
//修改分类时与自定义导航的关联操作  
$rs=mysql_fetch_object(mysql_query("select * from tm_art_class where art_id='$id'"));
if($rs->art_show<>$show1)
   {
   if($show1==1)
      {
	  $inurl=$nowurl."artlist.php?id=".$id;
	  mysql_query("insert into tm_mune(title,url) values('$title','$inurl')");
	  }
   else
      {
	  mysql_query("delete from tm_mune where url='".$nowurl."artlist.php?id=".$id."'");
	  }
   }
mysql_query("update tm_art_class set art_name='$title',art_show='$show1',filename='$filename',des='$des' where art_id='$id'");
echo "<script>location.href='class.php';</script>";
exit();
}
?>
<div class="h2">系统管理 >> 日志分类管理 >> <?php if($id=="") echo "添加";else echo "编辑";?></div>
<div class="list-div">

<table cellspacing='1' cellpadding='3'>
<tr>
  <th align='left'><table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><div align="left">&nbsp;&nbsp;日志分类管理 </div></td>
        <td width="150">&nbsp;</td>
      </tr>
    </table></th>
</tr>
<tr><td align="left" valign="top" style="background:#F5F5F5;padding-bottom:8px;">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0 CALLPADDING="0">
<?php 			  
if($action=="" and $id=="")
{
?>
<form id="form1" name="form1" method="post" action="class_w.php">
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">分类名称：
                                  <input name="title" type="text" id="title" size="50" />
			                  </span></TD>
    </TR>
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">文件名称：
                                  <input name="filename" type="text" id="filename" size="50" />
          <input name="button" type="button" onclick="javascript:checkIdType()" value="查验目录名是否可用" /><br />
          &nbsp;&nbsp;<span class="post_date">(提示:文章静态化存放目录，请使用英文,且每个栏目的名称不可相同,以及与当前系统目录不能相同,例:栏目名称为php技术,文件名可为:phpjishi)</span>
</span></TD>
    </TR>
<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">分类说明：
                                  <textarea name="des" cols="50" rows="5" id="des"></textarea>
                                 </span> <br />
			                 &nbsp;&nbsp; <span class="post_date">(提示:为SEO而设,也可以留空,栏目页的说明!) </span></TD>
    </TR>
			  <TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30>
			  <p style="background:#F0F0F0">导航显示：
			    <select name="show1" id="show1">
			      <option value="1" selected="selected">显示</option>
			      <option value="0">不显示</option>
			      </select>
			    </p>
			  </TD>
              </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><span class="help">
                  <input type="image" 
                        src="images/submit.gif" align="absmiddle" border="0" 
                        name="submit" onclick="return checkform(this.form)" />
                  <input name="action" type="hidden" id="action" value="add" />
                </span></TD>
              </TR>
 </form>
 <script language="javascript">
function checkform(theform)
{
if(theform.title.value=="")
  {
  alert("请输入栏目名称！");
  theform.title.focus();
  return false;
  }
if(theform.filename.value=="")
  {
  alert("请输入文件名称！");
  theform.filename.focus();
  return false;
  }
var check=/[^\u4E00-\u9FA5]/g;
var txtcheck=theform.filename.value;
if(txtcheck!="") 
        { 
            if(!(check.test(txtcheck))) 
            { 
                alert("文件名称不能以中文命名,请用E文字母!"); 
				theform.filename.focus();
                return false; 
            } 
        } 
return true;
}
</script>
 <?php 
 }
if($action=="" and $id<>"")
{
$sql="select * from tm_art_class where art_id='$id'";
$rsql=mysql_query($sql);
$rs=mysql_fetch_object($rsql);
 ?>
 <form id="form1" name="form1" method="post" action="class_w.php">
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">分类名称：
                                  <input name="title" type="text" id="title" value="<?php echo $rs->art_name;?>" size="50" />
			                  </span>			                  
							 
							  </TD>
    </TR>
<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">文件名称：
                                  <input name="filename" type="text" id="filename" value="<?php echo $rs->filename;?>" size="50" />
           <input name="button" type="button" onclick="javascript:checkIdType()" value="查验目录名是否可用" /><br />
           &nbsp;&nbsp;<span class="post_date">(提示:文章静态化存放目录，请使用英文,且每个栏目的名称不可相同,以及与当前系统目录不能相同,例:栏目名称为php技术,文件名可为:phpjishi)</span></span></TD>
    </TR>
	<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">分类说明：
                                  <textarea name="des" cols="50" rows="5" id="des"><?php echo $rs->des;?></textarea>
          </span>
		  <br />
			                 &nbsp;&nbsp; <span class="post_date">(提示:为SEO而设,也可以留空,栏目页的说明!) </span>
		  </TD>
    </TR>
			  <TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><p style="background:#F0F0F0">导航显示：                  
                    <select name="show1" id="show1">
                    <option value="1" <?php if($rs->art_show==1) echo "selected";?>>显示</option>
                    <option value="0" <?php if($rs->art_show==0) echo "selected";?>>不显示</option>
                  </select></p></TD>
              </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><span class="help">
                  <input type="image" 
                        src="images/submit.gif" align="absmiddle" border="0" 
                        name="submit" onclick="return checkform(this.form)" />
                  <input name="action" type="hidden" id="action" value="edit" />
                  <input name="id" type="hidden" id="id" value="<?php echo $id;?>" />
                </span></TD>
              </TR>
 </form>
  <script language="javascript">
function checkform(theform)
{
if(theform.title.value=="")
  {
  alert("请输入分类名称！");
  theform.title.focus();
  return false;
  }
return true;
}
</script>
 <?PHP }
 ?>
      </TABLE>
</td>
</tr></table></div>

<?php include "boot.php";?>
</body></html>
<script language="JavaScript" type="text/JavaScript">
<!--
	function checkIdType(){
		var pId = document.form1.filename.value;
		if(!checkIdPattern(pId)){
			return false;
		}
		var load = window.open('chkeuser.php?filename='+pId,'','scrollbars=no,menubar=no,height=100,width=280,resizable=no,toolbar=no,location=no,status=no');
		if(load == null){
			alert("此功能要用到弹出窗口！");
		}
	}
function checkIdPattern(pId){
			if (pId.length == 0) {
				 alert("不能为空!!");
				 return (false);
			}
			if (pId.length < 3) {
				 alert("不能少于3位!!");
				 return (false);
			}
			var check=/[^\u4E00-\u9FA5]/g;
			if(pId!="") 
        { 
            if(!(check.test(pId))) 
            { 
                alert("文件名称不能以中文命名,请用E文字母!"); 
                return false; 
            } 
        } 
         return true;
	}
</script>