<%
' ********************************************************************
' Copyright (C) 2001 Microsoft Corporation. All rights reserved.
'
'	IMPORTANT.  Please read the legal.txt file, located in the
'	"<CMS Install Directory>\Server\httpexec\WBC\Internals"
'	directory, governing the use of this internal file."
' ********************************************************************

' ********************************************************************
' File:		/NR/System/WBC/Internals/ServerControls/CNCPagingControl.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Implements the Paging Control for displaying "Prev | 1 | 2 | 3 | Next"
'			type of paging navigation
'
' Public Methods and Properties Exposed:
'	- Sub InitPagingControl( pNCPagingRenderer, nTotalItems, nItemsPerPage )
'	- Sub StopPropagateParameter( strName )
'	- Sub DisplayPagingLinks()
'	- Property CurrentPage [read/write]
'	- Property QueryStringForCurrentPage [readonly]
'
' INCPagingRenderer Callback Interface of CNCPagingControl
'	- Sub RenderPreviousLink( nPagingIndexForPrevious, nItemsPerPage, nTotalPages )
'	- Sub RenderPagingLink( nPagingIndexForLink, nItemsPerPage, nTotalPages, bIsCurrent )
'	- Sub RenderNextLink( nPagingIndexForNext, nItemsPerPage, nTotalPages )
' ********************************************************************

' -- Error Strings
Const IDS_NCPAGING_ERR_RENDERER = "<div><b>CNCPagingControl Error:</b> Invalid NCPagingRenderer value passed into InitPagingControl.</div>"
Const IDS_NCPAGING_ERR_TOTALITEMS = "<div><b>CNCPagingControl Error:</b> Invalid TotalItems value passed into InitPagingControl.</div>"
Const IDS_NCPAGING_ERR_ITEMSPERPAGE = "<div><b>CNCPagingControl Error:</b> Invalid ItemsPerPage value passed into InitPagingControl.</div>"
Const IDS_NCPAGING_ERR_NOINIT = "<div><b>CNCPagingControl Error:</b> InitPagingControl() has not been called before calling call DisplayPagingLinks().</div>"

' ---- QueryString param name for setting new value for CNCPagingConfigControl
Const IDS_NCPAGING_INDEX = "NCPagingCtrlSugIndex"

Class CNCPagingControl
	' -- Indicate the current Page in terms of the Paging set
	Public CurrentPage

	' -- Module internal state management
	Private m_bInitCalled
	' -- Mandatory parameters passed in by cleint for controlling the module
	Private m_pNCPagingRenderer, m_nTotalItems, m_nItemsPerPage
	' -- Class internal variables
	Private m_bInvisible, m_nTotalPages, m_strBaseURL

	' -- Dictionary for keeping track of QParams to stop propagating
	Private m_dictCallerEnforcedStopQParams

	' ---- [Private] Initialize the class
	Private Sub Class_Initialize()
		m_bInitCalled = False

		' -- Default values for mandatory settings configured through InitPagingControl()
		Set m_pNCPagingRenderer = Nothing
		m_nTotalItems = 0
		m_nItemsPerPage = 0

		m_bInvisible = True

		' -- Default values for optional settings configured through properties
		If (Request.QueryString(IDS_NCPAGING_INDEX) <> "") Then
			CurrentPage = CLng( Request.QueryString(IDS_NCPAGING_INDEX) )
		Else
			CurrentPage = 1
		End If



		' -- create the dictionary object to hold user-enforced parameters not to propagate
		Set m_dictCallerEnforcedStopQParams = CreateObject("Scripting.Dictionary")
	End Sub

	' ---- [Public] Initialize the Control with mandatory parameters
	Public Sub InitPagingControl( pNCPagingRenderer, nTotalItems, nItemsPerPage )
		' -- validate parameters first
		If (pNCPagingRenderer Is Nothing) Then
			Call Response.Write(IDS_NCPAGING_ERR_RENDERER)
			Exit Sub
		End If
		If (CInt(nTotalItems) <= 0) Or (CInt(nItemsPerPage) <= 0) Then
			' -- these values indicate that the Control should not be visible
			m_bInvisible = True
			Exit Sub
		Else
			m_bInvisible = False
		End If

		' -- flag that module has been initialized
		m_bInitCalled = True

		' -- Cache passed in values
		Set m_pNCPagingRenderer = pNCPagingRenderer
		m_nTotalItems = nTotalItems
		m_nItemsPerPage = nItemsPerPage

		' -- figure out total number of pages needed in Paging
		m_nTotalPages = m_nTotalItems \ m_nItemsPerPage
		If (m_nTotalItems Mod m_nItemsPerPage) > 0 Then
			m_nTotalPages = m_nTotalPages  + 1
		End If
	End Sub

	' ---- [Public] Display a List of Content Management Server objects
	Public Sub DisplayPagingLinks()
		' -- There is no work for this control to do
		If (m_bInvisible = True) Then
			Exit Sub
		End If

		' -- check if InitListControl() has been called
		If (m_bInitCalled = False) Then
			Call Response.Write(IDS_NCPAGING_ERR_NOINIT)
			Exit Sub
		End If

		' -- set the base URL for the current page
		m_strBaseURL = GetBaseURL()

		Dim nStartIndex, nEndIndex

		' -- use the Renderer to render the "Previous" link
		Call m_pNCPagingRenderer.RenderPreviousLink( CurrentPage-1, m_nTotalPages, m_strBaseURL & IDS_NCPAGING_INDEX & "=" & CStr(CurrentPage-1) )

		' -- use the Renderer to render the "1 | 2 | 3 | 4 ..." links
		Dim bIsCurrent, nPagingIndex
		For nPagingIndex = 1 To m_nTotalPages
			bIsCurrent = CBool(nPagingIndex = CurrentPage)
			Call m_pNCPagingRenderer.RenderPagingLink( nPagingIndex, m_nTotalPages, bIsCurrent, m_strBaseURL & IDS_NCPAGING_INDEX & "=" & CStr(nPagingIndex) )
		Next

		' -- use the Renderer to render the "Next" link
		Call m_pNCPagingRenderer.RenderNextLink( CurrentPage+1, m_nTotalPages, m_strBaseURL & IDS_NCPAGING_INDEX & "=" & CStr(CurrentPage+1)  )
	End Sub

	' ---- [Public] To remove propagation of a query string parameter to the next ASP page
	Public Sub StopPropagateParameter( strName )
		If Not m_dictCallerEnforcedStopQParams.Exists( strName ) Then
			Call m_dictCallerEnforcedStopQParams.Add( strName, strName )
		End If
	End Sub

	' ---- [Public] Property that returns the query string that specifies the CurrentPage value for this CNCPagingControl
	Property Get QueryStringForCurrentPage
		QueryStringForCurrentPage = IDS_NCPAGING_INDEX & "=" & CurrentPage
	End Property

	' ---- [Private] Generate the base URL for the Paging links
	Private Function GetBaseURL()
		Dim pQParam, strParamName
		GetBaseURL = Request.ServerVariables("URL") & "?"
		For Each pQParam In Request.QueryString
			' mask out some Querystring Params that should not be propagated to the next page
			strParamName = CStr(pQParam)
			If (strParamName <> IDS_NCPAGING_INDEX) And (strParamName <> IDS_NCPAGINGCONFIG_QPARAM_NAME) And _
													(Not m_dictCallerEnforcedStopQParams.Exists(strParamName)) Then
				GetBaseURL = GetBaseURL & Server.URLEncode(pQParam) & "=" & Server.URLEncode( Request.QueryString(pQParam) ) & "&"
			End If
		Next
	End Function

End Class

%>