<!-- #include virtual="/NR/System/WBC/Internals/Main/WBCApp.inc"-->

<!-- #include virtual="/NR/System/WBC/Customizable/WBCFuncPagesConfig/UIStringConstants/PropertiesDlgUIStrings.inc"-->
<!-- #include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/PropertiesDialog/Common/PropertiesDlgConstants.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/ChannelOpHooks.inc" -->
<!-- #include virtual="/NR/System/WBC/Customizable/Hooks/ChannelOpHooks.inc"-->

<!-- #include virtual="/NR/System/WBC/Internals/Integration/InternalHooks/WBCFuncPagesHooks/PropertiesDlgHooks.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/RevisionBrowser/
'			PropertiesDialog/Common/CNCASPPageManager_SetProperties.asp
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Encapsulate the ASP logic associated with PostingProperitesSave.asp or
'			ChannelPropertiesSave.asp
'
' Implements the INCASPPageManager interface:
'	- Sub InitManager()
'
' Exposes the following properties:
'	- Property CurrentObject
'	- Property RetryURL
'	- Property BasicEditURL
'	- Property ObjectType
'	- Property Error
' ********************************************************************

Class CNCASPPageManager_SetProperties

	' -- The Charset ASP page will be running under
	Public Charset

	' -- objects needed in the ASP page
	Public CurrentObject	' This is either a Page or Channel Object
	Public RetryURL			' The URL for Retry to save the Object
	Public BasicEditURL		' The original URL to open the properties
	Public ObjectType		' The current object type
	Public Error			' The error mesage

	Private m_pFormReader

	' ---- [Private] Class standard initialization routine
	Private Sub Class_Initialize()
		Error = ""
	End Sub


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

		Call SetupCurrentObject()
		Call SetupRetryURL()
		Call SetupCharset()
		Call SetupFormReader()
		Call SetupBasicEditURL()
		Call SetUpObjectType()
		Call SetProperties()

		' Invoke internal Hook
		Call OnInternalAfterPropertiesSave( Me )

	End Sub


	' ---- [Private] Initialize the current page or channel
	Private Sub SetupCurrentObject()
		Set CurrentObject = AutoSession.This
	End Sub


	' ---- [Private] Initialize the retry url when error happen
	Private Sub SetupRetryURL()
		If (CurrentObject.IsChannel) Then
			RetryURL =  "ChannelPropertiesSave.asp?" & CurrentObject.QueryStringModeUpdate
		ElseIf (CurrentObject.IsPosting) Then
			RetryURL =  "PostingPropertiesSave.asp?" & CurrentObject.QueryStringModeUpdate
		Else
			Error = IDS_UI_ERROR_INVALID_INTERFACE
			Exit Sub
		End If
	End Sub


	' ---- [Private] Initialize the Charset of this page
	Private Sub SetupCharset()
		Charset = CurrentObject.Charset
	End Sub


	' ---- [Private] Initialize the form reader
	Private Sub SetupFormReader()
		Set m_pFormReader = GetInitializedFormReaderSafe( False )
	End Sub


	' ---- [Private] Initialize the form reader
	Private Sub SetupBasicEditURL()
		BasicEditURL = GetWBCBasicEditURL( CurrentObject )
	End Sub


	' ---- [Private] Initialize the object type
	Private Sub SetUpObjectType()
		If CurrentObject.IsChannel Then
			ObjectType = "Channel"
		Else
			ObjectType = "Page"
		End If
	End Sub


	' ---- [Public] Create the hidden field again
	Public Sub GenerateFormElements()
		Call GenerateRoundTripFormElements()
	End Sub


	' ---- [Private] Save all the properties to the server
	Private Sub SetProperties()

		' check the form is empty or not
		If (m_pFormReader.Form.Count <= 0) Then
			Error = IDS_UI_ERROR_NO_OF_HIDDEN_FIELD
			Exit Sub
		End If

		' get the Root Node of XML
		Dim pXMLRootNode
		Set pXMLRootNode = GetRootNodeFromXMLTree()
		If (pXMLRootNode is Nothing) Then
			Exit Sub
		End If


		' let the AutoSession to capture the error
		On Error Resume Next

		' Call hook function to invoke customzied script before the operation
		If InternalBeforeUpdateObject() = False Then
			' hook function returns False to cancel the operation

			Exit Sub
		End If

		' parse the element in XML object to set the modified properites
		Dim	bResult
		bResult = true
		bResult = SetPropertiesBaseOnXMLTree( pXMLRootNode )

		If (bResult) Then
		    ' Everything okay, then commit the change and return true
		    Call AutoSession.CommitAll()
		Else
			' There is an error so rollback and return false
			Call AutoSession.RollbackAll()
		End If
		' if error encountered, leave the function and return false
		If (AutoSession.LastError.ID <> 0) Then

			Dim strErrorDescription
			Dim pASPUtils
			Set pASPUtils = GetInitializedASPUtils()
			Error = pASPUtils.HTMLEncode( AutoSession.LastError.Description )
			Exit Sub
		End If

		' Call hook function to invoke customzied script after the operation
		Call InternalAfterUpdateObject()
	End Sub

	' ---- [Private] Create the DOM Object and return the Root Node of the XML
	Private Function CreateXMLObjectSafe()
		On Error Resume Next
		Set CreateXMLObjectSafe = Nothing
		Set CreateXMLObjectSafe = Server.CreateObject( "Microsoft.XmlDom" )
	End Function

	' ---- [Private] Create the DOM Object and return the Root Node of the XML
	Private Function GetRootNodeFromXMLTree()

		Set GetRootNodeFromXMLTree = Nothing
		' initial variable and get the changed properites value form submit form
		Dim strModifiedProps
		strModifiedProps = m_pFormReader.Form( "WBC_inhidProperties" )
		If strModifiedProps = "" Then
			Exit Function
		End If

		' create the XML object
		Dim pObjXmlDoc
		Set pObjXmlDoc = CreateXMLObjectSafe()
		If (pObjXmlDoc Is Nothing) Then
			Error = IDS_UI_ERROR_CANNOT_XML_OBJECT
			Exit Function
		End If

		' load the XML data from Submit form to XML object.
		pObjXmlDoc.async = false
		pObjXmlDoc.loadXml(strModifiedProps)

		Dim pXMLRootNode
		Set pXMLRootNode = pObjXmlDoc.documentElement
		' if the XML is empty display error message or fail to retrieve root XML tag
		If (pXMLRootNode Is Nothing) Then
			Error = IDS_UI_ERROR_XML_EMPTY
			Exit Function
		End If

		Set GetRootNodeFromXMLTree = pObjXmlDoc.documentElement
	End Function

	' ---- [Private] Recursive call to retrieve each XML node and call the SetProperty
	Private Function SetPropertiesBaseOnXMLTree( pXMLNode )

		If (IsElementNode( pXMLNode )) Then
			SetPropertiesBaseOnXMLTree = SetProperty( pXMLNode )
		Else
			Dim i
			For i = 0 To (pXMLNode.childNodes.length - 1)
				SetPropertiesBaseOnXMLTree = SetPropertiesBaseOnXMLTree( pXMLNode.childNodes(i) )
				If (Not SetPropertiesBaseOnXMLTree) Then
					Exit Function
				End If
			Next
		End If
	End Function

	' ---- [Private] Check whether the Node is a element of text or CData section or EOP tag
	Private Function IsElementNode( pXMLNode )

		If (pXMLNode.firstChild.nodeType = ID_XML_NODE_TEXT) Or _
		   (pXMLNode.firstChild.nodeType = ID_XML_NODE_CDATA_SECTION) Or _
		   (pXMLNode.nodeName = IDS_XML_EOP) Or _
		   (pXMLNode.nodeName = IDS_XML_STARTDATE) Or _
		   (pXMLNode.nodeName = IDS_XML_EXPIRYDATE) Then
			IsElementNode = True
		Else
			IsElementNode = False
		End If
	End Function


	' ---- [Private] Hook function to invoke customzied script before the operation
	Private Function InternalBeforeUpdateObject()
		Dim bResult

		If (CurrentObject.IsChannel) Then
			' call Internal Hook
			bResult = OnInternalBeforeUpdateChannel( CurrentObject )
			' call external Customizable Hook
			bResult = bResult And OnBeforeUpdateChannel( CurrentObject )
		ElseIf (CurrentObject.IsPosting) Then
			' call Internal Hook
			bResult = OnInternalBeforeUpdatePosting( CurrentObject )
			' call Customizable Hook
			bResult = bResult And OnBeforeUpdatePosting( CurrentObject )
		Else
			' Go ahead with the operation
			bResult = True
		End if

		InternalBeforeUpdateObject = bResult
	End Function


	' ---- [Public] hook function to invoke customzied script after the operation
	Private Sub InternalAfterUpdateObject()
		Dim bResult

		If (CurrentObject.IsChannel) Then

			' call Internal Hook
			Call OnInternalAfterUpdateChannel( CurrentObject )
			' call Customizable Hook
			Call OnAfterUpdateChannel( CurrentObject )

		ElseIf (CurrentObject.IsPosting) Then

			' call Internal Hook
			Call OnInternalAfterUpdatePosting( CurrentObject )
			' call Customizable Hook
			Call OnAfterUpdatePosting( CurrentObject )

		Else
			' Do nothing
		End if
	End Sub


	' ---- [Private] set the current property value into server based on it name
	Private Function SetProperty( pXMLNode )
		Dim strNodeName
		Dim strNodeValue

		SetProperty = False

		If pXMLNode.nodeType <> ID_XML_NODE_ELEMENT Then
			SetProperty = True
			Exit Function
		End If

		' get the current Node name and its value
		strNodeName = pXMLNode.nodeName
		strNodeValue = GetXMLElementValue( pXMLNode )

		' let the AutoSession to capture the error
		On Error Resume Next

		' Note: It is necessary to write a guard to check whether the value is changed or not
		' because the comparison between the new and original value in client side script
		' doesn't work properly in some languages such as "Chinese". This causes the client side
		' to send the original value to server. Therefore, this guard uses to check whether
		' the value is changed or not and maintain the value won't change if the user
		' doesn't change anything.
		Select Case strNodeName
			Case IDS_XML_NAME
				If (CurrentObject.Name <> strNodeValue) Then
					CurrentObject.Name = strNodeValue
				End If
			Case IDS_XML_DISPLAYNAME
				If (CurrentObject.DisplayName <> strNodeValue) Then
					CurrentObject.DisplayName = strNodeValue
				End If
			Case IDS_XML_DESCRIPTION
				If (CurrentObject.Description <> strNodeValue) Then
					CurrentObject.Description = strNodeValue
				End If
			Case IDS_XML_STARTDATE

				' get the date object from XML
				Dim dtStartDate
				dtStartDate = GetXMLDateValue( pXMLNode )

				If (CurrentObject.StartDate <> dtStartDate ) Then
					CurrentObject.StartDate = dtStartDate
				End If
			Case IDS_XML_EXPIRYDATE

				' get the date object from XML
				Dim dtExpiryDate
				dtExpiryDate = GetXMLDateValue( pXMLNode )

				If (CurrentObject.ExpiryDate <> dtExpiryDate) Then
					CurrentObject.ExpiryDate = dtExpiryDate
				End If
			Case IDS_XML_ISIMPORTANT
				If (CurrentObject.IsImportant <> CBool( strNodeValue )) Then
					CurrentObject.IsImportant = CBool( strNodeValue )
				End If
			Case IDS_XML_ISROBOTFOLLOW
				If (CurrentObject.IsRobotFollowable <> CBool( strNodeValue )) Then
					CurrentObject.IsRobotFollowable = CBool( strNodeValue )
				End If
			Case IDS_XML_ISROBOTINDEX
				If (CurrentObject.IsRobotIndexable <> CBool( strNodeValue )) Then
					CurrentObject.IsRobotIndexable = CBool( strNodeValue )
				End If
			Case IDS_XML_ISHIDDENMODE
				If (CurrentObject.IsHiddenModePublished <> CBool( strNodeValue )) Then
					CurrentObject.IsHiddenModePublished = CBool( strNodeValue )
				End If
			Case IDS_XML_EOP

				' it is a EOP element
				Dim strEopName
				Dim strEopValue
				Dim bIsEopUsesDefault
				strEopName = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_EOPNAME ) )
				strEopValue = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_EOPVALUE ) )
				bIsEopUsesDefault = CBool( GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_EOPDEFAULT ) ) )

				If (bIsEopUsesDefault) Then
					If (Not CurrentObject.CustomProperties(strEopName).UsesDefaultValue) Then
						CurrentObject.CustomProperties(strEopName).ResetToDefault
					End If
				Else
					If (CurrentObject.CustomProperties(strEopName).UsesDefaultValue) Then

						' The original value is using default. So, set the new value to EOP
						CurrentObject.CustomProperties(strEopName).value = strEopValue
					ElseIf (CurrentObject.CustomProperties(strEopName).value <> strEopValue) Then

						' The original value isn't using default and has been changed.
						CurrentObject.CustomProperties(strEopName).value = strEopValue
					End If
				End If
			Case Else
				' do nothing
		End Select
		' if error encountered, leave the function and return false
		If (AutoSession.LastError.ID <> 0) Then
			SetProperty = False
			Exit Function
		End If

		SetProperty = true
	End Function


	' --- [Private] get the Node value base on the input element node
	' the XML with node with CDATA, For example
	'	<PROPS>
	'		<NAME><![CDATA[my name]]></NAME>
	'		<DISPLAYNAME><![CDATA[my display name]]></DISPLAYNAME>
	'		<DESCRIPTION><![CDATA[my description]]></DESCRIPTION>
	'		<STARTDATE>Feb 10 2001, 05:19:35 PM</STARTDATE>
	'		<EXPIRYDATE>Feb 12 2001, 05:19:35 PM</EXPIRYDATE>
	'		<ISIMPORTANT>false</ISIMPORTANT>
	'		<ISROBOTFOLLOW>false</ISROBOTFOLLOW>
	'		<ISROBOTINDEX>true</ISROBOTINDEX>
	'		<ISHIDDENMODE>false</ISHIDDENMODE>
	'		<EOPS>
	'			<EOP>
	'				<EOPNAME><![CDATA[my eop name]]></EOPNAME>
	'				<EOPVALUE><![CDATA[my eop value]]></EOPVALUE>
	'				<EOPDEFAULT>false</EOPDEFAULT>
	'			</EOP>
	'			<EOP>
	'				<EOPNAME><![CDATA[my eop name1 ]]></EOPNAME>
	'				<EOPVALUE><![CDATA[my eop value1]]></EOPVALUE>
	'				<EOPDEFAULT>true</EOPDEFAULT>
	'			</EOP>
	'			..... more EOP tag
	'		</EOPS>
	'	<PROPS>
	Private Function GetXMLElementValue( pXMLNode )
		Dim strNodeValue
		Dim pXMLValueNode
		Set pXMLValueNode = pXMLNode.firstChild

		If (pXMLValueNode.nodeType = ID_XML_NODE_TEXT) Or (pXMLValueNode.nodeType = ID_XML_NODE_CDATA_SECTION) Then
			strNodeValue = pXMLValueNode.nodeValue
		Else
			' This case shouldn't be happen. Since the Recursive function "SetPropertiesBaseOnXMLTree" will guard it.
			strNodeValue = ""
		End If

		GetXMLElementValue = strNodeValue
	End Function


	' --- [Private] get the Date object base on the input element node
	' <STARTDATE>
	'		<YEAR>2001</YEAR>
	'		<MONTH>2</MONTH>
	'		<DAY>28</DAY>
	'		<HOUR>4</HOUR>
	'		<MINUTE>40</MINUTE>
	'		<SECOND>50</SECOND>
	'</STARTDATE>
	Private Function GetXMLDateValue( pXMLNode )
		Dim varDateValue
		Dim nYear
		Dim nMonth
		Dim nDay
		Dim nHour
		Dim nMinute
		Dim nSecond

		' get each value of the date from XML
		nYear = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_YEAR ) )
		nMonth = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_MONTH ) )
		nDay = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_DAY ) )
		nHour = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_HOUR ) )
		nMinute = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_MINUTE ) )
		nSecond = GetXMLElementValue( pXMLNode.selectSingleNode( IDS_XML_SECOND ) )

		' construct the date object
		varDateValue = DateSerial( nYear, nMonth, nDay ) + TimeSerial( nHour, nMinute, nSecond )
		GetXMLDateValue = CDate( varDateValue )
	End Function
End Class

%>
