<%@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"-->
<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 ConfirmUnite()
{
  if (document.myform.ClassID.value==document.myform.TargetClassID.value)
  {
    alert("请不要在相同栏目内进行操作！");
	document.myform.TargetClassID.focus();
	return false;
  }
  if (document.myform.TargetClassID.value=="")
  {
    alert("目标栏目不能指定为含有子栏目的栏目！");
	document.myform.TargetClassID.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> 
<br> 
<% 
dim Action,FoundErr,ErrMsg 
Action=trim(request("Action")) 
if Action="Unite" then 
	call UniteClass() 
else 
	call main() 
end if 
if FoundErr=True then 
	WriteErrMsg() 
end if 
call CloseConn() 
 
sub main()
%> 
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" class="border">
  <tr class="title"> 
    <td height="22" colspan="3" align="center"><strong>栏 目 合 并</strong></td>
  </tr>
  <tr class="tdbg"> 
    <td height="100"><form name="myform" method="post" action="Admin_ClassUnite.asp" onSubmit="return ConfirmUnite();">
        &nbsp;&nbsp;将栏目 
        <select name="ClassID" id="ClassID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        合并到
        <select name="TargetClassID" id="TargetClassID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        <br> <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        <input name="Action" type="hidden" id="Action" value="Unite">
        <input type="submit" name="Submit" value=" 合并栏目 " style="cursor:hand;">
        &nbsp;&nbsp; 
        <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='Admin_ClassManage.asp'" style="cursor:hand;">
      </form>
	</td>
  </tr>
  <tr class="tdbg"> 
    <td height="60"><strong>注意事项：</strong><br>
      &nbsp;&nbsp;&nbsp;&nbsp;所有操作不可逆，请慎重操作！！！<br>
      &nbsp;&nbsp;&nbsp;&nbsp;不能在同一个栏目内进行操作，不能将一个栏目合并到其下属栏目中。目标栏目中不能含有子栏目。<br>
      &nbsp;&nbsp;&nbsp;&nbsp;合并后您所指定的栏目（或者包括其下属栏目）将被删除，所有文章将转移到目标栏目中。</td>
  </tr>
</table> 
</body> 
</html> 
<% 
end sub 

sub UniteClass()
	dim ClassID,TargetClassID,ParentPath,iParentPath,Depth,iParentID,Child,PrevID,NextID
	dim rs,trs,i,SuccessMsg
	ClassID=trim(request("ClassID"))
	TargetClassID=trim(request("TargetClassID"))
	if ClassID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定要合并的栏目！</li>"
	else
		ClassID=CLng(ClassID)
	end if
	if TargetClassID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请指定目标栏目！</li>"
	else
		TargetClassID=CLng(TargetClassID)
	end if
	if ClassID=TargetClassID then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>请不要在相同栏目内进行操作</li>"
	end if
	if FoundErr=True then
		exit sub
	end if
	'判断目标栏目是否有子栏目，如果有，则报错。
	set rs=conn.execute("select Child from ArticleClass where ClassID=" & TargetClassID)
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>目标栏目不存在，可能已经被删除！</li>"
	else
		if rs(0)>0 then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>目标栏目中含有子栏目，不能合并！</li>"
		end if
	end if
	if FoundErr=True then
		exit sub
	end if

	'得到当前栏目信息
	set rs=conn.execute("select ClassID,ParentID,ParentPath,PrevID,NextID,Depth from ArticleClass where ClassID="&ClassID)
	iParentID=rs(1)
	Depth=rs(5)
	if iParentID=0 then
		ParentPath=rs(0)
	else
		ParentPath=rs(2) & "," & rs(0)
	end if
	iParentPath=rs(0)
	PrevID=rs(3)
	NextID=rs(4)
	
	'判断是否是合并到其下属栏目中
	set rs=conn.execute("select ClassID from ArticleClass where ClassID="&TargetClassID&" and ParentPath like '"&ParentPath&"%'")
	if not (rs.eof and rs.bof) then
		FoundErr=true
		ErrMsg=ErrMsg & "<br><li>不能将一个栏目合并到其下属子栏目中</li>"
		exit sub
	end if
	
	'得到当前栏目的下属栏目ID
	set rs=conn.execute("select ClassID from ArticleClass where ParentPath like '"&ParentPath&"%'")
	i=0
	if not (rs.eof and rs.bof) then
		do while not rs.eof
			iParentPath=iParentPath & "," & rs(0)
			i=i+1
			rs.movenext
		loop
	end if
	if i>0 then
		ParentPath=iParentPath
	else
		ParentPath=ClassID
	end if
	
	'先修改上一栏目的NextID和下一栏目的PrevID
	if PrevID>0 then
		conn.execute "update ArticleClass set NextID=" & NextID & " where ClassID=" & PrevID
	end if
	if NextID>0 then
		conn.execute "update ArticleClass set PrevID=" & PrevID & " where ClassID=" & NextID
	end if
	
	'更新文章及评论所属栏目
	conn.execute("update Article set ClassID="&TargetClassID&" where ClassID in ("&ParentPath&")")
	conn.execute("update Comment set ClassID="&TargetClassID&" where ClassID in ("&ParentPath&")")
	
	'删除被合并栏目及其下属栏目
	conn.execute("delete from ArticleClass where ClassID in ("&ParentPath&")")
	
	'更新其原来所属栏目的子栏目数，排序相当于剪枝而不需考虑
	if Depth>0 then
		conn.execute("update ArticleClass set Child=Child-1 where ClassID="&iParentID)
	end if
	
	SuccessMsg="栏目合并成功！已经将被合并栏目及其下属子栏目的所有数据转入目标栏目中。<br><br>同时删除了被合并的栏目及其子栏目。"
	call WriteSuccessMsg(SuccessMsg)
	set rs=nothing
	set trs=nothing
end sub

%> 
