<%
' ********************************************************************
' 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/CNCPagingConfigControl.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Responsible for Displaying PagingConfig droplist
'
' Public Methods and Properties Exposed:
'	- Sub InitPagingConfigControl( strPagingConfigID, nDefaultItemsPerPage, bPersistedAcrossSessions )
'	- Sub DisplayPagingConfigControl()
'	- Sub StopPropagateParameter( strName )
'	- Property ItemsPerPage [read/write]
'	- Property IsSetNewValue [read/write]
' ********************************************************************

' -- Error Strings
Const IDS_NCPAGINGCONFIG_ERR_NOINIT = "<div><b>CNCPagingConfigControl Error:</b> InitPagingConfigControl() has not been called before calling DisplayPagingConfigControl().</div>"


' ---- Top level cookie name for CNCPagingConfigControl
Const IDS_NCPAGINGCONFIG_COOKIES_ID = "NCPagingConfig"
' ---- QueryString param name for setting new value for CNCPagingConfigControl
Const IDS_NCPAGINGCONFIG_QPARAM_NAME = "NCPagingConfigNewVal"

' ---- State Cookies persistence duration in days
Const C_INT_PAGINGCONFIG_PERSISTENCE_DURATION = 365


Class CNCPagingConfigControl
	' -- Class public properties
	Public ItemsPerPage
	Public IsSetNewValue

	' -- Mandatory parameters passed in by cleint for controlling the module
	Private m_strPagingConfigInstanceID
	' -- Module internal state management
	Private m_bInitCalled, m_strBaseURL
	' -- Dictionary for keeping track of QParams to stop propagating
	Private m_dictCallerEnforcedStopQParams


	' ---- [Private] Standard Class_Initialize
	Private Sub Class_Initialize()
		m_bInitCalled = False

		' -- some default values
		m_strPagingConfigInstanceID = "NCPagingConfigInstanceID"
		ItemsPerPage = 5
		IsSetNewValue = False

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

	' -------------------------------------------------------------------------
	'
	'	This subroutine initializes the CNCPagingConfigControl with all
	'	mandatory parameters.
	'
	'	@param strPagingConfigID [in] - a unique ID to identify this instance
	'				so that state persistence can be done in an instance-
	'				specifc manner. The result is that multiple
	'				CNCGridControl's can be used in different, or even the
	'				same, ASP page(s) without conflict.
	'
	'	@param nDefaultItemsPerPage [in] - for specifying the default number
	'				of items per page when there is no previous usage history
	'				to initialize the control setting
	'
	'	@param bPersistedAcrossSessions [in] - for specifying whetehr the
	'				state should be persisted across browser sessions. If True,
	'				state will be persisted for a year. If False, the
	'				state will be persisted for the current browser session
	'				only.
	'
	' -------------------------------------------------------------------------
	Public Sub InitPagingConfigControl( strPagingConfigID, nDefaultItemsPerPage, bPersistedAcrossSessions )
		m_bInitCalled = True

		' -- cache passed-in value
		m_strPagingConfigInstanceID = strPagingConfigID
		ItemsPerPage = nDefaultItemsPerPage

		' -- figure out what ItemsPerPage should be
		If ( Request.QueryString(IDS_NCPAGINGCONFIG_QPARAM_NAME) <> "" ) Then
			' -- If we are told of a new ItemsPerPage value from QueryString, store it into Cookies
			Response.Cookies(IDS_NCPAGINGCONFIG_COOKIES_ID)(m_strPagingConfigInstanceID) = Request.QueryString(IDS_NCPAGINGCONFIG_QPARAM_NAME)
			If (bPersistedAcrossSessions = True) Then
				Response.Cookies(IDS_NCPAGINGCONFIG_COOKIES_ID).Expires = Now + C_INT_PAGINGCONFIG_PERSISTENCE_DURATION
			End If

			ItemsPerPage = CLng( Request.QueryString(IDS_NCPAGINGCONFIG_QPARAM_NAME) )
			IsSetNewValue = True

		ElseIf ( Request.Cookies(IDS_NCPAGINGCONFIG_COOKIES_ID)(m_strPagingConfigInstanceID) <> "" ) Then
			' -- If Cookies have value for ItemsPerPage, use it to override the default
			ItemsPerPage = CLng( Request.Cookies(IDS_NCPAGINGCONFIG_COOKIES_ID)(m_strPagingConfigInstanceID) )

			If (bPersistedAcrossSessions = True) Then
				Response.Cookies(IDS_NCPAGINGCONFIG_COOKIES_ID).Expires = Now + C_INT_PAGINGCONFIG_PERSISTENCE_DURATION
			End If

		End If

	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine display the CNCPagingConfigControl as a droplist
	'
	' -------------------------------------------------------------------------
	Public Sub DisplayPagingConfigControl()
		' -- check if InitListControl() has been called
		If (m_bInitCalled = False) Then
			Call Response.Write(IDS_NCPAGINGCONFIG_ERR_NOINIT)
			Exit Sub
		End If

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

%>		<select onChange="document.location = '<%= m_strBaseURL & IDS_NCPAGINGCONFIG_QPARAM_NAME%>=' + this.options[this.selectedIndex].value">
			<option value="5" <% If (ItemsPerPage = 5) Then Response.Write "selected" %>>5</option>
			<option value="10" <% If (ItemsPerPage = 10) Then Response.Write "selected" %>>10</option>
			<option value="15" <% If (ItemsPerPage = 15) Then Response.Write "selected" %>>15</option>
			<option value="20" <% If (ItemsPerPage = 20) Then Response.Write "selected" %>>20</option>
			<option value="-1" <% If (ItemsPerPage = -1) Then Response.Write "selected" %>>All</option>
		</select>
<%
	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


	' ---- [Private] Generate the base URL for the PagingConfig 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_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

%>