EnVisageOnline/Main-RMO/Source/EnVisage/Views/User/Edit.cshtml

425 lines
18 KiB
Plaintext

@* ENV-648 @model EnVisage.AspNetUser *@
@model EnVisage.Models.UserModel
@using EnVisage.Code.HtmlHelpers
@{
//ViewBag.Title = (!string.IsNullOrEmpty(Model.Id)) ? "Edit " + Model.UserName : "Add New User";
ViewBag.Title = (Model.Id != Guid.Empty) ? "Edit " + Model.UserName : "Add New User";
}
@section Scripts
{
<link rel="stylesheet" href="~/Content/stylesheets/jquery.treegrid.css">
<script type="text/javascript" src="~/Scripts/ProjectTree.js"></script>
<script type="text/javascript">
emulateNavUrl = "/User";
init.push(function () {
@*if (!string.IsNullOrEmpty(Model.Id))*@
@if (Model.Id != Guid.Empty)
{
<text>
StartEdit('User', '@Model.Id.ToString()', "#btnDelete", "#btnsave", "erorMsgPlaceholder");
</text>
}
@*if (!string.IsNullOrWhiteSpace(Model.Id))*@
@if (Model.Id != Guid.Empty)
{
<text>
ProjectTree.buildProjectTree('@Url.Action("GetProjectAccessTree", "User")?userId=@Model.Id.ToString()');
</text>
}
else
{
<text>
ProjectTree.buildProjectTree('@Url.Action("GetProjectAccessTree", "User")?userId=');
</text>
}
$("input[name='areasread']").click(function () {
$(this).removeAttr("inherited");
});
$("input[name='areaswrite']").click(function () {
$(this).removeAttr("inherited");
var val = $(this).val();
var read = $("input[value='" + val + "'][name='areasread']");
if (read.length > 0 && this.checked)
read.removeAttr("inherited");
if($(this)[0].checked)
read.prop("checked", true);
});
$("input[name='roleitems']").click(function () {
var optionTexts = [];
$("input[name='roleitems']").each(function () {
if (this.checked)
optionTexts.push($(this).val());
});
var siteRoot = location.protocol + '//' + location.host;
$.ajax({
type: "post",
url: siteRoot + "/User/GetRolePermissions",
datatype: "json",
traditional: true,
data: { "roleId": optionTexts },
async: false,
error: function (returnval) {
console.writeln(returnval);
},
success: function (returnval) {
var arr = $.parseJSON(returnval);
$.each($("input[name='areasread']"), function (i, o) {
updatePermissionFormRole($(o), arr[0], 1, false);
});
$.each($("[name='areaswrite']"), function (i, o) {
updatePermissionFormRole($(o), arr[0], 2, false);
});
$.each($("input[name='projectlistread']"), function (i, o) {
updatePermissionFormRole($(o), arr[1], 1, true);
});
$.each($("input[name='projectlistwrite']"), function (i, o) {
updatePermissionFormRole($(o), arr[1], 2, true);
});
result = true;
}
});
});
$('#btnsave').click(function () {
if ($('#frmEditUser').valid()) {
blockUI();
beforeSubmit();
}
});
});
function updatePermissionFormRole(chk, arr, propIndx, useTreeObj) {
var inherited = false;
if (arr.length == 0) {
if (IsInherited(chk)) {
setInherited(chk, false, useTreeObj);
}
}
else {
for (var j = 0; j < arr.length; j++) {
var item = arr[j][0];
var itemAllow = arr[j][propIndx];
//area is inherited from role
if (item == chk.val() && IsInherited(chk)) {
setInherited(chk, itemAllow == "True", useTreeObj);
inherited = true;
break;
}
}
if (!inherited) {
if (IsInherited(chk)) {
setInherited(chk, false, useTreeObj);
}
}
}
}
function setInherited(o, checked, useTreeObj) {
o.attr("inherited", "inherited");
o.attr("role", checked ? '1' : '0');
if (useTreeObj == true) {
var obj = ProjectTree.setDataValue($(o).val(), $(o).attr("name") == "projectlistwrite" ? 'write' : 'read', $(o).attr("role") == "1",true);
ProjectTree.setParentValues(obj);
}
else {
o.prop("checked", checked);
}
}
function IsInherited(o) {
return o.attr("inherited") != null;
}
function reset() {
$.each($("input[name='areasread']"), function (i, o) {
//if ($(o).attr("inherited") != "inherited") {
//setInherited($(o), $(o).attr("role") == "1", false);
setInherited($(o), false, false);
// }
});
$.each($("[name='areaswrite']"), function (i, o) {
// if ($(o).attr("inherited") != "inherited") {
//setInherited($(o), $(o).attr("role") == "1", false);
setInherited($(o), false, false);
// }
});
$.each($("input[name='projectlistread']"), function (i, o) {
//if ($(o).attr("inherited") != "inherited") {
//setInherited($(o), $(o).attr("role") == "1", true);
setInherited($(o), false, true);
//}
});
$.each($("input[name='projectlistwrite']"), function (i, o) {
//if ($(o).attr("inherited") != "inherited") {
// setInherited($(o), $(o).attr("role") == "1", true);
setInherited($(o), false, true);
//}
});
}
var ProjectListArray = []
var AreaListArray = [];
function projectlist(id, read, write) {
this.id = id;
this.p = { read: read, write: write }
}
function arealist(id, read, write) {
this.id = id;
this.p = { read: read, write: write }
}
function beforeSubmit() {
$("input[name='overriden']").remove();
var form = document.getElementById("frmEditUser");
$.each($(".custominput"), function (i, o) {
if ($(o).attr("inherited") == "inherited") {
var hid = document.createElement('input');
$(hid).prop("type", "hidden");
$(hid).prop("name", "overriden");
$(hid).prop("value", $(o).prop("name") + "|" + $(o).prop("value"));
form.appendChild(hid);
}
});
var overriden = [];
var roles = [];
$.each($("input[name='roleitems']:checked"), function (i, o) {
var value = $(this).val();
roles.push(value);
});
$.each($("input[name='overriden']"), function (i, o) {
var value = $(this).val();
overriden.push(value);
});
$.each($("input[name='areasread']:checked"), function (i, o) {
var value = $(this).val();
var read = true;
var a = new arealist(value, read, false);
var results = $.grep(AreaListArray, function (e) { return e.id === value; });
if (results.length == 1) {
a = results[0];
var i = ProjectListArray.indexOf(pl);
a.p.read = read;
AreaListArray[i] = a;
}
else
AreaListArray.push(a);
});
$.each($("input[name='projectlistread']:checked"), function (i, o) {
var value = $(this).val();
var read = true;
var pl = new projectlist(value, read, false);
var results = $.grep(ProjectListArray, function (e) { return e.id === value; });
if (results.length == 1) {
pl = results[0];
var i = ProjectListArray.indexOf(pl);
pl.p.read =read;
ProjectListArray[i] = pl;
}
else
ProjectListArray.push(pl);
});
$.each($("input[name='projectlistwrite']:checked"), function (i, o) {
var value = $(this).val();
var write = true;
var pl = new projectlist(value, false, write);
var results = $.grep(ProjectListArray, function (e) { return e.id === value; });
if (results.length == 1) {
pl = results[0];
var i = ProjectListArray.indexOf(pl);
pl.p.write =write;
ProjectListArray[i] = pl;
}
else
ProjectListArray.push(pl);
});
$.each($("input[name='areaswrite']:checked"), function (i, o) {
var value = $(this).val();
var write = true;
var a = new arealist(value, false, write);
var results = $.grep(AreaListArray, function (e) { return e.id === value; });
if (results.length == 1) {
a = results[0];
var i = ProjectListArray.indexOf(a);
a.p.write = write;
AreaListArray[i] = a;
}
else
AreaListArray.push(a);
});
var id = $("input[name='Id']").val();
var username = $("input[name='UserName']").val();
var firstname = $("input[name='FirstName']").val();
var lastname = $("input[name='LastName']").val();
var email = $("input[name='Email']").val();
var preferredresourceallocation = $("input[name='PreferredResourceAllocation']").prop('checked') == true;
var discriminator = $("input[name='Discriminator']").val();
var projecttree = { ProjectList: ProjectListArray, AreaList: AreaListArray, Overriden: overriden, RoleItems: roles };
var aspnetuser = { Id: id, UserName: username, FirstName: firstname, LastName: lastname, Email: email, PreferredResourceAllocation: preferredresourceallocation, Discriminator: discriminator };
var form = $('#frmEditUser');
var token = $('input[name="__RequestVerificationToken"]', form).val();
var permissiondata = { projecttree: projecttree, aspnetuser: aspnetuser };
var CommonErrorMessage = 'An error occurred while processing your request. Please, try again later.';
$.ajax({
type: "post",
url: siteRoot + "/User/Edit",
context: document.body,
data: { __RequestVerificationToken: token, edituser: permissiondata },
error: function (response) {
unblockUI();
showErrorModal('Oops!', CommonErrorMessage);
},
success: function (response) {
window.location.href = response.Url;
}
});
return true;
}
$('#PreferredResourceAllocation').switcher({
on_state_content: 'Resources',
off_state_content: 'Hours'
});
$('#PreferredResourceAllocation').parent().css("width", "90px");
</script>
}
<div id="erorMsgPlaceholder"></div>
@using (Html.BeginForm("Edit", "User", FormMethod.Post, new { @id = "frmEditUser", @class = "panel form-horizontal" , @onsubmit = "return beforeSubmit();" }))
{
@Html.AntiForgeryToken()
<div class="panel-body">
@Html.Hidden("Id", Model.Id)
<div class="form-group">
@Html.LabelFor(model => model.UserName, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.LastName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Phone, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Phone)
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="Phone">Preferred Resource Allocation</label>
<div class="col-sm-10">
@Html.CheckBoxFor(model => model.PreferredResourceAllocation, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PreferredResourceAllocation)
</div>
</div>
<div class="form-group">
<span class="col-sm-2 control-label" style="font-weight:600;">Roles</span>
<div class="col-sm-10" style="padding-top:5px;padding-left:11px;">
@Html.GetRolesList(@Url, Model)
</div>
</div>
<div class="form-group">
<span class="col-sm-2 control-label" style="font-weight:600;"></span>
<div class="col-sm-10" style="padding-top:5px;padding-left:11px;">
<a class="btn btn-primary" href="javascript:reset();">Reset permissions</a>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="UserName">Permissions</label>
<div class="col-sm-10">
<table cellspacing="0" border="0">
<tbody>
<tr>
<td style="padding-top: 7px; vertical-align:top;">
<table>
<tbody>
@Html.GetAreaItemsList(@Url, Model)
</tbody>
</table>
</td>
<td style="padding:10px;vertical-align:top; padding-left:50px;" id="projectTree"></td>
</tr>
</tbody>
</table>
</div>
</div>
@Html.ValidationSummary(false, "The user could not be saved due to the following errors:")
<div class="form-group" style="margin-bottom: 0;">
<div class="col-sm-offset-2 col-sm-10">
<a class="btn btn-primary" href="@Url.Action("Index", "User")"><i class="fa fa-backward"></i> Back to list</a>
<button type="button" class="btn btn-success" id="btnsave"><i class="fa fa-save"></i> Save</button>
@if (Model != null)
{
@*if (Model.Projects.Count > 0)
{
<a id="btnDelete" class="btn btn-danger disabled" href="javascript:void(0);"><i class="fa fa-trash-o"></i> Delete</a>
}
else
{
*@
@* if (!string.IsNullOrEmpty(Model.Id))*@
if (Model.Id != Guid.Empty)
{
<a id="btnDelete" class="btn btn-danger" href="@Url.Action("Delete", "User", new { @id = Model.Id })"><i class="fa fa-trash-o"></i> Delete</a>
}
//}
}
</div>
@*@if (Model != null) { Html.HiddenFor(model => model.Id); }*@
@*@if (Model != null) { Html.HiddenFor(model => model.Discriminator); }*@
@if (Model != null) { Html.Hidden("Discriminator", Model.Discriminator); }
</div>
</div>
}