Files
Orchard/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs
Sebastien Ros 2680f394ba #17261 Changing BlogSlugConstraint to BlogPathContraint to handle homepage blogs
Work Item: 17261

--HG--
branch : dev
extra : transplant_source : Y%BC%1F%0E%A2%C5%10%BE%3B%82P%21%FC%FA%ED%FE%A7%26%9B%92
2011-01-31 13:25:16 -08:00

187 lines
8.0 KiB
C#

using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Orchard.Blogs.Extensions;
using Orchard.Blogs.Models;
using Orchard.Blogs.Routing;
using Orchard.Blogs.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Routable.Services;
using Orchard.Data;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.UI.Admin;
using Orchard.UI.Navigation;
using Orchard.UI.Notify;
using Orchard.Settings;
namespace Orchard.Blogs.Controllers {
[ValidateInput(false), Admin]
public class BlogAdminController : Controller, IUpdateModel {
private readonly IBlogService _blogService;
private readonly IBlogPostService _blogPostService;
private readonly IContentManager _contentManager;
private readonly ITransactionManager _transactionManager;
private readonly IBlogPathConstraint _blogPathConstraint;
private readonly ISiteService _siteService;
public BlogAdminController(
IOrchardServices services,
IBlogService blogService,
IBlogPostService blogPostService,
IContentManager contentManager,
ITransactionManager transactionManager,
IBlogPathConstraint blogPathConstraint,
ISiteService siteService,
IShapeFactory shapeFactory) {
Services = services;
_blogService = blogService;
_blogPostService = blogPostService;
_contentManager = contentManager;
_transactionManager = transactionManager;
_blogPathConstraint = blogPathConstraint;
_siteService = siteService;
T = NullLocalizer.Instance;
Shape = shapeFactory;
}
dynamic Shape { get; set; }
public Localizer T { get; set; }
public IOrchardServices Services { get; set; }
public ActionResult Create() {
if (!Services.Authorizer.Authorize(Permissions.ManageBlogs, T("Not allowed to create blogs")))
return new HttpUnauthorizedResult();
BlogPart blog = Services.ContentManager.New<BlogPart>("Blog");
if (blog == null)
return HttpNotFound();
dynamic model = Services.ContentManager.BuildEditor(blog);
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)model);
}
[HttpPost, ActionName("Create")]
public ActionResult CreatePOST() {
if (!Services.Authorizer.Authorize(Permissions.ManageBlogs, T("Couldn't create blog")))
return new HttpUnauthorizedResult();
var blog = Services.ContentManager.New<BlogPart>("Blog");
_contentManager.Create(blog, VersionOptions.Draft);
dynamic model = _contentManager.UpdateEditor(blog, this);
if (!ModelState.IsValid) {
_transactionManager.Cancel();
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)model);
}
_contentManager.Publish(blog.ContentItem);
_blogPathConstraint.AddPath(blog.As<IRoutableAspect>().Path);
return Redirect(Url.BlogForAdmin(blog));
}
public ActionResult Edit(int blogId) {
if (!Services.Authorizer.Authorize(Permissions.ManageBlogs, T("Not allowed to edit blog")))
return new HttpUnauthorizedResult();
var blog = _blogService.Get(blogId, VersionOptions.Latest);
if (blog == null)
return HttpNotFound();
dynamic model = Services.ContentManager.BuildEditor(blog);
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)model);
}
[HttpPost, ActionName("Edit")]
public ActionResult EditPOST(int blogId) {
if (!Services.Authorizer.Authorize(Permissions.ManageBlogs, T("Couldn't edit blog")))
return new HttpUnauthorizedResult();
var blog = _blogService.Get(blogId, VersionOptions.DraftRequired);
if (blog == null)
return HttpNotFound();
dynamic model = Services.ContentManager.UpdateEditor(blog, this);
if (!ModelState.IsValid) {
Services.TransactionManager.Cancel();
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)model);
}
_contentManager.Publish(blog);
_blogPathConstraint.AddPath(blog.As<IRoutableAspect>().Path);
Services.Notifier.Information(T("Blog information updated"));
return Redirect(Url.BlogsForAdmin());
}
[HttpPost]
public ActionResult Remove(int blogId) {
if (!Services.Authorizer.Authorize(Permissions.ManageBlogs, T("Couldn't delete blog")))
return new HttpUnauthorizedResult();
var blog = _blogService.Get(blogId, VersionOptions.Latest);
if (blog == null)
return HttpNotFound();
_blogService.Delete(blog);
Services.Notifier.Information(T("Blog was successfully deleted"));
return Redirect(Url.BlogsForAdmin());
}
public ActionResult List() {
var list = Services.New.List();
list.AddRange(_blogService.Get(VersionOptions.Latest)
.Select(b => {
var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin");
blog.TotalPostCount = _blogPostService.Get(b, VersionOptions.Latest).Count();
return blog;
}));
dynamic viewModel = Services.New.ViewModel()
.ContentItems(list);
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)viewModel);
}
public ActionResult Item(int blogId, PagerParameters pagerParameters) {
Pager pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
BlogPart blogPart = _blogService.Get(blogId, VersionOptions.Latest).As<BlogPart>();
if (blogPart == null)
return HttpNotFound();
var blogPosts = _blogPostService.Get(blogPart, pager.GetStartIndex(), pager.PageSize, VersionOptions.Latest)
.Select(bp => _contentManager.BuildDisplay(bp, "SummaryAdmin"));
dynamic blog = Services.ContentManager.BuildDisplay(blogPart, "DetailAdmin");
var list = Shape.List();
list.AddRange(blogPosts);
blog.Content.Add(Shape.Parts_Blogs_BlogPost_ListAdmin(ContentItems: list), "5");
var totalItemCount = _blogPostService.PostCount(blogPart, VersionOptions.Latest);
blog.Content.Add(Shape.Pager(pager).TotalItemCount(totalItemCount), "Content:after");
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)blog);
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
}
}