<%
dim action : action=getForm("action","get")
dim sortType:sortType=getForm("sortType","get")
dim sortTypeName,TagsCategory
Select case action	
	case "del" : DelTags
	case "add" : AddTags
	case "edit" : EditTags
	case "editsave" : EditTagsSave
	case "save" :SaveTags	
	case "order" :updateOrder	
End Select

select case sortType
	case "2"
		sortTypeName ="文章"
		TagsCategory = "C"
	case "3"
		sortTypeName ="产品"
		TagsCategory = "P"
	case "4"
		sortTypeName ="下载"
		TagsCategory = "DL"
	case "5"
		sortTypeName ="招聘"
		TagsCategory = "HR"
	case "6"
		sortTypeName ="相册"
		TagsCategory = "FO"

	case "8"
		sortTypeName = "视频"
		TagsCategory = "VI"
		
end select 
'单篇1,文章2,产品3,下载4,招聘5,相册6,链接7，视频8

Dim TagsID, TagsName, TagsOrder, TagsField	
Dim TagsOptions,TagsDiversification,TagsControlType,TagsNotNull

Sub EditTags
	dim sql,rsObj
	sql = "select * from {prefix}TagsSet where TagsID="&CInt(filterPara((getForm("ID","get"))))
	Set rsObj=conn.Exec(sql,"r1")	
	if rsObj.eof then exit sub
	TagsID = rsObj("TagsID")
	TagsName = rsObj("TagsName")
	TagsField = rsObj("TagsField")
	TagsNotNull = rsObj("TagsNotNull")
	TagsControlType = rsObj("TagsControlType")
	TagsOptions = rsObj("TagsOptions")
	TagsDiversification = rsObj("TagsDiversification")
	TagsOrder = rsObj("TagsOrder")

	TagsCategory = rsObj("TagsCategory")

	rsObj.close	:Set rsObj = nothing
	
End Sub

Sub EditTagsSave
	dim sql,rsObj
TagsField=filterPara(getForm("TagsField","post"))
TagsID=filterPara(getForm("TagsID","post"))
TagsOptions=filterPara(getForm("TagsOptions","post"))
TagsDiversification=filterPara(getForm("TagsDiversification","post"))
TagsControlType=filterPara(getForm("TagsControlType","post"))
TagsName=filterPara(getForm("TagsName","post"))
TagsCategory=filterPara(getForm("TagsCategory","post"))
TagsOrder=filterPara(getForm("TagsOrder","post"))
TagsNotNull=filterPara(getForm("TagsNotNull","post"))



if TagsNotNull = "on" then
TagsNotNull = true
else
TagsNotNull = false
end if
	sql = "select * from {prefix}TagsSet where TagsID="&TagsID
	Set rsObj=conn.Exec(sql,"r1")	
	
	
	'sql = "Alter table Ky_content drop Column "&rsObj("TagsCategory")&"_"&rsObj("TagsField") &""
	'conn.Exec sql,"exe"	
	'sql="ALTER TABLE Ky_Content ADD column "& TagsCategory&"_"& TagsField &" text"
	'conn.Exec sql,"exe"
	sql = "update {prefix}TagsSet set TagsName='"&TagsName&"',TagsCategory='"&TagsCategory&"',TagsOptions='"&TagsOptions&"',TagsControlType='"&TagsControlType&"',TagsOrder="&TagsOrder&",TagsNotNull="&TagsNotNull&" where TagsID="&TagsID
	conn.Exec sql,"exe"	
	alertMsgAndGo "修改成功","Ky_Tags.asp"
End Sub

Sub AddTags 
Dim rsObj,sql,tmpCount,i,fName

TagsField=filterPara(getForm("TagsField","post"))
TagsOptions=filterPara(getForm("TagsOptions","post"))
TagsDiversification=filterPara(getForm("TagsDiversification","post"))
TagsControlType=filterPara(getForm("TagsControlType","post"))
TagsName=filterPara(getForm("TagsName","post"))
TagsCategory=filterPara(getForm("TagsCategory","post"))
TagsOrder=filterPara(getForm("TagsOrder","post"))
TagsNotNull=filterPara(getForm("TagsNotNull","post"))



if TagsNotNull = "on" then
TagsNotNull = 1
else
TagsNotNull = 0
end if

TagsOptions = encode(TagsOptions)

fName = TagsCategory & "_" & TagsField

if isnul(TagsDiversification) then TagsDiversification = 0


 
	if isnul(TagsName) then alertMsgAndGo "参数名称不能为空，请修改","-1"
	if isnul(TagsField) then alertMsgAndGo "字段名称不能为空，请修改","-1"

	sql = "select count(*) from {prefix}TagsSet where TagsField='"&TagsField&"'"
	Set rsObj=conn.Exec(sql,"r1")
	tmpCount = rsObj(0)
	rsObj.close	:Set rsObj = nothing
	
	 
	
	sql = "select count(*) from {prefix}content"
	Set rsObj=conn.Exec(sql,"r1")
	for i=0 to rsObj.Fields.Count-1
		if rsObj.Fields(i).Name = fName then
			tmpCount = tmpCount + 1
		end if
	next
	rsObj.close	:Set rsObj = nothing
	
	if tmpCount > 0 then alertMsgAndGo "字段名称已存在，请修改","-1"
	
'0 文本,1 数字,2 编辑器,3 附件,4 日期,5 颜色,6 单选,7 多选
Select Case CInt(TagsControlType)
	Case 2
	sql = "ALTER TABLE {prefix}Content ADD "&fName&" memo"
	Case Else
	sql = "ALTER TABLE {prefix}Content ADD "&fName&" nvarchar(255)"

End Select
	conn.Exec sql,"exe"	
	if Err then alertMsgAndGo "添加字段失败,请联系管理员","-1"
	
	sql = "insert into {prefix}TagsSet(TagsName,TagsField,TagsOrder,TagsControlType,TagsCategory,TagsOptions,TagsNotNull,TagsDiversification) values('"&TagsName&"','"&TagsField&"',"&TagsOrder&",'"&TagsControlType&"','"&TagsCategory&"','"&TagsOptions&"',"&TagsNotNull&","&TagsDiversification&")"
	conn.Exec sql,"exe"		
	
	alertMsgAndGo "添加成功","Ky_Tags.asp"
End Sub	

Sub TagsList
dim TagsID, TagsName, TagsOrder, TagsField, sortType
sortType = filterPara(getForm("sortType","get"))
dim sql, msg, rsObj
	if sortType<>"" then
	Set rsObj=conn.Exec("select TagsID,TagsName,TagsField,TagsOrder,TagsCategory from {prefix}TagsSet where TagsCategory='"&TagsCategory&"'  Order by TagsOrder Asc,TagsID","r1")
	else
	Set rsObj=conn.Exec("select TagsID,TagsName,TagsField,TagsOrder,TagsCategory from {prefix}TagsSet  Order by TagsOrder Asc,TagsID","r1")
	end if
	If rsObj.Eof Then 
		echo"<tr bgcolor=""#FFFFFF"" align=""center"">"
		echo "<td colspan=""5"">没有数据</td>"
		echo "</tr>"
	Else
		Do while not rsObj.Eof 
		 echo "<tr bgcolor=""#FFFFFF"" align=""center"">"
		 echo "<td height=""28"">"
		 echo "<input type=""checkbox"" name=""id"" value="""&rsObj("TagsID")&""" class=""checkbox"" onclick='silbingCheck(this)' />"
		 echo "<input type=""checkbox"" name=""TagsField"" value=""'"&rsObj("TagsCategory")&"_"&rsObj("TagsField")&"'"" style=""display:none"" />"
		 echo "<input type=""hidden"" name=""TagsIDs"" value="""&rsObj("TagsID")&""" />"
		 echo "<input type=""hidden"" name=""TagsCategory"" value="""&rsObj("TagsCategory")&""" />"
		 
		 echo "</td>"
		 echo "<td>"&rsObj("TagsID")&"</td>"
		 echo "<td>"&rsObj("TagsName")&"</td>"
		 if  rsObj("TagsCategory") <> "" then
		 echo "<td>"&rsObj("TagsCategory")&"_"&rsObj("TagsField")&"</td>" 
		 else
		 echo "<td>"&rsObj("TagsField")&"</td>" 
		 end if
		 echo "<td><input class=""input"" style=""width:30px"" name=""TagsOrders"" value="""&rsObj("TagsOrder")&"""/></td>"
		 echo "<td> <a href=""Ky_TagsEdit.asp?action=update&id="&rsObj("TagsID")&"""  >修改</a> | <a href=""?action=del&id="&rsObj("TagsID")&"&TagsField="&rsObj("TagsCategory")&"_"&rsObj("TagsField")&"""  onClick=""return confirm('确定要删除吗')"">删除</a></td>"
		 echo "</tr>"
		  rsObj.MoveNext
		Loop
	End If
	rsObj.close : Set rsObj = nothing
End Sub

Sub DelTags 

dim m_TagsField
Dim sql,tmp
	dim ID 	:	ID = filterPara(getForm("id","both"))
	TagsField=getForm("TagsField","both")
	TagsCategory=getForm("TagsCategory","both")
	
	
	m_TagsField=replace(TagsField,"&apos;","")
	 
	TagsField = replace(TagsField,"&apos;","")
	'die m_TagsField
	tmp = m_TagsField
	
	if m_TagsField="p_price" then alertMsgAndGo "该参数在订购中固定使用，不可删除","Ky_Tags.asp"	
	if TagsCategory <> "" then tmp = TagsCategory & "_" & m_TagsField
	sql = "ALTER TABLE {prefix}Content drop column "&m_TagsField
	'die sql
	conn.Exec sql,"exe"
	
	if instr(m_TagsField,"'")=0 then m_TagsField = "'"&m_TagsField&"'"
	sql = "Delete from {prefix}TagsSet where TagsID in ("&ID&")"
	'echo sql & "<br>"
	conn.Exec sql,"exe"
	alertMsgAndGo "删除成功","Ky_Tags.asp"	
	
	if Err then echo err.description 'alertMsgAndGo "删除字段失败,请联系管理员","-1"
	
End Sub 

Sub updateOrder
dim sql, msg
	Dim ids				:	ids=split(filterPara(getForm("TagsIDs","post")),",")
	Dim orders		:	orders=split(filterPara(getForm("TagsOrders","post")),",")
	If Ubound(ids)=-1 Then 	'防止有值为空时下标越界
		ReDim ids(0)
		ids(0)=""
	End If	
	
	If Ubound(orders)=-1 Then
		ReDim orders(0)
		orders(0)=0
	End If
	Dim i
	
	For i=0 To Ubound(ids)		
		if isnum(trim(orders(i))) then
			Conn.Exec "update {prefix}TagsSet Set TagsOrder="&trim(orders(i))&" Where TagsID="&trim(ids(i)),"exe"	
		else
			Conn.Exec "update {prefix}TagsSet Set TagsOrder=0 Where TagsID="&trim(ids(i)),"exe"	
		end if
	Next
	
	
	alertMsgAndGo "更新排序成功","Ky_Tags.asp"
End Sub

%>