Imports EDSL.Entities
Imports EDSL.DAL
Imports System.Xml
Imports System.IO
Partial Public Class NewsEditControl
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtNewsSummary.BaseHref = ConfigurationManager.AppSettings("BaseHref") & "NewsAdmin"
        txtNewsSummary.BasePath = ConfigurationManager.AppSettings("BasePath")
        txtNewstext.BaseHref = ConfigurationManager.AppSettings("BaseHref") & "NewsAdmin"
        txtNewstext.BasePath = ConfigurationManager.AppSettings("BasePath")
        hypPage2.NavigateUrl = "~/NewsAdmin/edit2.aspx?id=" & Request.QueryString("id")
        If IsPostBack = False Then
            If Request.QueryString("id") = 0 OrElse Request.QueryString("id") = "" Then
                imgThumbnail.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "images/noimage.png"
                imgMainImage.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "images/noimage.png"
                btnDelete.Visible = False
            Else
                btnDelete.Visible = True
                Call GetDetails(Request.QueryString("id"))
            End If
        End If
    End Sub
    Private Sub GetDetails(ByVal intNewsID As Integer)
        Dim objMain As New dbMainNews
        Dim objMainEntity As entMainNews = objMain.GetItem(intNewsID)
        txtID.Value = objMainEntity.NewsID
        txtNewsHeadline.Text = objMainEntity.NewsHeadline
        txtNewsSummary.Value = objMainEntity.NewsSummary
        txtNewstext.Value = objMainEntity.NewsText
        txtDate.Text = objMainEntity.NewsDate

        If objMainEntity.ThumbImage = "" Then
            imgThumbnail.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "images/noimage.png"
        Else
            Dim strUploadPath As String = ConfigurationManager.AppSettings("UploadFileURL") & "images/"
            imgThumbnail.ImageUrl = strUploadPath & objMainEntity.ThumbImage
            txtThumbnail.Value = objMainEntity.ThumbImage
        End If

        If objMainEntity.MainImage = "" Then
            imgMainImage.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "images/noimage.png"
        Else
            Dim strUploadPath As String = ConfigurationManager.AppSettings("UploadFileURL") & "images/"
            imgMainImage.ImageUrl = strUploadPath & objMainEntity.MainImage
            txtMainImage.Value = objMainEntity.MainImage
        End If

        If objMainEntity.Active = 1 Then
            chkActive.Checked = True
        Else
            chkActive.Checked = False
        End If
    End Sub
    Protected Sub btnDelete_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim objMain As New dbMainNews
        Response.Redirect("Delete.aspx?id=" & txtID.Value & "&idtext=" & txtNewsHeadline.Text & "&r=e")
    End Sub
    Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSaveNext.Click, btnExit.Click
        Dim clsMisc As New miscFunctions
        Dim objGlobals As New TheGlobals
        Dim objMainAdmin As New dbMainNews
        Dim intResultFromSave As Integer
        Dim intID As Integer
        Dim strNewsSummary As String = txtNewsSummary.Value.ToString

        If Len(strNewsSummary) <= 0 Then
            CustomValidator1.IsValid = False
            CustomValidator1.ErrorMessage = "News summary - can not be blank"
            Exit Sub
        End If

        strNewsSummary = Replace(strNewsSummary, "<p>", "")
        strNewsSummary = Replace(strNewsSummary, "</p>", "")

        If txtID.Value = "" Then
            intID = 0
        Else
            intID = CInt(txtID.Value)
        End If

        divErrorPanel.InnerHtml = ""

        Dim strFileNameThumb As String
        Dim booTryToSaveThumbnail As Boolean = False
        If chkRemoveThumbnailImage.Checked = True Then
            strFileNameThumb = ""
            booTryToSaveThumbnail = True
        Else
            strFileNameThumb = clsMisc.UploadFile(filThumbnail, objGlobals.UploadFileTypes.Image)

            If strFileNameThumb = "" Then
                strFileNameThumb = txtThumbnail.Value
                booTryToSaveThumbnail = True
            ElseIf Left(strFileNameThumb, 17) = "EdenlightError:::" Then
                divErrorPanel.InnerHtml += "Thumb logo : " & Right(strFileNameThumb, Len(strFileNameThumb) - 17) & "<br />"
                booTryToSaveThumbnail = False
            Else
                booTryToSaveThumbnail = True
            End If
        End If

        Dim strFileNameMain As String
        Dim booTryToSaveMain As Boolean = False
        If chkRemoveMainImage.Checked = True Then
            strFileNameMain = ""
            booTryToSaveMain = True
        Else
            strFileNameMain = clsMisc.UploadFile(filMainImage, objGlobals.UploadFileTypes.Image)

            If strFileNameMain = "" Then
                strFileNameMain = txtMainImage.Value
                booTryToSaveMain = True
            ElseIf Left(strFileNameMain, 17) = "EdenlightError:::" Then
                divErrorPanel.InnerHtml += "Main logo : " & Right(strFileNameMain, Len(strFileNameMain) - 17) & "<br />"
                booTryToSaveMain = False
            Else
                booTryToSaveMain = True
            End If
        End If

        If booTryToSaveThumbnail = True And booTryToSaveMain = True Then
            intResultFromSave = objMainAdmin.SaveItem(intID, txtNewsHeadline.Text, strNewsSummary, txtNewstext.Value.ToString, txtDate.Text, strFileNameThumb, strFileNameMain, IIf(chkActive.Checked = True, 1, 0), Session("passover"))
            Select Case intResultFromSave
                Case 0
                    divErrorPanel.InnerHtml = "Error saving news item"
                    Exit Sub
                Case Else
                    'Call BuildXMLFile()
                    Response.Redirect("edit2.aspx?id=" & intResultFromSave)
            End Select
        End If
    End Sub

    Private Sub BuildXMLFile()
        Dim objMainEntity As List(Of entMainNews)
        Dim objMain As New dbMainNews
        objMainEntity = objMain.GetAll(" AND Active = 1 AND Deleted = 0 ", " ORDER BY NewsDate ASC ")

        Response.Clear()
        Response.ContentType = "text/xml"
        Dim TextWriter As New XmlTextWriter(ConfigurationManager.AppSettings("RSSFeedPathNews").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  news feed")
            .WriteElementString("link", ConfigurationManager.AppSettings("SiteURL").ToString())
            .WriteElementString("description", "EDSL news feed")
            .WriteElementString("language", "EN")

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

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

   
End Class
