<!-- #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/AuthoringMode/EntryProgrammingConstants.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PageOperation/AuthoringMode/InternalProgrammingConstants.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/PageTemplate/PlaceholdersSupport/SetPosting.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PageOperation/AuthoringMode/PageSave/PageSave.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/PlaceholderHooks.inc" -->
<!-- #include virtual="/NR/System/WBC/Customizable/Hooks/PlaceholderHooks.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/AuthoringMode/
'			PageSave/NewPageSave/CNCASPPageManager_NewPageSave.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Encapsulate the ASP logic associated with NewPageSave.asp
'
' Implements the INCASPPageManager interface:
'	- Sub InitManager()
'	- Sub GenerateRoundTripFormElementsForReSubmit
'	- Property URLRetrySaveLink()
'	- Property URLEditAgainLink()
'	- Property URLCancelEditLink()
'	- Property m_bContinueToAuthoring()
'	- Property CharSet()
'	- Property CodePage()
'	- Propertyn URLGoToAuthoringMode()
'	- Property URLGoToEditMode()
'	- Property ShowWarningReport()
'	- Property ErrorString()
'	- Property SourceNode()
' ********************************************************************

Class CNCASPPageManager_NewPageSave


	'm_pPosting...This is the dummy node that we need to get from Autosession.This
	'reason is for miltiview we are faking a real node
	Private m_pPosting

	'Try to do the save again
	Private m_strURLRetrySaveLink

	'Try to do the edit
	Private m_strURLEditAgainLink

	'Try to cancel
	Private m_strURLCancelEditLink

	'Did user pressed continue and quit / continue and save
	Private m_bContinueToAuthoring

	'm_strCharSet for the ErrorStringpage
	Private m_strCharSet

	'm_lCodePage is the posting's code page
	Private m_lCodePage

	'URL used to go back to Authoring mode if everything successful
	Private m_strURLGoToAuthoringMode

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

	'Show Report indicator in case there is filtering problem with the placeholder
	Private m_bShowWarningReport

	'Error string that captures the error so that the asp itself
	'know how to decode the error
	Private m_strError

	'The source node of the connected page if the page is
	'the connected page
	Private m_pSourceNode

	'Did we get a hook fail from the user hook?
	Private m_bHookFailed


	Public Property Get URLRetrySaveLink()
		URLRetrySaveLink = m_strURLRetrySaveLink
	End Property

	Public Property Get URLEditAgainLink()
		URLEditAgainLink = m_strURLEditAgainLink
	End Property

	Public Property Get URLCancelEditLink()
		URLCancelEditLink = m_strURLCancelEditLink
	End Property

	Public Property Get ContinueToAuthoring()
		ContinueToAuthoring = m_bContinueToAuthoring
	End Property

	Public Property Get CharSet()
		CharSet = m_strCharSet
	End Property

	Public Property Get CodePage()
		CodePage = m_lCodePage
	End Property

	Public Property Get URLGoToAuthoringMode()
		URLGoToAuthoringMode = m_strURLGoToAuthoringMode
	End Property

	Public Property Get URLGoToEditMode()
		URLGoToEditMode = m_strURLGoToEditMode
	End Property

	Public Property Get ShowWarningReport()
		ShowWarningReport = m_bShowWarningReport
	End Property

	Public Property Get ErrorString()
		ErrorString = m_strError
	End Property

	Public Property Get SourceNode()
		Set SourceNode = m_pSourceNode
	End Property

	Public Property Get HookFailed()
		HookFailed = m_bHookFailed
	End Property


	' ---- [Private] Class standard initialization routine
	Private Sub Class_Initialize()

		Set m_pPosting = Nothing
		m_strURLRetrySaveLink = "#"
		m_strURLEditAgainLink = "#"
		m_strURLCancelEditLink = "#"
		m_bContinueToAuthoring = False
		m_strCharSet = ""
		m_lCodePage = 0
		m_strURLGoToAuthoringMode = "#"
		m_strURLGoToEditMode = "#"
		m_bShowWarningReport = ""
		m_strError = IDS_E_NO_ERROR
		Set m_pSourceNode = Nothing
		m_bHookFailed = False
		m_bContinueToAuthoring = True

	End Sub


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

		Dim pNewPosting
		Dim pPostingAfterUpdate

		' Initiate the posting object
		Set m_pPosting = AutoSession.This

		If m_pPosting is Nothing Then
			m_strError = IDS_E_PAGE_DOES_NOT_EXIST
		Else

			' -- Set up URL needed for links in the ASP page in case
			'    there is problem and bomb out
			Call SetupSaveAgainLinkURL()
			Call SetupCancelEditLinkURL()
			m_strURLEditAgainLink = SetupAuthoringModeURL(m_pPosting) & "&" & _
						   IDS_QPARAM_WBCACCEPT & "=" & IDS_ACCEPTCONTENT_PHS

			'Get the char set
			Call SetupMLSInfo()

			'Read the required query string
			Call ReadQueryString()

			If ErrorString <> IDS_E_NO_ERROR Then
				Exit Sub
			End If

			Set pNewPosting =  CreateNewPosting(m_pPosting, m_bHookFailed)

			If m_bHookFailed = True Then
				' Hook vetoed the operation, so we get out of the this function
				' And make sure all return link used the virtual node
				m_strURLGoToEditMode = m_strURLCancelEditLink
				m_strURLGoToAuthoringMode = m_strURLEditAgainLink
				Exit Sub
			ElseIf AutoSession.LastError.ID <> 0 Then
				' something wrong during Posting creation
				m_strError = IDS_E_ERR_IN_PAGE_CREATION
				Exit Sub
			Else
				' The new Posting has been created successfully, update it with content.
				' The UpdateExistingPosting() is in PageSave.inc
				Set pPostingAfterUpdate = UpdateExistingPosting(pNewPosting, m_bHookFailed)

				' Hook vetoed the operation, so we get out of this function
				' And we make sure all return link used the new created node
				If m_bHookFailed = True Then
					m_strURLGoToEditMode = GetGoBackToEditModeAndRefreshURL(pNewPosting)
					m_strURLGoToAuthoringMode = GetGoBackToAuthoringModeAndRefreshURL(pNewPosting)
					Exit Sub
				ElseIf AutoSession.LastError.ID <> 0 Then
					' something wrong during Posting update
					m_strError = IDS_E_ERR_IN_PAGE_UPDATE
					Exit Sub
				End If

				' Check to make sure if we need to pop up a warning msg box when 
				' a placeholder type is "attached as icon".
				Call CheckForWarning()

			End If

			m_strURLGoToEditMode = GetGoBackToEditModeAndRefreshURL(pPostingAfterUpdate)
			m_strURLGoToAuthoringMode = GetGoBackToAuthoringModeAndRefreshURL(pPostingAfterUpdate)

		End If

	End Sub

	' ---- [Public] Generate HTML hidden form elements to retain user authored content
	'				in case there is any ErrorStringdruing the processing.  If we don't
	'               use this function to retain the user entered contents, the resubmit will
	'               loss the user entered info
	Public Sub GenerateRoundTripFormElementsForReSubmit()
		Call GenerateRoundTripFormElements()
	End Sub


	' ---- [Private]
	Private Sub SetupMLSInfo()
		m_strCharSet = m_pPosting.CharSet
		m_lCodePage = m_pPosting.CodePage
	End Sub


	' ---- [Private]
	Private Sub ReadQueryString()
		Dim strSourceNodeGUID
		Dim strContinueAuthoring

		strContinueAuthoring = (Request.QueryString(IDS_QSTR_PAGEOPERATIONAUTHORING_CONTINUEAUTHORING))

		If strContinueAuthoring = IDS_PAGEOPERATIONAUTHORING_CONTINUEAUTHORING_FALSE Then
			m_bContinueToAuthoring = False
		Else
			m_bContinueToAuthoring = True
		End If

		If m_pPosting.IsConnected Then
			strSourceNodeGUID = Request.QueryString(IDS_QSTR_NRPOSTINGGUID)

			If strSourceNodeGUID = "" Then
				m_strError = IDS_E_ERR_SOURCE_PAGE_NOT_FOUND
			Else
				Set m_pSourceNode = AutoSession.Searches.GetByGUID(strSourceNodeGUID)
				If m_pSourceNode Is Nothing Then
					m_strError = IDS_E_ERR_SOURCE_PAGE_NOT_FOUND
				End If
			End If

		End If
	End Sub


	' ---- [Private]
	Private Sub SetupSaveAgainLinkURL()
		m_strURLRetrySaveLink = IDS_FRAMEWORK_VIRTUAL_PATH & IDS_PAGEOPERATIONAUTHORING_PAGESAVENEW_PATH & _
							 "/NewPageSave.asp?" & URLEncodedQueryString()
	End Sub


	' ---- [Private]
	Private Sub SetupCancelEditLinkURL()
		' in the Create Page case, the Cancel link should lead back to Channel
		m_strURLCancelEditLink =  GetWBCBasicEditURL(AutoSession.ThisChannel)
	End Sub


	' ---- [Private]
	Private Function SetupAuthoringModeURL(pObject)
		If (pObject.IsFramed) Then
			' Framed case: since we are just targeting the Posting Frame, we
			'				will just grab the PostingFrame URL
			SetupAuthoringModeURL = AppendQueryString(pObject.URLPostingFrame(False), _
							IDS_QPARAM_NREMIT & "=" & IDS_EMIT_THINEDIT & "&" & _
							IDS_QPARAM_WBCPURPOSE & "=" & IDS_WBCPURPOSE_AUTHORING)
		Else
			SetupAuthoringModeURL = GetWBCAuthoringUrl(pObject)
		End If

	End Function



	' ---- [Private] B/c we may need to filter out placeholder info out based on
	'                placeholder's properties, we need to let the user know if this
	'                is the case.  g_bFiltered is defined in the setposting.inc.
	Private Sub CheckForWarning()
		If g_bFiltered And IDB_SHOW_FILTERED_WARNING Then
			m_bShowWarningReport = True
		Else
			m_bShowWarningReport = False
		End If
	End Sub



	' ---- [Private] Create a posting node
	'				 IMPORTANT: We must passed the newly created node out to
	'                to caller since AutoSesson.This and the created node
	'                has different GUID.
	Private Function CreateNewPosting(pPosting, bHookedFailed)

		' have to supress ErrorStringbecause we perform some operations that may fail
		On Error Resume Next
		Call AutoSession.ClearLastError()

		Dim pChannel
		Dim pTemplate
		Dim pNewPosting

		' set up default return object
		Set CreateNewPosting = pPosting

		' initialize the 2 main vars for Posting creation
		Set pChannel = AutoSession.ThisChannel
		Set pTemplate = pPosting.Template

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

		If m_pPosting.IsConnected Then

			' Call hook function to invoke customzied script before the operation
			If ( OnInternalBeforeCreateConnectedPosting( pChannel, pTemplate, m_pSourceNode ) And _
					OnBeforeCreateConnectedPosting( pChannel, pTemplate, m_pSourceNode ) ) Then

				Set pNewPosting = pChannel.CreateConnectedPosting(pTemplate, m_pSourceNode)
			Else
				' hook function returns False to cancel the operation
				bHookFailed = True
				Exit Function
			End If
		Else

			' Call hook function to invoke customzied script before the operation
			If (OnInternalBeforeCreatePosting( pChannel, pTemplate ) And _
				OnBeforeCreatePosting( pChannel, pTemplate )) Then

				' create new posting in specified Channel and check for ErrorString afterwards
				Set pNewPosting = pChannel.CreatePosting( pTemplate )
			Else
				' hook function returns False to cancel the operation
				bHookFailed = True
				Exit Function
			End If

		End If

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

		' set Posting content and check for error afterwards
		Dim pFormReader
		Set pFormReader = GetInitializedFormReaderSafe(False)

		If (pFormReader Is Nothing) OR (pFormReader.Form.Count <= 0) Then
			Exit Function
		End If

		pNewPosting.Name = pFormReader.Form("NC_Name")
		pNewPosting.DisplayName = pFormReader.Form("NC_DisplayName")

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

		'Do the commit before telling user node is created successfully
		Call AutoSession.CommitAll()

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

		' Call hook function to invoke customzied script after the operation

		If m_pPosting.IsConnected Then
			' call Internal Hook
			Call OnInternalAfterCreateConnectedPosting( m_pSourceNode, pNewPosting)
			' call Customizable Hook
			Call OnAfterCreateConnectedPosting( m_pSourceNode, pNewPosting)
		Else
			' call Internal Hook
			Call OnInternalAfterCreatePosting( pNewPosting )
			' call Customizable Hook
			Call OnAfterCreatePosting( pNewPosting )
		End If

		Set CreateNewPosting = pNewPosting

	End Function


End Class
%>


