' HtmlReport_RecentActions
Option Explicit On
' #################################################################
' Recent Actions Report

' Add the following text to KB articles related to this script:
' Note: This summary page is not currently accessible for customisation.
'
' Version: 1.02 (27/04/2016)

' Usage: Task Manager, Recent Actions folder
' #################################################################

' Module variables
Private sb As StringBuilder
Private UserPk As Integer

Public Function Main(userInterface As ISUserInterfaceBL, reports As ISfinReports, parameters As ISKeyValueList, ByRef html As String, ByRef dataSet As DataSet, ByRef stateData As ISKeyValueList, ByVal formData As ISKeyValueList, ByVal command As String, ByVal commandParameters As ISKeyValueList, ByRef commandMessage As String, ByRef commandMessageWarning As Boolean) As Boolean

  Dim GenerateHtml As Boolean

  ' Assume Success
  Main = True

  ' Initialise
  sb = New StringBuilder()
  UserPk = commandParameters.GetInteger("UserPk")

  If finBL.Users.ExistsPk(UserPk) Then
    ' Execute Command
    Select Case UCase(Command)
      Case "", "REFRESH"
        ' Refresh
        GenerateHtml = True

      Case Else
        ' Unhandled Command
        commandMessage = String.Format("Unhandled command '{0}' was invoked with {1} parameter(s).", Command, commandParameters.Count)
        commandMessageWarning = True
        GenerateHtml = False
    End Select
  Else
    ' Invalid User
    commandMessage = String.Format("Invalid User Pk {0}.", UserPk)
    commandMessageWarning = True
    GenerateHtml = False
  End If

  ' Generate HTML
  If GenerateHtml Then
    ' Heading Block
    AppendBlock_Heading()

    ' Recent Actions Block
    AppendBlock_Summary()

    ' End HTML
    sb.AppendLine("</div>")
    sb.AppendLine("</body>")
    sb.AppendLine("</html>")

    ' Resolve Templates
    Html = finBL.HtmlTemplateUtilities.ResolveTemplates(sb.ToString())
  End If

End Function

Private Sub AppendBlock_Heading()

  ' Begin HTML
  sb.AppendLine("<html>")
  sb.AppendLine("<head>")
  sb.AppendLine("<style>")
  sb.AppendLine("html {font-family:verdana;}")
  sb.AppendLine("body {margin:0;padding:0}")
  sb.AppendLine("h1 {font-size:22px; background-color:#EEEEEE; border-bottom:1px solid silver; margin-bottom:4px; padding:4px;}")
  sb.AppendLine("h2 {font-size:14px; background-color:#EEEEEE; border:1px solid silver; margin-top:4px; margin-bottom:4px; padding:2px;}")
  sb.AppendLine("small {font-size:8pt}")
  sb.AppendLine("table.ISTableData {border-collapse: collapse;}")
  sb.AppendLine(".ISTableData td, th {font-size:8pt; padding:4px;}")
  sb.AppendLine(".ISTableData th {border:1px solid silver;} {background:#f0f0f0; color:navy}")
  sb.AppendLine(".ISTableData td {border-bottom:1px solid #EEEEEE}")
  sb.AppendLine(".ISRowBackground {}")
  sb.AppendLine(".ISRowBackgroundAlt {background-color:#DCFFDC}")
  sb.AppendLine(".ISRowBackground2 {color:#808080}")
  sb.AppendLine(".ISRowBackgroundAlt2 {background-color:#F5FFF5; color:#808080}")
  sb.AppendLine("div.messageouter {margin-bottom:4px; margin-top:4px; text-align:left;}")
  sb.AppendLine("div.message_info {background-image:url(images/Info.gif); background-position:0px 0px; background-repeat:no-repeat; color:black; padding:2px; padding-left:20px; text-align:left; font-size:8.5pt; height:16px;}")
  sb.AppendLine("</style>")
  sb.AppendLine("</head>")
  sb.AppendLine("<body>")

  ' Heading
  sb.AppendLine("<h1>Recent Actions</h1>")

End Sub

Private Sub AppendBlock_Summary()

  Dim RecentAction As ISRecentAction
  Dim RecentActions As ISRecentActions
  Dim RowAlt As Boolean
  Dim Url As String

  ' Recent Actions
  If userPk = finBL.CurrentUser.UserPk Then
    ' Current User
    RecentActions = finBL.RecentActions
  Else
    ' Load Recent Actions for another User
    RecentActions = finbl.CreateRecentActions
    RecentActions.LoadPk(UserPk)
  End If

  sb.AppendLine("<div style='padding:0.5em; width:100%'>")

  If RecentActions.Count = 0 Then
    sb.AppendFormat("<small>There are no recent actions for user '<b>{0}</b>'.</small>",  finBL.Users.ItemByPk(UserPk).UserId)
  Else
    sb.AppendLine("<table class='ISTableData' width='100%'>")
    sb.AppendLine("<thead>")

    sb.AppendLine("<col style='width:10em'/>")
    sb.AppendLine("<col style='width:2em'/>")
    sb.AppendLine("<col style='width:2em'/>")
    sb.AppendLine("<col style='width:2em'/>")
    sb.AppendLine("<col style='width:8px'/>")
    sb.AppendLine("<col/>")
    sb.AppendLine("<col style='width:3em'/>")
    sb.AppendLine("<tr>")
    sb.AppendLine("<th align='right'>Date</th>")
    sb.AppendLine("<th align='left'>Action</th>")
    sb.AppendLine("<th align='left'>Source</th>")
    sb.AppendLine("<th align='left'>Code</th>")
    sb.AppendLine("<th align='center'></th>")
    sb.AppendLine("<th align='left'>Description</th>")
    sb.AppendLine("<th align='left'>Process</th>")
    sb.AppendLine("</tr>")

    sb.AppendLine("</thead>")
    sb.AppendLine("<tbody>")

    ' Actions
    For Each RecentAction In RecentActions
      With RecentAction
        If .ActionDate.Date = finBL.TimeZoneFunctions.GetCurrentDatabaseDate() Then
          If RowAlt Then
            sb.Append("<tr class='ISRowBackgroundAlt'")
          Else
            sb.Append("<tr class='ISRowBackground'")
          End If
        Else
          If RowAlt Then
            sb.Append("<tr class='ISRowBackgroundAlt2'")
          Else
            sb.Append("<tr class='ISRowBackground2'")
          End If
        End If
        sb.Append(">")
        RowAlt = Not RowAlt

        sb.AppendFormat("<td align='right'>{0}</td>", Format(.ActionDate, finBL.DateFormatGrids(True)))
        sb.AppendFormat("<td><nobr>{0}</nobr></td>", finBL.HtmlEncode(finBL.Runtime.TextUtilities.SplitWordsAtCapitals(.ActionType)))
        sb.AppendFormat("<td><nobr>{0}</nobr></td>", finBL.HtmlEncode(finBL.Runtime.TextUtilities.SplitWordsAtCapitals(.SourceObjectType)))

        ' Code (open associated form)
        Select Case UCase(.SourceObjectType)
          Case "ACCOUNT"
            ' Account
            sb.AppendFormat("<td><a href='app://FormShow?form=Accounts&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourceId)

          Case "ACCOUNTAPP"
            ' Account Application
            sb.AppendFormat("<td><a href='app://FormShow?form=AccountApps&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourceId)

          Case "ACCOUNTLOG"
            ' Account Log
            sb.AppendFormat("<td><a href='app://FormShow?form=AccountLog&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "ACCOUNTAPPLOG"
            ' Account Application Log
            sb.AppendFormat("<td><a href='app://FormShow?form=AccountAppLog&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "CLIENTLOG"
            ' Client Log
            sb.AppendFormat("<td><a href='app://FormShow?form=ClientLog&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "MAILBOXITEM"
            ' Mailbox Item
            sb.AppendFormat("<td><a href='app://FormShow?form=MailboxItem&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "SECURITYSTMT"
            ' Security Stmt
            sb.AppendFormat("<td><a href='app://FormShow?form=SecurityStmts&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourceId)

          Case "SECURITYSTMTITEMLOG"
            ' Security Stmt Item Log
            sb.AppendFormat("<td><a href='app://FormShow?form=SecurityStmtItemLog&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "SECURITYSTMTLOG"
            ' Security Stmt Log
            sb.AppendFormat("<td><a href='app://FormShow?form=SecurityStmtLog&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "USERLOG"
            ' User Log
            sb.AppendFormat("<td><a href='app://FormShow?form=UserLog&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourcePk)

          Case "WORKFLOW"
            ' Workflow
            sb.AppendFormat("<td><a href='app://FormShow?form=Workflows&id={1}'>{0}</a></td>",finBL.HtmlEncode(.SourceId),.SourceId)

          Case Else
            sb.AppendLine("<td>Unknown Object Type</td>")
        End Select

        ' Find in Task Manager (exclude certain types that will never appear in the Task Manager)
        Select Case UCase(.SourceObjectType)
          Case ""
            sb.AppendLine("<td></td>")

          Case Else
            Url = String.Format("app://FormAction?form=TaskManager&action=FindItem&type={0}&pk={1}", finBL.Runtime.HtmlUtilities.UrlEncode(.SourceObjectType), .SourcePk)
            sb.Append("<td><a title='Locate in Task Manager' href='" & Url & "'>{{icon|FindNext}}</a></td>")
        End Select

        sb.AppendFormat("<td>{0}</td>", finBL.HtmlEncode(.Description))
        sb.AppendFormat("<td><nobr>{0}</nobr></td>", finBL.HtmlEncode(.Process))
      End With
    Next

    ' End Table
    sb.AppendLine("</tbody>")
    sb.AppendLine("</table>")
  End If

  sb.AppendLine("</div>")

End Sub