<%
Response.Buffer = EW_RESPONSE_BUFFER
%>
<!--#include file="ewcfg11.asp"-->
<!--#include file="UserLevelsinfo.asp"-->
<!--#include file="zlogininfo.asp"-->
<!--#include file="aspfn11.asp"-->
<!--#include file="md5.asp"-->
<!--#include file="userfn11.asp"-->
<% Session.Timeout = 900 %>
<% Call ew_Header(False, EW_CHARSET) %>
<%

' Define page object
Dim userpriv
Set userpriv = New cuserpriv
Set Page = userpriv

' Page init processing
userpriv.Page_Init()

' Page main processing
userpriv.Page_Main()

' Global Page Rendering event (in userfn*.asp)
Page_Rendering()

' Page Rendering event
userpriv.Page_Render()
%>
<!--#include file="header.asp"-->
<script type="text/javascript">
// Page object
var userpriv = new ew_Page("userpriv");
userpriv.PageID = "userpriv"; // Page ID
var EW_PAGE_ID = userpriv.PageID; // For backward compatibility
// Form object
var fuserpriv = new ew_Form("fuserpriv");
// Form_CustomValidate event
fuserpriv.Form_CustomValidate = 
 function(fobj) { // DO NOT CHANGE THIS LINE!
 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
// Use JavaScript validation or not
<% If EW_CLIENT_VALIDATE Then %>
fuserpriv.ValidateRequired = true; // Use JavaScript validation
<% Else %>
fuserpriv.ValidateRequired = false; // No JavaScript validation
<% End If %>
// Dynamic selection lists
// Form object for search
</script>
<script type="text/javascript">
// Write your client script here, no need to add script tags.
</script>
<% If UserLevels.Export = "" Then %>
<% Breadcrumb.Render() %>
<% End If %>
<p><%= Language.Phrase("UserLevel") %><%= Security.GetUserLevelName(UserLevels.UserLevelID.CurrentValue) %>(<%= UserLevels.UserLevelID.CurrentValue %>)</p>
<% userpriv.ShowMessage %>
<script type="text/javascript">
var fuserpriv = new ew_Form("fuserpriv");
</script>
<form name="fuserpriv" id="fuserpriv" class="ewForm form-inline" action="<%= ew_CurrentPage %>" method="post">
<table class="ewGrid"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<input type="hidden" name="t" value="UserLevels">
<input type="hidden" name="a_edit" id="a_edit" value="U">
<!-- hidden tag for User Level ID -->
<input type="hidden" name="x_UserLevelID" id="x_UserLevelID" value="<%= UserLevels.UserLevelID.CurrentValue %>">
<table id="tbl_userpriv" class="ewTable ewTableSeparate">
	<thead>
	<tr class="ewTableHeader">
		<td><%= Language.Phrase("TableOrView") %></td>
		<td><label class="checkbox"><%= Language.Phrase("PermissionAddCopy") %><input type="checkbox" name="Add" id="Add" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
		<td><label class="checkbox"><%= Language.Phrase("PermissionDelete") %><input type="checkbox" name="Delete" id="Delete" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
		<td><label class="checkbox"><%= Language.Phrase("PermissionEdit") %><input type="checkbox" name="Edit" id="Edit" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
<% If EW_USER_LEVEL_COMPAT Then %>
		<td><label class="checkbox"><%= Language.Phrase("PermissionListSearchView") %><input type="checkbox" name="List" id="List" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
<% Else %>
		<td><label class="checkbox"><%= Language.Phrase("PermissionList") %><input type="checkbox" name="List" id="List" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
		<td><label class="checkbox"><%= Language.Phrase("PermissionView") %><input type="checkbox" name="View" id="View" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
		<td><label class="checkbox"><%= Language.Phrase("PermissionSearch") %><input type="checkbox" name="Search" id="Search" onclick="ew_SelectAll(this);"<%= userpriv.Disabled %>></label></td>
<% End If %>
	</tr>
	</thead>
	<tbody>
<%
For i = 0 to userpriv.TableNameCount - 1
	userpriv.TempPriv = Security.GetUserLevelPrivEx(userpriv.TableList(4,i) & userpriv.TableList(0,i), UserLevels.UserLevelID.CurrentValue)

	' Set row properties
	Call UserLevels.ResetAttrs()
%>
	<tr<%= UserLevels.RowAttributes %>>
		<td><%= userpriv.GetTableCaption(i) %></td>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="Add_<%= i %>" id="Add_<%= i %>" value="1"<% If (userpriv.TempPriv And EW_ALLOW_ADD) = EW_ALLOW_ADD Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="Delete_<%= i %>" id="Delete_<%= i %>" value="2"<% If (userpriv.TempPriv And EW_ALLOW_DELETE) = EW_ALLOW_DELETE Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="Edit_<%= i %>" id="Edit_<%= i %>" value="4"<% If (userpriv.TempPriv And EW_ALLOW_EDIT) = EW_ALLOW_EDIT Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
<% If EW_USER_LEVEL_COMPAT Then %>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="List_<%= i %>" id="List_<%= i %>" value="8"<% If (userpriv.TempPriv And EW_ALLOW_LIST) = EW_ALLOW_LIST Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
<% Else %>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="List_<%= i %>" id="List_<%= i %>" value="8"<% If (userpriv.TempPriv And EW_ALLOW_LIST) = EW_ALLOW_LIST Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="View_<%= i %>" id="View_<%= i %>" value="32"<% If (userpriv.TempPriv And EW_ALLOW_VIEW) = EW_ALLOW_VIEW Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
		<td class="ewCheckbox"><label class="checkbox"><input type="checkbox" name="Search_<%= i %>" id="Search_<%= i %>" value="64"<% If (userpriv.TempPriv And EW_ALLOW_SEARCH) = EW_ALLOW_SEARCH Then %> checked="checked"<% End If %><%= userpriv.Disabled %>></label></td>
<% End If %>
	</tr>
<% Next %>
	</tbody>				
</table>
</div>
</td></tr></table>
<button class="btn btn-primary ewButton" name="btnsubmit" id="btnsubmit" type="submit"<%= userpriv.Disabled %>><%= Language.Phrase("Update") %></button>
</form>
<script type="text/javascript">
// Write your startup script here
// document.write("page loaded");
</script>
<!--#include file="footer.asp"-->
<%

' Drop page object
Set userpriv = Nothing
%>
<%

' -----------------------------------------------------------------
' Page Class
'
Class cuserpriv

	' Page ID
	Public Property Get PageID()
		PageID = "userpriv"
	End Property

	' Project ID
	Public Property Get ProjectID()
		ProjectID = "{A697AD8C-67CB-4820-BF4F-84306CDBBEE7}"
	End Property

	' Page Object Name
	Public Property Get PageObjName()
		PageObjName = "userpriv"
	End Property

	' Page Name
	Public Property Get PageName()
		PageName = ew_CurrentPage()
	End Property

	' Page Url
	Public Property Get PageUrl()
		PageUrl = ew_CurrentPage() & "?"
	End Property

	' Message
	Public Property Get Message()
		Message = Session(EW_SESSION_MESSAGE)
	End Property

	Public Property Let Message(v)
		Dim msg
		msg = Session(EW_SESSION_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_MESSAGE) = msg
	End Property

	Public Property Get FailureMessage()
		FailureMessage = Session(EW_SESSION_FAILURE_MESSAGE)
	End Property

	Public Property Let FailureMessage(v)
		Dim msg
		msg = Session(EW_SESSION_FAILURE_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_FAILURE_MESSAGE) = msg
	End Property

	Public Property Get SuccessMessage()
		SuccessMessage = Session(EW_SESSION_SUCCESS_MESSAGE)
	End Property

	Public Property Let SuccessMessage(v)
		Dim msg
		msg = Session(EW_SESSION_SUCCESS_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_SUCCESS_MESSAGE) = msg
	End Property

	Public Property Get WarningMessage()
		WarningMessage = Session(EW_SESSION_WARNING_MESSAGE)
	End Property

	Public Property Let WarningMessage(v)
		Dim msg
		msg = Session(EW_SESSION_WARNING_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_WARNING_MESSAGE) = msg
	End Property

	' Show Message
	Public Sub ShowMessage()
		Dim hidden, html, sMessage
		hidden = False
		html = ""

		' Message
		sMessage = Message
		Call Message_Showing(sMessage, "")
		If sMessage <> "" Then ' Message in Session, display
			If Not hidden Then sMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sMessage
			html = html & "<div class=""alert alert-success ewSuccess"">" & sMessage & "</div>"
			Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session
		End If

		' Warning message
		Dim sWarningMessage
		sWarningMessage = WarningMessage
		Call Message_Showing(sWarningMessage, "warning")
		If sWarningMessage <> "" Then ' Message in Session, display
			If Not hidden Then sWarningMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sWarningMessage
			html = html & "<div class=""alert alert-warning ewWarning"">" & sWarningMessage & "</div>"
			Session(EW_SESSION_WARNING_MESSAGE) = "" ' Clear message in Session
		End If

		' Success message
		Dim sSuccessMessage
		sSuccessMessage = SuccessMessage
		Call Message_Showing(sSuccessMessage, "success")
		If sSuccessMessage <> "" Then ' Message in Session, display
			If Not hidden Then sSuccessMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sSuccessMessage
			html = html & "<div class=""alert alert-success ewSuccess"">" & sSuccessMessage & "</div>"
			Session(EW_SESSION_SUCCESS_MESSAGE) = "" ' Clear message in Session
		End If

		' Failure message
		Dim sErrorMessage
		sErrorMessage = FailureMessage
		Call Message_Showing(sErrorMessage, "failure")
		If sErrorMessage <> "" Then ' Message in Session, display
			If Not hidden Then sErrorMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sErrorMessage
			html = html & "<div class=""alert alert-error ewError"">" & sErrorMessage & "</div>"
			Session(EW_SESSION_FAILURE_MESSAGE) = "" ' Clear message in Session
		End If
		Response.Write "<table class=""ewStdTable""><tr><td><div class=""ewMessageDialog""" & ew_IIf(hidden, " style=""display: none;""", "") & ">" & html & "</div></td></tr></table>"
	End Sub
	Dim PageHeader
	Dim PageFooter

	' Show Page Header
	Public Sub ShowPageHeader()
		Dim sHeader
		sHeader = PageHeader
		Call Page_DataRendering(sHeader)
		If sHeader <> "" Then ' Header exists, display
			Response.Write "<p>" & sHeader & "</p>"
		End If
	End Sub

	' Show Page Footer
	Public Sub ShowPageFooter()
		Dim sFooter
		sFooter = PageFooter
		Call Page_DataRendered(sFooter)
		If sFooter <> "" Then ' Footer exists, display
			Response.Write "<p>" & sFooter & "</p>"
		End If
	End Sub

	' -----------------------
	'  Validate Page request
	'
	Public Function IsPageRequest()
		IsPageRequest = True
	End Function

	' -----------------------------------------------------------------
	'  Class initialize
	'  - init objects
	'  - open ADO connection
	'
	Private Sub Class_Initialize()
		If IsEmpty(StartTimer) Then StartTimer = Timer ' Init start time

		' Initialize language object
		If IsEmpty(Language) Then
			Set Language = New cLanguage
			Call Language.LoadPhrases()
		End If

		' Initialize table object
		If IsEmpty(UserLevels) Then Set UserLevels = New cUserLevels
		Set Table = UserLevels

		' Initialize urls
		' Initialize other table object

		If IsEmpty(zlogin) Then Set zlogin = New czlogin

		' Initialize form object
		Set ObjForm = Nothing

		' Intialize page id (for backward compatibility)
		EW_PAGE_ID = "userpriv"

		' Open connection to the database
		If IsEmpty(Conn) Then Call ew_Connect()
	End Sub

	' -----------------------------------------------------------------
	'  Subroutine Page_Init
	'  - called before page main
	'  - check Security
	'  - set up response header
	'  - call page load events
	'
	Sub Page_Init()
		Set Security = New cAdvancedSecurity
		If Not Security.IsLoggedIn() Then Call Security.AutoLogin()
		If Not Security.IsLoggedIn() Then
			Call Security.SaveLastUrl()
			Call Page_Terminate("login.asp")
		End If

		' Table Permission loading event
		Call Security.TablePermission_Loading()
		Call Security.LoadCurrentUserLevel(CurrentProjectID & "UserLevels")

		' Table Permission loaded event
		Call Security.TablePermission_Loaded()
		If Not Security.CanAdmin Then
			Call Security.SaveLastUrl()
			Call Page_Terminate( "login.asp")
		End If

		' Global page loading event (in userfn7.asp)
		Page_Loading()

		' Page load event, used in current page
		Page_Load()
	End Sub

	' -----------------------------------------------------------------
	'  Class terminate
	'  - clean up page object
	'
	Private Sub Class_Terminate()
		Call Page_Terminate("")
	End Sub

	' -----------------------------------------------------------------
	'  Subroutine Page_Terminate
	'  - called when exit page
	'  - clean up ADO connection and objects
	'  - if url specified, redirect to url
	'
	Sub Page_Terminate(url)

		' Page unload event, used in current page
		Call Page_Unload()

		' Global page unloaded event (in userfn60.asp)
		Call Page_Unloaded()
		Dim sRedirectUrl
		sReDirectUrl = url
		Call Page_Redirecting(sReDirectUrl)
		If Not (Conn Is Nothing) Then Conn.Close ' Close Connection
		Set Conn = Nothing
		Set Security = Nothing
		Set ObjForm = Nothing

		' Go to url if specified
		If sReDirectUrl <> "" Then
			If Response.Buffer Then Response.Clear
			Response.Redirect sReDirectUrl
		End If
	End Sub

	'
	'  Subroutine Page_Terminate (End)
	' ----------------------------------------

	Dim TempPriv, Disabled
	Dim Privileges
	Dim TableNameCount
	Dim ReportLanguage
	Dim UserLevelList
	Dim UserLevelPrivList
	Dim TableList

	' -----------------------------------------------------------------
	' Page main processing
	'
	Sub Page_Main()
		Set Breadcrumb = New cBreadcrumb
		Call Breadcrumb.Add("list", "UserLevels", "UserLevelslist.asp", "UserLevels", False)
		Call Breadcrumb.Add("userpriv", "UserLevelPermission", ew_CurrentUrl, "", True)

		' Load user levels from config file
		Call Security.LoadUserLevelFromConfigFile(UserLevelList, UserLevelPrivList, TableList, True)

		' Try to load ASP Report Maker language file
		' Note: The langauge IDs must be the same in both projects

		If EW_RELATED_LANGUAGE_FOLDER <> "" And IsEmpty(ReportLanguage) Then
			Set ReportLanguage = New cLanguage
			ReportLanguage.LanguageFolder = EW_RELATED_LANGUAGE_FOLDER
			Call ReportLanguage.LoadPhrases()
		End If
		If Not IsArray(TableList) Then
			FailureMessage = Language.Phrase("NoTableGenerated")
			Call Page_Terminate("UserLevelslist.asp") ' Return to list
		End If
		TableNameCount = UBound(TableList,2) + 1
		ReDim Privileges(UBound(TableList,2))

		' Get action
		If Request.Form("a_edit").Count <= 0 Then
			UserLevels.CurrentAction = "I" ' Display with input box

			' Load key from QueryString
			If Request.QueryString("UserLevelID").Count > 0 Then
				UserLevels.UserLevelID.QueryStringValue = Request.QueryString("UserLevelID")
			Else
				Call Page_Terminate("UserLevelslist.asp") ' Return to list
			End If
			If UserLevels.UserLevelID.QueryStringValue = "-1" Then
				Disabled = " disabled=""disabled"""
			Else
				Disabled = ""
			End If
		Else
			UserLevels.CurrentAction = Request.Form("a_edit")

			' Get fields from form
			UserLevels.UserLevelID.FormValue = Request.Form("x_UserLevelID")
			For i = 0 to TableNameCount - 1
				If EW_USER_LEVEL_COMPAT Then
					Privileges(i) = CInt(Request.Form("Add_" & i)) + _
						CInt(Request.Form("Delete_" & i)) + CInt(Request.Form("Edit_" & i)) + _
						CInt(Request.Form("List_" & i))
				Else
					Privileges(i) = CInt(Request.Form("Add_" & i)) + _
						CInt(Request.Form("Delete_" & i)) + CInt(Request.Form("Edit_" & i)) + _
						CInt(Request.Form("List_" & i)) + CInt(Request.Form("View_" & i)) + _
						CInt(Request.Form("Search_" & i))
				End If
			Next
		End If
		Select Case UserLevels.CurrentAction
			Case "I" ' Display
				If Not Security.SetUpUserLevelEx() Then ' Get all user level info
					Call Page_Terminate("UserLevelslist.asp") ' Return to list
				End If
			Case "U" ' Update
				If EditRow() Then ' Update Record based on key
					If SuccessMessage = "" Then SuccessMessage = Language.Phrase("UpdateSuccess") ' Set update success message

					' Alternatively, comment out the following line to go back to this page
					Call Page_Terminate("UserLevelslist.asp") ' Return to list
				End If
		End Select
	End Sub

	' -----------------------------------------------------------------
	' Update privileges
	'
	Function EditRow()
		Dim Sql
		Dim RowCnt, i
		For i = 0 to TableNameCount - 1
			Sql = "UPDATE " & EW_USER_LEVEL_PRIV_TABLE & " SET " & EW_USER_LEVEL_PRIV_PRIV_FIELD & " = " & Privileges(i) & " WHERE " & _
				EW_USER_LEVEL_PRIV_TABLE_NAME_FIELD & " = '" & ew_AdjustSql(TableList(4,i) & TableList(0,i)) & "' AND " & _
				EW_USER_LEVEL_PRIV_USER_LEVEL_ID_FIELD & " = " & UserLevels.UserLevelID.CurrentValue
			Conn.Execute Sql, RowCnt
			If RowCnt = 0 Then
				Sql = "INSERT INTO " & EW_USER_LEVEL_PRIV_TABLE & " (" & EW_USER_LEVEL_PRIV_TABLE_NAME_FIELD & ", " & EW_USER_LEVEL_PRIV_USER_LEVEL_ID_FIELD & ", " & EW_USER_LEVEL_PRIV_PRIV_FIELD & ") VALUES ('" & ew_AdjustSql(TableList(4,i) & TableList(0,i)) & "', " & UserLevels.UserLevelID.CurrentValue & ", " & Privileges(i) & ")"
				Conn.Execute Sql, RowCnt
			End If
		Next
		EditRow = True
	End Function

	' -------------------
	' Get table caption
	'
	Function GetTableCaption(i)
		Dim caption, report, other, project
		caption = ""
		If i < TableNameCount Then
			report = (TableList(4,i) = EW_RELATED_PROJECT_ID)
			other = (Not report And TableList(4,i) <> CurrentProjectID)
			If Not report And Not other Then
				caption = Language.TablePhrase(TableList(1,i), "TblCaption")
			End If
			If report And IsObject(ReportLanguage) Then
				caption = ReportLanguage.TablePhrase(TableList(1,i), "TblCaption")
			End If
			If caption = "" Then
				caption = TableList(2,i)
			End If
			If caption = "" Then
				caption = TableList(0,i)
			End If
			If report Then
				caption = caption & "<span class=""ewUserprivProject""> (" & Language.Phrase("Report") & ")</span>"
			End If
			If other Then
				If TableList(5,i) <> "" Then
					project = TableList(5,i)
					project = Mid(project, InStrRev(project, "\")+1)
				Else
					project = TableList(4,i)
				End If

				'project = TableList(4,i) ' *** Uncomment to use project id
				caption = caption & "<span class=""ewUserprivProject""> (" & project & ")</span>"
			End If
		End If
		GetTableCaption = caption
	End Function

	' Page Load event
	Sub Page_Load()

		'Response.Write "Page Load"
	End Sub

	' Page Unload event
	Sub Page_Unload()

		'Response.Write "Page Unload"
	End Sub

	' Page Redirecting event
	Sub Page_Redirecting(url)

		'url = newurl
	End Sub

	' Message Showing event
	' typ = ""|"success"|"failure"
	Sub Message_Showing(msg, typ)

		' Example:
		'If typ = "success" Then msg = "your success message"

	End Sub

	' Page Render event
	Sub Page_Render()

		'Response.Write "Page Render"
	End Sub

	' Page Data Rendering event
	Sub Page_DataRendering(header)

		' Example:
		'header = "your header"

	End Sub

	' Page Data Rendered event
	Sub Page_DataRendered(footer)

		' Example:
		'footer = "your footer"

	End Sub
End Class
%>
