Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Color
Partial Class Admin_AdminLeftBanner
    Inherits System.Web.UI.UserControl
    Public i As Integer
    Public j As Integer
    Dim con1 As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
         If Not IsPostBack Then

            DrawMenu()
        End If

    End Sub
    Public Sub DrawMenu()
        con1.Open()
        If CStr(Request.QueryString("sMenu")) <> "" Then
            If CStr(Session("SubMenuId1")) <> "" Then
                DrawSubMenu()
            Else
                Dim str As String = "Select ModuleID,ModuleName from MyModule "
                Dim cmd As New SqlCommand(str, con1)
                Dim dr As SqlDataReader
                dr = cmd.ExecuteReader()
                Do While dr.Read
                    DrawMainmenu(dr(0), dr(1))
                Loop
            End If
        Else
            If CStr(Request.QueryString("isSubMenu")) = "Y" Then
                DrawSubMenu()
            Else
                Dim str As String = "Select ModuleID,ModuleName from MyModule "
                Dim cmd As New SqlCommand(str, con1)
                Dim dr As SqlDataReader
                dr = cmd.ExecuteReader()
                Do While dr.Read
                    DrawMainmenu(dr(0), dr(1))
                Loop
            End If
        End If
    End Sub
    Protected Sub DrawMainmenu(ByVal l As Integer, ByVal s As String)

        Dim hpanel As New Panel
        j = j + 1

        hpanel.ID = Trim("Panel" & j)
        hpanel.Attributes.Add("runat", "server")
        'hpanel.CssClass = "leftHeading"

        Dim label As New Label
        label.Text = "    " & s
        label.Font.Bold = True
        label.ForeColor = Color.White
        label.BackColor = FromArgb(140, 142, 131)
        'label.CssClass = "leftHeading"
        label.EnableTheming = "False"
        label.Attributes.Add("runat", "server")
        label.Width = Unit.Pixel(160)
        Dim Image As New ImageButton
        Image.ID = "Image" & l
        Image.ImageUrl = "~/Images/Buttons/big_button.jpg"
        Image.AlternateText = "Show Menu"
        Image.Attributes.Add("runat", "server")


        hpanel.BorderColor = FromArgb(178, 198, 207)
        hpanel.BorderStyle = BorderStyle.Solid
        hpanel.BorderWidth = 1
        hpanel.BackColor = FromArgb(140, 142, 131)
        hpanel.Style.Add("Height", "16px")
        'hpanel.Style.Add("Width", "240px")

        Dim lblspace As New Label
        lblspace.Text = " "
        lblspace.Width = Unit.Pixel(10)
        lblspace.BackColor = FromArgb(81, 143, 216)
        hpanel.Controls.Add(Image)
        hpanel.Controls.Add(lblspace)
        hpanel.Controls.Add(label)
        PlaceHolder1.Controls.Add(hpanel)
        'hpanel.ScrollBars = ScrollBars.None
        Dim cpanel As New Panel
        j = j + 1
        cpanel.ID = Trim("Panel" & j)
        cpanel.Attributes.Add("runat", "server")
        ' cpanel.CssClass = "leftHeading"
        cpanel.BorderColor = FromArgb(178, 198, 207)
        cpanel.BorderStyle = BorderStyle.Solid
        cpanel.BorderWidth = 1
        cpanel.Style.Add("Height", "46px")

        'cpanel.Style.Add("Width", "300px")
        cpanel.ScrollBars = ScrollBars.None
        PlaceHolder1.Controls.Add(cpanel)
        Dim menu As DataList = New DataList
        menu.ID = Trim("Datalist" & l)
        menu.ItemTemplate = Page.LoadTemplate("..\usercontrol\UserControlItemTemplate.ascx")
        'menu.SeparatorTemplate = Page.LoadTemplate("usercontrols\ListSeperator.ascx")

        menu.Style.Add("Width", "210px")
        menu.CellPadding = 0.5
        menu.ForeColor = Color.AliceBlue
        menu.BorderColor = FromArgb(178, 198, 207)
        menu.GridLines = GridLines.Both
        menu.BorderStyle = BorderStyle.Solid
        menu.BorderWidth = 0
        menu.SelectedItemStyle.BackColor = Color.Brown
        menu.SelectedItemStyle.Font.Bold = False
        menu.SelectedItemStyle.ForeColor = Color.Lavender
        menu.ItemStyle.BackColor = FromArgb(245, 244, 240)
        ' menu.CssClass = "leftsubmenu"

        PlaceHolder1.Controls.Add(menu)
        cpanel.Controls.Add(menu)
        Dim cpe As New AjaxControlToolkit.CollapsiblePanelExtender
        cpe.ID = Trim("CollapsiblePanelExtender" & l)
        cpe.TargetControlID = cpanel.ID
        cpe.ExpandControlID = hpanel.ID
        cpe.CollapseControlID = hpanel.ID
        cpe.ScrollContents = "False"
        cpe.Collapsed = "true"

        cpe.ImageControlID = Image.ID
        cpe.ExpandedImage = "~/Images/Buttons/collapse_blue.jpg"
        cpe.CollapsedImage = "~/Images/Buttons/expand.jpg"
        'cpe.SuppressPostBack = "true"
        cpe.TextLabelID = "TextLabel"
        cpe.CollapsedText = "Collapsed"
        cpe.ExpandedText = "Opened"
        'cpe.AutoCollapse = True
        'cpe.AutoExpand = True

        PlaceHolder1.Controls.Add(cpe)
        showmainMenu(menu, l, hpanel, cpanel)


    End Sub

    Private Sub showmainMenu(ByVal dGrid As DataList, ByVal ModId As Integer, ByRef hp As Panel, ByRef cp As Panel)
        Try
            Dim cmd As New SqlCommand()


            Dim str As String = "Select * from MyMainMenu where mainMenuTitle in (" & FillMainMenu(ModId) & ") Order by mainMenuTitleOrder"
            Dim da As New SqlDataAdapter(str, con)
            Dim ds As New DataSet
            da.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                dGrid.DataSource = ds
                dGrid.DataBind()
            Else
                hp.Visible = False
                cp.Visible = False

            End If

        Catch ex As Exception
        End Try

    End Sub
    Private Function FillMainMenu(ByVal ModuleID As Integer) As String
        Try
            Dim comm As New SqlCommand()
            Dim reader As SqlDataReader
            Dim tmpMenuU As String
            Dim tmpMenuG As String
            Dim tmpUserMenu As String
            Dim tmpGroupMenu As String
            Dim strFinalMenu As String
            Dim UserTypeId As String = ""
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            comm.CommandText = "Select mainMenuTitle,mainURL from MyMainMenu where ModuleID=" & ModuleID & " Order by mainMenuTitleOrder"
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            Do While reader.Read
                tmpMenuU = tmpMenuU & "'" & reader(0) & "',"
            Loop
            tmpMenuU = tmpMenuU & "''"
            reader.Close()
            comm.Dispose()
            con.Close()
            '-----------------------------------------------------------------------------------------------------------
            con.Open()
            comm.CommandText = " SELECT UserAccessRights_MenuName, UserAccessRights_AccessFlag FROM UserAccessRights " & _
                      " WHERE UserAccessRights_AdminUserId ='" & Session("g_User") & "' AND UserAccessRights_MenuName IN (" & tmpMenuU & ")"
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            Do While reader.Read
                If reader("UserAccessRights_AccessFlag") = "1" Then
                    strFinalMenu = strFinalMenu & "'" & reader(0) & "',"
                End If
                tmpUserMenu = tmpUserMenu & "'" & reader(0) & "',"
            Loop
            tmpUserMenu = tmpUserMenu & "''"
            strFinalMenu = strFinalMenu & "''"
            reader.Close()
            con.Close()
            '===========================================================================================================
            REM : to get id which is not in user table
            con.Open()
            comm.CommandText = "Select mainMenuTitle,mainURL from MyMainMenu where ModuleID=" & ModuleID & " and mainMenuTitle not in (" & tmpUserMenu & ") Order by mainMenuTitleOrder"  'tmpUserMenu replace eith  strFinalMenu
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            Do While reader.Read
                tmpMenuG = tmpMenuG & "'" & reader(0) & "',"
            Loop
            tmpMenuG = tmpMenuG & "''"
            reader.Close()
            con.Close()
            '-----------------------------------------------------------------------------------
            'Get UserTypeId of that particular user 
            con.Open()
            Dim qry As String
            qry = " select AdminUser_UserTypeId from AdminUser where AdminUser_Kid='" & Session("g_User") & "'"
            comm.Connection = con
            comm.CommandText = qry
            comm.CommandType = CommandType.Text
            UserTypeId = comm.ExecuteScalar
            con.Close()
            '-------------------------------------------------------------------------------
            con.Open()
            comm.CommandText = " SELECT RoleAccessRights_MenuName, RoleAccessRights_AccessFlag FROM RoleAccessRights " & _
                                  " WHERE RoleAccessRights_UserTypeId ='" & UserTypeId & "' AND RoleAccessRights_MenuName IN (" & tmpMenuG & ")"
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            strFinalMenu = strFinalMenu & ","
            Do While reader.Read
                If reader("RoleAccessRights_AccessFlag") = 1 Then
                    strFinalMenu = strFinalMenu & "'" & reader(0) & "',"
                End If
                tmpUserMenu = tmpUserMenu & "'" & reader(0) & "',"
            Loop
            tmpUserMenu = tmpUserMenu & "''"
            strFinalMenu = strFinalMenu & "''"
            reader.Close()
            con.Close()
            '===================================================================================        
            Return strFinalMenu
        Catch ex As Exception
        End Try
    End Function
    Private Function FillsubMainMenu(ByVal MenuID As Integer) As String
        Try
            Dim comm As New SqlCommand()
            Dim reader As SqlDataReader
            Dim tmpMenuU As String
            Dim tmpMenuG As String
            Dim tmpUserMenu As String
            Dim tmpGroupMenu As String
            Dim strFinalMenu As String
            Dim UserTypeId As String = ""
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            con.Open()
            comm.CommandText = "Select subMenuTitle from MySubMenu where parentMenuID=" & MenuID & " Order by subMenuOrder"
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            Do While reader.Read
                tmpMenuU = tmpMenuU & "'" & reader(0) & "',"
            Loop
            tmpMenuU = tmpMenuU & "''"
            reader.Close()
            comm.Dispose()
            con.Close()
            '-----------------------------------------------------------------------------------------------------------
            con.Open()
            comm.CommandText = " SELECT UserAccessRights_MenuName, UserAccessRights_AccessFlag FROM UserAccessRights " & _
                      " WHERE UserAccessRights_AdminUserId ='" & Session("g_User") & "' AND UserAccessRights_MenuName IN (" & tmpMenuU & ")"
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            Do While reader.Read
                If reader("UserAccessRights_AccessFlag") = "1" Then
                    strFinalMenu = strFinalMenu & "'" & reader(0) & "',"
                End If
                tmpUserMenu = tmpUserMenu & "'" & reader(0) & "',"
            Loop
            tmpUserMenu = tmpUserMenu & "''"
            strFinalMenu = strFinalMenu & "''"
            reader.Close()
            con.Close()
            '===========================================================================================================
            REM : to get id which is not in user table
            con.Open()
            comm.CommandText = "Select subMenuTitle from MySubMenu where parentMenuID=" & MenuID & " and subMenuTitle not in (" & tmpUserMenu & ") Order by subMenuOrder"  'tmpUserMenu replace eith  strFinalMenu
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            Do While reader.Read
                tmpMenuG = tmpMenuG & "'" & reader(0) & "',"
            Loop
            tmpMenuG = tmpMenuG & "''"
            reader.Close()
            con.Close()
            '-----------------------------------------------------------------------------------
            'Get UserTypeId of that particular user 
            con.Open()
            Dim qry As String
            qry = " select AdminUser_UserTypeId from AdminUser where AdminUser_Kid='" & Session("g_User") & "'"
            comm.Connection = con
            comm.CommandText = qry
            comm.CommandType = CommandType.Text
            UserTypeId = comm.ExecuteScalar
            con.Close()
            '-------------------------------------------------------------------------------
            con.Open()
            comm.CommandText = " SELECT RoleAccessRights_MenuName, RoleAccessRights_AccessFlag FROM RoleAccessRights " & _
                                  " WHERE RoleAccessRights_UserTypeId ='" & UserTypeId & "' AND RoleAccessRights_MenuName IN (" & tmpMenuG & ")"
            comm.CommandType = CommandType.Text
            comm.Connection = con
            reader = comm.ExecuteReader
            strFinalMenu = strFinalMenu & ","
            Do While reader.Read
                If reader("RoleAccessRights_AccessFlag") = 1 Then
                    strFinalMenu = strFinalMenu & "'" & reader(0) & "',"
                End If
                tmpUserMenu = tmpUserMenu & "'" & reader(0) & "',"
            Loop
            tmpUserMenu = tmpUserMenu & "''"
            strFinalMenu = strFinalMenu & "''"
            reader.Close()
            con.Close()
            '===================================================================================        
            Return strFinalMenu
        Catch ex As Exception
        End Try
    End Function

    Private Sub DrawSubMenu()
        If CStr(Request.QueryString("isSubMenu")) = "Y" Then
            Session("SubMenuId1") = CInt(Request.QueryString("MenuId"))
        End If
        Dim backpanel As New Panel
        j = j + 1

        backpanel.ID = Trim("Panel" & j)
        backpanel.Attributes.Add("runat", "server")
        'backpanel.CssClass = "leftHeading"
        Dim backlabel As New Label
        ' backlabel.Text = s
        backlabel.Font.Bold = True

        backlabel.ForeColor = Color.White 'Color.Lavender
        backlabel.BackColor = FromArgb(81, 143, 216)
        backpanel.BackColor = FromArgb(81, 143, 216)
        backpanel.ForeColor = FromArgb(178, 198, 207)
        ' backlabel.CssClass = "leftHeading"
        backpanel.BorderColor = FromArgb(81, 143, 216)
        backpanel.BorderStyle = BorderStyle.Solid
        backpanel.BorderWidth = 1
        'hpanel.BackColor = Color.Red
        backpanel.Style.Add("Height", "16px")
        backpanel.Style.Add("Width", "198px")
        'backlabel.ForeColor = Color.White
        backpanel.Controls.Add(backlabel)

        PlaceHolder1.Controls.Add(backpanel)
        Dim hpanel As New Panel
        j = j + 1

        hpanel.ID = Trim("Panel" & j)
        hpanel.Attributes.Add("runat", "server")
        'hpanel.CssClass = "leftHeading"
        Dim label As New Label
        label.Font.Bold = True
        label.ForeColor = Color.Lavender
        'label.Style.Add("Width", "200px")
        label.EnableTheming = "False"
        label.Attributes.Add("runat", "server")
        'label.CssClass = "leftHeading"
        label.Width = Unit.Pixel(160)
        Dim Image As New ImageButton
        Image.ID = "Image" & 1
        Image.ImageUrl = "~/Images/Buttons/big_button.jpg"
        Image.AlternateText = "Show Menu"
        Image.Attributes.Add("runat", "server")


        hpanel.BorderColor = FromArgb(178, 198, 207)
        hpanel.BorderStyle = BorderStyle.Solid
        hpanel.BorderWidth = 1
        hpanel.BackColor = FromArgb(81, 143, 216)
        'hpanel.BackColor = Color.Red
        hpanel.Style.Add("Height", "16px")
        hpanel.Style.Add("Width", "198px")
        Dim lblspace As New Label
        lblspace.Text = ""
        lblspace.Width = Unit.Pixel(10)
        lblspace.BackColor = FromArgb(81, 143, 216)
        hpanel.Controls.Add(Image)
        hpanel.Controls.Add(lblspace)
        hpanel.Controls.Add(label)
        PlaceHolder1.Controls.Add(hpanel)

        Dim cpanel As New Panel
        j = j + 1
        cpanel.ID = Trim("Panel" & j)
        cpanel.Attributes.Add("runat", "server")
        'cpanel.CssClass = "leftHeading"
        cpanel.BorderColor = FromArgb(178, 198, 207)
        cpanel.BorderStyle = BorderStyle.Solid
        cpanel.BorderWidth = 1
        cpanel.Style.Add("Height", "16px")
        cpanel.Style.Add("Width", "198px")
        PlaceHolder1.Controls.Add(cpanel)

        Dim menu As DataList = New DataList
        menu.ID = Trim("Datalist" & 1)
        menu.ItemTemplate = Page.LoadTemplate("..\usercontrol\WebUserControlSubMenu.ascx")
        menu.Style.Add("Width", "200px")
        menu.CellPadding = 0.5

        menu.ForeColor = Color.AliceBlue
        menu.BorderColor = FromArgb(178, 198, 207)
        menu.GridLines = GridLines.Both
        menu.BorderStyle = BorderStyle.Solid
        menu.BorderWidth = 1
        'menu.AlternatingItemStyle.BackColor = Color.White
        menu.SelectedItemStyle.BackColor = Color.Brown
        menu.SelectedItemStyle.Font.Bold = True
        menu.SelectedItemStyle.ForeColor = Color.Lavender

        menu.ItemStyle.BackColor = FromArgb(218, 236, 250)
        menu.CssClass = "leftsubmenu"
        PlaceHolder1.Controls.Add(menu)

        cpanel.Controls.Add(menu)

        Dim cpe As New AjaxControlToolkit.CollapsiblePanelExtender
        cpe.ID = Trim("CollapsiblePanelExtender" & 1)
        cpe.TargetControlID = cpanel.ID
        cpe.ExpandControlID = hpanel.ID
        cpe.CollapseControlID = hpanel.ID
        cpe.ScrollContents = "False"
        cpe.Collapsed = "true"

        cpe.ImageControlID = Image.ID
        cpe.ExpandedImage = "~/Images/Buttons/big_button.jpg"
        cpe.CollapsedImage = "~/Images/Buttons/big_button.jpg"
        cpe.SuppressPostBack = "true"
        cpe.TextLabelID = "TextLabel"
        cpe.CollapsedText = "Collapsed"
        cpe.ExpandedText = "Opened"

        PlaceHolder1.Controls.Add(cpe)


        FilldgSubMenu(Session("SubMenuId1"), backlabel, menu, label)            ' repplace by new function

    End Sub
    Private Sub FilldgSubMenu(ByVal MenuId As Integer, ByRef label1 As Label, ByRef datalist1 As DataList, ByRef label2 As Label)
        Try
            Dim cn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
            Dim cm As New SqlCommand()
            Dim rdr As SqlDataReader
            Dim displaytext As String
            If cn.State = ConnectionState.Closed Then
                cn.Open()
            End If
            cm.CommandText = "Select mainMenuTitle from MyMainMenu where mainMenuID=" & MenuId
            cm.CommandType = CommandType.Text
            cm.Connection = cn
            rdr = cm.ExecuteReader
            If rdr.Read Then
                If Not IsDBNull(rdr("mainMenuTitle")) Then
                    label1.Text = getParentPath(MenuId)
                    label2.Text = rdr("mainMenuTitle")
                End If
            End If
            rdr.Close()
            cn.Close()
            FilldgMainMenu(datalist1, MenuId)
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub
    Private Sub FilldgMainMenu(ByVal list As DataList, ByVal parentid As Integer)
        Try
            Dim cn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
            Dim cmd As New SqlCommand()
            Dim rdr As SqlDataReader
            cmd.CommandText = "Select * from MySubMenu where subMenuTitle in (" & FillsubMainMenu(parentid) & ") Order by subMenuOrder"
            cn.Open()
            cmd.CommandType = CommandType.Text
            cmd.Connection = cn
            rdr = cmd.ExecuteReader
            list.DataSource = rdr
            list.DataBind()
            rdr.Close()
            cn.Close()
        Catch ex As Exception
            'MessageBox(ex.Message)
        End Try
    End Sub
    Private Function getParentPath(ByVal MenuId As Integer) As String
        Dim path As String
        Dim cn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
        Dim cmd As New SqlCommand()
        Dim rdr As SqlDataReader
        Dim HomePage As String = System.Configuration.ConfigurationManager.AppSettings("HomePage")
        Dim Menu_Id As Integer
        Dim isSubMenu As Char
        cn.Open()
        cmd.CommandText = "Select parentMenuID from MySubMenu where subMenuID=" & MenuId
        cmd.CommandType = CommandType.Text
        cmd.Connection = cn
        Menu_Id = cmd.ExecuteScalar
        cn.Close()
        If Menu_Id = 0 Then
            path = "<A class=leftbanner href='tester.aspx' target='_self'>Back to Home</A>"
        Else
            cn.Open()
            cmd.CommandText = "select URL from MySubMenu where subMenuID=(Select parentMenuID from MySubMenu where subMenuID=" & MenuId & ")"
            cmd.CommandType = CommandType.Text
            cmd.Connection = cn
            isSubMenu = CType(cmd.ExecuteScalar, Char)
            cn.Close()
            If isSubMenu <> "Y" Then
                path = "<A class=leftbanner href='tester.aspx' target='_self'>Back to Home</A>"
            Else
                cn.Open()
                cmd.CommandText = "Select * from MySubMenu where parentMenuID =(Select ParentIdparentMenuID from MySubMenu where subMenuID=" & MenuId & ")"
                cmd.CommandType = CommandType.Text
                cmd.Connection = cn
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                If rdr.Read Then
                    path = "<A class=leftbanner href='" & ConfigurationSettings.AppSettings("PageBase") & "/" & rdr("URL") & getURLChar(rdr("URL")) & "isSubMenu=Y &MenuId=" & rdr("subMenuID") & "' target='_self'>Back to " & rdr("subMenuTitle") & "</A>"
                Else
                    path = "<A class=leftbanner href='" & ConfigurationSettings.AppSettings("PageBase") & "/" & HomePage & "' target='_self'>Back to Home</A>"
                End If
                cn.Close()
            End If
        End If
        Return path
    End Function
    Public Function getURLChar(ByVal NavigateURL As String) As Char
        Dim ch As Char
        If InStr(NavigateURL, "?", CompareMethod.Text) > 0 Then
            ch = "&"
        Else
            ch = "?"
        End If
        Return ch
    End Function

End Class
