<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp"-->
<!--#include file="inc/check.asp"-->
<%
Dim HtmlContent,XMLDom,HeadTitle,action,topicmode,TopicID
Dim AskedPageList,strLink
Dim maxperpage,CurrentPage,Pcount,totalrec,totalnumber
topicmode = 10
maxperpage = 20
CurrentPage		= NewAsp.ChkNumeric(Request("page"))
If CurrentPage = 0 Then CurrentPage = 1
Action = NewAsp.CheckBadstr(Request("action"))
TopicID = NewAsp.ChkNumeric(Request("TopicID"))

Select Case LCase(Action)
	Case "add"
		Call AddFavorite()
	Case "del"
		Call DelFavorite()
Case Else
	HeadTitle = "我的收藏"
	HtmlContent = NewAsp.LoadTemplate("userpostlist")
	HtmlContent = Replace(HtmlContent, "{$HeadTitle}", HeadTitle)
	LoadTopiclist()
	Response.Write NewAsp.ArchiveHtml(HtmlContent)
End Select
NewAsp.CloseConn()

Sub LoadTopiclist()
	Dim SQL,Rs,topiclist,Node
	Set XMLDom = NewAsp.CreateXMLDoc("msxml2.FreeThreadedDOMDocument"& MsxmlVersion)
	XMLDom.appendChild(XMLDom.createElement("xml"))
	XMLDom.documentElement.setAttribute "action","favorite"
	XMLDom.documentElement.setAttribute "nowuserid",NewAsp.UserID
	XMLDom.documentElement.setAttribute "nowusername",NewAsp.UserName
	XMLDom.documentElement.setAttribute "topicmode",topicmode
	XMLDom.documentElement.setAttribute "headtitle",HeadTitle
	totalnumber = NewAsp.Execute("SELECT COUNT(*) FROM NC_Ask_Favorite WHERE userid="&NewAsp.UserID&"")(0)
	totalrec = totalnumber
	Pcount = CLng(totalrec / maxperpage)  '得到总页数
	If Pcount < totalrec / maxperpage Then Pcount = Pcount + 1
	If Pcount < 1 Then Pcount = 1
	If CurrentPage < 1 Then CurrentPage = 1
	If CurrentPage > Pcount Then CurrentPage = Pcount
	
	XMLDom.documentElement.setAttribute "pagesize",maxperpage
	XMLDom.documentElement.setAttribute "page",CurrentPage
	XMLDom.documentElement.setAttribute "totalnumber",totalnumber
	XMLDom.documentElement.setAttribute "totalrec",totalrec
	XMLDom.documentElement.setAttribute "pagecount",Pcount
	If Not IsObject(Conn) Then ConnectionDatabase
	Set Rs = NewAsp.CreateAXObject("ADODB.Recordset")
	SQL="SELECT A.favorid,A.userid,A.FavorTopic,A.TopicID,A.FavorUrl,A.FavorTime,A.FavorMode,B.classid,B.userid as postuserid,B.classname,B.title,B.PostUsername,B.Expired,B.Closed,B.PostTable,B.DateAndTime,B.Reward,B.Hits,B.PostNum,B.TopicMode FROM NC_Ask_Favorite A INNER JOIN [NC_Ask_Topic] B on A.TopicID=B.TopicID WHERE A.userid="&NewAsp.UserID&" ORDER BY A.favorid DESC"
	Rs.Open SQL,Conn,1,1
	If Not (Rs.BOF And Rs.EOF) Then
		If CurrentPage >1 Then
			Rs.Move (CurrentPage-1) * maxperpage
		End If
	End If
	If Not Rs.EOF Then
		SQL=Rs.GetRows(maxperpage)
		Set topiclist=NewAsp.ArrayToxml(SQL,Rs,"row","topic")
	Else
		Set topiclist=Nothing
	End If
	Set Rs=Nothing
	SQL=Empty
	If Not topiclist Is Nothing Then 
		For Each Node in topiclist.documentElement.SelectNodes("row")
			Node.selectSingleNode("@title").text=NewAsp.Checkstr(Node.selectSingleNode("@title").text)
			Node.selectSingleNode("@dateandtime").text=NewAsp.FormatDate(Node.selectSingleNode("@dateandtime").text,5)
			If CLng(Node.selectSingleNode("@closed").text) = 1 Then
				Node.selectSingleNode("@topicmode").text=5
			End If
		Next
		XMLDom.documentElement.appendChild(topiclist.documentElement)
		Set topiclist=Nothing
	End If
	strLink = NewAsp.InstallDir & "userfavorite.asp?"
	AskedPageList = showlistpage(CurrentPage,Pcount,strLink)
	transform_topicList()
	HtmlContent = Replace(HtmlContent, "{$AskedPageList}", AskedPageList)
End Sub
Sub transform_topicList()
	Dim proc,XMLStyle,node,cnode,XSLTemplate
	Set XSLTemplate=NewAsp.CreateAXObject("Msxml2.XSLTemplate" & MsxmlVersion )
	Set XMLStyle=NewAsp.CreateXmlDoc("msxml2.FreeThreadedDOMDocument"& MsxmlVersion )
	XMLStyle.load Server.MapPath(NewAsp.TemplatePath & "xslt/userpostlist.xslt")
	Set Node=XMLStyle.createNode(1,"xsl:variable","http://www.w3.org/1999/XSL/Transform")
	Set CNode=XMLStyle.createNode(2,"name","")
	CNode.text="installdir"
	Node.attributes.setNamedItem(CNode)
	Node.text=NewAsp.InstallDir
	XMLStyle.documentElement.appendChild(node)
	Set Node=XMLStyle.createNode(1,"xsl:variable","http://www.w3.org/1999/XSL/Transform")
	Set CNode=XMLStyle.createNode(2,"name","")
	CNode.text="AskedPageList"
	Node.attributes.setNamedItem(CNode)
	Node.text=AskedPageList
	XMLStyle.documentElement.appendChild(node)
	Set Node=XMLStyle.createNode(1,"xsl:variable","http://www.w3.org/1999/XSL/Transform")
	Set CNode=XMLStyle.createNode(2,"name","")
	CNode.text="skinurl"
	Node.attributes.setNamedItem(CNode)
	node.text=NewAsp.AskedSkinUrl
	XMLStyle.documentElement.appendChild(node)
	XSLTemplate.stylesheet=XMLStyle
	Set proc = XSLTemplate.createProcessor()
	proc.input = XMLDom
	proc.transform()
	Dim procstr
	procstr = proc.output
	HtmlContent = Replace(HtmlContent, "{$Topiclist}", procstr)
	Set XMLDom=Nothing 
	Set proc=Nothing
	Set XMLStyle=Nothing
	Set XSLTemplate=Nothing
End Sub

Sub DelFavorite()
	Dim favoriteid
	favoriteid = Request.Form("favoriteid")
	favoriteid = ChkArrayID(favoriteid)
	If favoriteid = "" Then
		NewAsp.FoundErr = True
		Response.Write "<script>alert('友情提示!\n\n请选择要删除的收藏内容!');</script>"
		Exit Sub
	End If
	NewAsp.Execute("DELETE FROM NC_Ask_Favorite WHERE userid="&NewAsp.UserID&" And favorid in(" & favoriteid & ")")
	Response.Write "<script language=""JavaScript"">"
	Response.Write "try{top.location='userfavorite.asp';"
	Response.Write "}catch(e){}"
	Response.Write "</script>"
End Sub

Sub AddFavorite()
	If NewAsp.FoundErr Then Exit Sub
	Dim Rs,SQL,m_strTitle,favtotalrec
	favtotalrec = NewAsp.Execute("SELECT COUNT(*) FROM NC_Ask_Favorite WHERE userid="&NewAsp.UserID&"")(0)
	If favtotalrec > 500 Then
		Response.Write "<script>alert('友情提示!\n\n你的收藏记录已经达到上限!');</script>"
		Exit Sub
	End If
	Set Rs = NewAsp.Execute("SELECT TopicID,userid,title,topicmode FROM NC_Ask_Topic WHERE topicid="&topicid&" And LockTopic=0")
	If Rs.BOF And Rs.EOF Then
		Set Rs = Nothing
		Response.Write "<script>alert('友情提示!\n\n没有找到你要收藏的信息!');</script>"
		Exit Sub
	Else
		TopicID = Rs("TopicID")
		m_strTitle = Rs("title")
		topicmode = Rs("topicmode")
		If Rs("userid") = NewAsp.UserID Then
			Response.Write "<script>alert('友情提示!\n\n不能收藏自己的提问!');</script>"
			Exit Sub
		End If
	End If
	Rs.Close:Set Rs = Nothing
	Set Rs = NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT * FROM NC_Ask_Favorite WHERE userid="&NewAsp.UserID&" And topicid="&topicid
	Rs.Open SQL,Conn,1,3
	If Rs.BOF And Rs.EOF Then
		Rs.Addnew
		Rs("userid") = NewAsp.UserID
		Rs("FavorTopic") = m_strTitle
		Rs("TopicID") = TopicID
		Rs("FavorUrl") = ""
		Rs("FavorTime") = Now()
		If topicmode = 3 Then
			Rs("FavorMode") = 0
		Else
			Rs("FavorMode") = 1
		End If
		Rs.Update
	Else
		Response.Write "<script>alert('友情提示!\n\n你已经收藏了该问题!');</script>"
		Exit Sub
	End If
	Rs.Close:Set Rs = Nothing
	Response.Write "<script>alert('友情提示!\n\n收藏成功!');</script>"
End Sub

%>