Exemplo n.º 1
0
        public ActionResult DeleteCategory(int id, int? MovePostsDestination, string MovePostOption, int? MoveChildrenDestination)
        {
            var model = new AdminDeleteCategoryViewModel();
            model.AddNavigation("Admin Panel", "Overview", "Admin", null);
            model.AddNavigation("Delete 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.

                Forum_Category CurrentCategory = db.GetCategoryByID(id);

                if (CurrentCategory == null)
                    return NotFoundView("Category");

                if (CurrentCategory.CategoryID == (int)BuildInCategory.Root)
                    return RedirectToAction("Overview");

                if (IsHttpPost && AntiForgeryTokenValid)
                {
                    db.DeleteCategory(CurrentCategory);

                    if (CurrentCategory.AllowPosts)
                    {
                        bool DeletePosts = String.Equals(MovePostOption, "Delete", StringComparison.Ordinal);
                        if (!DeletePosts && MovePostsDestination == null)
                            return RedirectToAction("Overview");

                        if (!DeletePosts)
                        {
                            foreach (var Thread in CurrentCategory.Forum_Threads)
                            {
                                Thread.CategoryID = (int)MovePostsDestination;
                            }
                        }
                        else
                        {
                            db.DeleteThreads(CurrentCategory.Forum_Threads);
                        }
                    }

                    if (CurrentCategory.Forum_Categories.Count > 0)
                    {
                        var NewParent = db.GetCategoryByID((int)MoveChildrenDestination);

                        if (NewParent == null)
                            return RedirectToAction("Overview");

                        foreach (var Child in CurrentCategory.Forum_Categories)
                        {
                            Child.ParentID = NewParent.CategoryID;
                        }
                    }

                    db.Save();
                    return RedirectToAction("Category");
                }

                model.ThreadCount = CurrentCategory.Forum_Threads.Count;
                model.HasChildCategories = CurrentCategory.Forum_Categories.Count > 0;
                model.CategoryName = CurrentCategory.Name;

                foreach (var Category in db.GetAllCategories())
                {
                    if (Category.CategoryID != (int)BuildInCategory.Root & Category.CategoryID != CurrentCategory.CategoryID)
                        model.MovePostsToOptions.Add(new AdminNamedID() { Name = Category.Name, ID = Category.CategoryID });
                    var Parent = Category;
                    while (Parent != null)
                    {
                        if (Parent == CurrentCategory) break;
                        Parent = Parent.Category1;
                    }
                    if (Parent != null) continue;

                    model.MoveChildrenToOptions.Add(new AdminNamedID() { Name = Category.Name, ID = Category.CategoryID });
                }

                return View(model);
            }
        }