|
- <%
- Class ImportController_Class
- Private m_useLayout
- Private m_title
-
- Private Sub Class_Initialize()
- m_useLayout = True
- m_title = "Import"
- End Sub
-
- Public Property Get useLayout
- useLayout = m_useLayout
- End Property
-
- Public Property Let useLayout(v)
- m_useLayout = v
- End Property
-
- Public Property Get Title
- Title = m_title
- End Property
-
- Public Property Let Title(v)
- m_title = v
- End Property
-
- '-------------------------------------------------------------------------------------------------------------------
- ' GET /import : upload form
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub index()
- Dim csrf : csrf = Csrf_Token("import")
- %>
- <!--#include file="../views/Import/index.asp" -->
- <%
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' POST /import/upload : store the file, show the column-mapping step
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub upload()
- Dim up, outName, outLocal, ext, token, saved, target, fso, tbl, csrf
- Dim headers, rows, sampleCount, fields
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set up = New FreeASPUpload
- up.chunkSize = 1048576
- up.Upload
-
- If Not Csrf_Valid("import", up.Form("csrf")) Then
- Flash().AddError "Invalid form state. Please try again."
- Response.Redirect "/import"
- End If
-
- If up.UploadedFiles.Count = 0 Then
- Flash().AddError "Choose a .csv, .xlsx or .xls file to import."
- Response.Redirect "/import"
- End If
-
- up.SaveOne ImportFile_UploadDir(), 0, outName, outLocal
- saved = ImportFile_UploadDir() & "\" & outLocal
- ext = LCase(Mid(outName, InStrRev(outName, ".") + 1))
-
- If ext <> "csv" And ext <> "xlsx" And ext <> "xls" Then
- If fso.FileExists(saved) Then fso.DeleteFile saved, True
- Flash().AddError "Only .csv, .xlsx and .xls files can be imported."
- Response.Redirect "/import"
- End If
-
- token = ImportFile_NewToken()
- target = ImportFile_UploadDir() & "\" & token & "." & ext
- fso.MoveFile saved, target
-
- Dim readError : readError = ""
- On Error Resume Next
- Set tbl = ImportFile_Read(target, 5)
- If Err.Number <> 0 Then
- readError = Err.Description
- Err.Clear
- End If
- On Error GoTo 0
-
- If Len(readError) = 0 Then
- If UBound(tbl("headers")) < 0 Then readError = "No columns were found in the first row."
- End If
-
- If Len(readError) > 0 Then
- If fso.FileExists(target) Then fso.DeleteFile target, True
- Flash().AddError "Could not read " & outName & ": " & readError
- Response.Redirect "/import"
- End If
-
- fields = ImportFile_Fields()
- headers = tbl("headers")
- Set rows = tbl("rows")
- csrf = Csrf_Token("import")
- Dim originalName : originalName = outName
- %>
- <!--#include file="../views/Import/map.asp" -->
- <%
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' POST /import/run : import every row using the chosen column mapping
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub run()
- Dim token, path, fso, tbl, colOf, i, fieldKey, ncols, fields, res
- Dim defaultIndustry, markCustomers, originalName
-
- If Not Csrf_Valid("import", CStr(Request.Form("csrf"))) Then
- Flash().AddError "Invalid form state. Please try again."
- Response.Redirect "/import"
- End If
-
- token = CStr(Request.Form("token"))
- path = ImportFile_PathForToken(token)
- If Len(path) = 0 Then
- Flash().AddError "The uploaded file is no longer available. Please upload it again."
- Response.Redirect "/import"
- End If
-
- Set colOf = CreateObject("Scripting.Dictionary")
- ncols = 0
- If IsNumeric(CStr(Request.Form("ncols"))) Then ncols = CLng(CStr(Request.Form("ncols")))
- For i = 0 To ncols - 1
- fieldKey = CStr(Request.Form("map_" & i))
- If Len(fieldKey) > 0 Then
- If Not colOf.Exists(fieldKey) Then colOf.Add fieldKey, i
- End If
- Next
-
- ' Only accept known field keys
- Dim valid, f, known
- fields = ImportFile_Fields()
- For Each fieldKey In colOf.Keys
- known = False
- For Each f In fields
- If f(0) = fieldKey Then known = True
- Next
- If Not known Then colOf.Remove fieldKey
- Next
-
- If Not colOf.Exists("name") Then
- Flash().AddError "Map one column to ""Business name"" before importing."
- Response.Redirect "/import"
- End If
-
- defaultIndustry = Left(Trim(CStr(Request.Form("default_industry"))), 100)
- markCustomers = (CStr(Request.Form("mark_customers")) = "1")
- originalName = Left(CStr(Request.Form("original_name")), 255)
-
- Server.ScriptTimeout = 1800
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set tbl = ImportFile_Read(path, 0)
- Set res = ImportService_Run(tbl, colOf, defaultIndustry, markCustomers, originalName)
-
- If Not res("failed") Then
- fso.DeleteFile path, True
- HTMLSecurity().ClearAntiCSRFToken "import"
- End If
- %>
- <!--#include file="../views/Import/result.asp" -->
- <%
- End Sub
-
- End Class
-
- Dim ImportController_Class__Singleton
- Function ImportController()
- If IsEmpty(ImportController_Class__Singleton) Then
- Set ImportController_Class__Singleton = New ImportController_Class
- End If
- Set ImportController = ImportController_Class__Singleton
- End Function
- %>
|