No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

175 líneas
6.1KB

  1. <%
  2. Class ImportController_Class
  3. Private m_useLayout
  4. Private m_title
  5. Private Sub Class_Initialize()
  6. m_useLayout = True
  7. m_title = "Import"
  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 /import : upload form
  23. '-------------------------------------------------------------------------------------------------------------------
  24. Public Sub index()
  25. Dim csrf : csrf = Csrf_Token("import")
  26. %>
  27. <!--#include file="../views/Import/index.asp" -->
  28. <%
  29. End Sub
  30. '-------------------------------------------------------------------------------------------------------------------
  31. ' POST /import/upload : store the file, show the column-mapping step
  32. '-------------------------------------------------------------------------------------------------------------------
  33. Public Sub upload()
  34. Dim up, outName, outLocal, ext, token, saved, target, fso, tbl, csrf
  35. Dim headers, rows, sampleCount, fields
  36. Set fso = CreateObject("Scripting.FileSystemObject")
  37. Set up = New FreeASPUpload
  38. up.chunkSize = 1048576
  39. up.Upload
  40. If Not Csrf_Valid("import", up.Form("csrf")) Then
  41. Flash().AddError "Invalid form state. Please try again."
  42. Response.Redirect "/import"
  43. End If
  44. If up.UploadedFiles.Count = 0 Then
  45. Flash().AddError "Choose a .csv, .xlsx or .xls file to import."
  46. Response.Redirect "/import"
  47. End If
  48. up.SaveOne ImportFile_UploadDir(), 0, outName, outLocal
  49. saved = ImportFile_UploadDir() & "\" & outLocal
  50. ext = LCase(Mid(outName, InStrRev(outName, ".") + 1))
  51. If ext <> "csv" And ext <> "xlsx" And ext <> "xls" Then
  52. If fso.FileExists(saved) Then fso.DeleteFile saved, True
  53. Flash().AddError "Only .csv, .xlsx and .xls files can be imported."
  54. Response.Redirect "/import"
  55. End If
  56. token = ImportFile_NewToken()
  57. target = ImportFile_UploadDir() & "\" & token & "." & ext
  58. fso.MoveFile saved, target
  59. Dim readError : readError = ""
  60. On Error Resume Next
  61. Set tbl = ImportFile_Read(target, 5)
  62. If Err.Number <> 0 Then
  63. readError = Err.Description
  64. Err.Clear
  65. End If
  66. On Error GoTo 0
  67. If Len(readError) = 0 Then
  68. If UBound(tbl("headers")) < 0 Then readError = "No columns were found in the first row."
  69. End If
  70. If Len(readError) > 0 Then
  71. If fso.FileExists(target) Then fso.DeleteFile target, True
  72. Flash().AddError "Could not read " & outName & ": " & readError
  73. Response.Redirect "/import"
  74. End If
  75. fields = ImportFile_Fields()
  76. headers = tbl("headers")
  77. Set rows = tbl("rows")
  78. csrf = Csrf_Token("import")
  79. Dim originalName : originalName = outName
  80. %>
  81. <!--#include file="../views/Import/map.asp" -->
  82. <%
  83. End Sub
  84. '-------------------------------------------------------------------------------------------------------------------
  85. ' POST /import/run : import every row using the chosen column mapping
  86. '-------------------------------------------------------------------------------------------------------------------
  87. Public Sub run()
  88. Dim token, path, fso, tbl, colOf, i, fieldKey, ncols, fields, res
  89. Dim defaultIndustry, markCustomers, originalName
  90. If Not Csrf_Valid("import", CStr(Request.Form("csrf"))) Then
  91. Flash().AddError "Invalid form state. Please try again."
  92. Response.Redirect "/import"
  93. End If
  94. token = CStr(Request.Form("token"))
  95. path = ImportFile_PathForToken(token)
  96. If Len(path) = 0 Then
  97. Flash().AddError "The uploaded file is no longer available. Please upload it again."
  98. Response.Redirect "/import"
  99. End If
  100. Set colOf = CreateObject("Scripting.Dictionary")
  101. ncols = 0
  102. If IsNumeric(CStr(Request.Form("ncols"))) Then ncols = CLng(CStr(Request.Form("ncols")))
  103. For i = 0 To ncols - 1
  104. fieldKey = CStr(Request.Form("map_" & i))
  105. If Len(fieldKey) > 0 Then
  106. If Not colOf.Exists(fieldKey) Then colOf.Add fieldKey, i
  107. End If
  108. Next
  109. ' Only accept known field keys
  110. Dim valid, f, known
  111. fields = ImportFile_Fields()
  112. For Each fieldKey In colOf.Keys
  113. known = False
  114. For Each f In fields
  115. If f(0) = fieldKey Then known = True
  116. Next
  117. If Not known Then colOf.Remove fieldKey
  118. Next
  119. If Not colOf.Exists("name") Then
  120. Flash().AddError "Map one column to ""Business name"" before importing."
  121. Response.Redirect "/import"
  122. End If
  123. defaultIndustry = Left(Trim(CStr(Request.Form("default_industry"))), 100)
  124. markCustomers = (CStr(Request.Form("mark_customers")) = "1")
  125. originalName = Left(CStr(Request.Form("original_name")), 255)
  126. Server.ScriptTimeout = 1800
  127. Set fso = CreateObject("Scripting.FileSystemObject")
  128. Set tbl = ImportFile_Read(path, 0)
  129. Set res = ImportService_Run(tbl, colOf, defaultIndustry, markCustomers, originalName)
  130. If Not res("failed") Then
  131. fso.DeleteFile path, True
  132. HTMLSecurity().ClearAntiCSRFToken "import"
  133. End If
  134. %>
  135. <!--#include file="../views/Import/result.asp" -->
  136. <%
  137. End Sub
  138. End Class
  139. Dim ImportController_Class__Singleton
  140. Function ImportController()
  141. If IsEmpty(ImportController_Class__Singleton) Then
  142. Set ImportController_Class__Singleton = New ImportController_Class
  143. End If
  144. Set ImportController = ImportController_Class__Singleton
  145. End Function
  146. %>

Powered by TurnKey Linux.