public virtual ActionResult AddModule(string role) { if (role == null) { return RedirectToAction("Index"); } AddModulesToRoleViewModel model = new AddModulesToRoleViewModel(); List<Module> temp = new List<Module>(); string render = ""; model.RoleName = role; model.Actions = actionRepo.Find(); //var rootModule = moduleRepo.Find().FindAll(x => x.ParentModule == null); Role r = roleRepo.FindByName(role); //foreach (Module m in rootModule) { // moduleRepo.GetAllChildrenInModule(m.ModuleName,ref temp, 0); //} //foreach (Module m in temp) { // render += DrawRowModule(m.lvl, m, model.Actions, r); //} List<Module> modules = moduleRepo.Find(); List<ModuleViewModel> mvms = new ModuleViewModel().MapRecursive(modules); foreach (ModuleViewModel m in mvms) { render += DrawRowModule(m.Level, m, model.Actions, r); } model.Render = render; return View(model); }
public List<ModuleViewModel> MapRecursive(List<Module> dbItems, string parentModule = null, int level = 1) { List<ModuleViewModel> result = new List<ModuleViewModel> { }; List<Module> modules = dbItems.FindAll(m => m.ParentModule == parentModule); ModuleViewModel mvm; int childLevel = level + 1; foreach (Module row in modules) { mvm = new ModuleViewModel(row, level); mvm.Actions = new List<string>(); var enumerator = row.Actions.GetEnumerator(); while (enumerator.MoveNext()) { mvm.Actions.Add(enumerator.Current.ActionName); } result.Add(mvm); result.AddRange(this.MapRecursive(dbItems, row.ModuleName, childLevel)); } return result; }
public virtual ActionResult GetModuleBinding(string moduleName) { List<Module> tempModules; List<Module> modules = moduleRepo.Find(); List<ModuleViewModel> response = new List<ModuleViewModel>(); bool hasChildren = false; tempModules = modules; if (moduleName == null) { modules = modules.FindAll(x => x.ParentModule == null); } else { modules = modules.FindAll(x => x.ParentModule == moduleName); } for (int i=0; i < modules.Count; ++i) { hasChildren = false; int countChildren = tempModules.FindAll(x => x.ParentModule == modules[i].ModuleName).Count; if(countChildren > 0){ hasChildren = true; } ModuleViewModel m = new ModuleViewModel() { ModuleName = modules[i].ModuleName, ParentModule = modules[i].ParentModule, HasChildren = hasChildren }; response.Add(m); } return Json(response.ToArray(), JsonRequestBehavior.AllowGet); }
public JsonResult GetListModule() { List<Module> tempModules = new List<Module>(); List<ModuleViewModel> result = new List<ModuleViewModel>(); List<Module> modules = moduleRepo.Find(); //modules = modules.FindAll(x => x.ParentModule == null); //foreach (Module m in modules) //{ // tempModules.Add(m); // moduleRepo.GetAllChildrenInModule(m.ModuleName, ref tempModules, 0); //} //foreach (Module m in tempModules) //{ // result.Add(new ModuleViewModel(m)); //} result = new ModuleViewModel().MapRecursive(modules); return Json(result, JsonRequestBehavior.AllowGet); }
//helper public string DrawRowModule(int level, ModuleViewModel m, List<Business.Entities.Action> actions, Role role) { string collumn = "col-md-" + 9 / (actions.Count + 1); string style = "margin-left:" + 15 * level + "px"; string row = @"<div class='row' style='border-bottom:1px solid #000'> <div class='col-md-3'><div style='" + style + "'>" + m.ModuleName + @"</div></div>"; foreach (Business.Entities.Action a in actions) { bool found = false; if (m.Actions.Count > 0) { ModulesInRole mr = repoModulesInRole.FindByRoleAndModule(role.RoleId, m.Id); found = false; if (mr != null)//check action { var enumerator = mr.Actions.GetEnumerator(); while (enumerator.MoveNext() && !found) { if (enumerator.Current.Id == a.Id) { found = true; } } } if (found) row += "<div class='" + collumn + @"'><input type='checkbox' checked='checked' value='" + m.Id + ";" + a.Id + "' /></div>"; else row += "<div class='" + collumn + @"'><input type='checkbox' value='" + m.Id + ";" + a.Id + "' /></div>"; } else { row += "<div class='" + collumn + @"'>-</div>"; } } row += " </div>"; return row; }