diff --git a/src/Umbraco.Web/Models/PublishedProperty.cs b/src/Umbraco.Web/Models/PublishedProperty.cs new file mode 100644 index 0000000000..65430ba5f6 --- /dev/null +++ b/src/Umbraco.Web/Models/PublishedProperty.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; + +namespace Umbraco.Web.Models +{ + public static class PublishedProperty + { + /// + /// Maps a collection of Property to a collection of IPublishedProperty for a specified collection of PublishedPropertyType. + /// + /// The published property types. + /// The properties. + /// A mapping function. + /// A collection of IPublishedProperty corresponding to the collection of PublishedPropertyType + /// and taking values from the collection of Property. + /// Ensures that all conversions took place correctly. + internal static IEnumerable MapProperties( + IEnumerable propertyTypes, IEnumerable properties, + Func map) + { + var peResolver = DataTypesResolver.Current; + var dtService = ApplicationContext.Current.Services.DataTypeService; + return MapProperties(propertyTypes, properties, peResolver, dtService, map); + } + + /// + /// Maps a collection of Property to a collection of IPublishedProperty for a specified collection of PublishedPropertyType. + /// + /// The published property types. + /// The properties. + /// A mapping function. + /// A DataTypesResolver instance. + /// An IDataTypeService instance. + /// A collection of IPublishedProperty corresponding to the collection of PublishedPropertyType + /// and taking values from the collection of Property. + /// Ensures that all conversions took place correctly. + internal static IEnumerable MapProperties( + IEnumerable propertyTypes, IEnumerable properties, + DataTypesResolver dataTypesResolver, IDataTypeService dataTypeService, + Func map) + { + return propertyTypes + .Select(x => + { + var p = properties.SingleOrDefault(xx => xx.Alias == x.PropertyTypeAlias); + var v = p == null || p.Value == null ? null : p.Value; + if (v != null) + { + var dataType = dataTypesResolver.DataTypes.SingleOrDefault(qq => qq.Id == x.PropertyEditorGuid); + if (dataType != null) + { + var data = dataType.Data; + data.Value = v; + var n = data.ToXMl(new XmlDocument()); + v = n.InnerXml; + } + } + // fixme - means that the IPropertyValueConverter will always get a string + // fixme and never an int or DateTime that's in the DB unless the value editor has + // fixme a way to say it's OK to use what's in the DB? + + return map(x, p, v); + }); + } + } +} diff --git a/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs b/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs index deafcaab40..e538d135d3 100644 --- a/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs @@ -18,7 +18,7 @@ namespace Umbraco.Web.PublishedCache private readonly IMember _member; private readonly MembershipUser _membershipUser; - private readonly List _properties; + private readonly IPublishedProperty[] _properties; private readonly PublishedContentType _publishedMemberType; public MemberPublishedContent(IMember member, MembershipUser membershipUser) @@ -28,19 +28,14 @@ namespace Umbraco.Web.PublishedCache _member = member; _membershipUser = membershipUser; - _properties = new List(); _publishedMemberType = PublishedContentType.Get(PublishedItemType.Member, _member.ContentTypeAlias); if (_publishedMemberType == null) { throw new InvalidOperationException("Could not get member type with alias " + _member.ContentTypeAlias); } - foreach (var propType in _publishedMemberType.PropertyTypes) - { - var val = _member.Properties.Any(x => x.Alias == propType.PropertyTypeAlias) == false - ? string.Empty - : _member.Properties[propType.PropertyTypeAlias].Value; - _properties.Add(new RawValueProperty(propType, val ?? string.Empty)); - } + _properties = PublishedProperty.MapProperties(_publishedMemberType.PropertyTypes, _member.Properties, + (t, p, v) => new RawValueProperty(t, v ?? string.Empty)) + .ToArray(); } #region Membership provider member properties diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 22fd963203..581893f2ea 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -1,4 +1,4 @@ - + @@ -300,6 +300,7 @@ +