Imports EDSL.Entities
Imports EDSL.DAL
Imports System.Xml
Imports System.IO

Public Class miscFunctions
    Public Function GetFilenameTimestamp() As String
        Dim strOutput As String
        strOutput = Format(Now(), "yyyyMMdd") & "_" & Format(Now(), "hhmmss") & "_"
        Return strOutput
    End Function
    Public Function CleanFilename(ByVal strFileName As String) As String
        Dim strOutput As String = strFileName

        strOutput = Replace(strOutput, "{", "")
        strOutput = Replace(strOutput, "}", "")
        strOutput = Replace(strOutput, " ", "")
        strOutput = Replace(strOutput, "!", "")
        strOutput = Replace(strOutput, "$", "")
        strOutput = Replace(strOutput, "&", "")
        strOutput = Replace(strOutput, "%", "")
        strOutput = Replace(strOutput, "#", "")
        strOutput = Replace(strOutput, "(", "")
        strOutput = Replace(strOutput, ")", "")
        strOutput = Replace(strOutput, "+", "")
        strOutput = Replace(strOutput, "*", "")
        strOutput = Replace(strOutput, ",", "")
        strOutput = Replace(strOutput, ";", "")
        strOutput = Replace(strOutput, "?", "")
        strOutput = Replace(strOutput, "@", "")
        strOutput = Replace(strOutput, "—", "")
        strOutput = Replace(strOutput, "–", "")
        strOutput = Replace(strOutput, "~", "")

        Return GetFilenameTimestamp() & strOutput
    End Function
    Public Function UploadFile(ByRef objFileUpload As FileUpload, ByVal intType As Integer) As String
        Dim strFileName As String = objFileUpload.PostedFile.FileName
        Dim strUploadDirectory As String = ConfigurationManager.AppSettings("UploadFilePath")

        If objFileUpload.HasFile Then
            Dim intSlashIsWhere As Integer = InStrRev(strFileName, "\", )
            If intSlashIsWhere > 0 Then

                strFileName = Right(strFileName, Len(strFileName) - intSlashIsWhere)

            End If


            strFileName = CleanFilename(strFileName)
            Dim strExtensionName As String = System.IO.Path.GetExtension(strFileName).ToLower()
            Dim intFileSize = objFileUpload.PostedFile.ContentLength / 1024

            Select Case intType
                Case 1
                    Select Case strExtensionName
                        Case ".jpg", ".gif", ".png"
                            If intFileSize < 1024 Then
                                objFileUpload.PostedFile.SaveAs(strUploadDirectory & "images\" & strFileName)
                            Else
                                strFileName = "EdenlightError:::Image is too large maximum size is 1Mb and that image is " & System.Math.Round(intFileSize / 1000, 2) & "Mb"
                            End If
                        Case Else
                            strFileName = "EdenlightError:::Can not upload that file type <br /> You can only upload jpg, gif or png files"
                    End Select
                Case 2
                    Select Case strExtensionName
                        Case ".docx", ".doc", ".pdf"
                            objFileUpload.PostedFile.SaveAs(strUploadDirectory & "pdf/" & strFileName)
                        Case Else
                            strFileName = "EdenlightError:::Can not upload that file type <br /> You can only upload .doc, .docx, .pdf files"
                    End Select
                Case Else

            End Select
        End If
        Return strFileName
    End Function

    Public Function CleanURL(ByVal strFileName As String) As String
        Dim strOutput As String = strFileName

        strOutput = Replace(strOutput, "{", "")
        strOutput = Replace(strOutput, "}", "")
        strOutput = Replace(strOutput, " ", "")
        strOutput = Replace(strOutput, "!", "")
        strOutput = Replace(strOutput, "$", "")
        strOutput = Replace(strOutput, "&", "")
        strOutput = Replace(strOutput, "%", "")
        strOutput = Replace(strOutput, "#", "")
        strOutput = Replace(strOutput, "(", "")
        strOutput = Replace(strOutput, ")", "")
        strOutput = Replace(strOutput, "+", "")
        strOutput = Replace(strOutput, "*", "")
        strOutput = Replace(strOutput, ",", "")
        strOutput = Replace(strOutput, ";", "")
        strOutput = Replace(strOutput, "?", "")
        strOutput = Replace(strOutput, "@", "")
        strOutput = Replace(strOutput, "—", "")
        strOutput = Replace(strOutput, "–", "")
        strOutput = Replace(strOutput, "~", "")
        strOutput = Replace(strOutput, "'", "")
        strOutput = Replace(strOutput, "’", "")
        strOutput = Replace(strOutput, ":", "")
        Return strOutput
    End Function

    Public Sub BuildCaseStudyXMLFile()
        Dim objMainEntity As List(Of entMainCaseStudy)
        Dim objMain As New dbMainCaseStudy
        objMainEntity = objMain.GetAll(" AND ( Active = 1 AND Deleted = 0 ) ", " ORDER BY CaseStudyDate ASC ")
        Dim TextWriter As New XmlTextWriter(ConfigurationManager.AppSettings("RSSFeedPathCase").ToString(), Encoding.UTF8)

        With TextWriter
            .Formatting = Formatting.Indented
            .WriteStartDocument()

            'Below tags are mandatory rss tag
            .WriteStartElement("rss")
            .WriteAttributeString("version", "2.0")

            ' Channel tag will contain RSS feed details
            .WriteStartElement("channel")
            .WriteElementString("title", "EDSL  case study feed")
            .WriteElementString("link", ConfigurationManager.AppSettings("SiteURL").ToString())
            .WriteElementString("description", "EDSL case study feed")
            .WriteElementString("language", "EN")

            For Each objMainEntityItem As entMainCaseStudy In objMainEntity
                .WriteStartElement("item")
                .WriteElementString("title", objMainEntityItem.CaseStudyHeadline.ToString())
                .WriteElementString("link", ConfigurationManager.AppSettings("SiteURL").ToString() & "NewsArticle.aspx?id=" & objMainEntityItem.CaseStudyID.ToString())
                .WriteElementString("guid", objMainEntityItem.CaseStudyID.ToString())
                .WriteElementString("pubDate", Format(CDate(objMainEntityItem.CaseStudyDate), "ddd, dd MMM yyyy hh:mm:ss") & " GMT")
                .WriteElementString("description", objMainEntityItem.CaseStudySummary.ToString())
                .WriteEndElement()
            Next

            .WriteEndElement()
            .WriteEndElement()
            .WriteEndDocument()
            .Flush()
            .Close()
        End With
    End Sub
End Class
