<%@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"-->
<%
dim Action,ClassID,FoundErr,ErrMsg
Action=trim(request("Action"))
ClassID=trim(Request("ClassID"))
if ClassID="" then
	FoundErr=True
	ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
else
	ClassID=CLng(ClassID)
end if
if FoundErr<>True then
	if Action="Del" then
		call DeleteClass(ClassID)
	elseif Action="Clear" then
		call ClearClass(ClassID)
	else
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
	end if
end if
if FoundErr=True then
	call WriteErrMsg()
end if
call CloseConn()      

sub DeleteClass(ClassID)
	dim sql,rs,PrevID,NextID
	
	sql="select ClassID,RootID,Depth,ParentID,Child,PrevID,NextID From ArticleClass where ClassID="&ClassID
	set rs=server.CreateObject ("Adodb.recordset")
	rs.open sql,conn,1,3
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>栏目不存在，或者已经被删除</li>"
	else
		if rs("Child")>0 then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>该栏目含有子栏目，请删除其子栏目后再进行删除本栏目的操作</li>"
		end if
	end if
	if FoundErr=True then
		rs.close
		set rs=nothing
		exit sub
	end if
	PrevID=rs("PrevID")
	NextID=rs("NextID")
	if rs("Depth")>0 then
		conn.execute("update ArticleClass set child=child-1 where ClassID=" & rs("ParentID"))
	end if
	rs.delete
	rs.update
	rs.close
	set rs=nothing
	'删除本栏目的所有文章和评论
	conn.execute("delete from Article where ClassID=" & ClassID)
	conn.execute("delete from Comment where ClassID=" & ClassID)
	
	'修改上一栏目的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
	call CloseConn()
	response.redirect "Admin_ClassManage.asp"
		
end sub

sub ClearClass(ClassID)
	dim strClassID,rs,trs,SuccessMsg
	strClassID=cstr(ClassID)
	set rs=conn.execute("select ClassID,Child,ParentPath from ArticleClass where ClassID=" & ClassID)
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>栏目不存在，或者已经被删除</li>"
		exit sub
	end if
	if rs(1)>0 then
		set trs=conn.execute("select ClassID from ArticleClass where ParentID=" & rs(0))
		do while not trs.eof
			strClassID=strClassID & "," & trs(0)
			trs.movenext
		loop
		trs.close
		set trs=conn.execute("select ClassID from ArticleClass where ParentPath like '" & rs(2) & "," & rs(0) & ",%'")
		do while not trs.eof
			strClassID=strClassID & "," & trs(0)
			trs.movenext
		loop
		trs.close
		set trs=nothing
	end if
	rs.close
	set rs=nothing
	conn.execute("update Article set Deleted=True where ClassID in (" & strClassID & ")")
	conn.execute("delete from Article where ClassID in (" & strClassID & ")")	
	SuccessMsg="此栏目（包括子栏目）的所有文章已经被移到回收站中！"
	call WriteSuccessMsg(SuccessMsg)
end sub
%>


