<%@ Language="VBSCRIPT" codepage="936" %>
<%option explicit%>
<!--#include file="../Conn.asp"-->
<!--#include file="../KS_Cls/Kesion.CommonCls.asp"-->
<!--#include file="../KS_Cls/Kesion.Label.CommonCls.asp"-->
<%
'****************************************************
' Software name:Kesion CMS 9.5
' Email: service@kesion.com . 营销QQ:4000080263  Tel:400-008-0263
' Web: http://www.kesion.com http://www.kesion.cn
' Copyright (C) Kesion Network All Rights Reserved.
'****************************************************
response.cachecontrol="no-cache"
response.addHeader "pragma","no-cache"
response.expires=-1
response.expiresAbsolute=now-1
Response.CharSet="gb2312"
Dim KS:Set KS=New PublicCls
Dim KSCls:Set KSCls=New DIYCls

Dim CurrPage,Tconn
Dim SqlLabel:SqlLabel=KS.S("LabelID")
CurrPage=KS.ChkClng(KS.S("curpage"))
If CurrPage<=0 Then CurrPage=1

Dim I,KS_RS_Obj,LabelName,UserParamArr,FunctionLabelParamArr,CirLabelContent,FunctionSQL,LabelContent,TempCirContent
Dim FunctionLabelType,ItemName,PageStyle,PerPageNumber,TotalPut,PageNum,J,TempStr,Ajax,DataSourceType,DataSourceStr
Dim Str
loadData
Function LoadData()
          if KS.IsNul(request.ServerVariables("http_referer")) Then
		    KS.Die "请不要非法调用!"
		  End If

		  LabelName    = Replace(Replace(Split(SqlLabel,"(")(0),"'",""),"""","")
		  '用户函数参数
		  UserParamArr = Split(Replace(Replace(Replace(Replace(SqlLabel,LabelName&"(",""),")}",""),"""",""),"'",""),",")   
		  
		   Dim L_Description:L_Description=KSCls.G_S_P(LabelName &"}",1)
		   If L_Description="" Then
		    str="对不起，标签不存在!":exit function
		   Else
		    FunctionLabelParamArr = Split(L_Description,"@@@")
		    LabelContent          = Replace(KSCls.G_S_P(LabelName &"}",2),Chr(10) ,"$KS:Page$")
		   End If
		  
		   FunctionSQL=FunctionLabelParamArr(0)           '查询语句
		   FunctionSQL=Replace(FunctionSQL,"{$CurrClassID}",KS.S("classID"))
		   FunctionSQL=Replace(FunctionSQL,"{$CurrInfoID}",KS.ChkClng(KS.S("infoID")))
		   FunctionSQL=Replace(FunctionSQL,"{$CurrClassChildID}",KS.GetFolderTid(KS.S("classID")))
		   FunctionSQL=Replace(FunctionSQL,"{$CurrUserName}",KS.C("UserName"),1,-1,1)
		   If Instr(FunctionSQL,"{$GetUserName}")<>0 Then
		    If Not KS.IsNul(KS.S("UserName")) Then
		     FunctionSQL=Replace(FunctionSQL,"{$GetUserName}",KS.DelSql(KS.UrlDecode(KS.S("UserName"))),1,-1,1)
			ElseIf Not KS.IsNul(Session("SpaceUserName")) Then
			 FunctionSQL=Replace(FunctionSQL,"{$GetUserName}",Session("SpaceUserName"))
            Else
		     FunctionSQL=Replace(FunctionSQL,"{$GetUserName}",Split(KS.DelSql(Replace(KS.UrlDecode(Request.ServerVariables("QUERY_STRING")),"'","")),"/")(0),1,-1,1)
			End If
		   End If
		   LabelContent = KSCls.ReplaceRequest(LabelContent)    '替换request的值
		   FunctionSQL = KSCls.ReplaceRequest(FunctionSQL)    '替换request的值

		   For I=0 To Ubound(UserParamArr)
		    FunctionSQL  = Replace(FunctionSQL,"{$Param("&I&")}",KS.DelSQL(UserParamArr(I)))
			LabelContent = Replace(LabelContent,"{$Param("&I&")}",KS.DelSQL(UserParamArr(I)))
		   Next
		   FunctionLabelType=FunctionLabelParamArr(2)
		   If Not Isnumeric(FunctionLabelType) Then FunctionLabelType=0
		   Ajax=FunctionLabelParamArr(5)
           		   
		   ItemName=FunctionLabelParamArr(3)
		   PageStyle=FunctionLabelParamArr(4)
		   DataSourceType=FunctionLabelParamArr(6)
		   DataSourceStr=FunctionLabelParamArr(7)
		   if DataSourceType=1 Or DataSourceType=5 Or DataSourceType=6 then	DataSourceStr=LFCls.GetAbsolutePath(DataSourceStr)
		   If OpenExtConn=false Then str="外部数据库连接出错!":exit function
		   on error resume next
		   Set KS_RS_Obj=Server.CreateObject("ADODB.RECORDSET")
           If DataSourceType=0 Then
		    KS_RS_Obj.Open FunctionSQL,Conn,1,1
		   Else
		    KS_RS_Obj.Open FunctionSQL,TConn,1,1
		   End IF
		   if err then 
		    err.clear
			KS_RS_Obj.close: set KS_RS_Obj=nothing
			KS.Die "非法调用!"
		   end if
		   
		   If Not KS_RS_Obj.Eof Then
			    Dim regEx, Matches, Match,LoopTimes
				Set regEx = New RegExp
				regEx.Pattern = "\[loop=\d*].+?\[/loop]"
				regEx.IgnoreCase = True
				regEx.Global = True
				Set Matches = regEx.Execute(LabelContent)
				If FunctionLabelType=1 Then                  '分页标签
				         PerPageNumber=0
				         For Each Match In Matches
							PerPageNumber=PerPageNumber+KSCls.GetLoopNum(Match.Value)   '每页记录数
						 Next
                         If PerPageNumber=0 Then str= "自定义函数标签的循环次数必须大于0":exit function
						 
				  		TotalPut = KS_RS_Obj.recordcount
						if (TotalPut mod PerPageNumber)=0 then
								PageNum = TotalPut \ PerPageNumber
						else
								PageNum = TotalPut \ PerPageNumber + 1
						end if
							 TempCirContent    = LabelContent
							 KS_RS_Obj.Move (CurrPage - 1) * PerPageNumber
						     For Each Match In Matches
								  LoopTimes=KSCls.GetLoopNum(Match.Value)   '循环次数
								  CirLabelContent = Replace(Replace(Match.value,"[loop=" & LoopTimes&"]",""),"[/loop]","")
								  TempCirContent    = Replace(TempCirContent,"[loop="&LoopTimes&"]"&CirLabelContent&"[/loop]",KSCls.GetCirLabelContent(CirLabelContent,KS_RS_Obj,LoopTimes,CurrPage),1,1)

								  If KS_RS_Obj.Eof Then Exit For
							 Next
							  TempStr = TempCirContent
						      str=Replace(KSCls.CleanLabel(TempStr),"$KS:Page$",vbcrlf)

				End If		 
		   Else
		     str="对不起，没有内容!":exit function
		   End if
		   KS_RS_Obj.Close:Set KS_RS_Obj=Nothing
		   If DataSourceType=0 Then
		   Conn.Close:Set Conn=Nothing
		   Else
		   TConn.Close:Set TConn=Nothing
		   End If
   End Function


Function OpenExtConn()
		 If DataSourceType=0 Then
		   OpenExtConn=True
		 Else
			on error resume next
		    Set tconn = Server.CreateObject("ADODB.Connection")
			tconn.open datasourcestr
			If Err Then 
			  Err.Clear
			  Set tconn = Nothing
			   OpenExtConn=False
			Else 
			   OpenExtConn=true
			End If
		 End If
End Function



Dim Tp,homeUrl,endUrl,prevUrl,nextUrl,startpage
Dim XML:Set XML=LFCls.GetXMLFromFile("pagestyle")
Dim Node:Set Node= XML.documentElement.selectSingleNode("/pagestyle/item[@name='" &PageStyle & "']/content")
If Not Node Is Nothing Then
		 Tp=Node.text
End If
 tp=replace(tp,"{$maxperpage}",PerPageNumber)
 tp=replace(tp,"{$totalput}",TotalPut)
 tp=replace(tp,"{$totalpage}",PageNum)
 tp=replace(tp,"{$currentpage}",CurrPage)
 tp=replace(tp,"{$itemunit}",ItemName)
 
homeUrl = "javascript:homePage(" & CurrPage & ");"
if (currPage=1) then
 prevUrl = "javascript:homePage();"
else
 prevUrl = "javascript:turn(" & currpage-1 & ");"
end if
if (currpage=pagenum) then
 NextUrl = "javascript:lastPage(" & currpage & "," & PageNum &");"
 endUrl = "javascript:lastPage(" & currpage & "," & PageNum &");"
else
 NextUrl = "javascript:turn(" & currpage+1 & ");"
 endUrl="javascript:turn(" & pagenum & ");"
end if
				
				
 Tp=Replace(Tp,"{$homeurl}",homeurl)
 Tp=Replace(Tp,"{$prevurl}",prevurl)
 Tp=Replace(Tp,"{$nexturl}",nexturl)
 Tp=Replace(Tp,"{$endurl}",endurl) 

 if (instr(Tp,"{$pagenumlist}")<>0) then
                Dim p,pageStr:pageStr=""
                startpage = 1
                if (CurrPage >= 7)  then startpage = CurrPage - 5
                if (PageNum - CurrPage < 5) then startpage = pageNum - 10
                if (startpage <= 0) then startpage = 1
                Dim nn:nn = 1
                for p = startpage to pageNum
                    if (p = CurrPage) then
                        pageStr=pageStr & " <a href=""#"" class=""curr"">" & p & "</a>"
                    else
                         pageStr=pageStr & " <a class=""num"" href=""javascript:turn(" & p& ");"">" & p & "</a>"
                    end if
                    nn=nn+1
					if (nn >= 10) then exit for
                Next
                Tp = replace(Tp, "{$pagenumlist}", pagestr)
End If	  

if (instr(Tp,"{$turnpage}")<>0) then
                pageStr="<select name=""page"" id=""turnpage"" onchange=""javascript:turn(this.options[this.selectedIndex].value);"">"
                for j = 1 to pageNum
                  pageStr=pageStr &"<option value=""" & j & """"
				  if j=currPage then pageStr=pageStr &" selected"
				  pageStr=pageStr &">第" & j & "页</option>"
                next
                pageStr=pageStr &"</select>"
                Tp = replace(Tp, "{$turnpage}", pageStr)
    end if
		

 if instr(str,"[KS:PageStyle]")=0 then
  str=str  &"[KS:PageStyle]"
 end if

response.write replace(str,"[KS:PageStyle]",tp)
%>
