<!-- #include virtual="/NR/System/WBC/Internals/Main/WBCApp.inc"-->
<!-- #include virtual="/NR/System/WBC/Customizable/WBCFuncPagesConfig/UIStringConstants/PageOperationUIStrings.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PageOperation/EditMode/InternalProgrammingConstants.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PageOperation/EditMode/EntryProgrammingConstants.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/PageTemplate/PlaceholdersSupport/SetPosting.inc"-->

<!-- #include virtual="/NR/System/WBC/Internals/Integration/InternalHooks/PostingOpHooks.inc" -->
<!-- #include virtual="/NR/System/WBC/Customizable/Hooks/PostingOpHooks.inc"-->

<!-- #include virtual="/NR/System/WBC/Internals/Integration/InternalHooks/WorkflowHooks.inc" -->
<!-- #include virtual="/NR/System/WBC/Customizable/Hooks/WorkflowHooks.inc"-->

<%
' ********************************************************************
' 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/WBCFuncPages/Cross-Browser/PageOperation
'			/PageLifeCycle/CNCASPPageManager_PageLifeCycleOp.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Encapsulate the ASP logic associated with PageLifeCycleOp.asp
'
' Implements the INCASPPageManager interface:
'	- Sub InitManager()
'
' Exposes the following methods/properties:
'	-  Function Charset()
'	-  Function CodePage()
'	-  Function URLRetryActionLink()
'	-  Function URLCancelActionLink()
'	-  Function URLGoToEditMode()
'	-  Function ActionType()
'	-  Function ErrorString()
'	-  Function HookFailed()
' ********************************************************************

Class CNCASPPageManager_PageLifeCycleOp

	'Used for date comparison to chk if the client's page was
	'modified on the server by somebody else
	Private m_strLastModifiedDate

	'm_strCharset for the Error page
	Private m_strCharset

	'm_lCodePage for the Error page
	Private m_lCodePage

	'URL used for retry the action
	Private m_strURLRetryActionLink

	'URL used to cancel out the operation when Error encountered
	Private m_strURLCancelActionLink

	'URL used to go back to Edit mode if everything successful
	Private m_strURLGoToEditMode

	'string that is used for storing the operation description
	Private m_strActionType

	'm_strError
	Private m_strError

	'Was hook successful
	Private m_bHookFailed


	Public Function Charset()
		Charset = m_strCharset
	End Function

	Public Function CodePage()
		CodePage = m_lCodePage
	End Function

	Public Function URLRetryActionLink()
		URLRetryActionLink = m_strURLRetryActionLink
	End Function

	Public Function URLCancelActionLink()
		URLCancelActionLink = m_strURLCancelActionLink
	End Function

	Public Function URLGoToEditMode()
		URLGoToEditMode = m_strURLGoToEditMode
	End Function

	Public Function ActionType()
		ActionType = m_strActionType
	End Function

	Public Function ErrorString()
		ErrorString = m_strError
	End Function

	Public Function HookFailed()
		HookFailed = m_bHookFailed
	End Function


	' ---- [Private] Class standard initialization routine
	Private Sub Class_Initialize()
		m_strLastModifiedDate = ""
		m_strCharset = ""
		m_lCodePage = 0
		m_strURLRetryActionLink = "#"
		m_strURLCancelActionLink = "#"
		m_strURLGoToEditMode = "#"
		m_strActionType = ""
		m_strError =  IDS_E_NO_ERROR
		m_strLastModifiedDate = ""
		m_bHookFailed = False
	End Sub


	' ---- [Public] InitManager will take care of all programming logic in the ASP page
	Public Sub InitManager()

		Dim pPostingAfterUpdate

		If AutoSession.ThisPosting is Nothing Then
			m_strError = IDS_E_PAGE_DOES_NOT_EXIST
		Else
			Call SetupMLSInfo()

			Call ReadQueryString()

			If ErrorString <> IDS_E_NO_ERROR Then
				Exit Sub
			End If

			' -- Set up URL needed for links in the ASP page
			'	 Note: We set this up before the operation b/c
			'          if the certain operation like delete if
			'          succssful, will remove the node so these
			'          URL will cause compiler problems
			Call SetupRetryActionLinkURL()
			Call SetupCancelActionLinkURL()

			'Do the posting operation based on the action type
			Set pPostingAfterUpdate = PerformOperationToPosting(AutoSession.ThisPosting, m_strActionType, m_bHookFailed)

			If m_bHookFailed = True Then
				m_strURLGoToEditMode =  GetWBCBasicEditURL(AutoSession.ThisPosting)
				Exit Sub
			ElseIf AutoSession.LastError.ID <> 0 Then
				' something went wrong during operation
				Exit Sub
			End If

			m_strURLGoToEditMode = GetWBCBasicEditURL(pPostingAfterUpdate)
		End If
	End Sub

	' ---- [Public] Generate HTML hidden form elements to retain user authored content
	Public Sub GenerateRoundTripFormElementsForReSubmit()
		Call GenerateRoundTripFormElements()
	End Sub


	' ---- [Private]
	Private Sub SetupMLSInfo()
		m_strCharset = AutoSession.ThisPosting.Charset
		m_lCodePage = AutoSession.ThisPosting.CodePage
	End Sub


	' ---- [Private]
	Private Sub ReadQueryString()

		' use Case statement to guard against malicious input (XSS attack)
		Select Case Request.QueryString(IDS_QPARAM_WBCACTION)
			Case IDS_ACTION_SUBMIT
				m_strActionType = IDS_ACTION_SUBMIT			
			Case IDS_ACTION_APPROVE
				m_strActionType = IDS_ACTION_APPROVE			
			Case IDS_ACTION_DECLINE
				m_strActionType = IDS_ACTION_DECLINE			
			Case IDS_ACTION_DELETE
				m_strActionType = IDS_ACTION_DELETE
			Case Else
				m_strActionType = ""
		End Select

		If m_strActiontype = "" Then
			m_strError = IDS_E_ERR_ACTION_TYPE_MISSING
		End If

		m_strLastModifiedDate = Request.QueryString(IDS_QSTR_PAGEOPERATIONEDIT_LASTMODIFIEDDATE)

	End Sub


	' ---- [Private]
	Private Sub SetupRetryActionLinkURL()
		m_strURLRetryActionLink = IDS_FRAMEWORK_VIRTUAL_PATH & IDS_PAGEOPERATIONEDIT_PAGELIFECYCLE_PATH  & _
							 "/PageLifeCycleOp.asp?" & URLEncodedQueryString()
	End Sub


	' ---- [Private]
	Private Sub SetupCancelActionLinkURL()
		m_strURLCancelActionLink =  GetWBCBasicEditURL(AutoSession.ThisPosting)
	End Sub


	' ---- [Private]
	Private Function PerformOperationToPosting(pPosting, strAction, bHookFailed)
		' since we are going to perform some operations on Posting, Error can be generated
		On Error Resume Next

		' by default, return Posting itself
		Set PerformOperationToPosting = pPosting

		' actually perform the operation
		If (strAction = IDS_ACTION_SUBMIT) Then

			' Call hook function to invoke customzied script before the operation
			If ( OnInternalBeforeWorkflowOp( pPosting, IDS_ACTION_SUBMIT ) = False Or _
					OnBeforeWorkflowOp( pPosting, IDS_ACTION_SUBMIT ) = False ) Then
				' hook function returns False to cancel the operation
				bHookFailed = True
				Exit Function
			End If

			Call pPosting.Submit()
			
			If (AutoSession.LastError.ID <> 0) Then
				Call AutoSession.RollbackAll()
				m_strError = IDS_E_ERR_IN_SUBMIT
				Exit Function
			End If

			Call AutoSession.CommitAll()
			If (AutoSession.LastError.ID <> 0) Then
				m_strError = IDS_E_ERR_IN_SUBMIT
				Exit Function
			End If

			' Call Internal Hook
			Call OnInternalAfterWorkflowOp( pPosting, IDS_ACTION_SUBMIT )

			' Call Customizable Hook
			Call OnAfterWorkflowOp( pPosting, IDS_ACTION_SUBMIT )

		ElseIf (strAction = IDS_ACTION_APPROVE) Then

			' Call hook function to invoke customzied script before the operation
			If ( OnInternalBeforeWorkflowOp( pPosting, IDS_ACTION_APPROVE ) = False Or _
					OnBeforeWorkflowOp( pPosting, IDS_ACTION_APPROVE ) = False ) Then
				' hook function returns False to cancel the operation
				bHookFailed = True
				Exit Function
			End If

			Call pPosting.Approve()
			
			If (AutoSession.LastError.ID <> 0) Then
				Call AutoSession.RollbackAll()
				m_strError = IDS_E_ERR_IN_APPROVE
				Exit Function
			End If

			Call AutoSession.CommitAll()
			
			If (AutoSession.LastError.ID <> 0) Then
				m_strError = IDS_E_ERR_IN_APPROVE
				Exit Function
			End If

			' Call Internal Hook
			Call OnInternalAfterWorkflowOp( pPosting, IDS_ACTION_APPROVE )

			' Call Customizable Hook
			Call OnAfterWorkflowOp( pPosting, IDS_ACTION_APPROVE )

		ElseIf (strAction = IDS_ACTION_DECLINE) Then

			' Call hook function to invoke customzied script before the operation
			If ( OnInternalBeforeWorkflowOp( pPosting, IDS_ACTION_DECLINE ) = False Or _
					OnBeforeWorkflowOp( pPosting, IDS_ACTION_DECLINE ) = False ) Then
				' hook function returns False to cancel the operation
				bHookFailed = True
				Exit Function
			End If

			Call pPosting.Decline()
			
			If (AutoSession.LastError.ID <> 0) Then
				Call AutoSession.RollbackAll()
				m_strError = IDS_E_ERR_IN_DECLINE
				Exit Function
			End If

			Call AutoSession.CommitAll()
			
			If (AutoSession.LastError.ID <> 0) Then
				m_strError = IDS_E_ERR_IN_DECLINE
				Exit Function
			End If

			' Call Internal Hook
			Call OnInternalAfterWorkflowOp( pPosting, IDS_ACTION_DECLINE )

			' Call Customizable Hook
			Call OnAfterWorkflowOp( pPosting, IDS_ACTION_DECLINE )

		ElseIf (strAction = IDS_ACTION_DELETE) Then

			' Call hook function to invoke customzied script before the operation
			If ( OnInternalBeforeDeletePosting( pPosting ) = False Or _
				OnBeforeDeletePosting( pPosting ) = False ) Then

				' hook function returns False to cancel the operation
				bHookFailed = True
				Exit Function
			End If

			' special case: Posting is deleted, so return the parent Channel
			Dim pParentChannel
			Set pParentChannel = pPosting.Parent

			If (AutoSession.LastError.ID <> 0) Then
				Exit Function
			End If

			' Make the ROM Posting Delete call
			Call pPosting.Delete()
			
			If (AutoSession.LastError.ID <> 0) Then
				Call AutoSession.RollbackAll()
				m_strError = IDS_E_ERR_IN_DELETE
				Exit Function
			End If

			' Call Internal Hook
			Call OnInternalAfterDeletePosting( pParentChannel, pPosting )

			' Call Customizable Hook
			Call OnAfterDeletePosting( pParentChannel, pPosting )

			' Commit Delete after call to the AfterHook, due to 
			' constraints imposed by Content Connector Integration code.
			Call AutoSession.CommitAll()

			' Delete is a special case: the parent Channel will be returned.
			Set PerformOperationToPosting = pParentChannel

		End If

	End Function

End Class
%>


