Created
August 28, 2016 20:32
-
-
Save sotirisf/b05338c3df2baf90d6e8d97288fc45b0 to your computer and use it in GitHub Desktop.
Umbraco Picker Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web; | |
using Umbraco.Web; | |
namespace DotSee.UmbracoExtensions | |
{ | |
/// <summary> | |
/// Helps with Umbraco content. | |
/// </summary> | |
public static class ContentHelper | |
{ | |
#region Constants | |
private const string UmbracoHelperKey = "umbHelper"; | |
#endregion | |
#region Methods | |
/// <summary> | |
/// Gets an UmbracoHelper. | |
/// </summary> | |
/// <returns>An UmbracoHelper.</returns> | |
/// <remarks> | |
/// This is optimized to create only one UmbracoHelper per HTTP request. | |
/// </remarks> | |
public static UmbracoHelper GetHelper() | |
{ | |
var items = HttpContext.Current.Items; | |
var exists = items.Contains(UmbracoHelperKey); | |
var helper = exists | |
? items[UmbracoHelperKey] as UmbracoHelper | |
: new UmbracoHelper(UmbracoContext.Current); | |
if (!exists) | |
{ | |
items[UmbracoHelperKey] = helper; | |
} | |
return helper; | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
namespace DotSee.UmbracoExtensions | |
{ | |
public static class Pickers | |
{ | |
public enum Ordering | |
{ | |
NaturalOrder = 1, | |
OrderAscending = 2, | |
OrderDescending = 3 | |
} | |
/// <summary> | |
/// Gets a list of picker items as an optionally ordered delimited string (for presentation) | |
/// </summary> | |
/// <param name="item">The node which contains the picker</param> | |
/// <param name="pickerAlias">The picker alias</param> | |
/// <param name="delimiter">Delimeter to use Optional, default is comma</param> | |
/// <param name="recurse">Recurse to parent items</param> | |
/// <param name="order">A value from the Ordering enum specifying natural order, ascending or descending order</param> | |
/// <returns></returns> | |
public static string GetPickerItemsAsCsv(this IPublishedContent item, string pickerAlias, string delimiter = ", ", bool recurse = false, Ordering order = Ordering.OrderAscending) | |
{ | |
//No value? Go home :) | |
if (!item.HasValue(pickerAlias)) { return null; } | |
//Get the picker items as IPublishedContent objects | |
IEnumerable<IPublishedContent> pickerItems = item.GetPickerItemsByAlias(pickerAlias, recurse); | |
//No items? Go home :) | |
if (!pickerItems.Any()) { return null; } | |
//Sort them and deliver them | |
IEnumerable<string> itemNames = pickerItems.Select(x => x.Name); | |
if (order == Ordering.OrderAscending) | |
{ | |
itemNames = itemNames.OrderBy(x => x); | |
} | |
else if (order == Ordering.OrderDescending) | |
{ | |
itemNames = itemNames.OrderByDescending(x => x); | |
} | |
return (string.Join(delimiter, itemNames)); | |
} | |
/// <summary> | |
/// Check if a node's id is contained in at least one picker within a group of nodes | |
/// </summary> | |
/// <param name="item">The node to check</param> | |
/// <param name="itemGroup">The group of nodes to check the picker for</param> | |
/// <param name="pickerAlias">The picker's alias</param> | |
/// <returns></returns> | |
public static bool IsContainedInAnyPicker(this IPublishedContent item, IEnumerable<IPublishedContent> itemGroup, string pickerAlias) | |
{ | |
foreach (IPublishedContent gItem in itemGroup) | |
{ | |
if (gItem.HasValue(pickerAlias) && item.Id.ToString().IsContainedInCsv(gItem.GetPropertyValue<string>(pickerAlias))) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
/// <summary> | |
/// Get strongly typed (intended for Models Builder) objects from picker values based on a picker alias | |
/// </summary> | |
/// <param name="item">The node which contains the picker</param> | |
/// <param name="pickerAlias">The alias of the property containing the picker</param> | |
/// /// <param name="recurse">If true, recurse to parent nodes</param> | |
/// <returns></returns> | |
public static IEnumerable<T> GetPickerItemsByAlias<T>(this IPublishedContent item, string pickerAlias, bool recurse = false) where T : IPublishedContent | |
{ | |
//Get a request-cached UmbracoHelper | |
UmbracoHelper u = ContentHelper.GetHelper(); | |
//If recursion is on and current item has no banners AND it's not the first node under the homepage then recurse. | |
//CHANGE THE "HomePage" STRING TO YOUR HOMEPAGE DOCTYPE ALIAS! | |
if (recurse && !item.HasValue(pickerAlias) && !item.Ancestor().DocumentTypeAlias.Equals("HomePage")) | |
{ | |
IEnumerable<T> retVals = item.Parent.GetPickerItemsByAlias<T>(pickerAlias, recurse); | |
foreach (T it in retVals) | |
{ | |
if (it != null) { yield return it; } | |
} | |
} | |
else | |
{ | |
if (!item.HasValue(pickerAlias)) { yield break; } | |
foreach (string pageId in item.GetPropertyValue<string>(pickerAlias, "").Split(',')) | |
{ | |
var it = (T)u.TypedContent(pageId); | |
if (it != null) { yield return it; } | |
} | |
} | |
} | |
/// <summary> | |
/// Get IPublishedContent objects from picker values based on a picker alias | |
/// </summary> | |
/// <param name="item">The node that contains the picker</param> | |
/// <param name="pickerAlias">The alias of the property containing the picker</param> | |
/// <param name="recurse">If true, recurse to parent nodes</param> | |
/// <returns></returns> | |
public static IEnumerable<IPublishedContent> GetPickerItemsByAlias(this IPublishedContent item, string pickerAlias, bool recurse = false) | |
{ | |
return (GetPickerItemsByAlias<IPublishedContent>(item, pickerAlias, recurse)); | |
} | |
/// <summary> | |
/// Get IPublishedContent object from a picker delimited string value | |
/// </summary> | |
/// <param name="item">The node that contains the picker</param> | |
/// <param name="pickerValue">The value of the picker (a comma delimited string of IDs)</param> | |
/// <returns></returns> | |
public static IEnumerable<IPublishedContent> GetPickerItemsByValue(this IPublishedContent item, object pickerValue) | |
{ | |
return (GetPickerItemsByValue<IPublishedContent>(item, pickerValue)); | |
} | |
/// <summary> | |
/// Get strongly-typed (intended for Models Builder) objects from a picker's delimited string value | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="item">The node that contains the picker</param> | |
/// <param name="pickerValue">The value of the picker</param> | |
/// <returns></returns> | |
public static IEnumerable<T> GetPickerItemsByValue<T>(this IPublishedContent item, object pickerValue) where T : IPublishedContent | |
{ | |
//Get a request-cached UmbracoHelper | |
UmbracoHelper u = ContentHelper.GetHelper(); | |
//No value? Go home :) | |
if (pickerValue == null) { yield break; } | |
foreach (string pageId in pickerValue.ToString().Split(',')) | |
{ | |
var it = (T)u.TypedContent(pageId); | |
if (it != null) { yield return it; } | |
} | |
} | |
/// <summary> | |
/// Get a typed media Ienumerable from a media picker | |
/// </summary> | |
/// <param name="item">The item that contains the picker</param> | |
/// <param name="pickerAlias">The alias of the property containing the picker</param> | |
/// <param name="recurse">If true, recurse to parent nodes</param> | |
/// <returns></returns> | |
public static IEnumerable<IPublishedContent> GetPickerMediaByAlias(this IPublishedContent item, string pickerAlias, bool recurse = false) | |
{ | |
if (!item.HasValue(pickerAlias)) | |
{ | |
if (recurse && item.Level > 1) | |
{ | |
foreach (var it in GetPickerMediaByAlias(item.Parent, pickerAlias, recurse)) | |
{ | |
if (it != null) { yield return it; } | |
} | |
} | |
else | |
{ | |
yield break; | |
} | |
} | |
//Get a request-cached UmbracoHelper | |
UmbracoHelper u = ContentHelper.GetHelper(); | |
foreach (string imageId in item.GetPropertyValue<string>(pickerAlias, "").Split(',')) | |
{ | |
var it = u.TypedMedia(imageId); | |
if (it != null) { yield return it; } | |
} | |
} | |
/// <summary> | |
/// Gets a typed media IEnumerable from a media picker value | |
/// </summary> | |
/// <param name="item"></param> | |
/// <param name="pickerValue"></param> | |
/// <returns></returns> | |
public static IEnumerable<IPublishedContent> GetPickerMediaByValue(this IPublishedContent item, object pickerValue) | |
{ | |
//No value? Go home :) | |
if (pickerValue == null) { yield break; } | |
//Get a request-cached UmbracoHelper | |
UmbracoHelper u = ContentHelper.GetHelper(); | |
foreach (string imageId in pickerValue.ToString().Split(',')) | |
{ | |
yield return u.TypedMedia(imageId); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment