using System.Collections.Generic; using System.Linq; using System.Web; using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Web { /// /// Provides extension methods for IPublishedItem. /// public static class PublishedFragmentExtensions { #region IsComposedOf /// /// Gets a value indicating whether the content is of a content type composed of the given alias /// /// The content. /// The content type alias. /// A value indicating whether the content is of a content type composed of a content type identified by the alias. public static bool IsComposedOf(this IPublishedFragment content, string alias) { return content.ContentType.CompositionAliases.Contains(alias); } #endregion #region HasProperty /// /// Gets a value indicating whether the content has a property identified by its alias. /// /// The content. /// The property alias. /// A value indicating whether the content has the property identified by the alias. /// The content may have a property, and that property may not have a value. public static bool HasProperty(this IPublishedFragment content, string alias) { return content.ContentType.GetPropertyType(alias) != null; } #endregion #region HasValue /// /// Gets a value indicating whether the content has a value for a property identified by its alias. /// /// The content. /// The property alias. /// A value indicating whether the content has a value for the property identified by the alias. /// Returns true if GetProperty(alias) is not null and GetProperty(alias).HasValue is true. public static bool HasValue(this IPublishedFragment content, string alias) { var prop = content.GetProperty(alias); return prop != null && prop.HasValue; } /// /// Returns one of two strings depending on whether the content has a value for a property identified by its alias. /// /// The content. /// The property alias. /// The value to return if the content has a value for the property. /// The value to return if the content has no value for the property. /// Either or depending on whether the content /// has a value for the property identified by the alias. public static IHtmlString HasValue(this IPublishedFragment content, string alias, string valueIfTrue, string valueIfFalse = null) { return content.HasValue(alias) ? new HtmlString(valueIfTrue) : new HtmlString(valueIfFalse ?? string.Empty); } #endregion #region GetPropertyValue /// /// Gets the value of a content's property identified by its alias. /// /// The content. /// The property alias. /// The value of the content's property identified by the alias. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, returns null. /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static object GetPropertyValue(this IPublishedFragment content, string alias) { var property = content.GetProperty(alias); return property?.Value; } /// /// Gets the value of a content's property identified by its alias, if it exists, otherwise a default value. /// /// The content. /// The property alias. /// The default value. /// The value of the content's property identified by the alias, if it exists, otherwise a default value. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, returns . /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static object GetPropertyValue(this IPublishedFragment content, string alias, string defaultValue) { var property = content.GetProperty(alias); return property == null || property.HasValue == false ? defaultValue : property.Value; } /// /// Gets the value of a content's property identified by its alias, if it exists, otherwise a default value. /// /// The content. /// The property alias. /// The default value. /// The value of the content's property identified by the alias, if it exists, otherwise a default value. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, returns . /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static object GetPropertyValue(this IPublishedFragment content, string alias, object defaultValue) { var property = content.GetProperty(alias); return property == null || property.HasValue == false ? defaultValue : property.Value; } #endregion #region GetPropertyValue /// /// Gets the value of a content's property identified by its alias, converted to a specified type. /// /// The target property type. /// The content. /// The property alias. /// The value of the content's property identified by the alias, converted to the specified type. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns default(T). /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static T GetPropertyValue(this IPublishedFragment content, string alias) { return content.GetPropertyValue(alias, false, default(T)); } /// /// Gets the value of a content's property identified by its alias, converted to a specified type, if it exists, otherwise a default value. /// /// The target property type. /// The content. /// The property alias. /// The default value. /// The value of the content's property identified by the alias, converted to the specified type, if it exists, otherwise a default value. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns . /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static T GetPropertyValue(this IPublishedFragment content, string alias, T defaultValue) { return content.GetPropertyValue(alias, true, defaultValue); } internal static T GetPropertyValue(this IPublishedFragment content, string alias, bool withDefaultValue, T defaultValue) { var property = content.GetProperty(alias); if (property == null) return defaultValue; return property.GetValue(withDefaultValue, defaultValue); } #endregion #region ToIndexedArray public static IndexedArrayItem[] ToIndexedArray(this IEnumerable source) where TContent : class, IPublishedFragment { var set = source.Select((content, index) => new IndexedArrayItem(content, index)).ToArray(); foreach (var setItem in set) setItem.TotalCount = set.Length; return set; } #endregion #region OfTypes // the .OfType() filter is nice when there's only one type // this is to support filtering with multiple types public static IEnumerable OfTypes(this IEnumerable contents, params string[] types) where T : IPublishedFragment { types = types.Select(x => x.ToLowerInvariant()).ToArray(); return contents.Where(x => types.Contains(x.ContentType.Alias.ToLowerInvariant())); } #endregion } }