2009-12-08 07:37:47 +08:00
|
|
|
using System.Linq;
|
2009-11-21 07:31:49 +08:00
|
|
|
using System.Web.Mvc;
|
2009-11-24 01:34:46 +08:00
|
|
|
using Orchard.Blogs.Extensions;
|
2009-11-21 07:31:49 +08:00
|
|
|
using Orchard.Blogs.Models;
|
|
|
|
using Orchard.Blogs.Services;
|
2009-11-24 00:26:24 +08:00
|
|
|
using Orchard.Blogs.ViewModels;
|
2009-11-24 03:24:53 +08:00
|
|
|
using Orchard.Data;
|
2009-11-24 01:34:46 +08:00
|
|
|
using Orchard.Localization;
|
|
|
|
using Orchard.Models;
|
|
|
|
using Orchard.Models.Driver;
|
2009-11-21 07:31:49 +08:00
|
|
|
using Orchard.Mvc.Results;
|
2009-12-04 06:53:49 +08:00
|
|
|
using Orchard.Security;
|
2009-11-24 01:34:46 +08:00
|
|
|
using Orchard.UI.Notify;
|
2009-11-21 07:31:49 +08:00
|
|
|
|
|
|
|
namespace Orchard.Blogs.Controllers {
|
2009-11-25 13:45:50 +08:00
|
|
|
[ValidateInput(false)]
|
2009-11-24 01:34:46 +08:00
|
|
|
public class BlogController : Controller, IUpdateModel {
|
2009-12-02 04:24:18 +08:00
|
|
|
private readonly ISessionLocator _sessionLocator;
|
2009-11-24 01:34:46 +08:00
|
|
|
private readonly IContentManager _contentManager;
|
2009-12-04 06:53:49 +08:00
|
|
|
private readonly IAuthorizer _authorizer;
|
2009-11-24 01:34:46 +08:00
|
|
|
private readonly INotifier _notifier;
|
2009-11-21 07:31:49 +08:00
|
|
|
private readonly IBlogService _blogService;
|
|
|
|
|
2009-12-20 16:57:20 +08:00
|
|
|
public BlogController(
|
|
|
|
ISessionLocator sessionLocator, IContentManager contentManager,
|
|
|
|
IAuthorizer authorizer, INotifier notifier,
|
|
|
|
IBlogService blogService) {
|
2009-12-02 04:24:18 +08:00
|
|
|
_sessionLocator = sessionLocator;
|
2009-11-24 01:34:46 +08:00
|
|
|
_contentManager = contentManager;
|
2009-12-04 06:53:49 +08:00
|
|
|
_authorizer = authorizer;
|
2009-11-24 01:34:46 +08:00
|
|
|
_notifier = notifier;
|
2009-11-21 07:31:49 +08:00
|
|
|
_blogService = blogService;
|
2009-11-24 01:34:46 +08:00
|
|
|
T = NullLocalizer.Instance;
|
2009-11-21 07:31:49 +08:00
|
|
|
}
|
|
|
|
|
2009-12-10 07:07:00 +08:00
|
|
|
private Localizer T { get; set; }
|
2009-11-24 01:34:46 +08:00
|
|
|
|
2009-12-03 09:37:45 +08:00
|
|
|
public ActionResult List() {
|
2009-12-08 17:21:13 +08:00
|
|
|
var model = new BlogsViewModel {
|
2009-12-21 09:30:24 +08:00
|
|
|
Blogs = _blogService.Get().Select(b => _contentManager.BuildDisplayModel(b, "Summary"))
|
2009-12-08 17:21:13 +08:00
|
|
|
};
|
2009-12-08 14:31:44 +08:00
|
|
|
|
2009-12-08 17:21:13 +08:00
|
|
|
return View(model);
|
2009-11-21 07:31:49 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 01:34:46 +08:00
|
|
|
//TODO: (erikpo) Should move the slug parameter and get call and null check up into a model binder
|
2009-11-21 08:38:41 +08:00
|
|
|
public ActionResult Item(string blogSlug) {
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
2009-11-21 07:31:49 +08:00
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2009-12-08 17:21:13 +08:00
|
|
|
var model = new BlogViewModel {
|
2009-12-21 09:30:24 +08:00
|
|
|
Blog = _contentManager.BuildDisplayModel(blog, "Detail")
|
2009-12-08 17:21:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return View(model);
|
2009-11-21 07:31:49 +08:00
|
|
|
}
|
2009-12-03 12:30:34 +08:00
|
|
|
|
2009-11-24 01:34:46 +08:00
|
|
|
public ActionResult Create() {
|
2009-12-09 16:23:06 +08:00
|
|
|
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
|
|
|
if (!_authorizer.Authorize(Permissions.CreateBlog, T("Not allowed to create blogs")))
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
Blog blog = _contentManager.New<Blog>("blog");
|
2009-12-09 23:39:36 +08:00
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
var model = new CreateBlogViewModel {
|
2009-12-21 09:30:24 +08:00
|
|
|
Blog = _contentManager.BuildEditorModel(blog)
|
2009-12-09 16:23:06 +08:00
|
|
|
};
|
|
|
|
|
2009-12-09 23:39:36 +08:00
|
|
|
return View(model);
|
2009-11-24 01:34:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public ActionResult Create(CreateBlogViewModel model) {
|
2009-12-09 16:23:06 +08:00
|
|
|
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
2009-12-04 06:53:49 +08:00
|
|
|
if (!_authorizer.Authorize(Permissions.CreateBlog, T("Couldn't create blog")))
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-21 09:30:24 +08:00
|
|
|
model.Blog = _contentManager.UpdateEditorModel(_contentManager.New<Blog>("blog"), this);
|
2009-12-09 16:23:06 +08:00
|
|
|
|
2009-11-24 01:34:46 +08:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
return View(model);
|
2009-12-09 16:23:06 +08:00
|
|
|
|
|
|
|
_contentManager.Create(model.Blog.Item.ContentItem);
|
2009-12-02 14:16:22 +08:00
|
|
|
|
2009-11-24 03:24:53 +08:00
|
|
|
//TEMP: (erikpo) ensure information has committed for this record
|
|
|
|
var session = _sessionLocator.For(typeof(BlogRecord));
|
|
|
|
session.Flush();
|
2009-11-24 01:34:46 +08:00
|
|
|
|
2009-12-09 16:23:06 +08:00
|
|
|
return Redirect(Url.BlogForAdmin(model.Blog.Item.Slug));
|
2009-11-24 01:34:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public ActionResult Edit(string blogSlug) {
|
2009-12-09 23:39:36 +08:00
|
|
|
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
|
|
|
if (!_authorizer.Authorize(Permissions.ModifyBlog, T("Not allowed to edit blog")))
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-02 04:24:18 +08:00
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2009-12-09 23:39:36 +08:00
|
|
|
var model = new BlogEditViewModel {
|
2009-12-21 09:30:24 +08:00
|
|
|
Blog = _contentManager.BuildEditorModel(blog)
|
2009-12-09 23:39:36 +08:00
|
|
|
};
|
|
|
|
|
2009-11-24 01:34:46 +08:00
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public ActionResult Edit(string blogSlug, FormCollection input) {
|
2009-12-04 06:53:49 +08:00
|
|
|
if (!_authorizer.Authorize(Permissions.ModifyBlog, T("Couldn't edit blog")))
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-02 04:24:18 +08:00
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2009-12-09 23:39:36 +08:00
|
|
|
var model = new BlogEditViewModel {
|
2009-12-21 09:30:24 +08:00
|
|
|
Blog = _contentManager.UpdateEditorModel(blog, this)
|
2009-12-09 23:39:36 +08:00
|
|
|
};
|
2009-11-24 01:34:46 +08:00
|
|
|
|
2009-12-09 23:39:36 +08:00
|
|
|
if (!ModelState.IsValid)
|
2009-11-24 01:34:46 +08:00
|
|
|
return View(model);
|
|
|
|
|
|
|
|
_notifier.Information(T("Blog information updated"));
|
|
|
|
|
2009-12-03 09:37:45 +08:00
|
|
|
return Redirect(Url.BlogsForAdmin());
|
2009-11-24 01:34:46 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 04:11:54 +08:00
|
|
|
//[HttpPost] <- todo: (heskew) make all add/edit/remove POST only and verify the AntiForgeryToken
|
2009-12-04 03:50:57 +08:00
|
|
|
public ActionResult Delete(string blogSlug) {
|
2009-12-04 06:53:49 +08:00
|
|
|
if (!_authorizer.Authorize(Permissions.DeleteBlog, T("Couldn't delete blog")))
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-04 03:50:57 +08:00
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
_blogService.Delete(blog);
|
|
|
|
|
|
|
|
_notifier.Information(T("Blog was successfully deleted"));
|
|
|
|
|
2009-12-04 04:11:54 +08:00
|
|
|
return Redirect(Url.BlogsForAdmin());
|
2009-12-04 03:50:57 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 01:34:46 +08:00
|
|
|
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
|
|
|
|
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
|
|
|
}
|
2009-12-08 14:13:08 +08:00
|
|
|
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
|
|
|
|
ModelState.AddModelError(key, errorMessage.ToString());
|
|
|
|
}
|
|
|
|
|
2009-11-21 07:31:49 +08:00
|
|
|
}
|
|
|
|
}
|