uQuery: Sync'd latest from uComponents - enhancements to GetProperty<T>() method.
This commit is contained in:
@@ -4,6 +4,7 @@ using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using umbraco.cms.businesslogic;
|
||||
using umbraco.cms.businesslogic.datatype;
|
||||
using System.Xml;
|
||||
|
||||
namespace umbraco
|
||||
{
|
||||
@@ -26,7 +27,6 @@ namespace umbraco
|
||||
return (property != null);
|
||||
}
|
||||
|
||||
#pragma warning disable 0618
|
||||
/// <summary>
|
||||
/// Get a value (of specified type) from a content item's property.
|
||||
/// </summary>
|
||||
@@ -35,33 +35,56 @@ namespace umbraco
|
||||
/// <param name="propertyAlias">alias of property to get</param>
|
||||
/// <returns>default(T) or property value cast to (T)</returns>
|
||||
public static T GetProperty<T>(this Content item, string propertyAlias)
|
||||
{
|
||||
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
|
||||
{
|
||||
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
|
||||
|
||||
if (typeConverter != null)
|
||||
{
|
||||
if (typeof(T) == typeof(bool))
|
||||
{
|
||||
return (T)typeConverter.ConvertFrom(item.GetPropertyAsBoolean(propertyAlias).ToString());
|
||||
// TODO: [LK -> HR] Maybe set 'GetPropertyAsBoolean' as a private/internal method?
|
||||
}
|
||||
if (typeConverter != null)
|
||||
{
|
||||
// Boolean
|
||||
if (typeof(T) == typeof(bool))
|
||||
{
|
||||
return (T)typeConverter.ConvertFrom(item.GetPropertyAsBoolean(propertyAlias).ToString());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return (T)typeConverter.ConvertFromString(item.GetPropertyAsString(propertyAlias));
|
||||
// TODO: [LK -> HR] Maybe set 'GetPropertyAsString' as a private/internal method?
|
||||
}
|
||||
catch
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
#pragma warning restore 0618
|
||||
// XmlDocument
|
||||
else if (typeof(T) == typeof(XmlDocument))
|
||||
{
|
||||
var xmlDocument = new XmlDocument();
|
||||
|
||||
try
|
||||
{
|
||||
xmlDocument.Load(item.GetPropertyAsString(propertyAlias));
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return (T)((object)xmlDocument);
|
||||
}
|
||||
|
||||
// // umbraco.MacroEngines.DynamicXml
|
||||
// else if (typeof(T) == typeof(DynamicXml))
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// return (T)((object)new DynamicXml(item.GetPropertyAsString(propertyAlias)));
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
|
||||
try
|
||||
{
|
||||
return (T)typeConverter.ConvertFromString(item.GetPropertyAsString(propertyAlias));
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a string value from a content item's property.
|
||||
@@ -71,7 +94,7 @@ namespace umbraco
|
||||
/// <returns>
|
||||
/// empty string, or property value as string
|
||||
/// </returns>
|
||||
internal static string GetPropertyAsString(this Content item, string propertyAlias)
|
||||
private static string GetPropertyAsString(this Content item, string propertyAlias)
|
||||
{
|
||||
var propertyValue = string.Empty;
|
||||
var property = item.getProperty(propertyAlias);
|
||||
@@ -92,7 +115,7 @@ namespace umbraco
|
||||
/// <returns>
|
||||
/// true if can cast value, else false for all other circumstances
|
||||
/// </returns>
|
||||
internal static bool GetPropertyAsBoolean(this Content item, string propertyAlias)
|
||||
private static bool GetPropertyAsBoolean(this Content item, string propertyAlias)
|
||||
{
|
||||
var propertyValue = false;
|
||||
var property = item.getProperty(propertyAlias);
|
||||
|
||||
@@ -261,7 +261,6 @@ namespace umbraco
|
||||
return (property != null);
|
||||
}
|
||||
|
||||
#pragma warning disable 0618
|
||||
/// <summary>
|
||||
/// Get a value of type T from a property
|
||||
/// </summary>
|
||||
@@ -275,27 +274,55 @@ namespace umbraco
|
||||
|
||||
if (typeConverter != null)
|
||||
{
|
||||
// Boolean
|
||||
if (typeof(T) == typeof(bool))
|
||||
{
|
||||
// Use the GetPropertyAsBoolean method, as this handles true also being stored as "1"
|
||||
return (T)typeConverter.ConvertFrom(node.GetPropertyAsBoolean(propertyAlias).ToString());
|
||||
}
|
||||
|
||||
try
|
||||
// XmlDocument
|
||||
else if (typeof(T) == typeof(XmlDocument))
|
||||
{
|
||||
return (T)typeConverter.ConvertFromString(node.GetPropertyAsString(propertyAlias));
|
||||
var xmlDocument = new XmlDocument();
|
||||
|
||||
try
|
||||
{
|
||||
xmlDocument.Load(node.GetPropertyAsString(propertyAlias));
|
||||
}
|
||||
catch
|
||||
{
|
||||
// xml probably invalid
|
||||
}
|
||||
|
||||
return (T)((object)xmlDocument);
|
||||
}
|
||||
catch
|
||||
|
||||
// // umbraco.MacroEngines DynamicXml
|
||||
// else if (typeof(T) == typeof(DynamicXml))
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// return (T)((object)new DynamicXml(node.GetPropertyAsString(propertyAlias)));
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
try
|
||||
{
|
||||
return (T)typeConverter.ConvertFromString(node.GetPropertyAsString(propertyAlias));
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
#pragma warning restore 0618
|
||||
|
||||
/// <summary>
|
||||
/// Get a string value for the supplied property alias
|
||||
@@ -303,7 +330,7 @@ namespace umbraco
|
||||
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
|
||||
/// <param name="propertyAlias">alias of propety to get</param>
|
||||
/// <returns>empty string, or property value as string</returns>
|
||||
internal static string GetPropertyAsString(this Node node, string propertyAlias)
|
||||
private static string GetPropertyAsString(this Node node, string propertyAlias)
|
||||
{
|
||||
var propertyValue = string.Empty;
|
||||
var property = node.GetProperty(propertyAlias);
|
||||
@@ -322,7 +349,7 @@ namespace umbraco
|
||||
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
|
||||
/// <param name="propertyAlias">alias of propety to get</param>
|
||||
/// <returns>true if can cast value, else false for all other circumstances</returns>
|
||||
internal static bool GetPropertyAsBoolean(this Node node, string propertyAlias)
|
||||
private static bool GetPropertyAsBoolean(this Node node, string propertyAlias)
|
||||
{
|
||||
var propertyValue = false;
|
||||
var property = node.GetProperty(propertyAlias);
|
||||
@@ -343,18 +370,6 @@ namespace umbraco
|
||||
return propertyValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension method on Node obj to get it's depth
|
||||
/// taken from: http://our.umbraco.org/wiki/how-tos/useful-helper-extension-methods-%28linq-null-safe-access%29
|
||||
/// </summary>
|
||||
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
|
||||
/// <returns>int for depth, starts at 1</returns>
|
||||
[Obsolete("Use .Level instead")]
|
||||
public static int GetDepth(this Node node)
|
||||
{
|
||||
return node.Path.Split(',').ToList().Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tell me the level of this node (0 = root)
|
||||
/// updated from Depth and changed to start at 0
|
||||
|
||||
@@ -201,7 +201,9 @@ namespace umbraco
|
||||
{
|
||||
queryStringId = HttpContext.Current.Request.QueryString["id"];
|
||||
}
|
||||
else if (HttpContext.Current.Request.UrlReferrer != null && !string.IsNullOrEmpty(HttpContext.Current.Request.UrlReferrer.Query))
|
||||
else if (HttpContext.Current.Request.CurrentExecutionFilePathExtension == ".asmx"
|
||||
&& HttpContext.Current.Request.UrlReferrer != null
|
||||
&& !string.IsNullOrEmpty(HttpContext.Current.Request.UrlReferrer.Query))
|
||||
{
|
||||
// Special case for MNTP CustomTreeService.asmx
|
||||
queryStringId = HttpUtility.ParseQueryString(HttpContext.Current.Request.UrlReferrer.Query)["id"];
|
||||
|
||||
Reference in New Issue
Block a user