mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-24 13:33:34 +08:00
Need to bring back the “Delete this draft” operation on the page/post editing page
* Added DiscardDraft to pages admin and blogpost admin. * Added an action link to edit pages when the "latest" is not "published" * Added ContentItemViewModel properties IsPublished, IsLatest, IsDraft** --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4046024
This commit is contained in:
@@ -11,32 +11,32 @@ using Orchard.UI.Notify;
|
||||
namespace Orchard.Blogs.Controllers {
|
||||
[ValidateInput(false)]
|
||||
public class BlogPostAdminController : Controller, IUpdateModel {
|
||||
private readonly IOrchardServices _services;
|
||||
private readonly IBlogService _blogService;
|
||||
private readonly IBlogPostService _blogPostService;
|
||||
|
||||
public BlogPostAdminController(IOrchardServices services, IBlogService blogService, IBlogPostService blogPostService) {
|
||||
_services = services;
|
||||
Services = services;
|
||||
_blogService = blogService;
|
||||
_blogPostService = blogPostService;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public IOrchardServices Services { get; set; }
|
||||
private Localizer T { get; set; }
|
||||
|
||||
public ActionResult Create(string blogSlug) {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Not allowed to create blog post")))
|
||||
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Not allowed to create blog post")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var blogPost = _services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
||||
var blogPost = Services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
||||
blogPost.Blog = blog;
|
||||
|
||||
var model = new CreateBlogPostViewModel {
|
||||
BlogPost = _services.ContentManager.BuildEditorModel(blogPost)
|
||||
BlogPost = Services.ContentManager.BuildEditorModel(blogPost)
|
||||
};
|
||||
|
||||
return View(model);
|
||||
@@ -44,7 +44,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Create(string blogSlug, CreateBlogPostViewModel model) {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't create blog post")))
|
||||
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't create blog post")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
@@ -52,29 +52,29 @@ namespace Orchard.Blogs.Controllers {
|
||||
return new NotFoundResult();
|
||||
|
||||
// Validate form input
|
||||
var blogPost = _services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
||||
var blogPost = Services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
||||
blogPost.Blog = blog;
|
||||
model.BlogPost = _services.ContentManager.UpdateEditorModel(blogPost, this);
|
||||
model.BlogPost = Services.ContentManager.UpdateEditorModel(blogPost, this);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
_services.TransactionManager.Cancel();
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(model);
|
||||
}
|
||||
|
||||
_services.ContentManager.Create(model.BlogPost.Item.ContentItem, VersionOptions.Draft);
|
||||
Services.ContentManager.Create(model.BlogPost.Item.ContentItem, VersionOptions.Draft);
|
||||
|
||||
// Execute publish command
|
||||
switch (Request.Form["Command"]) {
|
||||
case "PublishNow":
|
||||
_blogPostService.Publish(model.BlogPost.Item);
|
||||
_services.Notifier.Information(T("Blog post has been published"));
|
||||
Services.Notifier.Information(T("Blog post has been published"));
|
||||
break;
|
||||
case "PublishLater":
|
||||
_blogPostService.Publish(model.BlogPost.Item, model.BlogPost.Item.ScheduledPublishUtc.Value);
|
||||
_services.Notifier.Information(T("Blog post has been scheduled for publishing"));
|
||||
Services.Notifier.Information(T("Blog post has been scheduled for publishing"));
|
||||
break;
|
||||
default:
|
||||
_services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
Services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
}
|
||||
|
||||
public ActionResult Edit(string blogSlug, int postId) {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
||||
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
@@ -94,7 +94,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
return new NotFoundResult();
|
||||
|
||||
var model = new BlogPostEditViewModel {
|
||||
BlogPost = _services.ContentManager.BuildEditorModel(post)
|
||||
BlogPost = Services.ContentManager.BuildEditorModel(post)
|
||||
};
|
||||
|
||||
return View(model);
|
||||
@@ -102,7 +102,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
[HttpPost, ActionName("Edit")]
|
||||
public ActionResult EditPOST(string blogSlug, int postId) {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
||||
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
@@ -116,13 +116,13 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
// Validate form input
|
||||
var model = new BlogPostEditViewModel {
|
||||
BlogPost = _services.ContentManager.UpdateEditorModel(post, this)
|
||||
BlogPost = Services.ContentManager.UpdateEditorModel(post, this)
|
||||
};
|
||||
|
||||
TryUpdateModel(model);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
_services.TransactionManager.Cancel();
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(model);
|
||||
}
|
||||
|
||||
@@ -130,25 +130,63 @@ namespace Orchard.Blogs.Controllers {
|
||||
switch (Request.Form["Command"]) {
|
||||
case "PublishNow":
|
||||
_blogPostService.Publish(model.BlogPost.Item);
|
||||
_services.Notifier.Information(T("Blog post has been published"));
|
||||
Services.Notifier.Information(T("Blog post has been published"));
|
||||
break;
|
||||
case "PublishLater":
|
||||
_blogPostService.Publish(model.BlogPost.Item, model.BlogPost.Item.ScheduledPublishUtc.Value);
|
||||
_services.Notifier.Information(T("Blog post has been scheduled for publishing"));
|
||||
Services.Notifier.Information(T("Blog post has been scheduled for publishing"));
|
||||
break;
|
||||
default:
|
||||
//_blogPostService.Unpublish(model.BlogPost.Item);
|
||||
_services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
Services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
break;
|
||||
}
|
||||
|
||||
return Redirect(Url.BlogPostEdit(blogSlug, model.BlogPost.Item.Id));
|
||||
}
|
||||
|
||||
public ActionResult DiscardDraft(int id) {
|
||||
// get the current draft version
|
||||
var draft = Services.ContentManager.Get(id, VersionOptions.Draft);
|
||||
if (draft == null) {
|
||||
Services.Notifier.Information(T("There is no draft to discard."));
|
||||
return RedirectToEdit(id);
|
||||
}
|
||||
|
||||
// check edit permission
|
||||
if (!Services.Authorizer.Authorize(Permissions.EditOthersBlogPost, draft, T("Couldn't discard blog post draft")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
// locate the published revision to revert onto
|
||||
var published = Services.ContentManager.Get(id, VersionOptions.Published);
|
||||
if (published == null) {
|
||||
Services.Notifier.Information(T("Can not discard draft on unpublished blog post."));
|
||||
return RedirectToEdit(draft);
|
||||
}
|
||||
|
||||
// marking the previously published version as the latest
|
||||
// has the effect of discarding the draft but keeping the history
|
||||
draft.VersionRecord.Latest = false;
|
||||
published.VersionRecord.Latest = true;
|
||||
|
||||
Services.Notifier.Information(T("Blog post draft version discarded"));
|
||||
return RedirectToEdit(published);
|
||||
}
|
||||
|
||||
ActionResult RedirectToEdit(int id) {
|
||||
return RedirectToEdit(Services.ContentManager.GetLatest<BlogPost>(id));
|
||||
}
|
||||
|
||||
ActionResult RedirectToEdit(IContent item) {
|
||||
if (item == null || item.As<BlogPost>() == null)
|
||||
return new NotFoundResult();
|
||||
return RedirectToAction("Edit", new { BlogSlug = item.As<BlogPost>().Blog.Slug, PostId = item.ContentItem.Id });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Delete(string blogSlug, int postId) {
|
||||
//refactoring: test PublishBlogPost/PublishOthersBlogPost in addition if published
|
||||
if (!_services.Authorizer.Authorize(Permissions.DeleteBlogPost, T("Couldn't delete blog post")))
|
||||
if (!Services.Authorizer.Authorize(Permissions.DeleteBlogPost, T("Couldn't delete blog post")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
@@ -160,14 +198,14 @@ namespace Orchard.Blogs.Controllers {
|
||||
return new NotFoundResult();
|
||||
|
||||
_blogPostService.Delete(post);
|
||||
_services.Notifier.Information(T("Blog post was successfully deleted"));
|
||||
Services.Notifier.Information(T("Blog post was successfully deleted"));
|
||||
|
||||
return Redirect(Url.BlogForAdmin(blogSlug));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Publish(string blogSlug, int postId) {
|
||||
if (!_services.Authorizer.Authorize(Permissions.PublishBlogPost, T("Couldn't publish blog post")))
|
||||
if (!Services.Authorizer.Authorize(Permissions.PublishBlogPost, T("Couldn't publish blog post")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
@@ -179,7 +217,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
return new NotFoundResult();
|
||||
|
||||
_blogPostService.Publish(post);
|
||||
_services.Notifier.Information(T("Blog post information updated."));
|
||||
Services.Notifier.Information(T("Blog post information updated."));
|
||||
|
||||
return Redirect(Url.BlogForAdmin(blog.Slug));
|
||||
}
|
||||
|
Reference in New Issue
Block a user