自己修改的动力无限分类修改代码
郁闷的是动力无限分类OrderID的排序问题好奇怪.改完了才发现,OrderID是按照RootID来排序的(即如果栏目的RootID相同,那么OrderID不论层次的一直递增.).这样子用一句seletc * from Class Order by RootID,OrderID就可以按照栏目一级一级的显示出来.
'系统页面加载初始化参数设定
ClassID=request("ClassID")
if ClassID="" or not isnumeric(ClassID) then
response.Write("<script>alert('传递参数错误!系统即将返回!');history.go(-1);</script>")
response.End()
end if
'类别列表读取函数
Function ShowClassFormOption(sChannelID,CurrentID,sClassDepth)
dim rsClass,sqlClass,sTemp,tmpClassDepth,i,n
dim arrShowLine(10)
for i=0 to ubound(arrShowLine)
arrShowLine(i)=False
next
sTemp="<option value=""0"""
if CurrentID=0 then sTemp=sTemp & " selected"">顶级类别</option>"
sqlClass="Select ClassID,ClassName,ClassDepth,ChannelID,RootID,OrderID From SoftClass "
if sClassDepth>0 then
sqlClass=sqlClass & " where ClassDepth<"&sClassDepth&" order by ParentIDPath,OrderID"
else
sqlClass=sqlClass & " order by ParentIDPath,OrderID"
end if
set rsClass=conn.Execute(sqlClass)
if rsClass.bof and rsClass.bof then
ShowClassFormOption = sTemp & "<option value="""">添加顶级类别</option>"
rsClass.close
set rsClass=Nothing
Exit Function
else
ClassRecord=rsClass.GetRows(-1)
'获取最后一行记录
'数据字段1,类别ID,ClassID
'数据字段2,类别名称,ClassName
'数据字段3,类别深度,ClassDepth
'数据字段4,类别ID,Class
rsClass.close
set rsClass=Nothing
End if
Dim sChecked, tempClassName
For i=0 to Ubound(ClassRecord,2)
tmpClassDepth=ClassRecord(2,i)
'数据字段3,类别深度,ClassDepth
if ClassRecord(3,i)>0 then
arrShowLine(tmpClassDepth)=True
else
arrShowLine(tmpClassDepth)=False
end if
sChecked = "" : tempClassName = ""
if ClassRecord(0,i)=CurrentID then sChecked = " selected"
if tmpClassDepth>0 then
for n=1 to tmpClassDepth
tempClassName = tempClassName & " "
if n=tmpClassDepth then
if ClassRecord(3,i)>0 then
tempClassName = tempClassName & "├ "
else
tempClassName = tempClassName & "└ "
end if
else
if arrShowLine(n)=True then
tempClassName = tempClassName & "│"
else
tempClassName = tempClassName & " "
end if
end if
next
end if
tempClassName = tempClassName & ClassRecord(1,i)
sTemp=sTemp & "<option value=""" & ClassRecord(0,i) & """ "&sChecked&">" & tempClassName & "</option>"
Next
ShowClassFormOption=sTemp
ClassRecord=Empty
End Function
if request("action")="EditSave" then
ChannelID=cint(trim(Request.Form("ChannelID")))
ParentClassID=trim(Request.Form("ParentID"))
ClassName=trim(Request.Form("ClassName"))
ClassFolderName=trim(Request.Form("ClassFolderName"))
ClassMemo=trim(Request.Form("ClassMemo"))
ClassKeyWords=trim(Request.Form("ClassKeyWords"))
ClassDescription=trim(Request.Form("ClassDescription"))
IsCreateFolder=trim(Request.Form("IsCreateFolder"))
oldParentClassIDPath=trim(Request.Form("oldParentIDPath"))
if ClassName="" Then
response.write("<script>alert('系统出错!请返回填写栏目类别名称');history.go(-1);</script>")
response.End()
end if
if ClassFolderName="" Then
response.write("<script>alert('系统出错!请返回填写栏目类别目录文件夹');history.go(-1);</script>")
response.End()
end if
if ClassMemo="" Then
response.write("<script>alert('系统出错!请返回填写栏目类别说明');history.go(-1);</script>")
response.End()
end if
if ClassKeyWords="" Then
response.write("<script>alert('系统出错!请返回填写栏目类别搜索关键字');history.go(-1);</script>")
response.End()
end if
if ClassDescription="" Then
response.write("<script>alert('系统出错!请返回填写栏目类别搜索描述词');history.go(-1);</script>")
response.End()
end if
If IsValidWindowsFolderName(ClassFolderName)=False Then
response.Write("<script language=javascript>alert('目录名格式不正确,按确定返回重新填写');history.back();</script>")
response.End()
end if
ClassFolderName=Replace(PCase(ClassFolderName)," ","-")
'查询网站频道项目
set rs=server.CreateObject("Adodb.recordset")
sql="select * from SystemChannelList Where ChannelID="&ChannelID
rs.open sql,conn,1,3
if rs.eof and rs.bof then
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write("<script>alert('无此数据,系统即将返回!');location.href='ClassList.asp';</script>")
response.End()
else
ChannelName =rs("ChannelName")
ChannelFolderUrl=rs("ChannelFolderUrl")
ChannelFolderPath=rs("ChannelFolderPath")
end if
rs.close
set rs=nothing
'查询父类别ID是否改变
set rsClass=server.CreateObject("Adodb.recordset")
sqlClass="select * from SoftClass where ClassID="&ClassID
rsClass.open sqlClass,conn,1,3
if rsClass.eof and rsClass.bof then
response.write("<script>alert('错误!未查询到对应类别!系统即将返回');location.href='ClassList.asp';</script>")
response.End()
else
if rsClass("ParentID")<>ParentClassID then '更改了所属栏目,则要做一系列检查
if ParentClassID=rsClass("ClassID") then
response.write("<script>alert('所属栏目不能为自己!!系统即将返回');location.href='ClassList.asp';</script>")
response.End()
end if
'判断所指定的栏目是否为外部栏目或本栏目的下属栏目
if rsClass("ParentID")=0 then
if ParentClassID>0 then
set trs=conn.execute("select RootID From SoftClass where ClassID="&ParentClassID)
if trs.bof and trs.eof then
response.write("<script>alert('找不到移动到的栏目!系统即将返回');location.href='ClassList.asp';</script>")
response.End()
else
if rsClass("RootID")=trs(0) then
response.write("<script>alert('不能指定该栏目的下属栏目作为所属栏目!系统即将返回');location.href='ClassList.asp';</script>")
response.End()
end if
end if
trs.close
set trs=nothing
end if
else
set trs=conn.execute("select ClassID From SoftClass where ParentIDPath like '"&rsClass("ParentIDPath")&"," & rsClass("ClassID") & "%' and ClassID="&ParentClassID)
if not (trs.eof and trs.bof) then
response.write("<script>alert('不能指定该栏目的下属栏目作为所属栏目!系统即将返回');location.href='ClassList.asp';</script>")
response.End()
end if
trs.close
set trs=nothing
end if
end if
'读取类别相关历史数据
if rsClass("ParentID")=0 then
oldParentID=rsClass("ClassID")
oldParentClassID=0 '判断以前是否是顶级栏目
else
oldParentID=rsClass("ParentID")
oldParentClassID=rsClass("ParentID") '判断以前是否是顶级栏目
end if
oldClassDepth=rsClass("ClassDepth")
oldChild=rsClass("Child")
oldRootID=rsClass("RootID")
oldParentIDPath=rsClass("ParentIDPath")
oldPrevID=rsClass("PrevID")
oldNextID=rsClass("NextID")
oldOrderID=rsClass("OrderID")
rsClass.close
set rsClass=nothing
if ParentClassID=0 then
'查询父类相关数据
ParentClassName="" '父类名
ParentClassFolderName="" '父类目录名
ClassFolderPath=ClassFolderName
ClassLocalPath=ChannelFolderPath&"/"&ClassFolderPath
ClassFolderUrl=ChannelFolderUrl&"/"&Replace(ClassFolderPath,"/","/")
else
'查询父类相关数据
set rs=server.CreateObject("Adodb.recordset")
sql="select * from SoftClass where ClassID="&ParentClassID
rs.open sql,conn,1,3
if rs.eof and rs.bof then
response.write("<script>alert('未能查询到父类类别对应的数据!系统即将返回上一页');history.go(-1);</script>")
response.End()
else
ParentClassID=rs("ClassID")
ParentClassName=rs("ClassName") '父类名
ParentClassFolderName=rs("ClassFolderName") '父类目录名
ParentClassFolderPath=rs("ClassFolderPath") '父类目录名
ParentClassFolderUrl=rs("ClassFolderUrl") '父类访问路径
end if
rs.close
set rs=nothing
'根据父类数据得到当前路径相关参数
ClassFolderPath=ParentClassFolderPath&"/"&ClassFolderName
ClassLocalPath=ChannelFolderPath&"/"&ClassFolderPath
ClassFolderUrl=ChannelFolderUrl&"/"&Replace(ClassFolderPath,"/","/")
end if
'创建对应的类别目录
if IsCreateFolder=1 then
CreateFolder ClassLocalPath,IsCreateFolder
end if
'假如更改了所属栏目
'则要更新其原来所属栏目信息,包括深度、父级ID、栏目数、排序等数据
if clng(oldParentID)<>ParentClassID and not (oldParentClassID=0 and ParentClassID=0) then
'不为一级目录且非原来父类(更改了所属栏目)
'更新原来同一父栏目的上一个栏目的NextID和下一个栏目的PrevID
if oldPrevID>0 then
conn.execute "update SoftClass set NextID=" & oldNextID & " where ClassID=" & oldPrevID
end if
if oldNextID>0 then
conn.execute "update SoftClass set PrevID=" & oldPrevID & " where ClassID=" & oldNextID
end if
MaxRootID=conn.execute("select max(RootID) as List1 From SoftClass")("List1")'最后顶级分类记录
'如果原来不是一级分类改成一级分类
if oldParentClassID>0 and ParentClassID=0 then '如果原来不是一级分类改成一级分类
'得到上一个一级分类栏目
sql="select ClassID,NextID,RootID,ClassDepth from SoftClass where RootID=" & MaxRootID & " and ClassDepth=0"
set rs=server.CreateObject("Adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0) '得到新顶级分类栏目的PrevID值
rs(1)=ClassID '更新前一个顶级分类栏目的NextID值
rs.update
rs.close
set rs=nothing
ClassCount=conn.execute("select count(*) as List1 From SoftClass where ParentID=0")("List1")+1
'更新当前栏目数据
conn.execute("update SoftClass set ClassDepth=0,OrderID="&ClassCount&",RootID=0,ParentID=0,ParentIDPath='0',PrevID=" & PrevID & ",NextID=0 where ClassID="&ClassID)
'如果有下属栏目,则更新其下属栏目数据。
'下属栏目的排序不需考虑,只需更新下属栏目深度和一级排序ID(RootID)数据
if oldChild>0 then
i=0
tempParentIDPath=oldParentIDPath & ","
set rs=conn.execute("select * From SoftClass where ParentIDPath like '%"&tempParentIDPath & ClassID&"%'")
do while not rs.eof
i=i+1
mParentIDPath=replace(rs("ParentIDPath"),"0,"&ClassID)
conn.execute("update SoftClass set ClassDepth=ClassDepth-"&oldClassDepth&",RootID="&ClassID&",ParentIDPath='"&mParentIDPath&"' where ClassID="&rs("ClassID"))
rs.movenext
loop
rs.close
set rs=nothing
end if
'更新其原来所属栏目的栏目数,排序相当于剪枝而不需考虑
conn.execute("update SoftClass set child=child-1 where ClassID="&oldParentClassID)
'如果是将一个分栏目移动到其他分栏目下
elseif oldParentClassID>0 and ParentClassID>0 then '如果是将一个分栏目移动到其他分栏目下
'获得新的父类栏目的相关信息
set trs=conn.execute("select * From SoftClass where ClassID="&ParentClassID)
if trs("Child")>0 then
'得到与本栏目同级的最后一个栏目的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SoftClass where ParentID=" &ParentClassID)
PrevOrderID=rsPrevOrderID(0)
'得到与本栏目同级的最后一个栏目的ClassID
sql="select ClassID,NextID,ParentID,OrderID from SoftClass where ParentID=" & ParentClassID & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0) '得到新的PrevID
rs(1)=ClassID '更新上一个栏目的NextID的值
rs.update
rs.close
set rs=nothing
'得到同属于一个父栏目但比本栏目级数大的子栏目的最大OrderID,如果比前一个值大,则改用这个值。
'最大的所属父类子栏目OrderID可能是自己
set rsPrevOrderID=conn.execute("select Max(OrderID) From SoftClass where ParentIDPath like '" & trs("ParentIDPath") & "," & ParentClassID & ",%'")
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
PrevOrderID=trs("OrderID")
end if
'更新当前栏目数据
conn.execute("update SoftClass set ClassDepth="&trs("ClassDepth")&"+1,OrderID="&PrevOrderID&"+1,RootID="&trs("RootID")&",ParentID="&ParentClassID&",ParentIDPath='" & trs("ParentIDPath") & "," & ParentClassID & "',PrevID=" & PrevID & ",NextID=0 where ClassID="&ClassID)
'得到当前栏目的下属子栏目数
tempParentIDPath=oldParentIDPath & ","
'如果有子栏目则更新子栏目数据,深度为原来的相对深度加上当前所属栏目的深度
set rs=conn.execute("select * From SoftClass where ParentIDPath like '%"&tempParentIDPath&ClassID&"%' order by OrderID")
i=1
do while not rs.eof
i=i+1
ParentClassIDPath=trs("ParentIDPath") & "," & ParentClassID & "," & replace(rs("ParentIDPath"),tempParentIDPath,"")
conn.execute("update SoftClass set ClassDepth=ClassDepth-"&oldClassDepth&"+"&trs("ClassDepth")&"+1,OrderID="&PrevOrderID&"+"&i&",RootID="&trs("RootID")&",ParentIDPath='"&ParentClassIDPath&"' where ClassID="&rs("ClassID"))
rs.movenext
loop
rs.close
set rs=nothing
trs.close
set trs=nothing
'更新新的所属上级栏目的子栏目数
conn.execute("update SoftClass set child=child+1 where ClassID="&ParentClassID)
'更新原来所属上级栏目的子栏目数
conn.execute("update SoftClass set child=child-1 where ClassID="&oldParentClassID)
'更新原来父类下的同级栏目的排序
ClassCount=conn.execute("select count(*) as List1 From SoftClass where ParentIDPath like '%"&oldParentIDPath &"%'")("List1")
if not isnull(ClassCount) then
'在获得移动过来的栏目数后更新排序在指定栏目之后的栏目排序数据
conn.execute("update SoftClass set OrderID=OrderID-1 where ParentIDPath='" &tempParentIDPath&ClassID& "' and OrderID>" & oldOrderID)
end if
'如果原来是一级栏目改成其他栏目的下属栏目
else
'如果原来是一级栏目改成其他栏目的下属栏目
'得到移动的栏目总数
ClassCount=conn.execute("select count(*) as List1 From SoftClass where RootID="&oldRootID)("List1")
'获得目标栏目的相关信息(PrevID,PrevOrderID)
set trs=conn.execute("select * From SoftClass where ClassID="&ParentClassID)
if trs("Child")>0 then
'得到与本栏目同级的最后一个栏目的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SoftClass where ParentID=" &ParentClassID )
PrevOrderID=rsPrevOrderID(0)
sql="select ClassID,NextID,ParentID,OrderID from SoftClass where ParentID=" &ParentClassID& " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)
rs(1)=ClassID
rs.update
set rs=nothing
'得到同一父栏目但比本栏目级数大的子栏目的最大OrderID,如果比前一个值大,则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SoftClass where ParentIDPath ='" & trs("ParentIDPath") & "," & ParentClassID & "'")
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
PrevOrderID=0
end if
'在获得移动过来的栏目数后更新排序在指定栏目之后的栏目排序数据
conn.execute("update SoftClass set PrevID=" & PrevID & ",NextID=0 where ClassID=" & ClassID)
tempParentIDPath=trs("ClassIDPath")
set rs=conn.execute("select * From SoftClass where ParentID="&oldParentClassID&" order by OrderID")
i=0
do while not rs.eof
i=i+1
if rs("ParentID")=0 then
conn.execute("update SoftClass set ClassDepth=ClassDepth+"&trs("ClassDepth")&"+1,OrderID="&PrevOrderID&"+"&i&",RootID="&trs("ClassID")&",ParentIDPath='"&trs("ClassIDPath")&"',ParentID="&ParentClassID&" where ClassID="&rs("ClassID"))
else
tempParentIDPath=trs("ParentIDPath") & "," & ParentClassID & "," & replace(rs("ParentIDPath"),"0,","")
conn.execute("update SoftClass set ClassDepth=ClassDepth+"&trs("ClassDepth")&"+1,RootID="&trs("RootID")&",ParentIDPath='"&tempParentIDPath&"' where ClassID="&rs("ClassID"))
end if
rs.movenext
loop
rs.close
set rs=nothing
trs.close
set trs=nothing
'更新所指向的上级栏目栏目数
conn.execute("update SoftClass set child=child+1 where ClassID="&ParentClassID)
end if
end if
end if
'获取数据库参数
'添加新数据并修改相关字段数值
UserIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserIP = "" Then UserIP = Request.ServerVariables("REMOTE_ADDR")
UserFrom=Cstr(Request.ServerVariables("HTTP_REFERER"))
CorrectSqlClassFolderName=replace(ClassFolderName,"'","''")
CorrectSqlClassName=replace(ClassName,"'","''")
set rs=server.CreateObject("Adodb.recordset")
sql="select * from SoftClass where ClassID="&ClassID
rs.open sql,conn,3,3
if rs.eof and rs.bof then
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write("<script>alert('栏目类别数据不存在!系统即将返回');history.go(-1);</script>")
response.end
else
rs("ChannelID")=ChannelID
rs("ChannelName")=ChannelName
rs("ChannelFolderPath")=ChannelFolderPath
rs("ChannelFolderUrl")=ChannelFolderUrl
if ParentClassID=0 then
rs("ParentName")=""
else
rs("ParentName")=ParentClassName
end if
rs("ClassName")=ClassName
rs("ClassFolderName")=ClassFolderName
rs("ClassFolderPath")=ClassFolderPath
rs("ClassLocalPath")=ClassLocalPath
rs("ClassMemo")=ClassMemo
rs("ClassKeyWords")=ClassKeyWords
rs("ClassDescription")=ClassDescription
rs("IsCreateFolder")=IsCreateFolder
rs("UpdateUser")=Session("ManagerUserName")
rs("UpdateTime")=now()
rs("UpdateIP")=UserIP
rs("UpdateFrom")=UserFrom
rs.update
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write("<script>alert('栏目类别"&ClassName&"修改完成!系统即将返回列表');location.href='ClassList.asp';</script>")
response.End()
end if