Private objAPI
Private objCore
Private objXML
Private objUser
Private objCoop
Private MyIO

Private Sub Class_Initialize()
    Set objAPI = New WAPmoAPI
    Set objCore = objAPI.Core
    Set objXML = objCore.XMLParser
    Set objUser = objCore.User
    Set objCoop = objCore.Coop
End Sub

Private Sub Class_Terminate()
    Set objCoop = Nothing
    Set objUser = Nothing
    Set objXML = Nothing
    Set objCore = Nothing
    Set objAPI = Nothing
End Sub

Public Sub main()
    objXML.Align = "center"
    objXML.Println MyKernel.Config("QueryTitle")
    objXML.Align = "left"
    If MyKernel.Config("IsQuery") <> "1" Then
        objXML.Println "ùѱվر"
    ElseIf Request.ServerVariables("REQUEST_METHOD") = "POST" Then
        Set MyIO = Server.CreateObject("WMCore.IIO")
        Call doPost
        Set MyIO = Nothing
    Else
        Set MyIO = Server.CreateObject("WMCore.IIO")
        Call doGet
        Set MyIO = Nothing
    End If
    Call ExportForm
    objCore.ExportBackHome "1", "ҳ"
    objXML.Println ""
End Sub

Private Sub doGet()
    Dim lngStaple
    Dim intCate
    Dim strKey
    lngStaple = atol(MyIO.QueryString("Staple"))
    intCate = atoi(MyIO.QueryString("Category"))
    strKey = Trim(MyIO.QueryString("Keyword", 65001))
    If Len(strKey) > 1 Then
        Call doGetQuery(lngStaple, intCate, strKey)
    End If
End Sub

Private Sub doPost()
    If MyIO.Parse(KBYTE) <> 0 Then
        objXML.Println "󳤶ȣ" & KBYTE
    Else
        Dim lngStaple
        Dim intCate
        Dim strKey
        lngStaple = atol(MyIO.Form("Staple"))
        intCate = atoi(MyIO.Form("Category"))
        strKey = Trim(MyIO.Form("Keyword", 65001))
        If strKey = "" Then
            objXML.Println "ҪĹؼ"
        ElseIf Len(strKey) < 2 Then
            objXML.Println "ؼҪַ"
        Else
            Call doGetQuery(lngStaple, intCate, strKey)
        End If
    End If
End Sub

Private Sub doGetQuery(ByVal lngStaple, ByVal intCate, ByVal strKey)
    Dim objPage
    Dim rs
    Dim i
    Dim strWhere
    Set objPage = Server.CreateObject("WMKernel.IPage")
    objPage.PageName = "Page"
    objPage.PageSize = 10
    objPage.DataType = WM_DataType
    objPage.Increment = "SeqId"
    objPage.Column = "SeqId,StapleId,StapleTitle,Title,Matter,Hit,Intime"
    objPage.Table = T_CONTENT
    Select Case intCate
    Case 50
        strWhere = "Content LIKE '%$(Keyword)%' AND Hidden=0"
    Case 100
        strWhere = "(Title LIKE '%$(Keyword)%' Or Content LIKE '%$(Keyword)%') AND Hidden=0"
    Case Else
        strWhere = "Title LIKE '%$(Keyword)%' AND Hidden=0"
    End Select
    If lngStaple <> 0 Then
        strWhere = strWhere & getStapleWhere(lngStaple)
    ElseIf Not objCoop Is Nothing Then
        strWhere = strWhere & getCoopWhere()
    End If
    strWhere = Replace(strWhere, "$(Keyword)", SafeString(strKey))
    objPage.Where = strWhere
    objPage.Sort = "SeqId DESC"
    objPage.SortType = True
    Set rs = objPage.Exec()
    objXML.Println "" & objPage.TotalRow & "[" & strKey & "]ص"
    If objPage.TotalRow > 0 Then
        i = 0
        Do While rs.EOF = False And i < objPage.PageSize
            objXML.Printf "["
            objXML.Printf objCore.CreateA("staple.asp?StapleId=" & rs("StapleId"), rs("StapleTitle"), "")
            objXML.Printf "]"
            objXML.Println objCore.CreateA("content.asp?StapleId=" & rs("StapleId") & "&PageNo=" & objPage.PageNum & "&ContentId=" & rs("SeqId") & "&Intime=" & rs("Intime"), ((objPage.PageNum - 1) * objPage.PageSize + i + 1) & "." & rs("Title"), "")
            rs.MoveNext
            i = i + 1
        Loop
        objXML.Println ""
        Dim strURL
        strURL = "query.asp?Staple=$(Staple)&Category=$(Category)&Keyword=$(Keyword)&Page=$(Page)"
        strURL = Replace(strURL, "$(Staple)", lngStaple)
        strURL = Replace(strURL, "$(Category)", intCate)
        strURL = Replace(strURL, "$(Keyword)", URLEncode8(strKey))
        If objPage.PageNum < objPage.TotalPage Then
            objXML.Printf objCore.CreateA(Replace(strURL, "$(Page)", objPage.PageNum + 1), "һҳ", "")
            objXML.Printf "|"
            objXML.Println objCore.CreateA(Replace(strURL, "$(Page)", objPage.TotalPage), "ĩҳ", "")
        End If
        If objPage.PageNum > 1 Then
            objXML.Printf objCore.CreateA(Replace(strURL, "$(Page)", objPage.PageNum - 1), "һҳ", "")
            objXML.Printf "|"
            objXML.Println objCore.CreateA(Replace(strURL, "$(Page)", 1), "һҳ", "")
        End If
        If objPage.TotalPage > 2 Then
            objXML.SetForm "query.asp", "get", "", True
            Call objCore.setQuery
            objXML.SetInput "Staple", "hidden", lngStaple, "", "", False
            objXML.SetInput "Category", "hidden", intCate, "", "", False
            objXML.SetInput "Keyword", "hidden", strKey, "", "", False
            objXML.SetInput "Page", "text", "", objPage.PageNum & "/" & objPage.TotalPage & "ҳ>>", "ҳ", False, 5, 5, "*N"
            objXML.SetInput "", "submit", "GO", "", "", True
        End If
        objXML.Println ""
    End If
    If TypeName(rs) <> "Nothing" Then rs.Close
    Set rs = Nothing
    Set objPage = Nothing
End Sub

Private Sub ExportForm()
    objXML.SetForm objCore.FormatURL("query.asp"), "post", "", True
    If MyKernel.Config("QueryStaple") = "1" Then
        objXML.SetSelect "Staple", "ѡĿ", "", True
        objXML.SetOption 0, "Ŀ"
        If Not objCoop Is Nothing Then
            Call ExportCoopStaple
        Else
            Call ExportStaple
        End If
    End If
    objXML.SetInput "Keyword", "text", "", "ʣ", " ַ", True
    objXML.SetSelect "Category", "ѡ", "", True
    objXML.SetOption "0", ""
    objXML.SetOption "50", ""
    objXML.SetOption "100", ""
    objXML.SetInput "", "submit", "", "", "", True
End Sub

Private Function getCoopWhere()
    Dim ret
    ret = ""
    If objCoop.Item("Staple") <> "" Then
        Dim strSQL
        Dim rs
        strSQL = "SELECT Mark FROM $(Table) WHERE SeqId IN ($(Array))"
        strSQL = Replace(strSQL, "$(Table)", T_STAPLE)
        strSQL = Replace(strSQL, "$(Array)", objCoop.Item("Staple"))
        Set rs = MyKernel.Execute(strSQL)
        Do While rs.EOF = False
            ret = ret & IIf(ret = "", " AND (", " OR ") & "Left(Mark," & Len(rs("Mark")) & ")='" & rs("Mark") & "'"
            rs.MoveNext
        Loop
        ret = ret & ")"
        rs.Close
        Set rs = Nothing
    End If
    getCoopWhere = ret
End Function

Private Function getStapleWhere(ByVal lngId)
    Dim strMark
    Dim strSQL
    Dim strRet
    strSQL = getLimitSQL(1, "Mark", T_STAPLE, "SeqId=$(SeqId)", "", "")
    strSQL = Replace(strSQL, "$(SeqId)", lngId)
    strMark = getRecord(strSQL)
    If strMark <> "" Then
        strRet = " AND (StapleId=$(StapleId) Or LEFT(Mark,$(MarkLength))='$(Mark)_')"
        strRet = Replace(strRet, "$(StapleId)", lngId)
        strRet = Replace(strRet, "$(MarkLength)", Len(strMark) + 1)
        strRet = Replace(strRet, "$(Mark)", strMark)
    End If
    getStapleWhere = strRet
End Function

Private Sub ExportStaple()
    Dim strSQL
    Dim rs
    Dim arrStaple
    strSQL = "SELECT SeqId,Title,Follow FROM $(Table) WHERE Hidden=0 AND Category=$(Category)"
    strSQL = Replace(strSQL, "$(Table)", T_STAPLE)
    strSQL = Replace(strSQL, "$(Category)", STAPLE_NORMAL)
    Set rs = MyKernel.Execute(strSQL)
    arrStaple = rs.GetRows()
    ExportStapleX arrStaple, 0, 0, atoi(MyKernel.Config("QueryDepth"))
    rs.Close
    Set rs = Nothing
End Sub

Private Sub ExportStapleX(ByRef arrIn, ByVal lngFollow, ByVal intDeep, ByVal intDepth)
    Dim i
    For i = 0 To UBound(arrIn, 2)
        If arrIn(2, i) = lngFollow Then
            objXML.SetOption arrIn(0, i), String(intDeep * 2, "-") & arrIn(1, i)
            If intDepth > 0 And intDeep <> intDepth Then
                ExportStapleX arrIn, arrIn(0, i), intDeep + 1, intDepth
            End If
        End If
    Next
End Sub

Private Sub ExportCoopStaple()
    If objCoop.Item("Staple") = "" Then Exit Sub
    Dim strSQL
    Dim rs
    strSQL = "SELECT SeqId,Title FROM $(Table) WHERE SeqId IN ($(Array))"
    strSQL = Replace(strSQL, "$(Table)", T_STAPLE)
    strSQL = Replace(strSQL, "$(Array)", objCoop.Item("Staple"))
    Set rs = MyKernel.Execute(strSQL)
    Do While rs.EOF = False
        objXML.SetOption rs("SeqId"), rs("Title")
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
End Sub
