<%@language=vbscript codepage=936 %>
<%
option explicit
response.buffer=true	
Const PurviewLevel=2    '操作权限
%>
<!--#include file="Admin_ChkPurview.asp"-->
<!--#include file="inc/conn.asp"-->
<!--#include file="inc/function.asp"-->
<!--#include file="inc/admin_code.asp"-->
<%
dim Action,ParentID,SkinID,LayoutID,BrowsePurview,AddPurview,i,FoundErr,ErrMsg
dim SkinCount,LayoutCount
Action=trim(Request("Action"))
ParentID=trim(request("ParentID"))
if ParentID="" then
	ParentID=0
else
	ParentID=CLng(ParentID)
end if
if Action="Add" then
	call AddNewClass()
else
	call main()
end if
if FoundErr=True then
	call WriteErrMsg()
end if
call CloseConn()

sub AddNewClass()
	dim ClassID,ClassName,IsElite,Readme,ClassMaster,ClassPicUrl,LinkUrl,PrevOrderID
	dim sql,rs,trs
	dim RootID,ParentDepth,ParentPath,ParentStr,ParentName,MaxClassID,MaxRootID
	dim PrevID,NextID,Child

	ClassName=trim(request("ClassName"))
	ClassMaster=trim(request("ClassMaster"))
	IsElite=trim(request("IsElite"))
	Readme=trim(request("Readme"))
	ClassPicUrl=trim(request("ClassPicUrl"))
	LinkUrl=trim(request("LinkUrl"))
	SkinID=Trim(request("SkinID"))
	LayoutID=trim(request("LayoutID"))
	BrowsePurview=trim(request("BrowsePurview"))
	AddPurview=trim(request("AddPurview"))
	if ClassName="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>栏目名称不能为空！</li>"
	end if
	if IsElite="Yes" then
		IsElite=True
	else
		IsElite=False
	end if
	if SkinID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定栏目配色模板</li>"
	else
		SkinID=CLng(SkinID)
	end if
	if LayoutID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定版面设计模板</li>"
	else
		LayoutID=CLng(LayoutID)
	end if
	if ClassMaster<>"" then
		call AddMaster(ClassMaster)
	end if
	if FoundErr=True then
		exit sub
	end if

	set rs = conn.execute("select Max(ClassID) From ArticleClass")
	MaxClassID=rs(0)
	if isnull(MaxClassID) then
		MaxClassID=0
	end if
	rs.close
	ClassID=MaxClassID+1
	set rs=conn.execute("select max(rootid) From ArticleClass")
	MaxRootID=rs(0)
	if isnull(MaxRootID) then
		MaxRootID=0
	end if
	rs.close
	RootID=MaxRootID+1
	
	if ParentID>0 then
		sql="select * From ArticleClass where ClassID=" & ParentID & ""
		rs.open sql,conn,1,1
		if rs.bof and rs.eof then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>所属栏目已经被删除！</li>"
		else
			if rs("LinkUrl")<>"" then
				FoundErr=True
				ErrMsg=ErrMsg & "<br><li>不能指定外部栏目为所属栏目！</li>"
			end if
		end if
		if FoundErr=True then
			rs.close
			set rs=nothing
			exit sub
		else	
			RootID=rs("RootID")
			ParentName=rs("ClassName")
			ParentDepth=rs("Depth")
			ParentPath=rs("ParentPath")
			Child=rs("Child")
			if ParentPath="0" then        '如果父栏目为一级栏目，即此栏目为第二级栏目，
				ParentPath=ParentID       '则此栏目的父级栏目路径为父栏目的ClassID
			else
				ParentPath=ParentPath & "," & ParentID     '得到此栏目的父级栏目路径
			end if
			PrevOrderID=rs("OrderID")
			if Child>0 then		
				dim rsPrevOrderID
				'得到与本栏目同级的最后一个栏目的OrderID
				set rsPrevOrderID=conn.execute("select Max(OrderID) From ArticleClass where ParentID=" & ParentID)
				PrevOrderID=rsPrevOrderID(0)
				set trs=conn.execute("select ClassID from ArticleClass where ParentID=" & ParentID & " and OrderID=" & PrevOrderID)
				PrevID=trs(0)
				
				'得到同一父栏目但比本栏目级数大的子栏目的最大OrderID，如果比前一个值大，则改用这个值。
				set rsPrevOrderID=conn.execute("select Max(OrderID) From ArticleClass where ParentPath like '" & ParentPath & ",%'")
				if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
					if not IsNull(rsPrevOrderID(0))  then
				 		if rsPrevOrderID(0)>PrevOrderID then
							PrevOrderID=rsPrevOrderID(0)
						end if
					end if
				end if
			else
				PrevID=0
			end if

		end if
		rs.close
	else
		if MaxRootID>0 then
			set trs=conn.execute("select ClassID from ArticleClass where RootID=" & MaxRootID & " and Depth=0")
			PrevID=trs(0)
			trs.close
		else
			PrevID=0
		end if
		PrevOrderID=0
		ParentPath="0"
	end if

	sql="Select * From ArticleClass Where ParentID=" & ParentID & " AND ClassName='" & ClassName & "'"
	set rs=server.CreateObject("adodb.recordset")
	rs.open sql,conn,1,1
	if not(rs.bof and rs.eof) then
		FoundErr=True
		if ParentID=0 then
			ErrMsg=ErrMsg & "<br><li>已经存在一级栏目：" & ClassName & "</li>"
		else
			ErrMsg=ErrMsg & "<br><li>“" & ParentName & "”中已经存在子栏目“" & ClassName & "”！</li>"
		end if
		rs.close
		set rs=nothing
		exit sub
	end if
	rs.close

	sql="Select top 1 * From ArticleClass"
	rs.open sql,conn,1,3
    rs.addnew
	rs("ClassID")=ClassID
   	rs("ClassName")=ClassName
	rs("IsElite")=IsElite
	rs("ClassMaster")=ClassMaster
	rs("RootID")=RootID
	rs("ParentID")=ParentID
	if ParentID>0 then
		rs("Depth")=ParentDepth+1
	else
		rs("Depth")=0
	end if
	rs("ParentPath")=ParentPath
	rs("OrderID")=PrevOrderID
	rs("Child")=0
	rs("Readme")=Readme
	rs("ClassPicUrl")=ClassPicUrl
	rs("LinkUrl")=LinkUrl
	rs("SkinID")=SkinID
	rs("LayoutID")=LayoutID
	rs("BrowsePurview")=Cint(BrowsePurview)
	rs("AddPurview")=Cint(AddPurview)
	rs("PrevID")=PrevID
	rs("NextID")=0
	rs.update
	rs.Close
    set rs=Nothing
	
	'更新与本栏目同一父栏目的上一个栏目的“NextID”字段值
	if PrevID>0 then
		conn.execute("update ArticleClass set NextID=" & ClassID & " where ClassID=" & PrevID)
	end if
	
	if ParentID>0 then
		'更新其父类的子栏目数
		conn.execute("update ArticleClass set child=child+1 where ClassID="&ParentID)
		
		'更新该栏目排序以及大于本需要和同在本分类下的栏目排序序号
		conn.execute("update ArticleClass set OrderID=OrderID+1 where rootid=" & rootid & " and OrderID>" & PrevOrderID)
		conn.execute("update ArticleClass set OrderID=" & PrevOrderID & "+1 where ClassID=" & ClassID)
	end if
	
    call CloseConn()
	Response.Redirect "Admin_ClassManage.asp"  
end sub

sub main()
%>
<html>
<head>
<title>添加栏目</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="Admin_Style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
function check()
{
  if (document.form1.ClassName.value=="")
  {
    alert("栏目名称不能为空！");
	document.form1.ClassName.focus();
	return false;
  }
}
</script>

</head>
<body leftmargin="2" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
  <tr class="topbg"> 
    <td height="22" colspan="2" align="center"><strong>栏 目 管 理</strong></td>
  </tr>
  <tr class="tdbg"> 
    <td width="70" height="30"><strong>管理导航：</strong></td>
    <td height="30"><a href="Admin_ClassManage.asp">栏目管理首页</a> | <a href="Admin_ClassAdd.asp">添加一级栏目</a>&nbsp;|&nbsp;<a href="Admin_ClassOrder.asp">一级栏目排序</a>&nbsp;|&nbsp;<a href="Admin_ClassOrderN.asp">N级栏目排序</a>&nbsp;|&nbsp;<a href="Admin_ClassReset.asp">复位所有栏目</a>&nbsp;|&nbsp;<a href="Admin_ClassUnite.asp">栏目合并</a></td>
  </tr>
</table>
<form name="form1" method="post" action="Admin_ClassAdd.asp" onsubmit="return check()">
  <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
    <tr class="title"> 
      <td height="22" colspan="2" align="center"><strong>添 加 栏 目</strong></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>所属栏目：</strong><br>
        不能指定为外部栏目 </td>
      <td> <select name="ParentID">
          <%call Admin_ShowClass_Option(0,ParentID)%>
        </select></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目名称：</strong></td>
      <td><input name="ClassName" type="text" size="37" maxlength="20"></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目说明：<br>
        </strong> 鼠标移至栏目名称上时将显示设定的说明文字（不支持HTML）</td>
      <td><textarea name="Readme" cols="30" rows="4" id="Readme"></textarea></td>
    </tr>
    <tr class="tdbg">
      <td><strong>是否是推荐栏目：</strong><br>
        推荐栏目将在首页及此栏目的父栏目上显示文章列表</td>
      <td><input name="IsElite" type="radio" value="Yes" checked>
        是&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" name="IsElite" value="No">
        否 </td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目配色模板：</strong><br>
        相关模板中包含CSS、颜色、图片等信息</td>
      <td><%call Admin_ShowSkin_Option(SkinID)%></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>版面设计模板：</strong><br>
        相关模板中包含了版面设计的版式等信息，如果是自行添加的设计模板，可能会导致“栏目配色模板”失效。 </td>
      <td><%call Admin_ShowLayout_Option(2,LayoutID)%></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目图片地址：</strong><br>
        图片会显示在栏目前面。注意图片大小。</td>
      <td><input name="ClassPicUrl" type="text" id="ClassPicUrl" size="37" maxlength="255">
        （预留功能）</td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目编辑：</strong><br>
        多个编辑请用“|”分隔，如：3NIAO|dilys|sws2000<br>
        无需添加“文章总编”以上级别的管理员<br>
        管理员权限采用权限继承制度</td>
      <td><input name="ClassMaster" type="text" id="ClassMaster" size="37" maxlength="100"> 
      </td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目链接地址：</strong><br>
        如果想将栏目链接到外部地址，请输入完整的URL地址，否则请保持为空。</td>
      <td><input name="LinkUrl" type="text" id="LinkUrl" size="37" maxlength="255"></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目浏览权限：</strong><br>
        只有具有相应权限的人才能浏览此栏目中的文章。</td>
      <td><select name="BrowsePurview" id="BrowsePurview">
          <option value="9999" <%if BrowsePurview=9999 then response.write " selected"%>>游客</option>
          <option value="999" <%if BrowsePurview=999 then response.write " selected"%>>注册用户</option>
          <option value="99" <%if BrowsePurview=99 then response.write " selected"%>>收费用户</option>
          <option value="9" <%if BrowsePurview=9 then response.write " selected"%>>VIP用户</option>
          <option value="5" <%if BrowsePurview=5 then response.write " selected"%>>管理员</option>
        </select></td>
    </tr>
    <tr class="tdbg"> 
      <td width="350"><strong>栏目发表文章权限：</strong><br>
        只有具有相应权限的人才能在此栏目中发表文章。</td>
      <td><select name="AddPurview" id="AddPurview">
          <option value="999" <%if AddPurview=999 then response.write " selected"%>>注册用户</option>
          <option value="99" <%if AddPurview=99 then response.write " selected"%>>收费用户</option>
          <option value="9" <%if AddPurview=9 then response.write " selected"%>>VIP用户</option>
          <option value="5" <%if AddPurview=5 then response.write " selected"%>>管理员</option>
        </select></td>
    </tr>
    <tr class="tdbg"> 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="Add"> 
        <input name="Add" type="submit" value=" 添 加 " <%if SkinCount=0 or LayoutCount=0 then response.write " disabled"%> style="cursor:hand;"> 
        &nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='Admin_ClassManage.asp'" style="cursor:hand;"> 
        <%if SkinCount=0 then response.write "<li><font color=red>请先添加栏目配色模板</font></li>"
		if SkinCount=0 then response.write "<li><font color=red>请先添加栏目版面设计模板</font></li>" %></td>
    </tr>
  </table>
</form>
</body>
</html>
<%
end sub
%>
