<% Class BulkLogController_Class Private m_useLayout Private m_title Private Sub Class_Initialize() m_useLayout = True m_title = "Bulk log" End Sub Public Property Get useLayout useLayout = m_useLayout End Property Public Property Let useLayout(v) m_useLayout = v End Property Public Property Get Title Title = m_title End Property Public Property Let Title(v) m_title = v End Property '------------------------------------------------------------------------------------------------------------------- ' GET /bulk-log : paste emails + dates '------------------------------------------------------------------------------------------------------------------- Public Sub index() Dim csrf, fv csrf = Csrf_Token("bulklog") Set fv = Nothing RenderForm csrf, fv End Sub ' Shared by index and a failed preview: fv (unsaved values) is Nothing for a blank form. Private Sub RenderForm(csrf, fv) Dim today, emailsText, method, outcome, contactedAt, followUp, notes today = Date_Iso(Date) emailsText = "" : method = "email" : outcome = "" : contactedAt = today : followUp = "" : notes = "" If Not fv Is Nothing Then emailsText = fv("emails") : method = fv("method") : outcome = fv("outcome") contactedAt = fv("contacted_at") : followUp = fv("follow_up_date") : notes = fv("notes") End If %> <% End Sub '------------------------------------------------------------------------------------------------------------------- ' POST /bulk-log/preview : show who matched before anything is written '------------------------------------------------------------------------------------------------------------------- Public Sub preview() Dim v, parsed, matches, i, found, k, missing, skipped, toLog, csrf, emailsJoined Set v = ReadForm() If Not v.Exists("emails") Then Exit Sub ' ReadForm already redirected Set parsed = BulkLog_ParseEmails(v("emails")) Set matches = BusinessRepository().FindByEmails(parsed("valid")) Set found = CreateObject("Scripting.Dictionary") Set toLog = CreateObject("Scripting.Dictionary") Set skipped = CreateObject("Scripting.Dictionary") For i = 0 To matches.Count - 1 found(LCase(matches(i)("email") & "")) = 1 If matches(i)("status") = "removed" Then skipped.Add skipped.Count, matches(i) Else toLog.Add toLog.Count, matches(i) End If Next Set missing = CreateObject("Scripting.Dictionary") For Each k In parsed("valid").Keys If Not found.Exists(k) Then missing.Add missing.Count, k Next emailsJoined = Join(parsed("valid").Keys, ";") csrf = Csrf_Token("bulklog") m_title = "Bulk log: review" %> <% End Sub '------------------------------------------------------------------------------------------------------------------- ' POST /bulk-log/run : log the contact on every matching, non-removed business '------------------------------------------------------------------------------------------------------------------- Public Sub run() Dim v, parsed, matches, i, n, contactedAt, followUp Set v = ReadForm() If Not v.Exists("emails") Then Exit Sub contactedAt = Date_Parse(v("contacted_at")) If IsNull(contactedAt) Then contactedAt = Date followUp = Date_Parse(v("follow_up_date")) Set parsed = BulkLog_ParseEmails(v("emails")) Set matches = BusinessRepository().FindByEmails(parsed("valid")) n = 0 For i = 0 To matches.Count - 1 If matches(i)("status") <> "removed" Then BusinessRepository().LogInteraction CLng(matches(i)("id")), v("method"), v("outcome"), v("notes"), _ contactedAt, followUp, False, Null n = n + 1 End If Next If n = 0 Then Flash().AddError "No businesses were updated." Response.Redirect "/bulk-log" End If If IsNull(followUp) Then Flash().Success = "Logged a contact on " & n & " business" & Iif_(n = 1, "", "es") & "." Else Flash().Success = "Logged a contact on " & n & " business" & Iif_(n = 1, "", "es") & ". Follow-up set for " & Date_Fmt(followUp) & "." End If Response.Redirect "/bulk-log" End Sub '------------------------------------------------------------------------------------------------------------------- ' Shared checks '------------------------------------------------------------------------------------------------------------------- ' Validates the CSRF token and the form fields; returns them as a Dictionary (empty after a redirect on error). Private Function ReadForm() Dim v, d, contactedAt, followUp, method, outcome Set v = CreateObject("Scripting.Dictionary") Set ReadForm = v If Not Csrf_Valid("bulklog", F("csrf")) Then Flash().AddError "Invalid form state. Please try again." Response.Redirect "/bulk-log" End If method = F("method") outcome = F("outcome") If Not Businesses_IsKey(Businesses_Methods(), method) Then Flash().AddError "Choose how you contacted them." Response.Redirect "/bulk-log" End If ' "Remove from list" is deliberately not offered in bulk. If outcome = "remove" Or Not Businesses_IsKey(Businesses_Outcomes(), outcome) Then outcome = "" If Len(F("contacted_at")) > 0 Then contactedAt = Date_Parse(F("contacted_at")) If IsNull(contactedAt) Then Flash().AddError "The contact date isn't a valid date." Response.Redirect "/bulk-log" End If If contactedAt > Date + 1 Then Flash().AddError "The contact date can't be in the future." Response.Redirect "/bulk-log" End If End If If Len(F("follow_up_date")) > 0 Then followUp = Date_Parse(F("follow_up_date")) If IsNull(followUp) Then Flash().AddError "The follow-up date isn't a valid date." Response.Redirect "/bulk-log" End If End If Set d = BulkLog_ParseEmails(F("emails")) If d("valid").Count = 0 Then Flash().AddError "Enter at least one valid email address." Response.Redirect "/bulk-log" End If If d("valid").Count > 500 Then Flash().AddError "That's more than 500 addresses; split them into smaller batches." Response.Redirect "/bulk-log" End If v("emails") = Left(F("emails"), 60000) v("method") = method v("outcome") = outcome v("contacted_at") = F("contacted_at") v("follow_up_date") = F("follow_up_date") v("notes") = Left(F("notes"), 10000) Set ReadForm = v End Function Private Function F(name) F = CStr(Request.Form(name)) End Function End Class Dim BulkLogController_Class__Singleton Function BulkLogController() If IsEmpty(BulkLogController_Class__Singleton) Then Set BulkLogController_Class__Singleton = New BulkLogController_Class End If Set BulkLogController = BulkLogController_Class__Singleton End Function %>