Instantly share code, notes, and snippets.
Created
February 4, 2013 17:25
-
Save origamirobot/4708147 to your computer and use it in GitHub Desktop.
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 Sitecore.Data.Fields; | |
using Sitecore.Data.Items; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using System.Web; | |
namespace Heinz.Opal.Custom.OreIda.Controls | |
{ | |
/// <summary> | |
/// | |
/// </summary> | |
public static class BreadcrumbManager | |
{ | |
/// <summary> | |
/// Builds the breadcrumb from item. | |
/// </summary> | |
/// <param name="item">The item to build a breadcrumb from.</param> | |
/// <param name="fieldName">Name of the field on the current item to start the breadcrumb.</param> | |
/// <returns></returns> | |
public static BreadcrumbList BuildBreadcrumbFromItem(Item item) | |
{ | |
if (item == null) | |
throw new ArgumentNullException("The item parameter cant be null"); | |
BreadcrumbList list = new BreadcrumbList(); | |
Item homeItem = CurrentDB.GetItem(Sitecore.Context.Site.StartPath); | |
Item rootNav = CurrentDB.GetItem(OreIda.Data.TemplateIDs.NavRoot); | |
Item navItem = rootNav.Axes.SelectSingleItem(String.Format(".//*[@SourceNode = '{0}']", item.ID)); | |
while (navItem.TemplateID != OreIda.Data.TemplateIDs.NavSection && navItem.TemplateID != homeItem.TemplateID) | |
{ | |
bool excludeFromNav = ((CheckboxField)navItem.Fields["ExcludeFromNavigation"]).Checked; | |
if (!excludeFromNav) | |
{ | |
Item src = CurrentDB.GetItem(new Sitecore.Data.ID(navItem.Fields["SourceNode"].Value)); | |
list.Add(new BreadcrumbItem(navItem.Name, GetTitleFromNavItem(navItem), OreIda.LinkManager.GetLink(src))); | |
} | |
navItem = navItem.Parent; | |
if (navItem.TemplateID == OreIda.Data.TemplateIDs.NavSection) | |
{ | |
list.NavItemName = navItem.Name; | |
} | |
} | |
return list; | |
} | |
/// <summary> | |
/// Gets the title of the specified nav Item. | |
/// </summary> | |
/// <param name="navItem">The nav item to get the title for.</param> | |
/// <returns></returns> | |
private static String GetTitleFromNavItem(Item navItem) | |
{ | |
return (navItem.Fields["Title"] == null || String.IsNullOrWhiteSpace(navItem.Fields["Title"].Value)) ? navItem.Fields["Name"].Value : navItem.Fields["Title"].Value; | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public class BreadcrumbList : List<BreadcrumbItem> | |
{ | |
/// <summary> | |
/// Gets a value indicating whether this instance has nav item. | |
/// </summary> | |
public bool HasNavItem | |
{ | |
get { return !String.IsNullOrEmpty(NavItemName); } | |
} | |
/// <summary> | |
/// Gets or sets the name of the nav item. | |
/// </summary> | |
public String NavItemName { get; set; } | |
/// <summary> | |
/// Gets the trail of this breadcrumb list. | |
/// </summary> | |
public String Trail | |
{ | |
get { return JoinTrail(this); } | |
} | |
/// <summary> | |
/// Joins the trail provided into a single string delimited by underscores. | |
/// </summary> | |
/// <param name="trail">The trail to join together.</param> | |
/// <returns></returns> | |
public static String JoinTrail(List<String> trail) | |
{ | |
return String.Join("_", trail); | |
} | |
/// <summary> | |
/// Joins the trail provided into a single string delimited by underscores. | |
/// </summary> | |
/// <param name="trail">The trail to join together.</param> | |
/// <returns></returns> | |
public static String JoinTrail(BreadcrumbList trail) | |
{ | |
List<String> list = new List<string>(); | |
foreach (BreadcrumbItem item in trail) | |
list.Add(item.Name); | |
return JoinTrail(list); | |
} | |
/// <summary> | |
/// Gets joins all the items in this breadcrumb list and joins them together. | |
/// </summary> | |
/// <returns></returns> | |
public String GetFullTrail() | |
{ | |
return JoinTrail(this.ConvertAll(x => x.Name).ToList<String>()); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public class BreadcrumbItem | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="BreadcrumbItem" /> class. | |
/// </summary> | |
public BreadcrumbItem(String name, String title, String link) | |
{ | |
Name = name; | |
Title = title; | |
Link = link; | |
} | |
/// <summary> | |
/// Gets or sets the name of the item in the tree in sitecore. | |
/// </summary> | |
public String Name { get; set; } | |
/// <summary> | |
/// Gets or sets the title for display purposes. | |
/// </summary> | |
public String Title { get; set; } | |
/// <summary> | |
/// Gets or sets the url to link to. | |
/// </summary> | |
public String Link { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment