AdminCategory RecursivelyFillCategoryTree(ForumRespository db, Forum_Category ToFill, AdminCategoryModel model = null, int ID = 0) { AdminCategory newCategory = new AdminCategory() { AllowPosts = ToFill.AllowPosts, id = ToFill.CategoryID, InheritPermissions = ToFill.InheritPermissions, Name = ToFill.Name, Priority = ToFill.Priority }; if (newCategory.id == ID) model.CurrentCategory = newCategory; var SortedCategories = db.GetSortedCategories(ToFill.CategoryID); if (!ToFill.AllowPosts) SortedCategories = SortedCategories.OrderByDescending(C => C.AllowPosts); foreach (var Child in SortedCategories) newCategory.Children.Add(RecursivelyFillCategoryTree(db, Child, model, ID)); return newCategory; }
public ActionResult Category(int id, string NewCategory, string UpdateCategory, string MoveCategory) { var model = new AdminCategoryModel(); model.AddNavigation("Admin Panel", "Overview", "Admin", null); model.AddNavigation("Edit Category"); using (ForumRespository db = new ForumRespository()) { Forum_User CurrentUser = GetCurrentUser(db); if (!UserIdentity.IsAdmin) return AuthenticationHelper.AccessDeniedView(model); // Administrating the forum requires the user to be an Admin. HandlePermissionsLinkUpdates(); Forum_Category Root = db.GetCategoryByID((int)BuildInCategory.Root); Forum_Category CurrentCategory = id != 0 ? db.GetCategoryByID(id) : Root; if (CurrentCategory == null) return NotFoundView("Category"); bool IsRoot = Root == CurrentCategory; if (IsHttpPost && AntiForgeryTokenValid) { if (!String.IsNullOrEmpty(NewCategory)) { Forum_Category NewForumCategory = new Forum_Category(); NewForumCategory.ParentID = CurrentCategory.CategoryID; NewForumCategory.Name = "Untitled Category"; NewForumCategory.InheritPermissions = true; NewForumCategory.AllowPosts = false; db.AddCategory(NewForumCategory); db.Save(); return RedirectToAction("Category", new { id = NewForumCategory.CategoryID }); } if (!String.IsNullOrEmpty(UpdateCategory) && CurrentCategory != Root) { var Form = Request.Form; string NewName = Form["CurrentCategory.Name"]; bool InheritPermissions = !String.IsNullOrEmpty(Form["Inherit Permissions"]); bool AllowPosts = !String.IsNullOrEmpty(Form["Allow Posts"]); CurrentCategory.AllowPosts = AllowPosts; CurrentCategory.InheritPermissions = InheritPermissions; try { CurrentCategory.Priority = Convert.ToInt32(Form["CurrentCategory.Priority"]); } catch { } if (!String.IsNullOrWhiteSpace(NewName)) CurrentCategory.Name = NewName.Trim(); db.Save(); } if (!String.IsNullOrEmpty(MoveCategory) && CurrentCategory != Root) { var Form = Request.Form; int DestinationID = 0; try { DestinationID = Convert.ToInt32(Form["MoveToDestination"]); } catch {} var Parent = db.GetCategoryByID(DestinationID); if (Parent != null) { while (Parent != null) { if (Parent == CurrentCategory) break; Parent = Parent.Category1; } if (Parent == null) { CurrentCategory.ParentID = DestinationID; db.Save(); } } } } foreach (var Category in db.GetAllCategories()) { var Parent = Category; while (Parent != null) { if (Parent == CurrentCategory) break; Parent = Parent.Category1; } if (Parent != null) continue; if (Category == CurrentCategory.Category1) continue; model.MoveCategoryToOptions.Add(new AdminNamedID() { Name = Category.Name, ID = Category.CategoryID }); } model.Root = RecursivelyFillCategoryTree(db, Root, model, CurrentCategory.CategoryID); model.UserGroups = db.GetAllRoles().Where(R => R.RoleID != (int)BuildInRole.Administrator).ToClassList(R => new AdminNamedID() { ID = R.RoleID, Name = R.Name }); model.PermissionSets = db.GetAllPermissionSets().ToClassList(P => new AdminNamedID() { ID = P.PermissionID, Name = P.Name }); model.Fixed = AdminPermissionLinkEditors.FixedSet.Categories; model.FixedNamedID = new AdminNamedID() { ID = model.CurrentCategory.id, Name = model.CurrentCategory.Name }; model.PermissionLinkList = db.GetPermissionLinks(CurrentCategory).OrderBy(L => L.CategoryID).ToClassList(L => new AdminPermissionLink() { Category = new AdminNamedID() { ID = L.CategoryID, Name = db.GetCategoryByID(L.CategoryID).Name }, UserGroup = new AdminNamedID() { ID = L.RoleID, Name = db.GetRole(L.RoleID).Name }, PermissionSet = new AdminNamedID() { ID = L.PermissionID, Name = db.GetPermissionSetByID(L.PermissionID).Name }, }); return View(model); } }