You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 line
9.8KB

  1. <%
  2. Class BusinessesController_Class
  3. Private m_useLayout
  4. Private m_title
  5. Private Sub Class_Initialize()
  6. m_useLayout = True
  7. m_title = "Businesses"
  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 /businesses : searchable, filterable, sortable, paged list
  23. '-------------------------------------------------------------------------------------------------------------------
  24. Public Sub index()
  25. Const PER_PAGE = 50
  26. Dim f, k, page, pageCount, result, rows, total, industries, sort, dir, firstRow, lastRow
  27. Set f = CreateObject("Scripting.Dictionary")
  28. For Each k In Array("q", "industry", "status", "show", "contacted", "phone", "sort", "dir")
  29. f(k) = Trim(CStr(Request.QueryString(k)))
  30. Next
  31. f("q") = Left(f("q"), 100)
  32. page = 1
  33. Dim pageText : pageText = CStr(Request.QueryString("page"))
  34. If Len(pageText) > 0 And Len(pageText) <= 6 Then
  35. If IsNumeric(pageText) Then page = CLng(pageText)
  36. End If
  37. If page < 1 Then page = 1
  38. Set result = BusinessRepository().Search(f, page, PER_PAGE)
  39. total = result("total")
  40. Set rows = result("rows")
  41. Set industries = BusinessRepository().Industries()
  42. pageCount = Int((total + PER_PAGE - 1) / PER_PAGE)
  43. If pageCount < 1 Then pageCount = 1
  44. If page > pageCount Then
  45. page = pageCount
  46. Set result = BusinessRepository().Search(f, page, PER_PAGE)
  47. Set rows = result("rows")
  48. End If
  49. sort = f("sort") : dir = LCase(f("dir"))
  50. If dir <> "desc" Then dir = "asc"
  51. If total = 0 Then firstRow = 0 Else firstRow = (page - 1) * PER_PAGE + 1
  52. lastRow = firstRow + rows.Count - 1
  53. If total = 0 Then lastRow = 0
  54. %>
  55. <!--#include file="../views/Businesses/index.asp" -->
  56. <%
  57. End Sub
  58. '-------------------------------------------------------------------------------------------------------------------
  59. ' GET /businesses/:id : detail, history and the "log a touch" form
  60. '-------------------------------------------------------------------------------------------------------------------
  61. Public Sub show(id)
  62. Dim b
  63. If Not Valid_Id(id) Then Response.Redirect "/404"
  64. Set b = BusinessRepository().Find(CLng(id))
  65. If b Is Nothing Then Response.Redirect "/404"
  66. RenderShow id, b, Nothing, Nothing
  67. End Sub
  68. ' Renders the detail page. dup (a matching other business) and fv (unsaved form values) are Nothing normally.
  69. Private Sub RenderShow(id, b, dup, fv)
  70. Dim interactions, industries, csrf, today
  71. Set interactions = BusinessRepository().Interactions(CLng(id))
  72. Set industries = BusinessRepository().Industries()
  73. csrf = Csrf_Token("business")
  74. today = Date_Iso(Date)
  75. m_title = b("name")
  76. %>
  77. <!--#include file="../views/Businesses/show.asp" -->
  78. <%
  79. End Sub
  80. '-------------------------------------------------------------------------------------------------------------------
  81. ' POST /businesses/:id/update : edit details
  82. '-------------------------------------------------------------------------------------------------------------------
  83. Public Sub update(id)
  84. Dim d, email, phone, indName, indSel
  85. GuardPost id
  86. Set d = CreateObject("Scripting.Dictionary")
  87. d("name") = Left(Trim(F("name")), 255)
  88. If Len(d("name")) = 0 Then
  89. Flash().AddError "Business name is required."
  90. Response.Redirect "/businesses/" & id
  91. End If
  92. email = LCase(Left(Trim(F("email")), 255))
  93. If Len(email) > 0 And InStr(email, "@") = 0 Then
  94. Flash().AddError "That email address doesn't look right, so nothing was saved."
  95. Response.Redirect "/businesses/" & id
  96. End If
  97. phone = Left(Trim(F("phone")), 40)
  98. d("contact_name") = Left(Trim(F("contact_name")), 150)
  99. d("phone") = phone
  100. d("phone_digits") = ImportFile_PhoneDigits(phone)
  101. d("email") = email
  102. d("website") = Left(Trim(F("website")), 255)
  103. d("address1") = Left(Trim(F("address1")), 255)
  104. d("city") = Left(Trim(F("city")), 100)
  105. d("state") = Left(Trim(F("state")), 50)
  106. d("zip") = Left(Trim(F("zip")), 20)
  107. d("notes") = Left(F("notes"), 10000)
  108. If F("is_customer") = "1" Then d("is_customer") = 1 Else d("is_customer") = 0
  109. d("first_order_date") = Date_Parse(F("first_order_date"))
  110. indName = Trim(F("industry_new"))
  111. indSel = F("industry_id")
  112. If Valid_Id(indSel) Then d("industry_id") = CLng(indSel) Else d("industry_id") = Null
  113. ' Warn when a changed phone/email is already used by another business (unless "Save anyway" was pressed).
  114. If F("allow_duplicate") <> "1" Then
  115. Dim existing, chkPhone, chkEmail, dupMatch, fv
  116. Set existing = BusinessRepository().Find(CLng(id))
  117. chkPhone = "" : chkEmail = ""
  118. If d("phone_digits") <> existing("phone_digits") & "" Then chkPhone = d("phone_digits")
  119. If d("email") <> existing("email") & "" Then chkEmail = d("email")
  120. Set dupMatch = BusinessRepository().FindDuplicate(CLng(id), chkPhone, chkEmail)
  121. If Not dupMatch Is Nothing Then
  122. Set fv = CreateObject("Scripting.Dictionary")
  123. Dim k
  124. For Each k In d.Keys
  125. fv(k) = d(k)
  126. Next
  127. fv("_industry_new") = indName
  128. RenderShow id, existing, dupMatch, fv
  129. Exit Sub
  130. End If
  131. End If
  132. ' A typed-in industry wins over the dropdown; it is only created once the save is going ahead.
  133. If Len(indName) > 0 Then d("industry_id") = BusinessRepository().FindOrCreateIndustry(indName)
  134. BusinessRepository().Update CLng(id), d
  135. Flash().Success = "Saved."
  136. Response.Redirect "/businesses/" & id
  137. End Sub
  138. '-------------------------------------------------------------------------------------------------------------------
  139. ' POST /businesses/:id/log : record a call / email / visit / mailing
  140. '-------------------------------------------------------------------------------------------------------------------
  141. Public Sub log(id)
  142. Dim method, outcome, notes, contactedAt, followUp, removing, reason
  143. GuardPost id
  144. method = F("method")
  145. outcome = F("outcome")
  146. If Not Businesses_IsKey(Businesses_Methods(), method) Then
  147. Flash().AddError "Choose how you contacted them."
  148. Response.Redirect "/businesses/" & id
  149. End If
  150. If Len(outcome) > 0 And Not Businesses_IsKey(Businesses_Outcomes(), outcome) Then outcome = ""
  151. contactedAt = Date_Parse(F("contacted_at"))
  152. If IsNull(contactedAt) Then contactedAt = Date
  153. If contactedAt > Date + 1 Then
  154. Flash().AddError "The contact date can't be in the future."
  155. Response.Redirect "/businesses/" & id
  156. End If
  157. followUp = Date_Parse(F("follow_up_date"))
  158. notes = Left(F("notes"), 10000)
  159. removing = (outcome = "remove")
  160. reason = Businesses_Label(Businesses_Outcomes(), outcome)
  161. If removing And Len(Trim(notes)) > 0 Then reason = Left(Trim(notes), 255)
  162. BusinessRepository().LogInteraction CLng(id), method, outcome, notes, contactedAt, followUp, removing, reason
  163. If removing Then
  164. Flash().Success = "Logged and removed from the list."
  165. ElseIf Not IsNull(followUp) Then
  166. Flash().Success = "Logged. Follow-up set for " & Date_Fmt(followUp) & "."
  167. Else
  168. Flash().Success = "Logged."
  169. End If
  170. Response.Redirect "/businesses/" & id
  171. End Sub
  172. '-------------------------------------------------------------------------------------------------------------------
  173. ' POST /businesses/:id/remove and /restore
  174. '-------------------------------------------------------------------------------------------------------------------
  175. Public Sub remove(id)
  176. GuardPost id
  177. BusinessRepository().Remove CLng(id), Left(Trim(F("reason")), 255)
  178. Flash().Success = "Removed from the list."
  179. Response.Redirect "/businesses/" & id
  180. End Sub
  181. Public Sub restore(id)
  182. GuardPost id
  183. BusinessRepository().Restore CLng(id)
  184. Flash().Success = "Restored."
  185. Response.Redirect "/businesses/" & id
  186. End Sub
  187. '-------------------------------------------------------------------------------------------------------------------
  188. ' Shared checks and helpers
  189. '-------------------------------------------------------------------------------------------------------------------
  190. Private Sub GuardPost(id)
  191. If Not Valid_Id(id) Then Response.Redirect "/404"
  192. If BusinessRepository().Find(CLng(id)) Is Nothing Then Response.Redirect "/404"
  193. If Not Csrf_Valid("business", F("csrf")) Then
  194. Flash().AddError "Invalid form state. Please try again."
  195. Response.Redirect "/businesses/" & id
  196. End If
  197. End Sub
  198. Private Function F(name)
  199. F = CStr(Request.Form(name))
  200. End Function
  201. End Class
  202. Dim BusinessesController_Class__Singleton
  203. Function BusinessesController()
  204. If IsEmpty(BusinessesController_Class__Singleton) Then
  205. Set BusinessesController_Class__Singleton = New BusinessesController_Class
  206. End If
  207. Set BusinessesController = BusinessesController_Class__Singleton
  208. End Function
  209. %>

Powered by TurnKey Linux.