mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-22 21:02:08 +08:00
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:
@@ -56,7 +56,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
IEnumerable<ContentItemViewModel<BlogPost>> blogPosts = null;
|
||||
if (displayType.StartsWith("DetailAdmin")) {
|
||||
blogPosts = _blogPostService.Get(blog)
|
||||
blogPosts = _blogPostService.Get(blog, VersionOptions.Latest)
|
||||
.Select(bp => _contentManager.BuildDisplayModel(bp, "SummaryAdmin"));
|
||||
}
|
||||
else if (displayType.StartsWith("Detail")) {
|
||||
|
||||
@@ -9,7 +9,6 @@ using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Mvc.Results;
|
||||
using Orchard.Security;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Blogs.Controllers {
|
||||
@@ -45,7 +44,9 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (blog == null)
|
||||
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)
|
||||
return new NotFoundResult();
|
||||
@@ -108,7 +109,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
|
||||
var blogPost = Services.ContentManager.BuildEditorModel(Services.ContentManager.New<BlogPost>("blogpost"));
|
||||
blogPost.Item.Blog = blog;
|
||||
|
||||
@@ -130,11 +131,19 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (blog == null)
|
||||
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);
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
//TEMP: (erikpo) ensure information has committed for this record
|
||||
var session = _sessionLocator.For(typeof(BlogPostRecord));
|
||||
@@ -153,7 +162,7 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
BlogPost post = _blogPostService.Get(blog, postSlug);
|
||||
BlogPost post = _blogPostService.Get(blog, postSlug, VersionOptions.Latest);
|
||||
|
||||
if (post == null)
|
||||
return new NotFoundResult();
|
||||
@@ -176,10 +185,20 @@ namespace Orchard.Blogs.Controllers {
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
BlogPost post = _blogPostService.Get(blog, postSlug);
|
||||
BlogPost post = _blogPostService.Get(blog, postSlug, VersionOptions.Latest);
|
||||
|
||||
if (post == null)
|
||||
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 {
|
||||
BlogPost = Services.ContentManager.UpdateEditorModel(post, this)
|
||||
@@ -187,12 +206,14 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
TryUpdateModel(model);
|
||||
|
||||
if (ModelState.IsValid==false) {
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
Services.Notifier.Information(T("Blog post information updated."));
|
||||
|
||||
return Redirect(Url.BlogPostEdit(blog.Slug, post.Slug));
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Orchard.Blogs.Extensions {
|
||||
public static string PublishedState(this HtmlHelper<BlogPost> htmlHelper) {
|
||||
return htmlHelper.PublishedState(htmlHelper.ViewData.Model);
|
||||
}
|
||||
|
||||
public static string PublishedState(this HtmlHelper htmlHelper, BlogPost blogPost) {
|
||||
return htmlHelper.DateTime(blogPost.Published, "Draft");
|
||||
}
|
||||
@@ -14,6 +15,7 @@ namespace Orchard.Blogs.Extensions {
|
||||
public static string PublishedWhen(this HtmlHelper<BlogPost> htmlHelper) {
|
||||
return htmlHelper.PublishedWhen(htmlHelper.ViewData.Model);
|
||||
}
|
||||
|
||||
public static string PublishedWhen(this HtmlHelper htmlHelper, BlogPost blogPost) {
|
||||
return htmlHelper.DateTimeRelative(blogPost.Published, "as a Draft");
|
||||
}
|
||||
|
||||
@@ -3,50 +3,77 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.Blogs.Services {
|
||||
public class BlogPostService : IBlogPostService {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IRepository<BlogPostRecord> _blogPostRepository;
|
||||
|
||||
public BlogPostService(IContentManager contentManager, IRepository<BlogPostRecord> blogPostRepository) {
|
||||
public BlogPostService(IContentManager contentManager) {
|
||||
_contentManager = contentManager;
|
||||
_blogPostRepository = blogPostRepository;
|
||||
}
|
||||
|
||||
public BlogPost Get(Blog blog, string slug) {
|
||||
return Get(blog, slug, VersionOptions.Published);
|
||||
}
|
||||
|
||||
public BlogPost Get(Blog blog, string slug, VersionOptions versionOptions) {
|
||||
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().
|
||||
SingleOrDefault();
|
||||
}
|
||||
|
||||
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) {
|
||||
var query = GetBlogQuery(blog);
|
||||
var query = GetBlogQuery(blog, VersionOptions.Published);
|
||||
|
||||
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)
|
||||
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
|
||||
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();
|
||||
}
|
||||
|
||||
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
|
||||
_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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.Blogs.Services {
|
||||
public interface IBlogPostService : IDependency {
|
||||
BlogPost Get(Blog blog, string slug);
|
||||
BlogPost Get(Blog blog, string slug, VersionOptions versionOptions);
|
||||
IEnumerable<BlogPost> Get(Blog blog);
|
||||
IEnumerable<BlogPost> Get(Blog blog, VersionOptions versionOptions);
|
||||
IEnumerable<BlogPost> Get(Blog blog, ArchiveData archiveData);
|
||||
void Delete(BlogPost blogPost);
|
||||
void Publish(BlogPost blogPost);
|
||||
void Unpublish(BlogPost blogPost);
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,12 @@
|
||||
<%@ Import Namespace="Orchard.Blogs.Models"%>
|
||||
<fieldset>
|
||||
<legend><%=_Encoded("Publish Settings")%></legend>
|
||||
<label for="Command_SaveDraft"><%=Html.RadioButton("Command", "SaveDraft", true, new { id = "Command_SaveDraft" }) %> <%=_Encoded("Save Draft")%></label>
|
||||
</fieldset>
|
||||
<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>
|
||||
<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 Orchard.Pages.Models;
|
||||
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>
|
||||
<legend>Publish Settings</legend>
|
||||
<label for="Command_SaveDraft"><%=Html.RadioButton("Command", "SaveDraft", true, new { id = "Command_SaveDraft" }) %> Save Draft</label><br />
|
||||
<legend><%=_Encoded("Publish Settings")%></legend>
|
||||
<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>
|
||||
<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>
|
||||
<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) %>
|
||||
</fieldset>--%>
|
||||
@@ -112,8 +112,7 @@ namespace Orchard.Mvc.Html {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user