|
- <% Option Explicit %>
- <!--#include file="../../include_all.asp"-->
- <!--#include file="../../ViewModels/JurisdictionViewModels.asp"-->
- <%
- Class JurisdictionController
- Public Model
-
- Public Sub Index
-
- dim page_size : page_size = 10
- dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
- dim page_count, record_count
- set Model = new PagedIndex_ViewModel_Class
- Model.Title = "Jurisdiction"
- set Model.Jurisdiction = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count)
- Model.CurrentPageNumber = page_num
- Model.PageSize = page_size
- Model.PageCount = page_count
- Model.RecordCount = record_count
- %> <!--#include file="../../Views/Jurisdiction/Index.asp"--> <%
-
- End Sub
- Public Sub Search
-
- dim searchValue:searchValue = Choice(Request.Form.Count = 0,Request.QueryString("q"),Request.Form("q"))
- dim page_size : page_size = 10
- dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
- dim page_count, record_count
- set Model = new PagedIndex_ViewModel_Class
- Model.Title = "Jurisdiction"
- 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)
- Model.CurrentPageNumber = page_num
- Model.PageSize = page_size
- Model.PageCount = page_count
- Model.RecordCount = record_count
- %> <!--#include file="../../Views/Jurisdiction/Index.asp"--> <%
-
- End Sub
-
- Public Sub Edit
-
- dim id : id = Request.QueryString("Id")
- set Model = new Edit_ViewModel_Class
- set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
- set Model.Contacts = ContactsRepository.Find(Array("JURISCODE = ?",id),empty)
- Model.Title = "Edit Jurisdiction"
- HTMLSecurity.SetAntiCSRFToken "JurisdictionEditForm"
- %> <!--#include file="../../Views/Jurisdiction/Edit.asp"--> <%
- Flash.Success = "Jurisdiction Updated."
- End Sub
-
- Public Sub EditPost
- MVC.RequirePost
- HTMLSecurity.OnInvalidAntiCsrfTokenRedirectToActionExt "JurisdictionEditForm", Request.Form("nonce"), "Edit", Array("Id", Request.Form("Id"))
- dim JCode : JCode = Request.Form("Id")
- dim model : set model = JurisdictionRepository.FindByJCode(JCode)
- set model = Automapper.AutoMap(Request.Form, model)
- 'model.Validate
- 'If model.Validator.HasErrors then
- FormCache.SerializeForm "EditJurisdiction", Request.Form
- ' Flash.Errors = model.Validator.Errors
- ' MVC.RedirectToActionExt "Edit", Array("Id",JCode)
- 'Else
- JurisdictionRepository.Update model
- FormCache.ClearForm "EditJurisdiction"
- Flash.Success = "Jurisdiction updated."
- MVC.RedirectToAction "Index"
- 'End If
- End Sub
-
- Public Sub Create
-
- dim form_params : set form_params = FormCache.DeserializeForm("NewJurisdiction")
- If Not form_params Is Nothing then
- set Model = Automapper.AutoMap(form_params, New Create_ViewModel_Class)
- Else
- set Model = new Create_ViewModel_Class
- End If
-
- HTMLSecurity.SetAntiCSRFToken "JurisdictionCreateForm"
-
- %> <!--#include file="../../Views/Jurisdiction/Create.asp"--> <%
- End Sub
-
- Public Sub CreatePost
-
- MVC.RequirePost
- HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "JurisdictionCreateForm", Request.Form("nonce"), "Create"
-
- dim new_Jurisdiction_model : set new_Jurisdiction_model = Automapper.AutoMap(Request.Form, new JurisdictionModel_Class)
-
- 'new_Jurisdiction_model.Validator.Validate
-
- 'If new_Jurisdiction_model.Validator.HasErrors then
- ' FormCache.SerializeForm "NewJurisdiction", Request.Form
- ' Flash.Errors = new_Jurisdiction_model.Validator.Errors
- ' MVC.RedirectToAction "Create"
- 'Else
- JurisdictionRepository.AddNew new_Jurisdiction_model
- ' FormCache.ClearForm "NewJurisdiction"
- Flash.Success = "Jurisdiction added."
- MVC.RedirectToAction "Index"
- 'End If
- End Sub
-
- Public Sub Delete
- dim id : id = Request.QueryString("Id")
- set Model = new Delete_ViewModel_Class
- set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
- Model.Title = "Delete Jurisdiction"
-
- HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"
-
- %> <!--#include file="../../Views/Jurisdiction/Delete.asp"--> <%
- End Sub
-
- Public Sub DeletePost
- MVC.RequirePost
- HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "JurisdictionDeleteForm", Request.Form("nonce"), "Create"
-
- dim id : id = Request.Form("Id")
- JurisdictionRepository.Delete id
-
- Flash.Success = "Jurisdiction deleted."
- MVC.RedirectToAction "Index"
- End Sub
-
- Public Sub Createkit
- dim id : id = Request.QueryString("Id")
- set Model = new Delete_ViewModel_Class
- set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
- Model.Title = "Create Kit"
-
- HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"
-
- %> <!--#include file="../../Views/Jurisdiction/createkit.asp"--> <%
-
- End sub
-
- Public Sub CreateKitPost
- MVC.RequirePost
- HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "CreateKitForm", Request.Form("nonce"), "Index"
- dim id : id = Request.QueryString("Id")
- set Model = new Delete_ViewModel_Class
- set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
- Model.Title = "Create Kit"
-
- HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"
-
- %> <!--#include file="../../Views/Jurisdiction/createkit.asp"--> <%
-
- End sub
-
- Public Sub Import
-
- dim page_size : page_size = 10
- dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
- dim page_count, record_count
- set Model = new PagedIndex_ViewModel_Class
- Model.Title = "Jurisdiction"
- set Model.Jurisdiction = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count)
- Model.CurrentPageNumber = page_num
- Model.PageSize = page_size
- Model.PageCount = page_count
- 'Model.RecordCount = record_count
- %> <!--#include file="../../Views/Jurisdiction/import.asp"--> <%
-
- End Sub
-
- Public Sub ImportPost
-
- Dim Upload: Set Upload = New FreeASPUpload
- Dim uploadPath, uploadedFile, fileName, fileExt, fileSize
- Dim maxFileSize, dotPos, recordCount
-
- maxFileSize = 10485760 '10 MB in bytes
- uploadPath = Server.MapPath("/uploads")
-
- 'Parse upload data
- Upload.Upload
-
- 'Validate file upload
- If Upload.FileExists("filename") = False Then
- Flash.AddError "No file selected for upload."
- MVC.RedirectToAction "Import"
- Exit Sub
- End If
-
- Set uploadedFile = Upload.UploadedFiles("filename")
- fileName = uploadedFile.FileName
- fileSize = uploadedFile.Length
-
- 'Extract file extension
- dotPos = InStrRev(fileName, ".")
- If dotPos > 0 Then
- fileExt = LCase(Mid(fileName, dotPos))
- Else
- fileExt = ""
- End If
-
- 'Validate file type
- If fileExt <> ".csv" And fileExt <> ".txt" Then
- Flash.AddError "Only .csv and .txt files are allowed."
- MVC.RedirectToAction "Import"
- Exit Sub
- End If
-
- 'Validate file size
- If fileSize > maxFileSize Then
- Flash.AddError "File size exceeds 10 MB limit."
- MVC.RedirectToAction "Import"
- Exit Sub
- End If
-
- 'Save the file to configured folder
- Upload.Save uploadPath
-
- 'Remove the first line of the CSV (non-header info line)
- StripFirstLine uploadedFile.Path
-
- 'Open CSV with Jet driver and iterate records
- Dim conn, rs, connString, folderPath, csvFileName, fso
- Set fso = Server.CreateObject("Scripting.FileSystemObject")
- folderPath = fso.GetParentFolderName(uploadedFile.Path)
- csvFileName = fso.GetFileName(uploadedFile.Path)
- Set fso = Nothing
-
- Dim fmtType
- If fileExt = ".txt" Then
- fmtType = "TabDelimited"
- Else
- fmtType = "Delimited"
- End If
- connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folderPath & ";Extended Properties=""text;HDR=YES;FMT=" & fmtType & """;"
-
- Set conn = Server.CreateObject("ADODB.Connection")
- On Error Resume Next
- conn.Open connString
- If Err.Number <> 0 Then
- Flash.AddError "Unable to read CSV file."
- MVC.RedirectToAction "Import"
- Exit Sub
- End If
- On Error Goto 0
-
- Dim sql
- sql = "SELECT q.[County], q.[Jurisdiction], q.JCODE, " & _
- "IIf(InStr(1, UCase(q.NameRaw), ' CITY') > 0, " & _
- "'CITY OF ' & Trim(Replace(UCase(q.NameRaw), ' CITY', '')), " & _
- "q.NameRaw) AS [Name], " & _
- "q.[Mailing Address] AS Mailing_Address, " & _
- "q.[City & Township] & ' ' & q.[ZIP + 4] AS CSZ, " & _
- "'' AS IMB, " & _
- "'007' & q.[Mailer ID Option 1] & '000000' & Replace(q.[ZIP + 4], '-', '') AS IMB_Digits " & _
- "FROM (" & _
- "SELECT [County], [Jurisdiction], [Mailing Address], [City & Township], [ZIP + 4], [Mailer ID Option 1], " & _
- "IIf(InStr(1,[Jurisdiction],'(') > 0 And InStr(1,[Jurisdiction],')') > InStr(1,[Jurisdiction],'('), " & _
- "Trim(Mid([Jurisdiction], InStr(1,[Jurisdiction],'(') + 1, InStr(1,[Jurisdiction],')') - InStr(1,[Jurisdiction],'(') - 1)), " & _
- "Null) AS JCODE, " & _
- "IIf(InStr(1,[Jurisdiction],'(') > 0, " & _
- "Trim(Left([Jurisdiction], InStr(1,[Jurisdiction],'(') - 1)), " & _
- "Trim([Jurisdiction])) AS NameRaw " & _
- "FROM [" & csvFileName & "]) AS q"
-
- Set rs = conn.Execute(sql)
-
- recordCount = 0
- Do While Not rs.EOF
- Dim jurisdiction : Set jurisdiction = New JurisdictionModel_Class
- jurisdiction.JCode = Trim(rs.Fields("JCODE").Value & "")
- jurisdiction.Name = Trim(rs.Fields("Name").Value & "")
- jurisdiction.Mailing_Address = Trim(rs.Fields("Mailing_Address").Value & "")
- jurisdiction.CSZ = Trim(rs.Fields("CSZ").Value & "")
- jurisdiction.IMB_Digits = Trim(rs.Fields("IMB_Digits").Value & "")
- jurisdiction.IMB = GetIMBCodec.EncodeDigits(jurisdiction.IMB_Digits)
-
- 'JurisdictionRepository.AddNew jurisdiction
-
- recordCount = recordCount + 1
- rs.MoveNext
- Loop
-
- rs.Close
- conn.Close
- Set rs = Nothing
- Set conn = Nothing
-
- 'Set success message with record count
- Flash.Success = "File '" & fileName & "' uploaded successfully. Records imported: " & recordCount
- MVC.RedirectToAction "Import"
-
- End Sub
-
- Private Sub StripFirstLine(filePath)
- Dim fso, ts, remainingText
-
- Set fso = Server.CreateObject("Scripting.FileSystemObject")
- If Not fso.FileExists(filePath) Then
- Set fso = Nothing
- Exit Sub
- End If
-
- Set ts = fso.OpenTextFile(filePath, 1) ' 1 = ForReading
- If ts.AtEndOfStream Then
- ts.Close
- Set ts = Nothing
- Set fso = Nothing
- Exit Sub
- End If
-
- 'Skip the first line
- ts.ReadLine
-
- 'Read the rest of the file
- If Not ts.AtEndOfStream Then
- remainingText = ts.ReadAll
- Else
- remainingText = ""
- End If
- ts.Close
-
- 'Rewrite the file without the first line
- Set ts = fso.OpenTextFile(filePath, 2) ' 2 = ForWriting
- ts.Write remainingText
- ts.Close
-
- Set ts = Nothing
- Set fso = Nothing
- End Sub
- End Class
- MVC.Dispatch
- %>
|