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)
This commit is contained in:
agrath@gmail.com
2011-01-27 21:38:06 -13:00
parent 6382241e4c
commit a0dd8aa2ab
3 changed files with 102 additions and 0 deletions

View File

@@ -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<string, string> _propertyCache;
private Media _media;
public DynamicMedia(int mediaId)
{
_media = new Media(mediaId);
_propertyCache = new Dictionary<string, string>();
}
public DynamicMedia(Media media)
{
_media = media;
_propertyCache = new Dictionary<string, string>();
}
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;
}
}
}

View File

@@ -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)
{

View File

@@ -63,6 +63,7 @@
<ItemGroup>
<Compile Include="DLRScriptingEngine.cs" />
<Compile Include="DynamicDictionary.cs" />
<Compile Include="DynamicMedia.cs" />
<Compile Include="DynamicNode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RazorEngine.cs" />