Hooked up publishing for blog posts (still no date driven publishing [just draft or publish now]).

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045239
This commit is contained in:
ErikPorter
2010-01-11 18:27:42 +00:00
parent 4272b2609d
commit 21a2e4ca42
9 changed files with 93 additions and 33 deletions
@@ -56,7 +56,7 @@ namespace Orchard.Blogs.Controllers {
IEnumerable<ContentItemViewModel<BlogPost>> blogPosts = null; IEnumerable<ContentItemViewModel<BlogPost>> blogPosts = null;
if (displayType.StartsWith("DetailAdmin")) { if (displayType.StartsWith("DetailAdmin")) {
blogPosts = _blogPostService.Get(blog) blogPosts = _blogPostService.Get(blog, VersionOptions.Latest)
.Select(bp => _contentManager.BuildDisplayModel(bp, "SummaryAdmin")); .Select(bp => _contentManager.BuildDisplayModel(bp, "SummaryAdmin"));
} }
else if (displayType.StartsWith("Detail")) { else if (displayType.StartsWith("Detail")) {
@@ -9,7 +9,6 @@ using Orchard.Data;
using Orchard.Localization; using Orchard.Localization;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Mvc.Results; using Orchard.Mvc.Results;
using Orchard.Security;
using Orchard.UI.Notify; using Orchard.UI.Notify;
namespace Orchard.Blogs.Controllers { namespace Orchard.Blogs.Controllers {
@@ -45,7 +44,9 @@ namespace Orchard.Blogs.Controllers {
if (blog == null) if (blog == null)
return new NotFoundResult(); return new NotFoundResult();
BlogPost post = _blogPostService.Get(blog, postSlug); //TODO: (erikpo) Look up the current user and their permissions to this blog post and determine if they should be able to view it or not.
VersionOptions versionOptions = VersionOptions.Published;
BlogPost post = _blogPostService.Get(blog, postSlug, versionOptions);
if (post == null) if (post == null)
return new NotFoundResult(); return new NotFoundResult();
@@ -130,11 +131,19 @@ namespace Orchard.Blogs.Controllers {
if (blog == null) if (blog == null)
return new NotFoundResult(); return new NotFoundResult();
BlogPost blogPost = Services.ContentManager.Create<BlogPost>("blogpost", bp => { bp.Blog = blog; }); bool publishNow = false;
if (string.Equals(Request.Form["Command"], "PublishNow")) {
publishNow = true;
}
BlogPost blogPost = Services.ContentManager.Create<BlogPost>("blogpost", publishNow ? VersionOptions.Published : VersionOptions.Draft, bp => { bp.Blog = blog; });
model.BlogPost = Services.ContentManager.UpdateEditorModel(blogPost, this); model.BlogPost = Services.ContentManager.UpdateEditorModel(blogPost, this);
if (!ModelState.IsValid) if (!ModelState.IsValid) {
Services.TransactionManager.Cancel();
return View(model); return View(model);
}
//TEMP: (erikpo) ensure information has committed for this record //TEMP: (erikpo) ensure information has committed for this record
var session = _sessionLocator.For(typeof(BlogPostRecord)); var session = _sessionLocator.For(typeof(BlogPostRecord));
@@ -153,7 +162,7 @@ namespace Orchard.Blogs.Controllers {
if (blog == null) if (blog == null)
return new NotFoundResult(); return new NotFoundResult();
BlogPost post = _blogPostService.Get(blog, postSlug); BlogPost post = _blogPostService.Get(blog, postSlug, VersionOptions.Latest);
if (post == null) if (post == null)
return new NotFoundResult(); return new NotFoundResult();
@@ -176,23 +185,35 @@ namespace Orchard.Blogs.Controllers {
if (blog == null) if (blog == null)
return new NotFoundResult(); return new NotFoundResult();
BlogPost post = _blogPostService.Get(blog, postSlug); BlogPost post = _blogPostService.Get(blog, postSlug, VersionOptions.Latest);
if (post == null) if (post == null)
return new NotFoundResult(); return new NotFoundResult();
bool publishNow = false;
if (string.Equals(Request.Form["Command"], "PublishNow")) {
publishNow = true;
}
if (publishNow)
_blogPostService.Publish(post);
else
_blogPostService.Unpublish(post);
var model = new BlogPostEditViewModel { var model = new BlogPostEditViewModel {
BlogPost = Services.ContentManager.UpdateEditorModel(post, this) BlogPost = Services.ContentManager.UpdateEditorModel(post, this)
}; };
TryUpdateModel(model); TryUpdateModel(model);
if (ModelState.IsValid==false) { if (!ModelState.IsValid) {
Services.TransactionManager.Cancel(); Services.TransactionManager.Cancel();
return View(model); return View(model);
} }
Services.Notifier.Information(T("Blog post information updated.")); Services.Notifier.Information(T("Blog post information updated."));
return Redirect(Url.BlogPostEdit(blog.Slug, post.Slug)); return Redirect(Url.BlogPostEdit(blog.Slug, post.Slug));
} }
@@ -7,6 +7,7 @@ namespace Orchard.Blogs.Extensions {
public static string PublishedState(this HtmlHelper<BlogPost> htmlHelper) { public static string PublishedState(this HtmlHelper<BlogPost> htmlHelper) {
return htmlHelper.PublishedState(htmlHelper.ViewData.Model); return htmlHelper.PublishedState(htmlHelper.ViewData.Model);
} }
public static string PublishedState(this HtmlHelper htmlHelper, BlogPost blogPost) { public static string PublishedState(this HtmlHelper htmlHelper, BlogPost blogPost) {
return htmlHelper.DateTime(blogPost.Published, "Draft"); return htmlHelper.DateTime(blogPost.Published, "Draft");
} }
@@ -14,6 +15,7 @@ namespace Orchard.Blogs.Extensions {
public static string PublishedWhen(this HtmlHelper<BlogPost> htmlHelper) { public static string PublishedWhen(this HtmlHelper<BlogPost> htmlHelper) {
return htmlHelper.PublishedWhen(htmlHelper.ViewData.Model); return htmlHelper.PublishedWhen(htmlHelper.ViewData.Model);
} }
public static string PublishedWhen(this HtmlHelper htmlHelper, BlogPost blogPost) { public static string PublishedWhen(this HtmlHelper htmlHelper, BlogPost blogPost) {
return htmlHelper.DateTimeRelative(blogPost.Published, "as a Draft"); return htmlHelper.DateTimeRelative(blogPost.Published, "as a Draft");
} }
@@ -3,50 +3,77 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Orchard.Blogs.Models; using Orchard.Blogs.Models;
using Orchard.Core.Common.Records; using Orchard.Core.Common.Records;
using Orchard.Data;
using Orchard.ContentManagement; using Orchard.ContentManagement;
namespace Orchard.Blogs.Services { namespace Orchard.Blogs.Services {
public class BlogPostService : IBlogPostService { public class BlogPostService : IBlogPostService {
private readonly IContentManager _contentManager; private readonly IContentManager _contentManager;
private readonly IRepository<BlogPostRecord> _blogPostRepository;
public BlogPostService(IContentManager contentManager, IRepository<BlogPostRecord> blogPostRepository) { public BlogPostService(IContentManager contentManager) {
_contentManager = contentManager; _contentManager = contentManager;
_blogPostRepository = blogPostRepository;
} }
public BlogPost Get(Blog blog, string slug) { public BlogPost Get(Blog blog, string slug) {
return Get(blog, slug, VersionOptions.Published);
}
public BlogPost Get(Blog blog, string slug, VersionOptions versionOptions) {
return return
_contentManager.Query<BlogPost, BlogPostRecord>().Join<RoutableRecord>().Where(rr => rr.Slug == slug). _contentManager.Query<BlogPost, BlogPostRecord>(versionOptions).Join<RoutableRecord>().Where(rr => rr.Slug == slug).
Join<CommonRecord>().Where(cr => cr.Container == blog.Record.ContentItemRecord).List(). Join<CommonRecord>().Where(cr => cr.Container == blog.Record.ContentItemRecord).List().
SingleOrDefault(); SingleOrDefault();
} }
public IEnumerable<BlogPost> Get(Blog blog) { public IEnumerable<BlogPost> Get(Blog blog) {
return GetBlogQuery(blog).List(); return Get(blog, VersionOptions.Published);
}
public IEnumerable<BlogPost> Get(Blog blog, VersionOptions versionOptions) {
return GetBlogQuery(blog, versionOptions).List();
} }
public IEnumerable<BlogPost> Get(Blog blog, ArchiveData archiveData) { public IEnumerable<BlogPost> Get(Blog blog, ArchiveData archiveData) {
var query = GetBlogQuery(blog); var query = GetBlogQuery(blog, VersionOptions.Published);
if (archiveData.Day > 0) if (archiveData.Day > 0)
query = query.Where(cr => cr.CreatedUtc >= new DateTime(archiveData.Year, archiveData.Month, archiveData.Day) && cr.CreatedUtc < new DateTime(archiveData.Year, archiveData.Month, archiveData.Day + 1)); query =
query.Where(
cr =>
cr.CreatedUtc >= new DateTime(archiveData.Year, archiveData.Month, archiveData.Day) &&
cr.CreatedUtc < new DateTime(archiveData.Year, archiveData.Month, archiveData.Day + 1));
else if (archiveData.Month > 0) else if (archiveData.Month > 0)
query = query.Where(cr => cr.CreatedUtc >= new DateTime(archiveData.Year, archiveData.Month, 1) && cr.CreatedUtc < new DateTime(archiveData.Year, archiveData.Month + 1, 1)); query =
query.Where(
cr =>
cr.CreatedUtc >= new DateTime(archiveData.Year, archiveData.Month, 1) &&
cr.CreatedUtc < new DateTime(archiveData.Year, archiveData.Month + 1, 1));
else else
query = query.Where(cr => cr.CreatedUtc >= new DateTime(archiveData.Year, 1, 1) && cr.CreatedUtc < new DateTime(archiveData.Year + 1, 1, 1)); query =
query.Where(
cr =>
cr.CreatedUtc >= new DateTime(archiveData.Year, 1, 1) &&
cr.CreatedUtc < new DateTime(archiveData.Year + 1, 1, 1));
return query.List(); return query.List();
} }
public void Delete(BlogPost blogPost) { public void Delete(BlogPost blogPost) {
_blogPostRepository.Delete(blogPost.Record); _contentManager.Remove(blogPost.ContentItem);
} }
private IContentQuery<BlogPost, CommonRecord> GetBlogQuery(Blog blog) { public void Publish(BlogPost blogPost) {
_contentManager.Publish(blogPost.ContentItem);
blogPost.Published = DateTime.UtcNow;
}
public void Unpublish(BlogPost blogPost) {
_contentManager.Unpublish(blogPost.ContentItem);
blogPost.Published = null;
}
private IContentQuery<BlogPost, CommonRecord> GetBlogQuery(ContentPart<BlogRecord> blog, VersionOptions versionOptions) {
return return
_contentManager.Query<BlogPost, BlogPostRecord>().Join<CommonRecord>().Where( _contentManager.Query<BlogPost, BlogPostRecord>(versionOptions).Join<CommonRecord>().Where(
cr => cr.Container == blog.Record.ContentItemRecord).OrderByDescending(cr => cr.CreatedUtc); cr => cr.Container == blog.Record.ContentItemRecord).OrderByDescending(cr => cr.CreatedUtc);
} }
} }
@@ -1,11 +1,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using Orchard.Blogs.Models; using Orchard.Blogs.Models;
using Orchard.ContentManagement;
namespace Orchard.Blogs.Services { namespace Orchard.Blogs.Services {
public interface IBlogPostService : IDependency { public interface IBlogPostService : IDependency {
BlogPost Get(Blog blog, string slug); BlogPost Get(Blog blog, string slug);
BlogPost Get(Blog blog, string slug, VersionOptions versionOptions);
IEnumerable<BlogPost> Get(Blog blog); IEnumerable<BlogPost> Get(Blog blog);
IEnumerable<BlogPost> Get(Blog blog, VersionOptions versionOptions);
IEnumerable<BlogPost> Get(Blog blog, ArchiveData archiveData); IEnumerable<BlogPost> Get(Blog blog, ArchiveData archiveData);
void Delete(BlogPost blogPost); void Delete(BlogPost blogPost);
void Publish(BlogPost blogPost);
void Unpublish(BlogPost blogPost);
} }
} }
@@ -2,5 +2,12 @@
<%@ Import Namespace="Orchard.Blogs.Models"%> <%@ Import Namespace="Orchard.Blogs.Models"%>
<fieldset> <fieldset>
<legend><%=_Encoded("Publish Settings")%></legend> <legend><%=_Encoded("Publish Settings")%></legend>
<label for="Command_SaveDraft"><%=Html.RadioButton("Command", "SaveDraft", true, new { id = "Command_SaveDraft" }) %> <%=_Encoded("Save Draft")%></label> <label for="Command_SaveDraft"><%=Html.RadioButton("Command", "SaveDraft", Model.ContentItem.VersionRecord == null || !Model.ContentItem.VersionRecord.Published, new { id = "Command_SaveDraft" }) %> <%=_Encoded("Save Draft")%></label><br />
</fieldset> </fieldset>
<fieldset>
<label for="Command_PublishNow"><%=Html.RadioButton("Command", "PublishNow", Model.ContentItem.VersionRecord != null && Model.ContentItem.VersionRecord.Published, new { id = "Command_PublishNow" })%> <%=_Encoded("Publish Now")%></label>
</fieldset>
<%--<fieldset>
<label for="Command_PublishLater"><%=Html.RadioButton("Command", "PublishLater", new { id = "Command_PublishLater" }) %> <%=_Encoded("Publish Later")%></label>
<%=Html.EditorFor(m => m.Published) %>
</fieldset>--%>
@@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Orchard.Pages.Models; using Orchard.Pages.Models;
using Orchard.Core.Common.Records; using Orchard.Core.Common.Records;
@@ -1,12 +1,12 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Orchard.Pages.Models.Page>" %> <%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.Pages.Models.Page>" %>
<fieldset> <fieldset>
<legend>Publish Settings</legend> <legend><%=_Encoded("Publish Settings")%></legend>
<label for="Command_SaveDraft"><%=Html.RadioButton("Command", "SaveDraft", true, new { id = "Command_SaveDraft" }) %> Save Draft</label><br /> <label for="Command_SaveDraft"><%=Html.RadioButton("Command", "SaveDraft", Model.ContentItem.VersionRecord == null || !Model.ContentItem.VersionRecord.Published, new { id = "Command_SaveDraft" })%> <%=_Encoded("Save Draft")%></label><br />
</fieldset> </fieldset>
<fieldset> <fieldset>
<label for="Command_PublishNow"><%=Html.RadioButton("Command", "PublishNow", new { id = "Command_PublishNow" }) %> Publish Now</label> <label for="Command_PublishNow"><%=Html.RadioButton("Command", "PublishNow", Model.ContentItem.VersionRecord != null && Model.ContentItem.VersionRecord.Published, new { id = "Command_PublishNow" })%> <%=_Encoded("Publish Now")%></label>
</fieldset> </fieldset>
<%--<fieldset> <%--<fieldset>
<label for="Command_PublishLater"><%=Html.RadioButton("Command", "PublishLater", new { id = "Command_PublishLater" }) %> Publish Later</label> <label for="Command_PublishLater"><%=Html.RadioButton("Command", "PublishLater", new { id = "Command_PublishLater" }) %> <%=_Encoded("Publish Later")%></label>
<%=Html.EditorFor(m => m.Published) %> <%=Html.EditorFor(m => m.Published) %>
</fieldset>--%> </fieldset>--%>
+1 -2
View File
@@ -112,8 +112,7 @@ namespace Orchard.Mvc.Html {
return "a moment ago"; return "a moment ago";
} }
public static string DateTime(this HtmlHelper htmlHelper, DateTime? value, string defaultIfNull) public static string DateTime(this HtmlHelper htmlHelper, DateTime? value, string defaultIfNull) {
{
return value.HasValue ? htmlHelper.DateTime(value.Value) : defaultIfNull; return value.HasValue ? htmlHelper.DateTime(value.Value) : defaultIfNull;
} }