25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

271 satır
11KB

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

Powered by TurnKey Linux.