Merge with 6.0.3

This commit is contained in:
Shannon Deminick
2013-03-27 03:09:57 +06:00

View File

@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Linq;
using System.Xml;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
using umbraco.NodeFactory;
namespace umbraco
@@ -20,6 +21,17 @@ namespace umbraco
/// <param name="level">The level.</param>
/// <returns>Returns an ancestor node by path level.</returns>
public static Node GetAncestorByPathLevel(this Node node, int level)
{
return (Node) GetAncestorByPathLevel((INode) node, level);
}
/// <summary>
/// Gets the ancestor by path level.
/// </summary>
/// <param name="node">The node.</param>
/// <param name="level">The level.</param>
/// <returns>Returns an ancestor node by path level.</returns>
public static INode GetAncestorByPathLevel(this INode node, int level)
{
var nodeId = uQuery.GetNodeIdByPathLevel(node.Path, level);
return uQuery.GetNode(nodeId);
@@ -32,12 +44,23 @@ namespace umbraco
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
/// <returns>Node as IEnumerable</returns>
public static IEnumerable<Node> GetAncestorNodes(this Node node)
{
return GetAncestorNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'ancestor'
/// Get the Ancestor Nodes from current to root (useful for breadcrumbs)
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEnumerable</returns>
public static IEnumerable<INode> GetAncestorNodes(this INode node)
{
var ancestor = node.Parent;
while (ancestor != null)
{
yield return ancestor as Node;
yield return ancestor as INode;
ancestor = ancestor.Parent;
}
@@ -49,6 +72,16 @@ namespace umbraco
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
/// <returns>Node as IEnumerable</returns>
public static IEnumerable<Node> GetAncestorOrSelfNodes(this Node node)
{
return GetAncestorOrSelfNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'ancestor-or-self'
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEnumerable</returns>
public static IEnumerable<INode> GetAncestorOrSelfNodes(this INode node)
{
yield return node;
@@ -64,10 +97,20 @@ namespace umbraco
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
/// <returns>Node as IEumerable</returns>
public static IEnumerable<Node> GetPrecedingSiblingNodes(this Node node)
{
return GetPrecedingSiblingNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'preceding-sibling'
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEumerable</returns>
public static IEnumerable<INode> GetPrecedingSiblingNodes(this INode node)
{
if (node.Parent != null)
{
var parentNode = node.Parent as Node;
var parentNode = node.Parent as INode;
foreach (var precedingSiblingNode in parentNode.GetChildNodes().Where(childNode => childNode.SortOrder < node.SortOrder))
{
yield return precedingSiblingNode;
@@ -81,10 +124,20 @@ namespace umbraco
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
/// <returns>Node as IEumerable</returns>
public static IEnumerable<Node> GetFollowingSiblingNodes(this Node node)
{
return GetFollowingSiblingNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'following-sibling'
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEumerable</returns>
public static IEnumerable<INode> GetFollowingSiblingNodes(this INode node)
{
if (node.Parent != null)
{
var parentNode = node.Parent as Node;
var parentNode = node.Parent as INode;
foreach (var followingSiblingNode in parentNode.GetChildNodes().Where(childNode => childNode.SortOrder > node.SortOrder))
{
yield return followingSiblingNode;
@@ -98,10 +151,20 @@ namespace umbraco
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
/// <returns>Node as IEumerable</returns>
public static IEnumerable<Node> GetSiblingNodes(this Node node)
{
return GetSiblingNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Gets all sibling Nodes
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEumerable</returns>
public static IEnumerable<INode> GetSiblingNodes(this INode node)
{
if (node.Parent != null)
{
var parentNode = node.Parent as Node;
var parentNode = node.Parent as INode;
foreach (var siblingNode in parentNode.GetChildNodes().Where(childNode => childNode.Id != node.Id))
{
yield return siblingNode;
@@ -115,6 +178,16 @@ namespace umbraco
/// <param name="node">an umbraco.presentation.nodeFactory.Node object</param>
/// <returns>Node as IEnumerable</returns>
public static IEnumerable<Node> GetDescendantOrSelfNodes(this Node node)
{
return GetDescendantOrSelfNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'descendant-or-self'
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEnumerable</returns>
public static IEnumerable<INode> GetDescendantOrSelfNodes(this INode node)
{
yield return node;
@@ -133,7 +206,19 @@ namespace umbraco
/// <returns>Node as IEnumerable</returns>
public static IEnumerable<Node> GetDescendantNodes(this Node node)
{
foreach (Node child in node.Children)
return GetDescendantNodes((INode) node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'descendant'
/// Make the All Descendants LINQ queryable
/// taken from: http://our.umbraco.org/wiki/how-tos/useful-helper-extension-methods-%28linq-null-safe-access%29
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEnumerable</returns>
public static IEnumerable<INode> GetDescendantNodes(this INode node)
{
foreach (INode child in node.ChildrenAsList)
{
yield return child;
@@ -153,7 +238,19 @@ namespace umbraco
/// <returns>Nodes as IEnumerable</returns>
public static IEnumerable<Node> GetDescendantNodes(this Node node, Func<Node, bool> func)
{
foreach (Node child in node.Children)
return GetDescendantNodes((INode)node, (Func<INode, bool>) func).Cast<Node>();
}
/// <summary>
/// Drills down into the descendant nodes returning those where Func is true, when Func is false further descendants are not checked
/// taken from: http://ucomponents.codeplex.com/discussions/246406
/// </summary>
/// <param name="node">The <c>umbraco.interfaces.INode</c>.</param>
/// <param name="func">The func</param>
/// <returns>INode as IEnumerable</returns>
public static IEnumerable<INode> GetDescendantNodes(this INode node, Func<INode, bool> func)
{
foreach (INode child in node.ChildrenAsList)
{
if (func(child))
{
@@ -175,6 +272,18 @@ namespace umbraco
/// <param name="documentTypeAlias">The document type alias.</param>
/// <returns>Nodes as IEnumerable</returns>
public static IEnumerable<Node> GetDescendantNodesByType(this Node node, string documentTypeAlias)
{
return GetDescendantNodesByType((INode)node, documentTypeAlias).Cast<Node>();
}
/// <summary>
/// Gets the descendant nodes by document-type.
/// Get all descendants, and then return only those that match the requested typeAlias
/// </summary>
/// <param name="node">The <c>umbraco.interfaces.INode</c>.</param>
/// <param name="documentTypeAlias">The document type alias.</param>
/// <returns>INodes as IEnumerable</returns>
public static IEnumerable<INode> GetDescendantNodesByType(this INode node, string documentTypeAlias)
{
return node.GetDescendantNodes().Where(x => x.NodeTypeAlias == documentTypeAlias);
}
@@ -189,10 +298,20 @@ namespace umbraco
/// <returns>Node as IEnumerable</returns>
public static IEnumerable<Node> GetChildNodes(this Node node)
{
foreach (Node child in node.Children)
{
yield return child;
}
return GetChildNodes((INode)node).Cast<Node>();
}
/// <summary>
/// Functionally similar to the XPath axis 'child'
/// Make the imediate Children LINQ queryable
/// Performance optimised for just imediate children.
/// taken from: http://our.umbraco.org/wiki/how-tos/useful-helper-extension-methods-%28linq-null-safe-access%29
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <returns>INode as IEnumerable</returns>
public static IEnumerable<INode> GetChildNodes(this INode node)
{
return node.ChildrenAsList;
}
/// <summary>
@@ -203,7 +322,18 @@ namespace umbraco
/// <returns>Nodes as IEnumerable</returns>
public static IEnumerable<Node> GetChildNodes(this Node node, Func<Node, bool> func)
{
foreach (Node child in node.Children)
return GetChildNodes((INode)node, (Func<INode, bool>) func).Cast<Node>();
}
/// <summary>
/// Gets the child nodes.
/// </summary>
/// <param name="node">The <c>umbraco.interfaces.INode</c>.</param>
/// <param name="func">The func.</param>
/// <returns>INodes as IEnumerable</returns>
public static IEnumerable<INode> GetChildNodes(this INode node, Func<INode, bool> func)
{
foreach (INode child in node.ChildrenAsList)
{
if (func(child))
{
@@ -219,6 +349,17 @@ namespace umbraco
/// <param name="documentTypeAlias">The document type alias.</param>
/// <returns>Nodes as IEnumerable</returns>
public static IEnumerable<Node> GetChildNodesByType(this Node node, string documentTypeAlias)
{
return GetChildNodesByType((INode)node, documentTypeAlias).Cast<Node>();
}
/// <summary>
/// Gets the child nodes by document-type.
/// </summary>
/// <param name="node">The <c>umbraco.interfaces.INode</c>.</param>
/// <param name="documentTypeAlias">The document type alias.</param>
/// <returns>INodes as IEnumerable</returns>
public static IEnumerable<INode> GetChildNodesByType(this INode node, string documentTypeAlias)
{
return node.GetChildNodes(n => n.NodeTypeAlias == documentTypeAlias);
}
@@ -231,20 +372,18 @@ namespace umbraco
/// <returns>null or Node</returns>
public static Node GetChildNodeByName(this Node parentNode, string nodeName)
{
Node node = null;
return (Node) GetChildNodeByName((INode)parentNode, nodeName);
}
foreach (Node child in parentNode.Children)
{
if (child.Name == nodeName)
{
node = child;
break;
}
}
return node;
//// return node.GetChildNodes(n => n.Name == nodeName);
/// <summary>
/// Extension method on Node to retun a matching child node by name
/// </summary>
/// <param name="parentNode">an umbraco.interfaces.INode object</param>
/// <param name="nodeName">name of node to search for</param>
/// <returns>null or INode</returns>
public static INode GetChildNodeByName(this INode parentNode, string nodeName)
{
return parentNode.ChildrenAsList.FirstOrDefault(child => child.Name == nodeName);
}
/// <summary>
@@ -256,6 +395,19 @@ namespace umbraco
/// <c>true</c> if the specified node has property; otherwise, <c>false</c>.
/// </returns>
public static bool HasProperty(this Node node, string propertyAlias)
{
return HasProperty((INode)node, propertyAlias);
}
/// <summary>
/// Determines whether the specified node has property.
/// </summary>
/// <param name="node">The INode.</param>
/// <param name="propertyAlias">The property alias.</param>
/// <returns>
/// <c>true</c> if the specified node has property; otherwise, <c>false</c>.
/// </returns>
public static bool HasProperty(this INode node, string propertyAlias)
{
var property = node.GetProperty(propertyAlias);
return (property != null);
@@ -269,6 +421,18 @@ namespace umbraco
/// <param name="propertyAlias">alias of property to get</param>
/// <returns>default(T) or property cast to (T)</returns>
public static T GetProperty<T>(this Node node, string propertyAlias)
{
return GetProperty<T>((INode)node, propertyAlias);
}
/// <summary>
/// Get a value of type T from a property
/// </summary>
/// <typeparam name="T">type T to cast to</typeparam>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <param name="propertyAlias">alias of property to get</param>
/// <returns>default(T) or property cast to (T)</returns>
public static T GetProperty<T>(this INode node, string propertyAlias)
{
// check to see if return object handles it's own object hydration
if (typeof(uQuery.IGetProperty).IsAssignableFrom(typeof(T)))
@@ -282,6 +446,8 @@ namespace umbraco
return (T)t;
}
//TODO: This should be converted to our extension method TryConvertTo<T> ....
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
if (typeConverter != null)
{
@@ -343,6 +509,17 @@ namespace umbraco
/// <param name="propertyAlias">alias of propety to get</param>
/// <returns>empty string, or property value as string</returns>
private static string GetPropertyAsString(this Node node, string propertyAlias)
{
return GetPropertyAsString((INode)node, propertyAlias);
}
/// <summary>
/// Get a string value for the supplied property alias
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <param name="propertyAlias">alias of propety to get</param>
/// <returns>empty string, or property value as string</returns>
private static string GetPropertyAsString(this INode node, string propertyAlias)
{
var propertyValue = string.Empty;
var property = node.GetProperty(propertyAlias);
@@ -362,6 +539,17 @@ namespace umbraco
/// <param name="propertyAlias">alias of propety to get</param>
/// <returns>true if can cast value, else false for all other circumstances</returns>
private static bool GetPropertyAsBoolean(this Node node, string propertyAlias)
{
return GetPropertyAsBoolean((INode)node, propertyAlias);
}
/// <summary>
/// Get a boolean value for the supplied property alias (works with built in Yes/No dataype)
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <param name="propertyAlias">alias of propety to get</param>
/// <returns>true if can cast value, else false for all other circumstances</returns>
private static bool GetPropertyAsBoolean(this INode node, string propertyAlias)
{
var propertyValue = false;
var property = node.GetProperty(propertyAlias);
@@ -390,6 +578,18 @@ namespace umbraco
/// <param name="node"></param>
/// <returns></returns>
public static int Level(this Node node)
{
return Level((INode)node);
}
/// <summary>
/// Tell me the level of this node (0 = root)
/// updated from Depth and changed to start at 0
/// to align with other 'Level' methods (eg xslt)
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public static int Level(this INode node)
{
return node.Path.Split(',').Length - 1;
}
@@ -400,6 +600,16 @@ namespace umbraco
/// <param name="node">The node.</param>
/// <returns>Returns an <c>XmlNode</c> for the selected Node</returns>
public static XmlNode ToXml(this Node node)
{
return ToXml((INode) node);
}
/// <summary>
/// Gets the XML for the Node.
/// </summary>
/// <param name="node">The INode.</param>
/// <returns>Returns an <c>XmlNode</c> for the selected Node</returns>
public static XmlNode ToXml(this INode node)
{
return ((IHasXmlNode)umbraco.library.GetXmlNodeById(node.Id.ToString()).Current).GetNode();
}
@@ -412,6 +622,18 @@ namespace umbraco
/// <param name="cropName">name of crop to get url for</param>
/// <returns>emtpy string or url</returns>
public static string GetImageCropperUrl(this Node node, string propertyAlias, string cropName)
{
return GetImageCropperUrl((INode)node, propertyAlias, cropName);
}
/// <summary>
/// Returns the url for a given crop name using the built in Image Cropper datatype
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <param name="propertyAlias">property alias</param>
/// <param name="cropName">name of crop to get url for</param>
/// <returns>emtpy string or url</returns>
public static string GetImageCropperUrl(this INode node, string propertyAlias, string cropName)
{
string cropUrl = string.Empty;
@@ -453,6 +675,18 @@ namespace umbraco
/// <param name="value">value to set</param>
/// <returns>the same node object on which this is an extension method</returns>
public static Node SetProperty(this Node node, string propertyAlias, object value)
{
return (Node) SetProperty((INode)node, propertyAlias, value);
}
/// <summary>
/// Sets a property value on this node
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <param name="propertyAlias">alias of property to set</param>
/// <param name="value">value to set</param>
/// <returns>the same node object on which this is an extension method</returns>
public static INode SetProperty(this INode node, string propertyAlias, object value)
{
var document = new Document(node.Id);
@@ -468,6 +702,17 @@ namespace umbraco
/// <param name="useAdminUser">if true then publishes under the context of User(0), if false uses current user</param>
/// <returns>the same node object on which this is an extension method</returns>
public static Node Publish(this Node node, bool useAdminUser)
{
return (Node) Publish((INode)node, useAdminUser);
}
/// <summary>
/// Republishes this node
/// </summary>
/// <param name="node">an umbraco.interfaces.INode object</param>
/// <param name="useAdminUser">if true then publishes under the context of User(0), if false uses current user</param>
/// <returns>the same node object on which this is an extension method</returns>
public static INode Publish(this INode node, bool useAdminUser)
{
var document = new Document(node.Id);
@@ -484,6 +729,18 @@ namespace umbraco
/// Returns a random node from a collection of nodes.
/// </returns>
public static Node GetRandom(this IList<Node> nodes)
{
return (Node)GetRandom(nodes.Cast<INode>().ToList());
}
/// <summary>
/// Gets a random node.
/// </summary>
/// <param name="nodes">The nodes.</param>
/// <returns>
/// Returns a random INode from a collection of INodes.
/// </returns>
public static INode GetRandom(this IList<INode> nodes)
{
var random = umbraco.library.GetRandom();
return nodes[random.Next(0, nodes.Count)];
@@ -499,7 +756,21 @@ namespace umbraco
/// </returns>
public static IEnumerable<Node> GetRandom(this IList<Node> nodes, int numberOfItems)
{
var output = new List<Node>(numberOfItems);
var randomNodes = GetRandom(nodes.Cast<INode>().ToList(), numberOfItems);
return randomNodes.Cast<Node>().ToList();
}
/// <summary>
/// Gets a collection of random nodes.
/// </summary>
/// <param name="nodes">The nodes.</param>
/// <param name="numberOfItems">The number of items.</param>
/// <returns>
/// Returns the specified number of random INodes from a collection of INodes.
/// </returns>
public static IEnumerable<INode> GetRandom(this IList<INode> nodes, int numberOfItems)
{
var output = new List<INode>(numberOfItems);
while (output.Count < numberOfItems)
{
@@ -519,6 +790,16 @@ namespace umbraco
/// <param name="node">The node.</param>
/// <returns></returns>
public static string GetFullNiceUrl(this Node node)
{
return GetFullNiceUrl((INode)node);
}
/// <summary>
/// Gets the full nice URL.
/// </summary>
/// <param name="node">The node as INode.</param>
/// <returns></returns>
public static string GetFullNiceUrl(this INode node)
{
if (!string.IsNullOrEmpty(node.NiceUrl) && node.NiceUrl[0] == '/')
{
@@ -535,6 +816,17 @@ namespace umbraco
/// <param name="language">The language.</param>
/// <returns></returns>
public static string GetFullNiceUrl(this Node node, string language)
{
return GetFullNiceUrl((INode)node, language);
}
/// <summary>
/// Gets the full nice URL.
/// </summary>
/// <param name="node">The node as INode.</param>
/// <param name="language">The language.</param>
/// <returns></returns>
public static string GetFullNiceUrl(this INode node, string language)
{
return node.GetFullNiceUrl(language, false);
}
@@ -547,6 +839,18 @@ namespace umbraco
/// <param name="ssl">if set to <c>true</c> [SSL].</param>
/// <returns></returns>
public static string GetFullNiceUrl(this Node node, string language, bool ssl)
{
return GetFullNiceUrl((INode)node, language, ssl);
}
/// <summary>
/// Gets the full nice URL.
/// </summary>
/// <param name="node">The node as INode.</param>
/// <param name="language">The language.</param>
/// <param name="ssl">if set to <c>true</c> [SSL].</param>
/// <returns></returns>
public static string GetFullNiceUrl(this INode node, string language, bool ssl)
{
foreach (var domain in library.GetCurrentDomains(node.Id))
{
@@ -566,6 +870,17 @@ namespace umbraco
/// <param name="domain">The domain.</param>
/// <returns></returns>
public static string GetFullNiceUrl(this Node node, Domain domain)
{
return GetFullNiceUrl((INode)node, domain);
}
/// <summary>
/// Gets the full nice URL.
/// </summary>
/// <param name="node">The node as INode.</param>
/// <param name="domain">The domain.</param>
/// <returns></returns>
public static string GetFullNiceUrl(this INode node, Domain domain)
{
return node.GetFullNiceUrl(domain, false);
}
@@ -578,6 +893,18 @@ namespace umbraco
/// <param name="ssl">if set to <c>true</c> [SSL].</param>
/// <returns></returns>
public static string GetFullNiceUrl(this Node node, Domain domain, bool ssl)
{
return GetFullNiceUrl((INode)node, domain, ssl);
}
/// <summary>
/// Gets the full nice URL.
/// </summary>
/// <param name="node">The node as INode.</param>
/// <param name="domain">The domain.</param>
/// <param name="ssl">if set to <c>true</c> [SSL].</param>
/// <returns></returns>
public static string GetFullNiceUrl(this INode node, Domain domain, bool ssl)
{
// TODO: [OA] Document on Codeplex
if (!string.IsNullOrEmpty(node.NiceUrl) && node.NiceUrl[0] == '/')
@@ -590,6 +917,7 @@ namespace umbraco
#pragma warning disable 0618
/// <summary>
/// Converts legacy nodeFactory collection to INode collection
/// </summary>