2010-01-12 01:04:11 +00:00
|
|
|
using System;
|
2010-01-11 22:07:03 +00:00
|
|
|
using System.Web.Mvc;
|
|
|
|
using Orchard.Blogs.Extensions;
|
|
|
|
using Orchard.Blogs.Models;
|
|
|
|
using Orchard.Blogs.Services;
|
|
|
|
using Orchard.Blogs.ViewModels;
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
using Orchard.ContentManagement.Records;
|
2010-01-13 23:01:52 +00:00
|
|
|
using Orchard.Core.Common.Models;
|
2010-01-11 22:07:03 +00:00
|
|
|
using Orchard.Data;
|
|
|
|
using Orchard.Localization;
|
|
|
|
using Orchard.Mvc.Results;
|
|
|
|
using Orchard.UI.Notify;
|
|
|
|
|
|
|
|
namespace Orchard.Blogs.Controllers {
|
2010-01-12 01:04:11 +00:00
|
|
|
[ValidateInput(false)]
|
2010-01-11 22:07:03 +00:00
|
|
|
public class BlogPostAdminController : Controller, IUpdateModel {
|
|
|
|
private readonly IOrchardServices _services;
|
|
|
|
private readonly IBlogService _blogService;
|
|
|
|
private readonly IBlogPostService _blogPostService;
|
|
|
|
private readonly ISessionLocator _sessionLocator;
|
|
|
|
|
|
|
|
public BlogPostAdminController(IOrchardServices services, IBlogService blogService, IBlogPostService blogPostService, ISessionLocator sessionLocator) {
|
|
|
|
_services = services;
|
|
|
|
_blogService = blogService;
|
|
|
|
_blogPostService = blogPostService;
|
|
|
|
_sessionLocator = sessionLocator;
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Localizer T { get; set; }
|
|
|
|
|
|
|
|
public ActionResult Create(string blogSlug) {
|
|
|
|
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
2010-01-20 20:18:42 +00:00
|
|
|
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Not allowed to create blog post")))
|
2010-01-11 22:07:03 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
2010-01-20 09:26:33 +00:00
|
|
|
|
2010-01-11 22:07:03 +00:00
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
2010-01-20 09:26:33 +00:00
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
var blogPost = _services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
2010-01-20 09:26:33 +00:00
|
|
|
blogPost.Blog = blog;
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
var model = new CreateBlogPostViewModel {
|
2010-01-20 09:26:33 +00:00
|
|
|
BlogPost = _services.ContentManager.BuildEditorModel(blogPost)
|
|
|
|
};
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public ActionResult Create(string blogSlug, CreateBlogPostViewModel model) {
|
2010-01-20 20:18:42 +00:00
|
|
|
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't create blog post")))
|
2010-01-11 22:07:03 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2010-01-12 01:04:11 +00:00
|
|
|
//TODO: (erikpo) Move this duplicate code somewhere else
|
|
|
|
DateTime? publishDate = null;
|
2010-01-11 22:07:03 +00:00
|
|
|
bool publishNow = false;
|
|
|
|
if (string.Equals(Request.Form["Command"], "PublishNow")) {
|
|
|
|
publishNow = true;
|
2010-01-12 01:04:11 +00:00
|
|
|
} else if (string.Equals(Request.Form["Publish"], "Publish")) {
|
|
|
|
DateTime publishDateValue;
|
|
|
|
if (DateTime.TryParse(Request.Form["Publish"], out publishDateValue)) {
|
|
|
|
publishDate = publishDateValue;
|
|
|
|
}
|
2010-01-11 22:07:03 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
model.BlogPost = _services.ContentManager.UpdateEditorModel(_services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name), this);
|
2010-01-15 23:44:22 +00:00
|
|
|
model.BlogPost.Item.Blog = blog;
|
|
|
|
if (!publishNow && publishDate != null)
|
|
|
|
model.BlogPost.Item.Published = publishDate.Value;
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
_services.TransactionManager.Cancel();
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
//todo: (heskew) make it so we no longer need to set as draft on create then publish (all to get the publishing & published events fired)
|
|
|
|
_services.ContentManager.Create(model.BlogPost.Item.ContentItem, VersionOptions.Draft);
|
|
|
|
|
|
|
|
if (publishNow)
|
|
|
|
_services.ContentManager.Publish(model.BlogPost.Item.ContentItem);
|
2010-01-15 23:44:22 +00:00
|
|
|
|
2010-01-11 22:07:03 +00:00
|
|
|
//TEMP: (erikpo) ensure information has committed for this record
|
|
|
|
var session = _sessionLocator.For(typeof(ContentItemRecord));
|
|
|
|
session.Flush();
|
|
|
|
|
2010-01-13 23:01:52 +00:00
|
|
|
return Redirect(Url.BlogPost(blogSlug, model.BlogPost.Item.As<RoutableAspect>().Slug));
|
2010-01-11 22:07:03 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
public ActionResult Edit(string blogSlug, int postId) {
|
2010-01-20 20:18:42 +00:00
|
|
|
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
2010-01-11 22:07:03 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
if (post == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
var model = new BlogPostEditViewModel {
|
|
|
|
BlogPost = _services.ContentManager.BuildEditorModel(post)
|
|
|
|
};
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Edit")]
|
2010-01-21 20:17:40 +00:00
|
|
|
public ActionResult EditPOST(string blogSlug, int postId) {
|
2010-01-20 20:18:42 +00:00
|
|
|
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
2010-01-11 22:07:03 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2010-01-19 19:52:08 +00:00
|
|
|
bool isDraft = false;
|
|
|
|
|
2010-01-11 22:07:03 +00:00
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
if (post == null)
|
|
|
|
return new NotFoundResult();
|
2010-01-12 01:04:11 +00:00
|
|
|
|
|
|
|
//TODO: (erikpo) Move this duplicate code somewhere else
|
|
|
|
DateTime? publishDate = null;
|
2010-01-11 22:07:03 +00:00
|
|
|
bool publishNow = false;
|
|
|
|
if (string.Equals(Request.Form["Command"], "PublishNow")) {
|
|
|
|
publishNow = true;
|
2010-01-12 01:04:11 +00:00
|
|
|
} else if (string.Equals(Request.Form["Publish"], "Publish")) {
|
|
|
|
DateTime publishDateValue;
|
|
|
|
if (DateTime.TryParse(Request.Form["Publish"], out publishDateValue)) {
|
|
|
|
publishDate = publishDateValue;
|
|
|
|
}
|
2010-01-11 22:07:03 +00:00
|
|
|
}
|
|
|
|
|
2010-01-12 01:04:11 +00:00
|
|
|
//TODO: (erikpo) Move this duplicate code somewhere else
|
2010-01-11 22:07:03 +00:00
|
|
|
if (publishNow)
|
|
|
|
_blogPostService.Publish(post);
|
2010-01-12 01:04:11 +00:00
|
|
|
else if (publishDate != null)
|
|
|
|
_blogPostService.Publish(post, publishDate.Value);
|
2010-01-19 19:52:08 +00:00
|
|
|
else {
|
|
|
|
isDraft = true;
|
2010-01-11 22:07:03 +00:00
|
|
|
_blogPostService.Unpublish(post);
|
2010-01-19 19:52:08 +00:00
|
|
|
}
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
var model = new BlogPostEditViewModel {
|
|
|
|
BlogPost = _services.ContentManager.UpdateEditorModel(post, this)
|
|
|
|
};
|
|
|
|
|
|
|
|
TryUpdateModel(model);
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
_services.TransactionManager.Cancel();
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
_services.Notifier.Information(T("Blog post information updated."));
|
|
|
|
|
2010-01-19 19:52:08 +00:00
|
|
|
if (isDraft) {
|
2010-01-21 20:17:40 +00:00
|
|
|
return Redirect(Url.BlogPostEdit(blog.Slug, post.Id));
|
2010-01-19 19:52:08 +00:00
|
|
|
}
|
|
|
|
return Redirect(Url.BlogForAdmin(blog.Slug));
|
2010-01-11 22:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
2010-01-21 20:17:40 +00:00
|
|
|
public ActionResult Delete(string blogSlug, int postId) {
|
2010-01-20 20:18:42 +00:00
|
|
|
//refactoring: test PublishBlogPost/PublishOthersBlogPost in addition if published
|
|
|
|
if (!_services.Authorizer.Authorize(Permissions.DeleteBlogPost, T("Couldn't delete blog post")))
|
2010-01-11 22:07:03 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
2010-01-11 22:07:03 +00:00
|
|
|
|
|
|
|
if (post == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
_blogPostService.Delete(post);
|
|
|
|
|
|
|
|
_services.Notifier.Information(T("Blog post was successfully deleted"));
|
|
|
|
|
|
|
|
return Redirect(Url.BlogForAdmin(blogSlug));
|
|
|
|
}
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
[HttpPost]
|
|
|
|
public ActionResult Publish(string blogSlug, int postId) {
|
2010-01-20 20:50:09 +00:00
|
|
|
if (!_services.Authorizer.Authorize(Permissions.PublishBlogPost, T("Couldn't publish blog post")))
|
2010-01-20 19:50:42 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
|
|
|
Blog blog = _blogService.Get(blogSlug);
|
|
|
|
|
|
|
|
if (blog == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
2010-01-21 20:17:40 +00:00
|
|
|
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
2010-01-20 19:50:42 +00:00
|
|
|
|
|
|
|
if (post == null)
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
_blogPostService.Publish(post);
|
|
|
|
|
|
|
|
_services.Notifier.Information(T("Blog post information updated."));
|
|
|
|
|
|
|
|
return Redirect(Url.BlogForAdmin(blog.Slug));
|
|
|
|
}
|
|
|
|
|
2010-01-11 22:07:03 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|