/* * Creator: Stefan Kip * © InfoCaster 2010 */ using System; using System.Collections.Generic; using System.Linq; using System.Web; using umbraco.presentation.nodeFactory; using umbraco.cms.businesslogic.web; namespace InfoCaster.Umbraco.Library { public static class UmbracoExtensions { public static string DefaultMLLanguage { get; set; } public static string MLDelimiter { get; set; } /// /// Returns the child nodes as IEnumerable collection /// /// The parent node /// The child nodes as IEnumerable public static IEnumerable GetChildNodes(this Node node) { return node.Children.OfType(); } /// /// Returns the value (not null and empty) of the property with the given alias recursively /// /// The node /// The alias of the property /// The property's value public static string GetPropertyValueRecursive(this Node node, string propertyAlias) { if (node.GetProperty(propertyAlias) != null && !string.IsNullOrEmpty(node.GetProperty(propertyAlias).Value)) return node.GetProperty(propertyAlias).Value; if (node.Parent != null) return GetPropertyValueRecursive(node.Parent, propertyAlias); return string.Empty; } /// /// Returns the multilanguage value (not null and empty) of the property with the given alias recursively /// /// The node /// The alias of the property /// The two-letter language code (e.g. 'en', 'nl' or 'de') /// The property's value public static string GetPropertyValueRecursive(this Node node, string propertyAlias, string lang) { UpdatePropertyAlias(ref propertyAlias, lang); return node.GetPropertyValueRecursive(propertyAlias); } /// /// Returns the value of the property with the given alias or @default /// /// The node /// The alias of the property /// The alternative default value delegate /// The property's value or @default if empty public static string GetPropertyValueOrDefault(this Node node, string propertyAlias, Func @default) { if (node.GetProperty(propertyAlias) != null && !string.IsNullOrEmpty(node.GetProperty(propertyAlias).Value)) return node.GetProperty(propertyAlias).Value; if (@default != null) return @default(); return null; } /// /// Returns the multilanguage value of the property with the given alias or @default /// /// The node /// The alias of the property /// The alternative default value delegate /// The two-letter language code (e.g. 'en', 'nl' or 'de') /// The property's value or @default if empty public static string GetPropertyValueOrDefault(this Node node, string propertyAlias, Func @default, string lang) { UpdatePropertyAlias(ref propertyAlias, lang); return node.GetPropertyValueOrDefault(propertyAlias, @default); } /// /// Returns the value of the property with the given alias or @default /// /// The node /// The alias of the property /// The alternative default value /// The property's value or @default if empty public static string GetPropertyValueOrDefault(this Node node, string propertyAlias, string @default) { if (node.GetProperty(propertyAlias) != null && !string.IsNullOrEmpty(node.GetProperty(propertyAlias).Value)) return node.GetProperty(propertyAlias).Value; return @default; } /// /// Returns the multilanguage value of the property with the given alias or @default /// /// The node /// The alias of the property /// The alternative default value /// The two-letter language code (e.g. 'en', 'nl' or 'de') /// The property's value or @default if empty public static string GetPropertyValueOrDefault(this Node node, string propertyAlias, string @default, string lang) { UpdatePropertyAlias(ref propertyAlias, lang); return node.GetPropertyValueOrDefault(propertyAlias, @default); } /// /// Returns the value of the property with the given alias /// /// The node /// The alias of the property /// The property's value public static string GetPropertyValue(this Node node, string propertyAlias) { if (node.GetProperty(propertyAlias) != null && !string.IsNullOrEmpty(node.GetProperty(propertyAlias).Value)) return node.GetProperty(propertyAlias).Value; return string.Empty; } /// /// Returns the multilanguage value of the property with the given alias /// /// The node /// The alias of the property /// The two-letter language code (e.g. 'en', 'nl' or 'de') /// The property's value public static string GetPropertyValue(this Node node, string propertyAlias, string lang) { UpdatePropertyAlias(ref propertyAlias, lang); return node.GetPropertyValue(propertyAlias); } /// /// Checks if a target node is in the path of the current node /// /// The current node /// The target node /// True if the target node exists in the current node's path public static bool ContainsNodeInPath(this Node node, Node target) { if (target == null) throw new ArgumentNullException("target"); Node current = node; while (current != null) { if (target.Id == current.Id) return true; current = current.Parent; } return false; } /// /// Saves and publishes a document /// /// The document to save and publish public static void SaveAndPublish(this Document document) { if (document != null) { document.Save(); document.Publish(document.User); umbraco.library.UpdateDocumentCache(document.Id); } } /// /// Checks if the DefaultMLLanguage is set and updates the propertyAlias /// /// The updated propertyAlias /// DefaultMLLanguage is not set static void UpdatePropertyAlias(ref string propertyAlias, string lang) { if (string.IsNullOrEmpty(DefaultMLLanguage)) throw new InvalidOperationException("DefaultMLLanguage has to be set first"); if (lang != DefaultMLLanguage) propertyAlias = string.Format("{0}{1}{2}", propertyAlias, !string.IsNullOrEmpty(MLDelimiter) ? MLDelimiter : "_", lang); } } }