Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

333 lignes
11KB

  1. <% Option Explicit %>
  2. <!--#include file="../../include_all.asp"-->
  3. <!--#include file="../../ViewModels/JurisdictionViewModels.asp"-->
  4. <%
  5. Class JurisdictionController
  6. Public Model
  7. Public Sub Index
  8. dim page_size : page_size = 10
  9. dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
  10. dim page_count, record_count
  11. set Model = new PagedIndex_ViewModel_Class
  12. Model.Title = "Jurisdiction"
  13. set Model.Jurisdiction = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count)
  14. Model.CurrentPageNumber = page_num
  15. Model.PageSize = page_size
  16. Model.PageCount = page_count
  17. Model.RecordCount = record_count
  18. %> <!--#include file="../../Views/Jurisdiction/Index.asp"--> <%
  19. End Sub
  20. Public Sub Search
  21. dim searchValue:searchValue = Choice(Request.Form.Count = 0,Request.QueryString("q"),Request.Form("q"))
  22. dim page_size : page_size = 10
  23. dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
  24. dim page_count, record_count
  25. set Model = new PagedIndex_ViewModel_Class
  26. Model.Title = "Jurisdiction"
  27. set Model.Jurisdiction = JurisdictionRepository.SearchTablePaged(Array("JCode","%" & searchValue & "%","Name","%" & searchValue & "%","Mailing_Address","%" & searchValue & "%","CSZ","%" & searchValue & "%","IMB","%" & searchValue & "%","IMB_Digits","%" & searchValue & "%"),"JCode", page_size, page_num, page_count, record_count)
  28. Model.CurrentPageNumber = page_num
  29. Model.PageSize = page_size
  30. Model.PageCount = page_count
  31. Model.RecordCount = record_count
  32. %> <!--#include file="../../Views/Jurisdiction/Index.asp"--> <%
  33. End Sub
  34. Public Sub Edit
  35. dim id : id = Request.QueryString("Id")
  36. set Model = new Edit_ViewModel_Class
  37. set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
  38. set Model.Contacts = ContactsRepository.Find(Array("JURISCODE = ?",id),empty)
  39. Model.Title = "Edit Jurisdiction"
  40. HTMLSecurity.SetAntiCSRFToken "JurisdictionEditForm"
  41. %> <!--#include file="../../Views/Jurisdiction/Edit.asp"--> <%
  42. Flash.Success = "Jurisdiction Updated."
  43. End Sub
  44. Public Sub EditPost
  45. MVC.RequirePost
  46. HTMLSecurity.OnInvalidAntiCsrfTokenRedirectToActionExt "JurisdictionEditForm", Request.Form("nonce"), "Edit", Array("Id", Request.Form("Id"))
  47. dim JCode : JCode = Request.Form("Id")
  48. dim model : set model = JurisdictionRepository.FindByJCode(JCode)
  49. set model = Automapper.AutoMap(Request.Form, model)
  50. 'model.Validate
  51. 'If model.Validator.HasErrors then
  52. FormCache.SerializeForm "EditJurisdiction", Request.Form
  53. ' Flash.Errors = model.Validator.Errors
  54. ' MVC.RedirectToActionExt "Edit", Array("Id",JCode)
  55. 'Else
  56. JurisdictionRepository.Update model
  57. FormCache.ClearForm "EditJurisdiction"
  58. Flash.Success = "Jurisdiction updated."
  59. MVC.RedirectToAction "Index"
  60. 'End If
  61. End Sub
  62. Public Sub Create
  63. dim form_params : set form_params = FormCache.DeserializeForm("NewJurisdiction")
  64. If Not form_params Is Nothing then
  65. set Model = Automapper.AutoMap(form_params, New Create_ViewModel_Class)
  66. Else
  67. set Model = new Create_ViewModel_Class
  68. End If
  69. HTMLSecurity.SetAntiCSRFToken "JurisdictionCreateForm"
  70. %> <!--#include file="../../Views/Jurisdiction/Create.asp"--> <%
  71. End Sub
  72. Public Sub CreatePost
  73. MVC.RequirePost
  74. HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "JurisdictionCreateForm", Request.Form("nonce"), "Create"
  75. dim new_Jurisdiction_model : set new_Jurisdiction_model = Automapper.AutoMap(Request.Form, new JurisdictionModel_Class)
  76. 'new_Jurisdiction_model.Validator.Validate
  77. 'If new_Jurisdiction_model.Validator.HasErrors then
  78. ' FormCache.SerializeForm "NewJurisdiction", Request.Form
  79. ' Flash.Errors = new_Jurisdiction_model.Validator.Errors
  80. ' MVC.RedirectToAction "Create"
  81. 'Else
  82. JurisdictionRepository.AddNew new_Jurisdiction_model
  83. ' FormCache.ClearForm "NewJurisdiction"
  84. Flash.Success = "Jurisdiction added."
  85. MVC.RedirectToAction "Index"
  86. 'End If
  87. End Sub
  88. Public Sub Delete
  89. dim id : id = Request.QueryString("Id")
  90. set Model = new Delete_ViewModel_Class
  91. set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
  92. Model.Title = "Delete Jurisdiction"
  93. HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"
  94. %> <!--#include file="../../Views/Jurisdiction/Delete.asp"--> <%
  95. End Sub
  96. Public Sub DeletePost
  97. MVC.RequirePost
  98. HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "JurisdictionDeleteForm", Request.Form("nonce"), "Create"
  99. dim id : id = Request.Form("Id")
  100. JurisdictionRepository.Delete id
  101. Flash.Success = "Jurisdiction deleted."
  102. MVC.RedirectToAction "Index"
  103. End Sub
  104. Public Sub Createkit
  105. dim id : id = Request.QueryString("Id")
  106. set Model = new Delete_ViewModel_Class
  107. set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
  108. Model.Title = "Create Kit"
  109. HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"
  110. %> <!--#include file="../../Views/Jurisdiction/createkit.asp"--> <%
  111. End sub
  112. Public Sub CreateKitPost
  113. MVC.RequirePost
  114. HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "CreateKitForm", Request.Form("nonce"), "Index"
  115. dim id : id = Request.QueryString("Id")
  116. set Model = new Delete_ViewModel_Class
  117. set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
  118. Model.Title = "Create Kit"
  119. HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"
  120. %> <!--#include file="../../Views/Jurisdiction/createkit.asp"--> <%
  121. End sub
  122. Public Sub Import
  123. dim page_size : page_size = 10
  124. dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
  125. dim page_count, record_count
  126. set Model = new PagedIndex_ViewModel_Class
  127. Model.Title = "Jurisdiction"
  128. set Model.Jurisdiction = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count)
  129. Model.CurrentPageNumber = page_num
  130. Model.PageSize = page_size
  131. Model.PageCount = page_count
  132. 'Model.RecordCount = record_count
  133. %> <!--#include file="../../Views/Jurisdiction/import.asp"--> <%
  134. End Sub
  135. Public Sub ImportPost
  136. Dim Upload: Set Upload = New FreeASPUpload
  137. Dim uploadPath, uploadedFile, fileName, fileExt, fileSize
  138. Dim maxFileSize, dotPos, recordCount
  139. maxFileSize = 10485760 '10 MB in bytes
  140. uploadPath = Server.MapPath("/uploads")
  141. 'Parse upload data
  142. Upload.Upload
  143. 'Validate file upload
  144. If Upload.FileExists("filename") = False Then
  145. Flash.AddError "No file selected for upload."
  146. MVC.RedirectToAction "Import"
  147. Exit Sub
  148. End If
  149. Set uploadedFile = Upload.UploadedFiles("filename")
  150. fileName = uploadedFile.FileName
  151. fileSize = uploadedFile.Length
  152. 'Extract file extension
  153. dotPos = InStrRev(fileName, ".")
  154. If dotPos > 0 Then
  155. fileExt = LCase(Mid(fileName, dotPos))
  156. Else
  157. fileExt = ""
  158. End If
  159. 'Validate file type
  160. If fileExt <> ".csv" And fileExt <> ".txt" Then
  161. Flash.AddError "Only .csv and .txt files are allowed."
  162. MVC.RedirectToAction "Import"
  163. Exit Sub
  164. End If
  165. 'Validate file size
  166. If fileSize > maxFileSize Then
  167. Flash.AddError "File size exceeds 10 MB limit."
  168. MVC.RedirectToAction "Import"
  169. Exit Sub
  170. End If
  171. 'Save the file to configured folder
  172. Upload.Save uploadPath
  173. 'Remove the first line of the CSV (non-header info line)
  174. StripFirstLine uploadedFile.Path
  175. 'Open CSV with Jet driver and iterate records
  176. Dim conn, rs, connString, folderPath, csvFileName, fso
  177. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  178. folderPath = fso.GetParentFolderName(uploadedFile.Path)
  179. csvFileName = fso.GetFileName(uploadedFile.Path)
  180. Set fso = Nothing
  181. Dim fmtType
  182. If fileExt = ".txt" Then
  183. fmtType = "TabDelimited"
  184. Else
  185. fmtType = "Delimited"
  186. End If
  187. connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folderPath & ";Extended Properties=""text;HDR=YES;FMT=" & fmtType & """;"
  188. Set conn = Server.CreateObject("ADODB.Connection")
  189. On Error Resume Next
  190. conn.Open connString
  191. If Err.Number <> 0 Then
  192. Flash.AddError "Unable to read CSV file."
  193. MVC.RedirectToAction "Import"
  194. Exit Sub
  195. End If
  196. On Error Goto 0
  197. Dim sql
  198. sql = "SELECT q.[County], q.[Jurisdiction], q.JCODE, " & _
  199. "IIf(InStr(1, UCase(q.NameRaw), ' CITY') > 0, " & _
  200. "'CITY OF ' & Trim(Replace(UCase(q.NameRaw), ' CITY', '')), " & _
  201. "q.NameRaw) AS [Name], " & _
  202. "q.[Mailing Address] AS Mailing_Address, " & _
  203. "q.[City & Township] & ' ' & q.[ZIP + 4] AS CSZ, " & _
  204. "'' AS IMB, " & _
  205. "'007' & q.[Mailer ID Option 1] & '000000' & Replace(q.[ZIP + 4], '-', '') AS IMB_Digits " & _
  206. "FROM (" & _
  207. "SELECT [County], [Jurisdiction], [Mailing Address], [City & Township], [ZIP + 4], [Mailer ID Option 1], " & _
  208. "IIf(InStr(1,[Jurisdiction],'(') > 0 And InStr(1,[Jurisdiction],')') > InStr(1,[Jurisdiction],'('), " & _
  209. "Trim(Mid([Jurisdiction], InStr(1,[Jurisdiction],'(') + 1, InStr(1,[Jurisdiction],')') - InStr(1,[Jurisdiction],'(') - 1)), " & _
  210. "Null) AS JCODE, " & _
  211. "IIf(InStr(1,[Jurisdiction],'(') > 0, " & _
  212. "Trim(Left([Jurisdiction], InStr(1,[Jurisdiction],'(') - 1)), " & _
  213. "Trim([Jurisdiction])) AS NameRaw " & _
  214. "FROM [" & csvFileName & "]) AS q"
  215. Set rs = conn.Execute(sql)
  216. recordCount = 0
  217. Do While Not rs.EOF
  218. Dim jurisdiction : Set jurisdiction = New JurisdictionModel_Class
  219. jurisdiction.JCode = Trim(rs.Fields("JCODE").Value & "")
  220. jurisdiction.Name = Trim(rs.Fields("Name").Value & "")
  221. jurisdiction.Mailing_Address = Trim(rs.Fields("Mailing_Address").Value & "")
  222. jurisdiction.CSZ = Trim(rs.Fields("CSZ").Value & "")
  223. jurisdiction.IMB_Digits = Trim(rs.Fields("IMB_Digits").Value & "")
  224. jurisdiction.IMB = GetIMBCodec.EncodeDigits(jurisdiction.IMB_Digits)
  225. 'JurisdictionRepository.AddNew jurisdiction
  226. recordCount = recordCount + 1
  227. rs.MoveNext
  228. Loop
  229. rs.Close
  230. conn.Close
  231. Set rs = Nothing
  232. Set conn = Nothing
  233. 'Set success message with record count
  234. Flash.Success = "File '" & fileName & "' uploaded successfully. Records imported: " & recordCount
  235. MVC.RedirectToAction "Import"
  236. End Sub
  237. Private Sub StripFirstLine(filePath)
  238. Dim fso, ts, remainingText
  239. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  240. If Not fso.FileExists(filePath) Then
  241. Set fso = Nothing
  242. Exit Sub
  243. End If
  244. Set ts = fso.OpenTextFile(filePath, 1) ' 1 = ForReading
  245. If ts.AtEndOfStream Then
  246. ts.Close
  247. Set ts = Nothing
  248. Set fso = Nothing
  249. Exit Sub
  250. End If
  251. 'Skip the first line
  252. ts.ReadLine
  253. 'Read the rest of the file
  254. If Not ts.AtEndOfStream Then
  255. remainingText = ts.ReadAll
  256. Else
  257. remainingText = ""
  258. End If
  259. ts.Close
  260. 'Rewrite the file without the first line
  261. Set ts = fso.OpenTextFile(filePath, 2) ' 2 = ForWriting
  262. ts.Write remainingText
  263. ts.Close
  264. Set ts = Nothing
  265. Set fso = Nothing
  266. End Sub
  267. End Class
  268. MVC.Dispatch
  269. %>

Powered by TurnKey Linux.