'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
'#Uses "GlobalUtilities.mmbas"

Option Explicit

Sub Main

    Dim addin As Mm5WebExportAddIn
    Dim webex As WebExport

    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport

    Dim template As String
    Dim bodyString As String

    Dim oneTopic As Topic
    Dim topicData As DataContainer

    On Error GoTo ReportError

    webex.Trace("Entering BoxDataContainerItem.mmbas",2)

    Set oneTopic = webex.ActiveTopic
    Set topicData = oneTopic.DataContainer

    If    webex.GetParameter("Strings.Show.DataContainer") = False _
       Or DoExportDataContainer(topicData) = False Then

       ' Don't export data container section
       bodyString = ""

    Else

	   bodyString = ExportDataContainer(webex, oneTopic, topicData)

    End If

    ' Write out the result
    webex.PushString(bodyString)

    webex.Trace("Exiting BoxDataContainerItem.mmbas",2)

    Exit Sub

ReportError:
    ProcessError(webex)
    Exit Sub

End Sub

Function DoExportDataContainer (topicData As DataContainer)

    DoExportDataContainer  = False

    If topicData.DataContainerType <> mmDataContainerTypeNone Then
    	DoExportDataContainer = True
    End If

End Function

Private Function ExportDataContainer(ByRef webex As WebExport, ByRef oneTopic As Topic, ByRef topicData As DataContainer) As String

	If topicData.DataContainerType = mmDataContainerTypeCustomProperties Then
		ExportDataContainer = ExportCustomProperties(webex, oneTopic, topicData.CustomProperties)
	ElseIf topicData.DataContainerType = mmDataContainerTypeTable Then
		ExportDataContainer = ExportTable(webex, oneTopic, topicData.Table)
	ElseIf topicData.DataContainerType = mmDataContainerTypeCustom Then
		ExportDataContainer = ExportCustomData(webex, oneTopic, topicData.CustomDataContainer)
	Else
		ExportDataContainer = ""
	End If

End Function

Private Function ExportCustomProperties(ByRef webex As WebExport, ByRef oneTopic As Topic, ByRef attBox As CustomProperties) As String
	ExportCustomProperties = attBox.GetAsHTML("", True)
End Function

Private Function ExportTable(ByRef webex As WebExport, ByRef oneTopic As Topic, ByRef tableData As Table) As String
	ExportTable = tableData.GetAsHTML("", True, True, True)
End Function

Private Function ExportCustomData(ByRef webex As WebExport, ByRef oneTopic As Topic, ByRef customData As CustomDataContainer) As String
    On Error GoTo CannotExportCustomData
    
    ' build file name from export page the topic is on plus a counter
    Dim imageFileName As String
    Dim fullImagePath As String
    imageFileName =   webex.Pages.FindPage(oneTopic).BaseFilename _
                    + "_img" _
                    + CStr(webex.GetParameter("CustomParameter.ImageCounter")) _
                    + ".gif"
    fullImagePath = webex.FullExportFilename(imageFileName)

    ' Save image
    customData.SaveAsImage(fullImagePath, mmGraphicTypeGif)

    Dim alternateText As String
    alternateText = webex.GetParameter("Strings.SectionHeaders.DataContainer")

    webex.SetParameter("CustomParameter.DataContainerImage", imageFileName)
    webex.SetParameter("CustomParameter.DataContainerImageText", alternateText)

    ' Increment image counter
    webex.SetParameter("CustomParameter.ImageCounter", _
                       webex.GetParameter("CustomParameter.ImageCounter") + 1)

    ' Read our template
    Dim template As String

    template = webex.ReadFile("BoxDataContainerItemImage.htm")

    ' Process all commands and parameters
    ProcessString webex, template
	ExportCustomData = template
	
	Exit Function
	
	CannotExportCustomData:
	    ExportCustomData = ""
	    Exit Function
End Function
