<%
Class ClassClass
	Private Conn  
	Private IForm  
	
	Public Property Let TForm(ByRef ExteriorForm)
		if IsObject(ExteriorForm) then
			Set IForm = ExteriorForm
		else
			Class_Terminate()
		end if
	End Property
	
	Private Sub Class_initialize() 
		Dim DBC
		Set DBC = New DataBaseClass
		Set Conn = DBC.OpenConnection()
		Set DBC = Nothing
	End Sub 
	
	Private Sub Class_Terminate()
		Set Conn=Nothing
	End Sub 
	
	Private Function CheckClass(ClassID,ParentID,ClassEName,ClassCName,ClassTemp,SaveFilePath,FileExtName)
		Dim TempSqlClass,RsTempClassObj
		CheckClass = ""
		if ClassID <> "" then
			if (Conn.Execute("Select * from NewsClass where ClassID = '" & ClassID & "'").Eof) then 
				CheckClass = CheckClass & "栏目不存在，可能已经被删除"  
				Exit Function
			end if
		end if
		if ParentID <> "0" then
			if (Conn.Execute("Select * from NewsClass where ClassID = '" & ParentID & "'").Eof) then 
				CheckClass = CheckClass & "父栏目不存在，可能已经被删除"  
				Exit Function
			end if
		end if
		if ClassEName = "" then
		   CheckClass = CheckClass & "栏目的英文名没有填写！"
		end if
		if ClassCName = "" then
		   CheckClass = CheckClass & "栏目的中文名没有填写！"
		end if
		if ClassTemp = "" then
		   CheckClass = CheckClass & "栏目的模板没有填写！"
		end if
		if SaveFilePath = "" then
		   CheckClass = CheckClass & "栏目的保存路径没有填写！"
		end if
		if FileExtName = "" then
		   CheckClass = CheckClass & "栏目的生成扩展名没有填写！"
		end if
		if ClassID = "" then
			TempSqlClass = "Select ClassEName from NewsClass where ClassEName='"& ClassEName & "'"
		else
			TempSqlClass = "Select ClassEName from NewsClass where ClassEName='"& ClassEName & "' and ClassID<>'" & ClassID & "'"
		end if
		if Not (Conn.Execute(TempSqlClass).Eof) then
		   CheckClass = CheckClass & "栏目的英文名已经存在！"
		end if
	End Function
	
	Public Function AddAndModifyClass()
		Dim RsClassObj
		Dim SqlClass,ErrStr,EditTF,TTempSaveFilePath,TeempFileExtName
		Dim ParentID,ClassEName,ClassCName,ClassTemp,ClassID,Contribution,AddTime,SaveFilePath,FileExtName,BrowPop
		Dim TempSysRootDir
		if SysRootDir = "" then
			TempSysRootDir = ""
		else
			TempSysRootDir = "/" & SysRootDir
		end if
		AddTime = IForm("AddTime")
		ClassID = IForm("ClassID")
		ParentID = IForm("ParentID")
		ClassEName = IForm("ClassEName")
		ClassCName = IForm("ClassCName")
		ClassTemp = IForm("ClassTemp")
		Contribution = IForm("Contribution")
		SaveFilePath = IForm("SaveFilePath") 
		FileExtName = IForm("FileExtName")
		BrowPop = IForm("BrowPop")
		if BrowPop <> "" then
			FileExtName = "asp"
		end if
		if ClassID = "" then
			EditTF = False
		else
			EditTF = true
		end if
		ErrStr = CheckClass(ClassID,ParentID,ClassEName,ClassCName,ClassTemp,SaveFilePath,FileExtName)
		if ErrStr <> "" then
			AddAndModifyClass= ErrStr 
			Exit Function
		end if
		Set RsClassObj = Server.CreateObject("ADODB.Recordset")
		if ClassID = "" then
			ClassID = GetRandomID18()
			SqlClass = "select * from NewsClass"
			RsClassObj.Open SqlClass,Conn,3,3
			RsClassObj.AddNew
		else
			ClassID = ClassID
			SqlClass = "select * from NewsClass where ClassID='" & ClassID & "'"
			RsClassObj.Open SqlClass,Conn,3,3
			TTempSaveFilePath = RsClassObj("SaveFilePath")
			TeempFileExtName = RsClassObj("FileExtName")
		end if
		RsClassObj("ClassID") = ClassID
		RsClassObj("ClassEName") = ClassEName
		RsClassObj("ClassCName") = ClassCName
		RsClassObj("ParentID") = ParentID
		RsClassObj("SaveFilePath") = SaveFilePath
		RsClassObj("FileExtName") = FileExtName
		if BrowPop <> "" then
			RsClassObj("BrowPop") = CInt(BrowPop)
		else
			RsClassObj("BrowPop") = 0
		end if
		if AddTime <> "" then
			RsClassObj("AddTime") = AddTime
		else
			RsClassObj("AddTime") = Now
		end if
		RsClassObj("ChildNum") = 0
		RsClassObj("ClassTemp") = ClassTemp
		if Contribution = "1" then
			RsClassObj("Contribution") = 1
		else
			RsClassObj("Contribution") = 0
		end if
		RsClassObj.UpDate
		RsClassObj.Close
		Conn.Execute("Update NewsClass Set ChildNum = ChildNum + 1 where ClassID = '" & ParentID & "'")
		Set RsClassObj = Nothing
		If ClassID <> "" then
			Dim MyFile
			Set MyFile=Server.CreateObject("Scripting.FileSystemObject")
			If Cstr(TTempSaveFilePath) <> Cstr(SaveFilePath) then
				If MyFile.FolderExists(Server.Mappath(TempSysRootDir&TTempSaveFilePath&"/"&ClassEName)) then
					MyFile.DeleteFolder(Server.Mappath(TempSysRootDir&TTempSaveFilePath&"/"&ClassEName))
				End if
			End If
			If Cstr(TeempFileExtName) <> Cstr(FileExtName) then
				If MyFile.FileExists(Server.Mappath(TempSysRootDir&TTempSaveFilePath&"/"&ClassEName)&"/index."&TeempFileExtName) then
					MyFile.DeleteFile(Server.Mappath(TempSysRootDir&TTempSaveFilePath&"/"&ClassEName)&"/index."&TeempFileExtName)
				End if
			End If
			Set MyFile = Nothing
		End If
		if err.Number = 0 then
			AddAndModifyClass = ""  
			Exit Function
		else
			if EditTF then
				AddAndModifyClass = "栏目修改失败"  
				Exit Function
			else
				AddAndModifyClass = "栏目添加失败"  
				Exit Function
			end if
		end if
	End Function
	
	Public Function MoveClass(SourceClassID,ObjectClassID)
		Dim RsClassObj,SqlClass,SourceClassParentID,TempArray,LoopVar
		TempArray = Split(SourceClassID,",")
		for LoopVar = LBound(TempArray) to UBound(TempArray)
			SqlClass = "Select ParentID from NewsClass where ClassID='" & TempArray(LoopVar) & "'"
			Set RsClassObj = Conn.Execute(SqlClass)
			if Not RsClassObj.Eof then
				SourceClassParentID = RsClassObj("ParentID")
			else
				SourceClassParentID = ""
			end if
			MoveClass = "Update NewsClass Set ParentID = '" & ObjectClassID & "' where ClassID = '" & TempArray(LoopVar) & "';"
			Conn.Execute(MoveClass)
			if SourceClassParentID <> "" then
				MoveClass = "Update NewsClass Set ChildNum=ChildNum-1 where ClassID = '" & SourceClassParentID & "';"
				Conn.Execute(MoveClass)
			end if
			MoveClass = "Update NewsClass Set ChildNum=ChildNum+1 where ClassID = '" & ObjectClassID & "';"
			Conn.Execute(MoveClass)
		Next
	End Function
	
	Public Function CopyClass(SourceClassID,ObjectClassID)
		Dim RsSourceClassObj,SourceClassSql,RsObjectClassObj,ObjectClassSql,i
		SourceClassSql = "Select * from News where ClassID='" & SourceClassID & "'"
		ObjectClassSql = "Select * from News where ClassID='" & ObjectClassID & "'"
		Set RsObjectClassObj = Server.CreateObject("ADODB.Recordset")
		RsObjectClassObj.Open ObjectClassSql,Conn,3,3
		Set RsSourceClassObj = Conn.Execute(SourceClassSql)
		if Not RsSourceClassObj.Eof then
			
		else
		
		end if
		Set RsSourceClassObj = Nothing
		Set RsObjectClassObj = Nothing
	End Function
End Class
%>
