Imports System.Data
Imports System.Data.SqlClient

Public Class News
    Inherits System.Web.UI.Page

    Protected WithEvents Conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))
    Protected PID As Integer
    Public TopImagePath As String


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
        Me.daViewPage = New System.Data.SqlClient.SqlDataAdapter
        Me.DsViewPage1 = New AllSaints.dsViewPage
        CType(Me.DsViewPage1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "[GetPageInfo]"
        Me.SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        '
        'SqlUpdateCommand1
        '
        Me.SqlUpdateCommand1.CommandText = "[SetPageInfo]"
        Me.SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure
        '
        'daViewPage
        '
        Me.daViewPage.SelectCommand = Me.SqlSelectCommand1
        Me.daViewPage.UpdateCommand = Me.SqlUpdateCommand1
        '
        'DsViewPage1
        '
        Me.DsViewPage1.DataSetName = "dsViewPage"
        Me.DsViewPage1.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DsViewPage1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub
    Protected WithEvents NewsTable As System.Web.UI.HtmlControls.HtmlTable
    Protected WithEvents addNewslink As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents imgTop As System.Web.UI.WebControls.Image
    Protected WithEvents RemoveTPic As System.Web.UI.WebControls.LinkButton
    Protected WithEvents btnTopPicUpdate As System.Web.UI.WebControls.Button
    Protected WithEvents TopPicture As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents daViewPage As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents DsViewPage1 As AllSaints.dsViewPage

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim action As String = ""
        Dim id As String = ""

        If Not Request.Params("action") Is Nothing Then
            action = Request.Params("action").ToString()
            id = Request.Params("id").ToString()
            PerformAction(action, id)
        End If

        Dim url As String = Request.Url.ToString()
        Dim UrlArray() As String = Split(url, "?")
        Dim addNewsUrl As String = UrlArray(0).Replace("News.aspx", "addNews.aspx")

        addNewslink.Attributes.Add("href", addNewsUrl)

        LoadNews()

        'local PID = 10414
        'Live PID = 10650
        PID = 10650

        If Not Page.IsPostBack Then

            Dim reader As SqlDataReader
            Dim cmd As New SqlCommand

            If Conn.State = ConnectionState.Closed Then
                Conn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
            End If

            With cmd
                .CommandText = "select TopPicture from as_Pages where ID = " & PID
                .CommandType = CommandType.Text
                If Conn.State = ConnectionState.Closed Then Conn.Open()
                .Connection = Conn
                reader = CType(.ExecuteReader(), SqlDataReader)
            End With

            If reader.HasRows Then
                While reader.Read()
                    TopImagePath = CType(reader("TopPicture"), String)
                    Exit While
                End While
            End If

            Page.DataBind()
        End If

    End Sub

    Private Sub PerformAction(ByVal action As String, ByVal id As String)
        Try
            Dim cmd As New SqlCommand
            Dim query As String = ""

            If (action = "disable") Then
                query = "Update as_News set Status = 0 where ID = " & CType(id, Int32)
            ElseIf (action = "enable") Then
                query = "Update as_News set Status = 1 where ID = " & CType(id, Int32)
            ElseIf (action = "delete") Then
                query = "delete from as_News where ID = " & CType(id, Int32)
            Else
                query = ""
            End If

            If (query <> "") Then
                If Conn.State = ConnectionState.Closed Then
                    Conn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
                End If

                With cmd
                    .CommandText = query
                    .CommandType = CommandType.Text
                    If Conn.State = ConnectionState.Closed Then Conn.Open()
                    .Connection = Conn
                    .ExecuteNonQuery()
                End With

                If Conn.State = ConnectionState.Open Then Conn.Close()
            End If
        Catch ex As Exception
            If Conn.State = ConnectionState.Open Then Conn.Close()
            Response.Write(ex.Message)
        End Try
    End Sub
    Private Sub LoadNews()
        Try
            Dim reader As SqlDataReader
            Dim cmd As New SqlCommand

            If Conn.State = ConnectionState.Closed Then
                Conn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
            End If

            With cmd
                .CommandText = "Select * from as_News"
                .CommandType = CommandType.Text
                If Conn.State = ConnectionState.Closed Then Conn.Open()
                .Connection = Conn
                reader = CType(.ExecuteReader(), SqlDataReader)
            End With

            Dim url As String = Request.Url.ToString()
            Dim UrlArray() As String = Split(url, "?")

            Dim addNewsUrl As String = UrlArray(0).Replace("News.aspx", "addNews.aspx")

            If reader.HasRows Then
                While reader.Read()
                    Dim thr As New HtmlTableRow
                    Dim thc As New HtmlTableCell
                    Dim str As String

                    thc.InnerHtml = reader("Date")
                    thc.Width = 80
                    'thc.Attributes.Add("style", "text-align:left;font-size:12px;padding-left:1px")
                    thc.Attributes.Add("valign", "top")
                    thc.Attributes.Add("class", "dataRowClass")
                    thr.Controls.Add(thc)
                    thc = New HtmlTableCell

                    thc.InnerHtml = "<b>" & reader("Title") & "</b> <br />" & reader("Summary")
                    thc.Width = 430
                    'thc.Attributes.Add("style", "text-align:left;font-size:12px;padding-left:1px")
                    thc.Attributes.Add("class", "dataRowClass")
                    thr.Controls.Add(thc)
                    thc = New HtmlTableCell
                    thc.NoWrap = True

                    If (CType(reader("Status"), String) = "1") Then
                        str = "<a href='" & UrlArray(0) & "?action=disable&id=" & reader("ID") & "'>Disable</a>"
                    Else
                        str = "<a href='" & UrlArray(0) & "?action=enable&id=" & reader("ID") & "'>Enable</a>"
                    End If

                    thc.InnerHtml = str & "&nbsp;|&nbsp;<a href='" & addNewsUrl & "?id=" & reader("ID") & "'>Edit</a>&nbsp;|&nbsp;<a href='" & UrlArray(0) & "?action=delete&id=" & reader("ID") & "'>Delete</a>"
                    thc.Width = 100
                    'thc.Attributes.Add("style", "text-align:left;font-size:12px;padding-left:1px")
                    thc.Attributes.Add("valign", "top")
                    thc.Attributes.Add("class", "dataRowClass")
                    thc.Attributes.Add("style", "white-space: nowrap")
                    thr.Controls.Add(thc)

                    'thr.Attributes.Add("class", "incell")
                    NewsTable.Rows.Add(thr)
                End While
            End If

            If Conn.State = ConnectionState.Open Then Conn.Close()
        Catch ex As Exception
            If Conn.State = ConnectionState.Open Then Conn.Close()
            Response.Write(ex.Message)
        End Try
    End Sub

    ' Pic Logic... Sohaib

    Private Sub imgTop_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgTop.PreRender
        Dim img As System.Web.UI.WebControls.Image = CType(sender, System.Web.UI.WebControls.Image)
        If img.ImageUrl.Length > 0 Then
            img.ImageUrl = img.ImageUrl.Substring(2)
            img.ImageUrl = "..\" & img.ImageUrl
            img.Visible = True
        Else
            img.ImageUrl = "..\contents\images\noimage.jpg"
            img.Visible = False
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub

    Private Sub btnTopPicUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTopPicUpdate.Click

        Dim cmd As New SqlCommand
        Dim fid As Integer = PID

        With TopPicture.PostedFile
            If .FileName <> "" Then
                Select Case Trim(.ContentType.ToLower)

                    Case "image/gif", "image/pjpg", "image/jpeg", "image/pjpeg", "image/jpg", "image/png", "image/x-png", "image/bmp"
                        Try
                            Dim sFilename As String = System.IO.Path.GetExtension(.FileName)
                            sFilename = "\contents\images\" & fid & "top" & sFilename
                            .SaveAs(Server.MapPath("..") & sFilename)

                            With cmd
                                .CommandText = "Update as_Pages Set TopPicture='." & sFilename & "' where ID=" & fid
                                .CommandType = CommandType.Text
                                .Connection = Conn
                                If Conn.State = ConnectionState.Closed Then
                                    Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                                    Try
                                        Conn.ConnectionString = sConStr
                                    Catch ex As Exception
                                    End Try
                                    Conn.Open()
                                End If
                                .ExecuteNonQuery()
                                If Conn.State = ConnectionState.Open Then Conn.Close()
                                Response.Redirect(Request.Url.ToString())
                            End With

                        Catch ex As Exception
                            Response.Write(ex.ToString)

                        End Try

                    Case Else
                        'nothing
                End Select
            End If
        End With

        If Conn.State = ConnectionState.Open Then Conn.Close()

    End Sub

    Private Sub RemoveTPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveTPic.Click
        Dim cmd As New SqlCommand
        Dim fid As Integer = PID

        With cmd
            .CommandText = "Select TopPicture from as_Pages where ID=" & fid
            .CommandType = CommandType.Text
            .Connection = Conn
            If Conn.State = ConnectionState.Closed Then
                Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                Try
                    Conn.ConnectionString = sConStr
                Catch ex As Exception
                End Try
                Conn.Open()
            End If
            Dim sFilename As String = CType(.ExecuteScalar, String)

            If sFilename.Length > 0 Then
                Try
                    sFilename = sFilename.Substring(1)
                    'System.IO.File.Delete(Server.MapPath("..") & sFilename)
                Catch ex As Exception
                    'do nothing
                End Try
                .CommandText = "Update as_Pages Set TopPicture='' where ID=" & fid
                .CommandType = CommandType.Text
                .Connection = Conn
                If Conn.State = ConnectionState.Closed Then
                    Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                    Try
                        Conn.ConnectionString = sConStr
                    Catch ex As Exception
                    End Try
                    Conn.Open()
                End If
                .ExecuteNonQuery()
                If Conn.State = ConnectionState.Open Then Conn.Close()
                Response.Redirect(Request.Url.ToString())
            End If

        End With

        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub

End Class
