From 48b6e709877f7d76ef1620e42ebab406a3e647f4 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 8 Jun 2012 10:28:32 -0700 Subject: [PATCH] Adding Site tokens and custom fields --HG-- branch : 1.x --- .hgsubstate | 2 +- src/Orchard.Web/Core/Settings/Migrations.cs | 9 +- .../Core/Settings/Tokens/SettingsTokens.cs | 89 +++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/Orchard.Web/Core/Settings/Tokens/SettingsTokens.cs diff --git a/.hgsubstate b/.hgsubstate index 945cd9089..6f86e734d 100644 --- a/.hgsubstate +++ b/.hgsubstate @@ -8,5 +8,5 @@ e7fc05ff6137ed5459d198b2bea9a5804818b0bd src/Orchard.Web/Modules/Orchard.Forms aca1f5aeb5dd426bc80c6142afc7f2717fc6d5a1 src/Orchard.Web/Modules/Orchard.Tokens f6fecd1702066225a84a482ac029e3e6daff38f3 src/Orchard.Web/Modules/Orchard.ViewPermissions 4ed51e0e76c2aacc2de90ce9984fd00cfdfae2ce src/orchard.web/Modules/Orchard.Alias -cca0d94ab4000687eab9ed2bf1fb90f673fb36fb src/orchard.web/Modules/Orchard.Projections +109844c63717ca7f881fb126a845184cca8de6f6 src/orchard.web/Modules/Orchard.Projections 0826b86677cd77cfb05342b3ba570f522dc3e786 src/orchard.web/modules/Orchard.Fields diff --git a/src/Orchard.Web/Core/Settings/Migrations.cs b/src/Orchard.Web/Core/Settings/Migrations.cs index 56b20fb39..7d451920d 100644 --- a/src/Orchard.Web/Core/Settings/Migrations.cs +++ b/src/Orchard.Web/Core/Settings/Migrations.cs @@ -1,4 +1,5 @@ -using Orchard.Data.Migration; +using Orchard.ContentManagement.MetaData; +using Orchard.Data.Migration; namespace Orchard.Core.Settings { public class Migrations : DataMigrationImpl { @@ -121,5 +122,11 @@ namespace Orchard.Core.Settings { return 3; } + + public int UpdateFrom3() { + ContentDefinitionManager.AlterTypeDefinition("Site", cfg => { }); + + return 4; + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Settings/Tokens/SettingsTokens.cs b/src/Orchard.Web/Core/Settings/Tokens/SettingsTokens.cs new file mode 100644 index 000000000..d881c55a1 --- /dev/null +++ b/src/Orchard.Web/Core/Settings/Tokens/SettingsTokens.cs @@ -0,0 +1,89 @@ +using System; +using System.Linq; +using Orchard.ContentManagement; +using Orchard.ContentManagement.MetaData; +using Orchard.Events; +using Orchard.Localization; +using Orchard.Settings; +using Orchard.ContentManagement.FieldStorage; + +namespace Orchard.Core.Settings.Tokens { + public interface ITokenProvider : IEventHandler { + void Describe(dynamic context); + void Evaluate(dynamic context); + } + + public class SettingsTokens : ITokenProvider { + private readonly IOrchardServices _orchardServices; + private readonly IContentDefinitionManager _contentDefinitionManager; + + public SettingsTokens(IOrchardServices orchardServices, IContentDefinitionManager contentDefinitionManager) { + _orchardServices = orchardServices; + _contentDefinitionManager = contentDefinitionManager; + } + + public Localizer T { get; set; } + + public void Describe(dynamic context) { + + context.For("Site", T("Site Settings"), T("Tokens for Site Settings")) + .Token("SiteName", T("Site Name"), T("The name of the site.")) + .Token("SuperUser", T("Super User"), T("The name of the super user.")) + .Token("Culture", T("Site Culture"), T("The current culture of the site.")) + .Token("BaseUrl", T("Base Url"), T("The base url the site.")) + .Token("TimeZone", T("Time Zone"), T("The current time zone of the site.")) + ; + + // Token descriptors for fields + var customSettingsPart = _contentDefinitionManager.GetPartDefinition("Site"); + if (customSettingsPart != null && customSettingsPart.Fields.Any()) { + var partContext = context.For("Site"); + foreach (var partField in customSettingsPart.Fields) { + var field = partField; + var tokenName = field.Name; + + // the token is chained with the technical name + partContext.Token(tokenName, T("{0}", field.Name), T("The setting named {0}.", partField.DisplayName), field.Name); + } + } + + } + + public void Evaluate(dynamic context) { + var forContent = context.For("Site", (Func)(() => _orchardServices.WorkContext.CurrentSite)); + + forContent + .Token("SiteName", (Func)(content => content.SiteName)) + .Token("SuperUser", (Func)(content => content.SuperUser)) + .Token("Culture", (Func) (content => content.SiteCulture)) + .Token("BaseUrl", (Func) (content => content.BaseUrl)) + .Token("TimeZone", (Func) (content => content.SiteTimeZone)) + ; + + if (context.Target == "Site") { + // is there a content available in the context ? + if (forContent.Data != null && forContent.Data.ContentItem != null) { + var customSettingsPart = _contentDefinitionManager.GetPartDefinition("Site"); + foreach (var partField in customSettingsPart.Fields) { + var field = partField; + var tokenName = partField.Name; + forContent.Token( + tokenName, + (Func)(content => LookupField(content, field.Name).Storage.Get())); + forContent.Chain( + tokenName, + partField.FieldDefinition.Name, + (Func)(content => LookupField(content, field.Name))); + } + } + } + } + + private static ContentField LookupField(IContent content, string fieldName) { + return content.ContentItem.Parts + .Where(part => part.PartDefinition.Name == "Site") + .SelectMany(part => part.Fields.Where(field => field.Name == fieldName)) + .FirstOrDefault(); + } + } +} \ No newline at end of file