mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-22 20:13:50 +08:00
Fix Blog Post publishing to follow same logic as Pages
Scheduling, publishing, etc. --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045947
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Blogs.Extensions;
|
||||
using Orchard.Blogs.Models;
|
||||
@@ -26,13 +25,10 @@ namespace Orchard.Blogs.Controllers {
|
||||
private Localizer T { get; set; }
|
||||
|
||||
public ActionResult Create(string blogSlug) {
|
||||
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Not allowed to create blog post")))
|
||||
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();
|
||||
|
||||
@@ -51,47 +47,36 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't create blog post")))
|
||||
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();
|
||||
|
||||
//TODO: (erikpo) Move this duplicate code somewhere else
|
||||
DateTime? publishDate = null;
|
||||
bool publishNow = false;
|
||||
if (string.Equals(Request.Form["Command"], "PublishNow")) {
|
||||
publishNow = true;
|
||||
}
|
||||
else if (string.Equals(Request.Form["Command"], "PublishLater")) {
|
||||
DateTime publishDateValue;
|
||||
if (DateTime.TryParse(Request.Form["Published"], out publishDateValue)) {
|
||||
publishDate = publishDateValue;
|
||||
}
|
||||
}
|
||||
|
||||
model.BlogPost = _services.ContentManager.UpdateEditorModel(_services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name), this);
|
||||
model.BlogPost.Item.Blog = blog;
|
||||
if (!publishNow && publishDate != null)
|
||||
model.BlogPost.Item.Published = publishDate.Value;
|
||||
// Validate form input
|
||||
var blogPost = _services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
||||
blogPost.Blog = blog;
|
||||
model.BlogPost = _services.ContentManager.UpdateEditorModel(blogPost, this);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
_services.TransactionManager.Cancel();
|
||||
return View(model);
|
||||
}
|
||||
|
||||
//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);
|
||||
|
||||
if (publishNow)
|
||||
_services.Notifier.Information(T("Blog post has been published"));
|
||||
else if (publishDate != null)
|
||||
_services.Notifier.Information(T("Blog post has been scheduled for publishing"));
|
||||
else
|
||||
_services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
// Execute publish command
|
||||
switch (Request.Form["Command"]) {
|
||||
case "PublishNow":
|
||||
_blogPostService.Publish(model.BlogPost.Item);
|
||||
_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"));
|
||||
break;
|
||||
default:
|
||||
_services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
break;
|
||||
}
|
||||
|
||||
return Redirect(Url.BlogPostEdit(blogSlug, model.BlogPost.Item.Id));
|
||||
}
|
||||
@@ -100,14 +85,11 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
||||
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();
|
||||
|
||||
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
||||
|
||||
if (post == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
@@ -123,15 +105,12 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (!_services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
|
||||
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();
|
||||
|
||||
// Get draft (create a new version if needed)
|
||||
BlogPost post = _blogPostService.Get(postId, VersionOptions.DraftRequired);
|
||||
|
||||
if (post == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
@@ -144,37 +123,24 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
_services.TransactionManager.Cancel();
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
//TODO: (erikpo) Move this duplicate code somewhere else
|
||||
DateTime? publishDate = null;
|
||||
bool publishNow = false;
|
||||
if (string.Equals(Request.Form["Command"], "PublishNow")) {
|
||||
publishNow = true;
|
||||
// Execute publish command
|
||||
switch (Request.Form["Command"]) {
|
||||
case "PublishNow":
|
||||
_blogPostService.Publish(model.BlogPost.Item);
|
||||
_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"));
|
||||
break;
|
||||
default:
|
||||
_blogPostService.Unpublish(model.BlogPost.Item);
|
||||
_services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
break;
|
||||
}
|
||||
else if (string.Equals(Request.Form["Command"], "PublishLater")) {
|
||||
DateTime publishDateValue;
|
||||
if (DateTime.TryParse(Request.Form["Published"], out publishDateValue)) {
|
||||
publishDate = publishDateValue;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: (erikpo) Move this duplicate code somewhere else
|
||||
if (publishNow)
|
||||
_blogPostService.Publish(post);
|
||||
else if (publishDate != null)
|
||||
_blogPostService.Publish(post, publishDate.Value);
|
||||
else
|
||||
_blogPostService.Unpublish(post);
|
||||
|
||||
if (publishNow)
|
||||
_services.Notifier.Information(T("Blog post has been published"));
|
||||
else if (publishDate != null)
|
||||
_services.Notifier.Information(T("Blog post has been scheduled for publishing"));
|
||||
else
|
||||
_services.Notifier.Information(T("Blog post draft has been saved"));
|
||||
|
||||
return Redirect(Url.BlogPostEdit(blogSlug, model.BlogPost.Item.Id));
|
||||
}
|
||||
@@ -185,19 +151,15 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (!_services.Authorizer.Authorize(Permissions.DeleteBlogPost, T("Couldn't delete blog post")))
|
||||
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();
|
||||
|
||||
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
||||
|
||||
if (post == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
_blogPostService.Delete(post);
|
||||
|
||||
_services.Notifier.Information(T("Blog post was successfully deleted"));
|
||||
|
||||
return Redirect(Url.BlogForAdmin(blogSlug));
|
||||
@@ -208,19 +170,15 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (!_services.Authorizer.Authorize(Permissions.PublishBlogPost, T("Couldn't publish blog post")))
|
||||
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();
|
||||
|
||||
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
|
||||
|
||||
if (post == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
_blogPostService.Publish(post);
|
||||
|
||||
_services.Notifier.Information(T("Blog post information updated."));
|
||||
|
||||
return Redirect(Url.BlogForAdmin(blog.Slug));
|
||||
|
@@ -9,7 +9,7 @@ namespace Orchard.Blogs.Extensions {
|
||||
}
|
||||
|
||||
public static string PublishedState(this HtmlHelper htmlHelper, BlogPost blogPost) {
|
||||
return htmlHelper.DateTime(blogPost.Published, "Draft");
|
||||
return htmlHelper.DateTime(blogPost.PublishedUtc, "Draft");
|
||||
}
|
||||
|
||||
public static string PublishedWhen(this HtmlHelper<BlogPost> htmlHelper) {
|
||||
@@ -17,7 +17,7 @@ namespace Orchard.Blogs.Extensions {
|
||||
}
|
||||
|
||||
public static string PublishedWhen(this HtmlHelper htmlHelper, BlogPost blogPost) {
|
||||
return htmlHelper.DateTimeRelative(blogPost.Published, "as a Draft");
|
||||
return htmlHelper.DateTimeRelative(blogPost.PublishedUtc, "as a Draft");
|
||||
}
|
||||
}
|
||||
}
|
@@ -29,10 +29,10 @@ namespace Orchard.Blogs.Models {
|
||||
set { this.As<ICommonAspect>().Owner = value; }
|
||||
}
|
||||
|
||||
public DateTime? Published
|
||||
{
|
||||
get { return this.As<CommonAspect>().PublishedUtc; }
|
||||
set { this.As<CommonAspect>().PublishedUtc = value; }
|
||||
public DateTime? PublishedUtc {
|
||||
get { return this.As<ICommonAspect>().VersionPublishedUtc; }
|
||||
}
|
||||
|
||||
public DateTime? ScheduledPublishUtc { get; set;}
|
||||
}
|
||||
}
|
@@ -28,6 +28,8 @@ namespace Orchard.Blogs.Models {
|
||||
Filters.Add(new ActivatingFilter<RoutableAspect>(BlogPostDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<BodyAspect>(BlogPostDriver.ContentType.Name));
|
||||
|
||||
OnLoaded<BlogPost>((context, p) => p.ScheduledPublishUtc = _blogPostService.GetScheduledPublishUtc(p));
|
||||
|
||||
Action<Blog> updateBlogPostCount =
|
||||
(blog => {
|
||||
// Ensure we get the "right" set of published posts for the blog
|
||||
|
@@ -6,15 +6,18 @@ using Orchard.Blogs.Models;
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Data;
|
||||
using Orchard.Tasks.Scheduling;
|
||||
|
||||
namespace Orchard.Blogs.Services {
|
||||
public class BlogPostService : IBlogPostService {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IRepository<BlogArchiveRecord> _blogArchiveRepository;
|
||||
private readonly IPublishingTaskManager _publishingTaskManager;
|
||||
|
||||
public BlogPostService(IContentManager contentManager, IRepository<BlogArchiveRecord> blogArchiveRepository) {
|
||||
public BlogPostService(IContentManager contentManager, IRepository<BlogArchiveRecord> blogArchiveRepository, IPublishingTaskManager publishingTaskManager) {
|
||||
_contentManager = contentManager;
|
||||
_blogArchiveRepository = blogArchiveRepository;
|
||||
_publishingTaskManager = publishingTaskManager;
|
||||
}
|
||||
|
||||
public BlogPost Get(Blog blog, string slug) {
|
||||
@@ -82,25 +85,28 @@ namespace Orchard.Blogs.Services {
|
||||
}
|
||||
|
||||
public void Delete(BlogPost blogPost) {
|
||||
_publishingTaskManager.DeleteTasks(blogPost.ContentItem);
|
||||
_contentManager.Remove(blogPost.ContentItem);
|
||||
}
|
||||
|
||||
public void Publish(BlogPost blogPost) {
|
||||
_publishingTaskManager.DeleteTasks(blogPost.ContentItem);
|
||||
_contentManager.Publish(blogPost.ContentItem);
|
||||
}
|
||||
|
||||
public void Publish(BlogPost blogPost, DateTime publishDate) {
|
||||
//TODO: Implement task scheduling
|
||||
//TODO: (erikpo) This logic should move out of blogs and pages and into content manager
|
||||
//if (blogPost.Published != null && blogPost.Published.Value >= DateTime.UtcNow)
|
||||
// _contentManager.Unpublish(blogPost.ContentItem);
|
||||
//blogPost.Published = publishDate;
|
||||
public void Publish(BlogPost blogPost, DateTime scheduledPublishUtc) {
|
||||
_publishingTaskManager.Publish(blogPost.ContentItem, scheduledPublishUtc);
|
||||
}
|
||||
|
||||
public void Unpublish(BlogPost blogPost) {
|
||||
_contentManager.Unpublish(blogPost.ContentItem);
|
||||
}
|
||||
|
||||
public DateTime? GetScheduledPublishUtc(BlogPost blogPost) {
|
||||
var task = _publishingTaskManager.GetPublishTask(blogPost.ContentItem);
|
||||
return (task == null ? null : task.ScheduledUtc);
|
||||
}
|
||||
|
||||
private IContentQuery<ContentItem, CommonRecord> GetBlogQuery(ContentPart<BlogRecord> blog, VersionOptions versionOptions) {
|
||||
return
|
||||
_contentManager.Query(versionOptions, BlogPostDriver.ContentType.Name).Join<CommonRecord>().Where(
|
||||
|
@@ -15,7 +15,8 @@ namespace Orchard.Blogs.Services {
|
||||
IEnumerable<KeyValuePair<ArchiveData, int>> GetArchives(Blog blog);
|
||||
void Delete(BlogPost blogPost);
|
||||
void Publish(BlogPost blogPost);
|
||||
void Publish(BlogPost blogPost, DateTime publishDate);
|
||||
void Publish(BlogPost blogPost, DateTime scheduledPublishUtc);
|
||||
void Unpublish(BlogPost blogPost);
|
||||
DateTime? GetScheduledPublishUtc(BlogPost blogPost);
|
||||
}
|
||||
}
|
@@ -10,8 +10,8 @@
|
||||
<label class="forcheckbox" for="Command_PublishNow"><%=_Encoded("Publish Now")%></label>
|
||||
</div>
|
||||
<div>
|
||||
<%=Html.RadioButton("Command", "PublishLater", Model.Published != null && Model.Published.Value > DateTime.UtcNow, new { id = "Command_PublishLater" }) %>
|
||||
<%=Html.RadioButton("Command", "PublishLater", Model.ScheduledPublishUtc != null, new { id = "Command_PublishLater" })%>
|
||||
<label class="forcheckbox" for="Command_PublishLater"><%=_Encoded("Publish Later")%></label>
|
||||
<%=Html.EditorFor(m => m.Published) %>
|
||||
<%=Html.EditorFor(m => m.ScheduledPublishUtc)%>
|
||||
</div>
|
||||
</fieldset>
|
@@ -195,8 +195,8 @@ namespace Orchard.Pages.Controllers {
|
||||
Services.Notifier.Information(T("Page has been scheduled for publishing"));
|
||||
break;
|
||||
default:
|
||||
_pageService.Unpublish(model.Page.Item);
|
||||
Services.Notifier.Information(T("Page draft has been saved"));
|
||||
_pageService.Unpublish(page);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -213,7 +213,6 @@ namespace Orchard.Pages.Controllers {
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
_pageService.Delete(page);
|
||||
|
||||
Services.Notifier.Information(T("Page was successfully deleted"));
|
||||
|
||||
return RedirectToAction("List");
|
||||
|
@@ -5,5 +5,5 @@
|
||||
<%@ Import Namespace="Orchard.Blogs.Extensions"%>
|
||||
<%@ Import Namespace="Orchard.Blogs.Models"%>
|
||||
<h3><%=Html.Link(Html.Encode(Model.Item.Title), Url.BlogPost(Model.Item.Blog.Slug, Model.Item.Slug)) %></h3>
|
||||
<div class="meta"><%=Html.PublishedState(Model.Item) %> | <%=Html.Link(_Encoded("?? comments").ToString(), "") %></div>
|
||||
<div class="meta"><%=Html.PublishedState(Model.Item) %> | <%Html.Zone("meta");%></div>
|
||||
<div class="postsummary"><%=Model.Item.As<BodyAspect>().Text ?? string.Format("<p><em>{0}</em></p>", _Encoded("there's no content for this blog post"))%></div>
|
@@ -7,7 +7,6 @@
|
||||
|
||||
<h3><%=Html.Link(Html.Encode(Model.Item.Title), Url.BlogPost(Model.Item.Blog.Slug, Model.Item.Slug)) %></h3>
|
||||
|
||||
<div class="meta"><%=Html.PublishedState(Model.Item) %> | <%=Html.Link(_Encoded("?? comments").ToString(), "") %>
|
||||
</div>
|
||||
<div class="meta"><%=Html.PublishedState(Model.Item) %> | <%Html.Zone("meta");%></div>
|
||||
|
||||
<div class="postsummary"><%=Model.Item.As<BodyAspect>().Text ?? string.Format("<p><em>{0}</em></p>", _Encoded("there's no content for this blog post"))%></div>
|
||||
|
Reference in New Issue
Block a user