Imports System
Imports System.Data
Imports System.Web.UI.WebControls
Imports ASPNET.StarterKit.TimeTracker.BusinessLogicLayer

Namespace ASPNET.StarterKit.TimeTracker.Web

    '*********************************************************
    '
    ' List page of users, also allow users to assign different roles.
    ' Drills into UserDetail Page.
    '
    '*********************************************************

    Public Class UserList
        Inherits System.Web.UI.Page
        Protected WithEvents btnNewProject As System.Web.UI.WebControls.Button
        Protected WithEvents Users As System.Web.UI.WebControls.DataGrid
        Protected WithEvents NewUserButton As System.Web.UI.WebControls.Button

        ' Use User component to capture user's input during edit mode
        Protected _userInput As New TTUser(0, String.Empty, String.Empty, String.Empty)

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        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

        '*******************************************************
        '
        ' The Page_Load server event handler on this page is used
        ' to populate information fields on this page
        '
        '*******************************************************
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Ensure that the visiting user has access to view the current page
            If TTSecurity.IsInRole(TTUser.UserRoleAdministrator) = False Then
                Response.Redirect("AccessDenied.aspx?Index=-1", True)
            End If

            If Not Page.IsPostBack Then
                LoadUsers()
            End If
        End Sub 'Page_Load

        '*******************************************************
        '
        ' GetRoles methods returns a list of User Roles from Components.Roles
        '
        '*******************************************************

        Protected Function GetRoles() As RolesCollection
            Return Roles.GetRoles()
        End Function 'GetRoles

        '*******************************************************
        '
        ' LoadUsers method populate the DataGrid with a list of Time Tracker users.
        '
        '*******************************************************

        Private Sub LoadUsers()
            Dim usrs As UsersCollection = TTUser.GetUsers(TTSecurity.GetUserID(), TTSecurity.GetUserRole())

            ' Check for sorting when binding grid
            SortGridData(usrs, SortField, SortAscending)
            Users.DataSource = usrs
            Users.DataKeyField = "UserID"
            Users.DataBind()
        End Sub 'LoadUsers

        '*******************************************************
        '
        ' SortGridData methods sorts the Users Grid based on which
        ' sort field is being selected.  Also does reverse sorting based on the boolean.
        '
        '*******************************************************

        Private Sub SortGridData(ByVal list As UsersCollection, ByVal sortField As String, ByVal asc As Boolean)
            Dim sortCol As UsersCollection.UserFields = UsersCollection.UserFields.InitValue

            Select Case sortField
                Case "Name"
                    sortCol = UsersCollection.UserFields.Name
                Case "RoleName"
                    sortCol = UsersCollection.UserFields.RoleName
            End Select

            list.Sort(sortCol, asc)
        End Sub 'SortGridData

        '*******************************************************
        '
        ' Users_Sort server event handler on this page is used to
        ' sorts the DataGrid according to the sort field
        ' being selected.
        '
        '*******************************************************

        Private Sub Users_Sort(ByVal [source] As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles Users.SortCommand
            SortField = e.SortExpression
            LoadUsers()
        End Sub 'Users_Sort

        '*******************************************************
        '
        ' Users_PageIndexChanged server event handler on this page is used
        ' for changing page index
        '
        '*******************************************************

        Private Sub Users_PageIndexChanged(ByVal [source] As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Users.PageIndexChanged
            Users.EditItemIndex = -1
            Users.CurrentPageIndex = e.NewPageIndex
            LoadUsers()
        End Sub 'Users_PageIndexChanged

        Private Sub NewUserButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewUserButton.Click
            Dim mainIndex As Integer = IIf(Request("index") Is Nothing, 0, Convert.ToInt32(Request("index")))
            Dim adminIndex As Integer = IIf(Request("adminindex") Is Nothing, 0, Convert.ToInt32(Request("adminindex")))

            Response.Redirect([String].Format("UserDetail.aspx?index={0}&adminindex={1}", mainIndex, adminIndex), False)
        End Sub 'NewUserButton_Click

        Property SortField() As String
            Get
                Dim o As Object = ViewState("SortField")
                If o Is Nothing Then
                    Return String.Empty
                End If
                Return CStr(o)
            End Get

            Set(ByVal Value As String)
                ' Same as current sort file, toggle sort direction
                If Value = SortField Then
                    SortAscending = Not SortAscending
                End If
                ViewState("SortField") = Value
            End Set
        End Property

        '*********************************************************************
        '
        ' SortAscending property is tracked in ViewState
        '
        '*********************************************************************

        Property SortAscending() As Boolean
            Get
                Dim o As Object = ViewState("SortAscending")
                If o Is Nothing Then
                    Return True
                End If
                Return CBool(o)
            End Get

            Set(ByVal Value As Boolean)
                ViewState("SortAscending") = Value
            End Set
        End Property
    End Class 'UserList
End Namespace 'ASPNET.StarterKit.TimeTracker.Web



