<%
' ********************************************************************
' 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/State/CNCStateControl.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	A state control which has persistence
'
' Public Interface of the CNCStateControl
'	- Sub InitStateControl( strInstanceID, pStateRenderer, strDefaultStateID, bPersistedAcrossSessions )
'	- Sub AddState( strStateID )
'	- Sub PropagateParameter( strName, strValue )
'	- Sub DisplayStateControl()
'	- Property CurrentState [read/write]
'	- Property IsSetNewState [read]
'
' INCStateRenderer Callback Interface of CNCStateControl
'	- Sub RenderState( strStateID, strRoundTripURL, bIsCurrent )
' *********************************************************************

' -- Error Strings
Const IDS_NCSTATE_ERR_NOINIT = "<div><b>CNCStateControl Error:</b> InitStateControl() has not been called before calling DisplayStateControl().</div>"
Const IDS_NCSTATE_ERR_ENTRIES = "<div><b>CNCStateControl Error:</b> Duplicate State Entries.</div>"
Const IDS_NCSTATE_ERR_CURRENTSTATE_NOTFOUND = "<div><b>CNCStateControl Error:</b> Current state is not found in the list of the managed states. Please check client programming logic.</div>"

' ---- Top level cookie name for CNCStateControl
Const IDS_NCNCSTATE_COOKIES_ID = "NCStateControl"

' ---- Base QueryString param name for the current state
Const IDS_NCNCSTATE_QPARAM_NAME = "NCStateCtrlCurrentState"

' ---- Default persistence duration in days
Const C_INT_STATE_PERSISTENCE_DURATION = 365


Class CNCStateControl

	' -- Returns whether the StateControl has just been set a new state in the current ASP page
	Public IsSetNewState

	' -- Class internal variables
	Private m_bInitCalled
	Private m_strInstanceID
	Private m_strInstanceQParamName
	Private m_pStateRenderer
	Private m_strCurrentState
	Private m_bPersistedAcrossSessions
	Private m_dictCallerEnforcedQParams, m_dictStates

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

		' -- some default values
		m_strInstanceID = "NCStateInstanceID"
		m_strInstanceQParamName = ""
		m_strCurrentState = ""
		m_bPersistedAcrossSessions = False
		IsSetNewState = False

		' -- class internal variables
		Set m_dictCallerEnforcedQParams  = CreateObject("Scripting.Dictionary")
		Set m_dictStates = CreateObject("Scripting.Dictionary")
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine initializes the CNCStateControl with all mandatory
	'	parameters.
	'
	'	@param strInstanceID [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 CNCStateControl's can be used
	'				in different, or even the same, ASP page(s) without conflict.
	'
	'	@param pStateRenderer [in] - a renderer object which supports the
	'				INCStateRenderer defined at the top of this file; for
	'				painting the state in HTML.
	'
	'	@param strDefaultStateID [in] - the default state. State ID is a
	'				unique state identifier that represents a state for the
	'				control to keep track of.
	'
	'	@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 InitStateControl( strInstanceID, pStateRenderer, strDefaultStateID, bPersistedAcrossSessions )
		m_bInitCalled = True

		' -- cache passed-in value
		m_strInstanceID = strInstanceID
		set m_pStateRenderer = pStateRenderer
		m_strCurrentState = strDefaultStateID
		m_bPersistedAcrossSessions = bPersistedAcrossSessions

		m_strInstanceQParamName = IDS_NCNCSTATE_QPARAM_NAME & "_" & m_strInstanceID

		' -- figure out the current state
		If ( Request.QueryString(m_strInstanceQParamName) <> "" ) Then
			' -- If we are told of a new value from QueryString, store it into Cookies
			Response.Cookies(IDS_NCNCSTATE_COOKIES_ID)(m_strInstanceID) = Request.QueryString(m_strInstanceQParamName)
			If (m_bPersistedAcrossSessions = True) Then
				Response.Cookies(IDS_NCNCSTATE_COOKIES_ID).Expires = Now + C_INT_STATE_PERSISTENCE_DURATION
			End If
			m_strCurrentState = CStr( Request.QueryString(m_strInstanceQParamName) )
			IsSetNewState = True
		ElseIf ( Request.Cookies(IDS_NCNCSTATE_COOKIES_ID)(m_strInstanceID) <> "" ) Then
			' -- If Cookies have value for us, use it and extend its life
			m_strCurrentState = CStr( Request.Cookies(IDS_NCNCSTATE_COOKIES_ID)(m_strInstanceID) )
			If (m_bPersistedAcrossSessions = True) Then
				Response.Cookies(IDS_NCNCSTATE_COOKIES_ID).Expires = Now + C_INT_STATE_PERSISTENCE_DURATION
			End If
		End If
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine adds a state to the CNCStateControl.
	'
	'	@param strStateID [in] - a unique state identifier that represents a
	'				state for the control to keep track of.
	'
	' -------------------------------------------------------------------------
	Public Sub AddState( strStateID )
		' Avoid putting duplicate columns into dictionary
		If ( m_dictStates.Exists(strStateID) ) Then
			Call Response.Write(IDS_NCSTATE_ERR_ENTRIES)
			Exit Sub
		Else
			Call m_dictStates.Add( strStateID, strStateID )
		End If
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine allows client to enforce some QueryString parameters
	'	to over-ride default behavior of retaining all querysting parameters
	'	when round-tripping.
	'
	'	@param strName [in] - name of the querystring parameter
	'
	'	@param strValue [in] - value of the querystring parameter to be
	'							enforced.
	'
	' -------------------------------------------------------------------------
	Public Sub PropagateParameter( strName, strValue )
		' Remove old one if it exists already
		If (m_dictCallerEnforcedQParams.Exists( strName ) = True) Then
			Call m_dictCallerEnforcedQParams.Remove( strName )
		End If
		' Add the new one to the internal dictionary
		Call m_dictCallerEnforcedQParams.Add( strName, strValue )
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine displays the CNCStateControl.  For each state that the
	'	caller has defined, a renderer call back will be invoked to give the
	'	renderer a chance to render the state.
	'
	' -------------------------------------------------------------------------
	Public Sub DisplayStateControl()
		' -- check if InitStateControl() has been called
		If (m_bInitCalled = False) Then
			Call Response.Write(IDS_NCSTATE_ERR_NOINIT)
			Call Response.End
			Exit Sub
		End If

		' -- verify that m_strCurrentState is one of the states that we have been passed
		If (m_dictStates.Exists(m_strCurrentState) = False) Then
			Call Response.Write(IDS_NCSTATE_ERR_CURRENTSTATE_NOTFOUND)
			Call Response.End
			Exit Sub
		End If


		' -- set a default state if it is not already known
		Dim aStates
		aStates = m_dictStates.Keys
		If m_strCurrentState = "" Then
			m_strCurrentState = aStates(0)
		End If

		' -- tell the renderer to render the states, passing him the round trip URLs
		Dim nIndex, strState, strRoundTripURL, bIsCurrent
		For nIndex = 0 To UBound(aStates)
			strState = aStates(nIndex)
			bIsCurrent = (m_strCurrentState = strState)
			strRoundTripURL = GetRoundTripURL(strState)
			Call m_pStateRenderer.RenderState(strState, strRoundTripURL, bIsCurrent)
		Next
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine exposes the current state the control is in
	'
	' -------------------------------------------------------------------------
	Public Property Get CurrentState()
		CurrentState = m_strCurrentState
	End Property

	' -------------------------------------------------------------------------
	'
	'	This subroutine allows client to change the state
	'	when round-tripping.
	'
	'	@param strState [in] - name of the state
	'
	' -------------------------------------------------------------------------
	Public Property Let CurrentState( strState )
		' -- verify that m_strCurrentState is one of the states that we have been passed
		If (m_dictStates.Exists(strState) = False) Then
			Call Response.Write(IDS_NCSTATE_ERR_CURRENTSTATE_NOTFOUND)
			Call Response.End
			Exit Property
		End If

		Response.Cookies(IDS_NCNCSTATE_COOKIES_ID)(m_strInstanceID) = strState
		If (m_bPersistedAcrossSessions = True) Then
			Response.Cookies(IDS_NCNCSTATE_COOKIES_ID).Expires = Now + C_INT_STATE_PERSISTENCE_DURATION
		End If
		m_strCurrentState = CStr( strState )
	End Property

	' ---- [Private] Return the roundtrip sort URL for a certain state.  The built URL will
	'                have the current query strings, enforced query strings
	Private Function GetRoundTripURL( strState )
		GetRoundTripURL = Request.ServerVariables("URL") & "?" & _
							m_strInstanceQParamName & "=" & strState & "&"

		' -- Deal with the caller enforced QParams
		Dim aKeys, aItems, nIndex
		aItems = m_dictCallerEnforcedQParams.Items
		aKeys = m_dictCallerEnforcedQParams.Keys
		For nIndex = 0 To UBound(aKeys)
			GetRoundTripURL = GetRoundTripURL & aKeys(nIndex) & "=" & Server.URLEncode(aItems(nIndex)) & "&"
		Next

		' -- Regenerate other QParams that are not managed by this Control
		Dim pQParam, strParamName, pSubQParam
		For Each pQParam In Request.QueryString
			strParamName = CStr(pQParam)
			' Mask out all QParam's that are from this tree
			If ((strParamName <> m_strInstanceQParamName) AND _
					Not m_dictCallerEnforcedQParams.Exists(strParamName) ) Then
				For Each pSubQParam In Request.QueryString(pQParam)
					GetRoundTripURL = GetRoundTripURL & Server.URLEncode(strParamName) & "=" & Server.URLEncode(CStr(pSubQParam)) & "&"
				Next
			End If
		Next
	End Function

End Class




%>