Adding comment tokens

--HG--
branch : 1.x
This commit is contained in:
kevink
2012-02-10 11:35:20 -08:00
parent bbfbf0b927
commit 016f464fd3
2 changed files with 46 additions and 1 deletions

View File

@@ -2,5 +2,5 @@
6cd31f00d00d734408da2f2928220b0b7e59b293 src/Orchard.Web/Modules/Orchard.Projections
204bdef384f41bb5e463bed6b98a056945a7d839 src/Orchard.Web/Modules/Orchard.Rules
ce578373f907c0a55fd91229a344f0755f290174 src/Orchard.Web/Modules/Orchard.TaskLease
cf73534c335f39e6d9695c2a0ce64c426d1be9f2 src/Orchard.Web/Modules/Orchard.Tokens
dfeb1f4ebcdc587267ab21bc6bbd1d5dde96e264 src/Orchard.Web/Modules/Orchard.Tokens
114e75928872042f092b0cc7cafa1a58c208d8ae src/orchard.web/modules/Orchard.Fields

View File

@@ -0,0 +1,45 @@
using System;
using Orchard.Comments.Models;
using Orchard.ContentManagement;
using Orchard.Events;
using Orchard.Localization;
namespace Orchard.Comments.Tokens {
public interface ITokenProvider : IEventHandler {
void Describe(dynamic context);
void Evaluate(dynamic context);
}
public class CommentTokens : ITokenProvider {
private readonly IContentManager _contentManager;
public CommentTokens(IContentManager contentManager) {
_contentManager = contentManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(dynamic context) {
context.For("Content", T("Content Items"), T("Content Items"))
.Token("CommentedOn", T("Commented On"), T("The content item this comment was created on."))
.Token("CommentMessage", T("Comment Message"), T("The text of the comment itself"))
.Token("CommentAuthor", T("Comment Author"), T("The author of the comment."))
;
}
public void Evaluate(dynamic context) {
context.For<IContent>("Content")
.Token("CommentedOn", (Func<IContent, object>)(content => content.As<CommentPart>().Record.CommentedOn))
.Chain("CommentedOn", "Content", (Func<IContent, object>)(content => _contentManager.Get(content.As<CommentPart>().Record.CommentedOn)))
.Token("CommentMessage", (Func<IContent, object>)(content => content.As<CommentPart>().Record.CommentText))
.Token("CommentAuthor", (Func<IContent, object>)CommentAuthor)
;
}
private static string CommentAuthor(IContent comment) {
var commentPart = comment.As<CommentPart>();
return String.IsNullOrWhiteSpace(commentPart.Record.UserName) ? commentPart.Record.Author : commentPart.Record.UserName;
}
}
}