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.

374 satır
15KB

  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", "outcome", "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. ' GET /business/new : blank form to enter a business by hand ("new" is reserved in VBScript, hence the action name)
  94. '-------------------------------------------------------------------------------------------------------------------
  95. Public Sub add()
  96. Dim fv
  97. Set fv = CreateObject("Scripting.Dictionary")
  98. Set fv("_area_ids") = CreateObject("Scripting.Dictionary")
  99. RenderAdd Nothing, fv
  100. End Sub
  101. ' dup is a matching other business (or Nothing); fv holds the form values to show.
  102. Private Sub RenderAdd(dup, fv)
  103. Dim industries, allAreas, areaSel, csrf, ix, indKey2
  104. Set industries = BusinessRepository().Industries()
  105. Set allAreas = AreaRepository().All()
  106. Set areaSel = fv("_area_ids")
  107. csrf = Csrf_Token("business")
  108. m_title = "Add business"
  109. %>
  110. <!--#include file="../views/Businesses/add.asp" -->
  111. <%
  112. End Sub
  113. '-------------------------------------------------------------------------------------------------------------------
  114. ' POST /business/create
  115. '-------------------------------------------------------------------------------------------------------------------
  116. Public Sub create()
  117. Dim d, email, phone, indName, indSel, areaIds, areaNew, ai, areaId, areaName, dupMatch, fv, k, newId
  118. If Not Csrf_Valid("business", F("csrf")) Then
  119. Flash().AddError "Invalid form state. Please try again."
  120. Response.Redirect "/business/new"
  121. End If
  122. Set d = CreateObject("Scripting.Dictionary")
  123. d("name") = Left(Trim(F("name")), 255)
  124. email = LCase(Left(Trim(F("email")), 255))
  125. phone = Left(Trim(F("phone")), 40)
  126. d("contact_name") = Left(Trim(F("contact_name")), 150)
  127. d("phone") = phone
  128. d("phone_digits") = ImportFile_PhoneDigits(phone)
  129. d("email") = email
  130. d("website") = Left(Trim(F("website")), 255)
  131. d("address1") = Left(Trim(F("address1")), 255)
  132. d("city") = Left(Trim(F("city")), 100)
  133. d("state") = Left(Trim(F("state")), 50)
  134. d("zip") = Left(Trim(F("zip")), 20)
  135. d("notes") = Left(F("notes"), 10000)
  136. If F("is_customer") = "1" Then d("is_customer") = 1 Else d("is_customer") = 0
  137. d("first_order_date") = Date_Parse(F("first_order_date"))
  138. indName = Trim(F("industry_new"))
  139. indSel = F("industry_id")
  140. If Valid_Id(indSel) Then d("industry_id") = CLng(indSel) Else d("industry_id") = Null
  141. Set areaIds = CreateObject("Scripting.Dictionary")
  142. For ai = 1 To Request.Form("area_ids").Count
  143. If Valid_Id(Request.Form("area_ids")(ai)) Then areaIds(CStr(CLng(Request.Form("area_ids")(ai)))) = True
  144. Next
  145. areaNew = Left(Trim(F("area_new")), 500)
  146. Set fv = CreateObject("Scripting.Dictionary")
  147. For Each k In d.Keys
  148. fv(k) = d(k)
  149. Next
  150. fv("_industry_new") = indName
  151. fv("_area_new") = areaNew
  152. Set fv("_area_ids") = areaIds
  153. If Len(d("name")) = 0 Then
  154. Flash().AddError "Business name is required."
  155. RenderAdd Nothing, fv
  156. Exit Sub
  157. End If
  158. If Len(email) > 0 And InStr(email, "@") = 0 Then
  159. Flash().AddError "That email address doesn't look right, so nothing was saved."
  160. RenderAdd Nothing, fv
  161. Exit Sub
  162. End If
  163. ' Warn when the phone/email already belongs to another business (unless "Save anyway" was pressed).
  164. If F("allow_duplicate") <> "1" Then
  165. Set dupMatch = BusinessRepository().FindDuplicate(0, d("phone_digits"), d("email"))
  166. If Not dupMatch Is Nothing Then
  167. RenderAdd dupMatch, fv
  168. Exit Sub
  169. End If
  170. End If
  171. If Len(indName) > 0 Then d("industry_id") = BusinessRepository().FindOrCreateIndustry(indName)
  172. newId = BusinessRepository().Create(d)
  173. For Each areaName In Split(areaNew, ",")
  174. areaId = AreaRepository().FindOrCreate(areaName)
  175. If Not IsNull(areaId) Then areaIds(CStr(areaId)) = True
  176. Next
  177. AreaRepository().SetForBusiness newId, areaIds
  178. Flash().Success = "Added " & d("name") & "."
  179. Response.Redirect "/businesses/" & newId
  180. End Sub
  181. '-------------------------------------------------------------------------------------------------------------------
  182. ' POST /businesses/:id/update : edit details
  183. '-------------------------------------------------------------------------------------------------------------------
  184. Public Sub update(id)
  185. Dim d, email, phone, indName, indSel, areaIds, areaNew, ai, areaId, areaName
  186. GuardPost id
  187. Set d = CreateObject("Scripting.Dictionary")
  188. d("name") = Left(Trim(F("name")), 255)
  189. If Len(d("name")) = 0 Then
  190. Flash().AddError "Business name is required."
  191. Response.Redirect "/businesses/" & id
  192. End If
  193. email = LCase(Left(Trim(F("email")), 255))
  194. If Len(email) > 0 And InStr(email, "@") = 0 Then
  195. Flash().AddError "That email address doesn't look right, so nothing was saved."
  196. Response.Redirect "/businesses/" & id
  197. End If
  198. phone = Left(Trim(F("phone")), 40)
  199. d("contact_name") = Left(Trim(F("contact_name")), 150)
  200. d("phone") = phone
  201. d("phone_digits") = ImportFile_PhoneDigits(phone)
  202. d("email") = email
  203. d("website") = Left(Trim(F("website")), 255)
  204. d("address1") = Left(Trim(F("address1")), 255)
  205. d("city") = Left(Trim(F("city")), 100)
  206. d("state") = Left(Trim(F("state")), 50)
  207. d("zip") = Left(Trim(F("zip")), 20)
  208. d("notes") = Left(F("notes"), 10000)
  209. If F("is_customer") = "1" Then d("is_customer") = 1 Else d("is_customer") = 0
  210. d("first_order_date") = Date_Parse(F("first_order_date"))
  211. indName = Trim(F("industry_new"))
  212. indSel = F("industry_id")
  213. If Valid_Id(indSel) Then d("industry_id") = CLng(indSel) Else d("industry_id") = Null
  214. ' Areas: the ticked boxes, plus any comma-separated new names (created once the save is going ahead).
  215. Set areaIds = CreateObject("Scripting.Dictionary")
  216. For ai = 1 To Request.Form("area_ids").Count
  217. If Valid_Id(Request.Form("area_ids")(ai)) Then areaIds(CStr(CLng(Request.Form("area_ids")(ai)))) = True
  218. Next
  219. areaNew = Left(Trim(F("area_new")), 500)
  220. ' Warn when a changed phone/email is already used by another business (unless "Save anyway" was pressed).
  221. If F("allow_duplicate") <> "1" Then
  222. Dim existing, chkPhone, chkEmail, dupMatch, fv
  223. Set existing = BusinessRepository().Find(CLng(id))
  224. chkPhone = "" : chkEmail = ""
  225. If d("phone_digits") <> existing("phone_digits") & "" Then chkPhone = d("phone_digits")
  226. If d("email") <> existing("email") & "" Then chkEmail = d("email")
  227. Set dupMatch = BusinessRepository().FindDuplicate(CLng(id), chkPhone, chkEmail)
  228. If Not dupMatch Is Nothing Then
  229. Set fv = CreateObject("Scripting.Dictionary")
  230. Dim k
  231. For Each k In d.Keys
  232. fv(k) = d(k)
  233. Next
  234. fv("_industry_new") = indName
  235. fv("_area_new") = areaNew
  236. Set fv("_area_ids") = areaIds
  237. RenderShow id, existing, dupMatch, fv
  238. Exit Sub
  239. End If
  240. End If
  241. ' A typed-in industry wins over the dropdown; it is only created once the save is going ahead.
  242. If Len(indName) > 0 Then d("industry_id") = BusinessRepository().FindOrCreateIndustry(indName)
  243. For Each areaName In Split(areaNew, ",")
  244. areaId = AreaRepository().FindOrCreate(areaName)
  245. If Not IsNull(areaId) Then areaIds(CStr(areaId)) = True
  246. Next
  247. BusinessRepository().Update CLng(id), d
  248. AreaRepository().SetForBusiness CLng(id), areaIds
  249. Flash().Success = "Saved."
  250. Response.Redirect "/businesses/" & id
  251. End Sub
  252. '-------------------------------------------------------------------------------------------------------------------
  253. ' POST /businesses/:id/log : record a call / email / visit / mailing
  254. '-------------------------------------------------------------------------------------------------------------------
  255. Public Sub log(id)
  256. Dim method, outcome, notes, contactedAt, followUp, removing, reason
  257. GuardPost id
  258. method = F("method")
  259. outcome = F("outcome")
  260. If Not Businesses_IsKey(Businesses_Methods(), method) Then
  261. Flash().AddError "Choose how you contacted them."
  262. Response.Redirect "/businesses/" & id
  263. End If
  264. If Len(outcome) > 0 And Not Businesses_IsKey(Businesses_Outcomes(), outcome) Then outcome = ""
  265. contactedAt = Date_Parse(F("contacted_at"))
  266. If IsNull(contactedAt) Then contactedAt = Date
  267. If contactedAt > Date + 1 Then
  268. Flash().AddError "The contact date can't be in the future."
  269. Response.Redirect "/businesses/" & id
  270. End If
  271. followUp = Date_Parse(F("follow_up_date"))
  272. notes = Left(F("notes"), 10000)
  273. removing = (outcome = "remove")
  274. reason = Businesses_Label(Businesses_Outcomes(), outcome)
  275. If removing And Len(Trim(notes)) > 0 Then reason = Left(Trim(notes), 255)
  276. BusinessRepository().LogInteraction CLng(id), method, outcome, notes, contactedAt, followUp, removing, reason
  277. If removing Then
  278. Flash().Success = "Logged and removed from the list."
  279. ElseIf Not IsNull(followUp) Then
  280. Flash().Success = "Logged. Follow-up set for " & Date_Fmt(followUp) & "."
  281. Else
  282. Flash().Success = "Logged."
  283. End If
  284. Response.Redirect "/businesses/" & id
  285. End Sub
  286. '-------------------------------------------------------------------------------------------------------------------
  287. ' POST /businesses/:id/remove and /restore
  288. '-------------------------------------------------------------------------------------------------------------------
  289. Public Sub remove(id)
  290. GuardPost id
  291. BusinessRepository().Remove CLng(id), Left(Trim(F("reason")), 255)
  292. Flash().Success = "Removed from the list."
  293. Response.Redirect "/businesses/" & id
  294. End Sub
  295. Public Sub restore(id)
  296. GuardPost id
  297. BusinessRepository().Restore CLng(id)
  298. Flash().Success = "Restored."
  299. Response.Redirect "/businesses/" & id
  300. End Sub
  301. '-------------------------------------------------------------------------------------------------------------------
  302. ' Shared checks and helpers
  303. '-------------------------------------------------------------------------------------------------------------------
  304. Private Sub GuardPost(id)
  305. If Not Valid_Id(id) Then Response.Redirect "/404"
  306. If BusinessRepository().Find(CLng(id)) Is Nothing Then Response.Redirect "/404"
  307. If Not Csrf_Valid("business", F("csrf")) Then
  308. Flash().AddError "Invalid form state. Please try again."
  309. Response.Redirect "/businesses/" & id
  310. End If
  311. End Sub
  312. Private Function F(name)
  313. F = CStr(Request.Form(name))
  314. End Function
  315. End Class
  316. Dim BusinessesController_Class__Singleton
  317. Function BusinessesController()
  318. If IsEmpty(BusinessesController_Class__Singleton) Then
  319. Set BusinessesController_Class__Singleton = New BusinessesController_Class
  320. End If
  321. Set BusinessesController = BusinessesController_Class__Singleton
  322. End Function
  323. %>

Powered by TurnKey Linux.