From a0dd8aa2ab3019db420c451e0589d8f90214f568 Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Thu, 27 Jan 2011 21:38:06 -1300 Subject: [PATCH] Improves access to media items when working in Razor Added a new DynamicMedia class that wraps umbraco.cms.businesslogic.media.Media which caches GetProperty calls (while DynamicMedia is instantiated) Added method to DynamicNode to retrieve a property as DynamicMedia: @Model.Media("catPicture"); or @Model.Media("catPicture").umbracoFile; Added overload to DynamicNode Media method for shorthand if you only want one property; @Model.Media("catPicture","umbracoFile"); @Model.catPicture will still return the nodeId of the media item because we can't easily check the propertyType (no propertyType on IProperty) --- umbraco.MacroEngines.Juno/DynamicMedia.cs | 59 +++++++++++++++++++ umbraco.MacroEngines.Juno/DynamicNode.cs | 42 +++++++++++++ .../umbraco.MacroEngines.csproj | 1 + 3 files changed, 102 insertions(+) create mode 100644 umbraco.MacroEngines.Juno/DynamicMedia.cs diff --git a/umbraco.MacroEngines.Juno/DynamicMedia.cs b/umbraco.MacroEngines.Juno/DynamicMedia.cs new file mode 100644 index 0000000000..fa7f7794dd --- /dev/null +++ b/umbraco.MacroEngines.Juno/DynamicMedia.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Dynamic; +using umbraco.cms.businesslogic.media; +using umbraco.cms.businesslogic.property; + +namespace umbraco.MacroEngines +{ + public class DynamicMedia : DynamicObject + { + private Dictionary _propertyCache; + private Media _media; + public DynamicMedia(int mediaId) + { + _media = new Media(mediaId); + _propertyCache = new Dictionary(); + } + public DynamicMedia(Media media) + { + _media = media; + _propertyCache = new Dictionary(); + } + public DynamicMedia() + { + + } + + public override bool TryGetMember(GetMemberBinder binder, out object result) + { + string name = binder.Name; + if (_propertyCache != null && _propertyCache.ContainsKey(name)) + { + result = _propertyCache[name]; + return true; + } + if (_media != null) + { + Property prop = _media.getProperty(name); + if (prop != null) + { + result = prop.Value; + if (_propertyCache != null) + { + _propertyCache.Add(name, string.Format("{0}", prop.Value)); + } + return true; + } + //return false because we have a media item now but the property doesn't exist + result = null; + return false; + } + result = null; + //return true because the _media is likely null, meaning we're in test mode + return true; + } + } +} diff --git a/umbraco.MacroEngines.Juno/DynamicNode.cs b/umbraco.MacroEngines.Juno/DynamicNode.cs index d675db85af..a47fe3013c 100644 --- a/umbraco.MacroEngines.Juno/DynamicNode.cs +++ b/umbraco.MacroEngines.Juno/DynamicNode.cs @@ -249,6 +249,48 @@ namespace umbraco.MacroEngines return false; } + public DynamicMedia Media(string propertyAlias) + { + if (n != null) + { + IProperty prop = n.GetProperty(propertyAlias); + if (prop != null) + { + int mediaNodeId; + if (int.TryParse(prop.Value, out mediaNodeId)) + { + return new DynamicMedia(mediaNodeId); + } + } + return null; + } + return null; + } + public string Media(string propertyAlias, string mediaPropertyAlias) + { + if (n != null) + { + IProperty prop = n.GetProperty(propertyAlias); + if (prop != null) + { + int mediaNodeId; + if (int.TryParse(prop.Value, out mediaNodeId)) + { + umbraco.cms.businesslogic.media.Media media = new cms.businesslogic.media.Media(mediaNodeId); + if (media != null) + { + Property mprop = media.getProperty(mediaPropertyAlias); + if (mprop != null) + { + return string.Format("{0}", mprop.Value); + } + } + } + } + } + return null; + } + //this is from SqlMetal and just makes it a bit of fun to allow pluralisation private static string MakePluralName(string name) { diff --git a/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj b/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj index 7f57d39be2..495da69411 100644 --- a/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj +++ b/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj @@ -63,6 +63,7 @@ +