From 016f464fd38da81ffe062ffd473a6144437435a1 Mon Sep 17 00:00:00 2001 From: kevink Date: Fri, 10 Feb 2012 11:35:20 -0800 Subject: [PATCH] Adding comment tokens --HG-- branch : 1.x --- .hgsubstate | 2 +- .../Orchard.Comments/Tokens/CommentTokens.cs | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Comments/Tokens/CommentTokens.cs diff --git a/.hgsubstate b/.hgsubstate index 9f741571e..dc42b7d06 100644 --- a/.hgsubstate +++ b/.hgsubstate @@ -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 diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Tokens/CommentTokens.cs b/src/Orchard.Web/Modules/Orchard.Comments/Tokens/CommentTokens.cs new file mode 100644 index 000000000..94b9dff2b --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Comments/Tokens/CommentTokens.cs @@ -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("Content") + .Token("CommentedOn", (Func)(content => content.As().Record.CommentedOn)) + .Chain("CommentedOn", "Content", (Func)(content => _contentManager.Get(content.As().Record.CommentedOn))) + .Token("CommentMessage", (Func)(content => content.As().Record.CommentText)) + .Token("CommentAuthor", (Func)CommentAuthor) + ; + } + + private static string CommentAuthor(IContent comment) { + var commentPart = comment.As(); + return String.IsNullOrWhiteSpace(commentPart.Record.UserName) ? commentPart.Record.Author : commentPart.Record.UserName; + } + } +} \ No newline at end of file