/// <summary> /// Determines whether the item is a year, month or day item. /// </summary> /// <param name="item">The item.</param> /// <param name="config">The config.</param> /// <returns> /// <c>true</c> if [is item year month or day] [the specified item]; otherwise, <c>false</c>. /// </returns> private bool IsItemYearMonthOrDay(Item item, TemplateConfiguration config) { return item.TemplateID == config.YearFolder.Template.ID || (config.MonthFolder != null && item.TemplateID == config.MonthFolder.Template.ID) || (config.DayFolder != null && item.TemplateID == config.DayFolder.Template.ID); }
/// <summary> /// Loads the legacy settings. /// </summary> protected void LoadLegacySettings() { // The original version of this module supported a single template. // we don't want to break this so lets convert the legacy settings into // our new TemplateConfiguration. // we only want to do this once since this class is instantiated once by Sitecore // and kept around for all usages (i.e. some sort of singleton) if (!_legacyConfigLoaded) { if (HasLegacyConfiguration) { // create a new wrapper around the old config var config = new TemplateConfiguration(SitecoreDatabase, ArticleTemplate, DateField, YearTemplate, MonthTemplate, DayTemplate); Templates.Add(config.Template.ID, config); } _legacyConfigLoaded = true; } }
/// <summary> /// Organizes the item in the configurd year, [month], [day] structure. /// It will also remove any empty folders /// </summary> /// <param name="item">The item.</param> /// <param name="config">The config.</param> /// <param name="articleDate">The article date.</param> protected void OrganizeItem(Item item, TemplateConfiguration config, DateTime articleDate) { Item root = GetRoot(item, config); // get/create the year folder root = GetOrCreateChild(root, config.YearFolder.Template, config.YearFolder.GetName(articleDate), config.SortOrder); // get/create any month -> day structure we need if (config.MonthFolder != null) { root = GetOrCreateChild(root, config.MonthFolder.Template, config.MonthFolder.GetName(articleDate), config.SortOrder); if (config.DayFolder != null) { root = GetOrCreateChild(root, config.DayFolder.Template, config.DayFolder.GetName(articleDate), config.SortOrder); } } // if the item is already where it should be, then bail out if (string.Equals(item.Parent.Paths.FullPath, root.Paths.FullPath, StringComparison.OrdinalIgnoreCase)) { return; } // save the original location so we can clean up Item originalParent = item.Parent; // move the item to the proper location item.MoveTo(root); // delete the original parent if there are no children. // keep walking up while we are a year/month/day while ((!originalParent.HasChildren) && IsItemYearMonthOrDay(originalParent, config)) { Item parent = originalParent.Parent; originalParent.Delete(); originalParent = parent; } if ((!Sitecore.Context.IsBackgroundThread) && Sitecore.Context.ClientPage.IsEvent) { var args = new MoveCompletedArgs() { Article = item, Root = item.Database.GetRootItem() }; CorePipeline.Run("NewsMover.MoveCompleted", args); } }
/// <summary> /// Gets the root of where we start organization. /// i.e. the parent of the 'year' node /// </summary> /// <param name="item">The item.</param> /// <param name="config">The config.</param> /// <returns></returns> protected Item GetRoot(Item item, TemplateConfiguration config) { Item parent = item.Parent; while (Templates.ContainsKey(parent.TemplateID) || IsItemYearMonthOrDay(parent, config)) { parent = parent.Parent; } // enforce that sub-item sorting is set if (config.SortOrder != SortOrder.None && parent[FieldIDs.SubitemsSorting] != config.SortOrder.ToDescription()) { using (new Sitecore.Data.Items.EditContext(parent)) { parent.Fields[FieldIDs.SubitemsSorting].Value = config.SortOrder.ToDescription(); } } return parent; }