Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

199 řádky
7.6KB

  1. <%
  2. Class BulkLogController_Class
  3. Private m_useLayout
  4. Private m_title
  5. Private Sub Class_Initialize()
  6. m_useLayout = True
  7. m_title = "Bulk log"
  8. End Sub
  9. Public Property Get useLayout
  10. useLayout = m_useLayout
  11. End Property
  12. Public Property Let useLayout(v)
  13. m_useLayout = v
  14. End Property
  15. Public Property Get Title
  16. Title = m_title
  17. End Property
  18. Public Property Let Title(v)
  19. m_title = v
  20. End Property
  21. '-------------------------------------------------------------------------------------------------------------------
  22. ' GET /bulk-log : paste emails + dates
  23. '-------------------------------------------------------------------------------------------------------------------
  24. Public Sub index()
  25. Dim csrf, fv
  26. csrf = Csrf_Token("bulklog")
  27. Set fv = Nothing
  28. RenderForm csrf, fv
  29. End Sub
  30. ' Shared by index and a failed preview: fv (unsaved values) is Nothing for a blank form.
  31. Private Sub RenderForm(csrf, fv)
  32. Dim today, emailsText, method, outcome, contactedAt, followUp, notes
  33. today = Date_Iso(Date)
  34. emailsText = "" : method = "email" : outcome = "" : contactedAt = today : followUp = "" : notes = ""
  35. If Not fv Is Nothing Then
  36. emailsText = fv("emails") : method = fv("method") : outcome = fv("outcome")
  37. contactedAt = fv("contacted_at") : followUp = fv("follow_up_date") : notes = fv("notes")
  38. End If
  39. %>
  40. <!--#include file="../views/BulkLog/index.asp" -->
  41. <%
  42. End Sub
  43. '-------------------------------------------------------------------------------------------------------------------
  44. ' POST /bulk-log/preview : show who matched before anything is written
  45. '-------------------------------------------------------------------------------------------------------------------
  46. Public Sub preview()
  47. Dim v, parsed, matches, i, found, k, missing, skipped, toLog, csrf, emailsJoined
  48. Set v = ReadForm()
  49. If Not v.Exists("emails") Then Exit Sub ' ReadForm already redirected
  50. Set parsed = BulkLog_ParseEmails(v("emails"))
  51. Set matches = BusinessRepository().FindByEmails(parsed("valid"))
  52. Set found = CreateObject("Scripting.Dictionary")
  53. Set toLog = CreateObject("Scripting.Dictionary")
  54. Set skipped = CreateObject("Scripting.Dictionary")
  55. For i = 0 To matches.Count - 1
  56. found(LCase(matches(i)("email") & "")) = 1
  57. If matches(i)("status") = "removed" Then
  58. skipped.Add skipped.Count, matches(i)
  59. Else
  60. toLog.Add toLog.Count, matches(i)
  61. End If
  62. Next
  63. Set missing = CreateObject("Scripting.Dictionary")
  64. For Each k In parsed("valid").Keys
  65. If Not found.Exists(k) Then missing.Add missing.Count, k
  66. Next
  67. emailsJoined = Join(parsed("valid").Keys, ";")
  68. csrf = Csrf_Token("bulklog")
  69. m_title = "Bulk log: review"
  70. %>
  71. <!--#include file="../views/BulkLog/preview.asp" -->
  72. <%
  73. End Sub
  74. '-------------------------------------------------------------------------------------------------------------------
  75. ' POST /bulk-log/run : log the contact on every matching, non-removed business
  76. '-------------------------------------------------------------------------------------------------------------------
  77. Public Sub run()
  78. Dim v, parsed, matches, i, n, contactedAt, followUp
  79. Set v = ReadForm()
  80. If Not v.Exists("emails") Then Exit Sub
  81. contactedAt = Date_Parse(v("contacted_at"))
  82. If IsNull(contactedAt) Then contactedAt = Date
  83. followUp = Date_Parse(v("follow_up_date"))
  84. Set parsed = BulkLog_ParseEmails(v("emails"))
  85. Set matches = BusinessRepository().FindByEmails(parsed("valid"))
  86. n = 0
  87. For i = 0 To matches.Count - 1
  88. If matches(i)("status") <> "removed" Then
  89. BusinessRepository().LogInteraction CLng(matches(i)("id")), v("method"), v("outcome"), v("notes"), _
  90. contactedAt, followUp, False, Null
  91. n = n + 1
  92. End If
  93. Next
  94. If n = 0 Then
  95. Flash().AddError "No businesses were updated."
  96. Response.Redirect "/bulk-log"
  97. End If
  98. If IsNull(followUp) Then
  99. Flash().Success = "Logged a contact on " & n & " business" & Iif_(n = 1, "", "es") & "."
  100. Else
  101. Flash().Success = "Logged a contact on " & n & " business" & Iif_(n = 1, "", "es") & ". Follow-up set for " & Date_Fmt(followUp) & "."
  102. End If
  103. Response.Redirect "/bulk-log"
  104. End Sub
  105. '-------------------------------------------------------------------------------------------------------------------
  106. ' Shared checks
  107. '-------------------------------------------------------------------------------------------------------------------
  108. ' Validates the CSRF token and the form fields; returns them as a Dictionary (empty after a redirect on error).
  109. Private Function ReadForm()
  110. Dim v, d, contactedAt, followUp, method, outcome
  111. Set v = CreateObject("Scripting.Dictionary")
  112. Set ReadForm = v
  113. If Not Csrf_Valid("bulklog", F("csrf")) Then
  114. Flash().AddError "Invalid form state. Please try again."
  115. Response.Redirect "/bulk-log"
  116. End If
  117. method = F("method")
  118. outcome = F("outcome")
  119. If Not Businesses_IsKey(Businesses_Methods(), method) Then
  120. Flash().AddError "Choose how you contacted them."
  121. Response.Redirect "/bulk-log"
  122. End If
  123. ' "Remove from list" is deliberately not offered in bulk.
  124. If outcome = "remove" Or Not Businesses_IsKey(Businesses_Outcomes(), outcome) Then outcome = ""
  125. If Len(F("contacted_at")) > 0 Then
  126. contactedAt = Date_Parse(F("contacted_at"))
  127. If IsNull(contactedAt) Then
  128. Flash().AddError "The contact date isn't a valid date."
  129. Response.Redirect "/bulk-log"
  130. End If
  131. If contactedAt > Date + 1 Then
  132. Flash().AddError "The contact date can't be in the future."
  133. Response.Redirect "/bulk-log"
  134. End If
  135. End If
  136. If Len(F("follow_up_date")) > 0 Then
  137. followUp = Date_Parse(F("follow_up_date"))
  138. If IsNull(followUp) Then
  139. Flash().AddError "The follow-up date isn't a valid date."
  140. Response.Redirect "/bulk-log"
  141. End If
  142. End If
  143. Set d = BulkLog_ParseEmails(F("emails"))
  144. If d("valid").Count = 0 Then
  145. Flash().AddError "Enter at least one valid email address."
  146. Response.Redirect "/bulk-log"
  147. End If
  148. If d("valid").Count > 500 Then
  149. Flash().AddError "That's more than 500 addresses; split them into smaller batches."
  150. Response.Redirect "/bulk-log"
  151. End If
  152. v("emails") = Left(F("emails"), 60000)
  153. v("method") = method
  154. v("outcome") = outcome
  155. v("contacted_at") = F("contacted_at")
  156. v("follow_up_date") = F("follow_up_date")
  157. v("notes") = Left(F("notes"), 10000)
  158. Set ReadForm = v
  159. End Function
  160. Private Function F(name)
  161. F = CStr(Request.Form(name))
  162. End Function
  163. End Class
  164. Dim BulkLogController_Class__Singleton
  165. Function BulkLogController()
  166. If IsEmpty(BulkLogController_Class__Singleton) Then
  167. Set BulkLogController_Class__Singleton = New BulkLogController_Class
  168. End If
  169. Set BulkLogController = BulkLogController_Class__Singleton
  170. End Function
  171. %>

Powered by TurnKey Linux.