/// <summary>Shorthand for resolving a node adapter.</summary> /// <param name="engine">Used to resolve the provider.</param> /// <param name="item">The item whose adapter to get.</param> /// <returns>The most relevant adapter.</returns> internal static NodeAdapter GetNodeAdapter(this IEngine engine, ContentItem item) { return(engine.GetContentAdapter <NodeAdapter>(item)); }
/// <summary>Invokes an event and and executes an action unless the event is cancelled.</summary> /// <param name="handler">The event handler to signal.</param> /// <param name="source">The item affected by this operation.</param> /// <param name="destination">The destination of this operation.</param> /// <param name="sender">The source of the event.</param> /// <param name="finalAction">The default action to execute if the event didn't signal cancel.</param> /// <returns>The result of the action (if any).</returns> public static ContentItem InvokeEvent(EventHandler <CancellableDestinationEventArgs> handler, object sender, ContentItem source, ContentItem destination, Func <ContentItem, ContentItem, ContentItem> finalAction, EventHandler <DestinationEventArgs> postHandler) { if (handler != null && (VersionsTriggersEvents || !source.VersionOf.HasValue)) { CancellableDestinationEventArgs args = new CancellableDestinationEventArgs(source, destination, finalAction); handler.Invoke(sender, args); if (args.Cancel) { return(null); } args.AffectedItem = args.FinalAction(args.AffectedItem, args.Destination); if (postHandler != null) { postHandler(sender, args); } return(args.AffectedItem); } var result2 = finalAction(source, destination); if (postHandler != null) { postHandler(sender, new DestinationEventArgs(result2, destination)); } return(result2); }
/// <summary>Shorthand for resolving an adapter.</summary> /// <typeparam name="T">The type of adapter to get.</typeparam> /// <param name="engine">Used to resolve the provider.</param> /// <param name="item">The item whose adapter to get.</param> /// <returns>The most relevant adapter.</returns> internal static T GetContentAdapter <T>(this IEngine engine, ContentItem item) where T : AbstractContentAdapter { return(engine.Resolve <IContentAdapterProvider>().ResolveAdapter <T>(item)); }
/// <summary>Inserts an item among a parent item's children using a comparer to determine the location.</summary> /// <param name="item">The item to insert.</param> /// <param name="newParent">The parent item.</param> /// <param name="sortExpression">The sort expression to use.</param> /// <returns>The index of the item among it's siblings.</returns> public static int Insert(ContentItem item, ContentItem newParent, string sortExpression) { return(Insert(item, newParent, new Collections.ItemComparer(sortExpression))); }
/// <summary>Invokes an event and and executes an action unless the event is cancelled.</summary> /// <param name="preHandlers">The event handler to signal.</param> /// <param name="item">The item affected by this operation.</param> /// <param name="sender">The source of the event.</param> /// <param name="finalAction">The default action to execute if the event didn't signal cancel.</param> public static void InvokeEvent(EventHandler <CancellableItemEventArgs> preHandlers, ContentItem item, object sender, Action <ContentItem> finalAction, EventHandler <ItemEventArgs> postHandlers) { if (preHandlers != null && (VersionsTriggersEvents || !item.VersionOf.HasValue)) { CancellableItemEventArgs args = new CancellableItemEventArgs(item, finalAction); preHandlers.Invoke(sender, args); if (!args.Cancel) { args.FinalAction(args.AffectedItem); if (postHandlers != null) { postHandlers(sender, args); } } } else { finalAction(item); if (postHandlers != null) { postHandlers(sender, new ItemEventArgs(item)); } } }
/// <summary>Iterates items and ensures that the item's sort order is ascending.</summary> /// <param name="siblings">The items to iterate.</param> /// <returns>A list of items whose sort order was changed.</returns> public static IEnumerable <ContentItem> UpdateSortOrder(IEnumerable <ContentItem> siblings) { List <ContentItem> updatedItems = new List <ContentItem>(); int lastSortOrder = int.MinValue; int sortOrderBeforeLast = int.MinValue; ContentItem last = null; bool lastSortOrderChanged = false; int index = 0; foreach (ContentItem current in siblings) { if (current.SortOrder <= lastSortOrder) { int gapBeforeLast = (int)Math.Min((long)lastSortOrder - sortOrderBeforeLast, int.MaxValue); int gapBeforeCurrent = (int)Math.Min((long)current.SortOrder - sortOrderBeforeLast, int.MaxValue); if (gapBeforeLast > 1 && gapBeforeCurrent > 2) { if (index == 1) { // we added something first with a higher sortorder than the next last.SortOrder = lastSortOrder = current.SortOrder - 10; updatedItems.Add(last); } else if (current.SortOrder > sortOrderBeforeLast) { // 0 // -1b last.SortOrder = lastSortOrder = current.SortOrder - (current.SortOrder - sortOrderBeforeLast) / 2; updatedItems.Add(last); } else { // 0 // -2b last.SortOrder = lastSortOrder = sortOrderBeforeLast + 1; updatedItems.Add(last); current.SortOrder = sortOrderBeforeLast + 2; updatedItems.Add(current); lastSortOrderChanged = true; } } else { current.SortOrder = lastSortOrder + 1; updatedItems.Add(current); lastSortOrderChanged = true; } } else { lastSortOrderChanged = false; } sortOrderBeforeLast = lastSortOrder; lastSortOrder = current.SortOrder; last = current; index++; } if (lastSortOrderChanged) { last.SortOrder += 9; } return(updatedItems); }
/// <summary>Moves an item in a list to a new index.</summary> /// <param name="siblings">A list of items where the item to move is listed.</param> /// <param name="itemToMove">The item that should be moved (must be in the list)</param> /// <param name="newIndex">The new index onto which to place the item.</param> /// <remarks>To persist the new ordering one should call <see cref="Utility.UpdateSortOrder"/> and save the returned items. If the items returned from the <see cref="ContentItem.GetChildren()"/> are moved with this method the changes will not be persisted since this is a new list instance.</remarks> public static void MoveToIndex(IList <ContentItem> siblings, ContentItem itemToMove, int newIndex) { siblings.Remove(itemToMove); siblings.Insert(newIndex, itemToMove); }
public static IDictionary <string, object> ToDictionary(this ContentItem item) { return(new ContentDictionary(item)); }
public ContentDictionary(ContentItem item) { this.item = item; }
internal static Regex PropertyReplacementRegex = new Regex("(?:\\$\\$|{{)(.*?)(?:\\$\\$|}})"); // allow {{token}} or $$token$$ internal static string Substitute(string p, ContentItem item) { return(PropertyReplacementRegex.Replace(p, match => ComputeReplacement(item, match))); }
private static string ComputeReplacement(ContentItem item, Match match) { var pn = match.Groups[1].Value.Split(new[] { ':', '|' }, 2); // allow : or | as token parameter separators try { var d1 = item.GetDetail(pn[0]); if (d1 == null) { // hard-coded a few properties here to avoid reflection if (pn[0].Equals("Title", StringComparison.OrdinalIgnoreCase)) { d1 = item.Title; } else if (pn[0].Equals("Url", StringComparison.OrdinalIgnoreCase)) { d1 = item.Url; } else if (pn[0].Equals("Id", StringComparison.OrdinalIgnoreCase)) { d1 = item.ID; } else if (pn[0].Equals("Published", StringComparison.OrdinalIgnoreCase)) { d1 = item.Published; } else if (pn[0].Equals("TranslationKey", StringComparison.OrdinalIgnoreCase)) { d1 = item.TranslationKey; } else if (pn[0].Equals("SavedBy", StringComparison.OrdinalIgnoreCase)) { d1 = item.SavedBy; } else if (pn[0].Equals("Updated", StringComparison.OrdinalIgnoreCase)) { d1 = item.Updated; } else if (pn[0].Equals("Published", StringComparison.OrdinalIgnoreCase)) { d1 = item.Published; } else if (pn[0].Equals("Path", StringComparison.OrdinalIgnoreCase)) { d1 = item.Path; } else { // Use Reflection to resolve property. var type = item.GetType(); var props = type.GetProperties().Where(f => f.Name.Equals(pn[0], StringComparison.OrdinalIgnoreCase)).ToArray(); if (props.Length > 0) { d1 = props[0].GetValue(item, null); } // it's a property else { var fields = type.GetFields().Where(f => f.Name.Equals(pn[0], StringComparison.OrdinalIgnoreCase)).ToArray(); if (fields.Length > 0) { d1 = fields[0].GetValue(item); // it's a field } } } } if (d1 == null) { return(String.Concat('{', pn[0], ":null}")); } return(pn.Length == 2 ? String.Format(String.Concat("{0:", pn[1], '}'), d1) : d1.ToString()); } catch (Exception err) { return(err.ToString()); } }
public CancellableDestinationEventArgs(ContentItem item, ContentItem destination) : base(item, destination) { }
public CancellableDestinationEventArgs(ContentItem item, ContentItem destination, Func <ContentItem, ContentItem, ContentItem> finalAction) : base(item, destination) { this.finalAction = finalAction; }
/// <summary>Creates a new instance of the ItemEventArgs.</summary> /// <param name="item">The item the associated with these event arguments.</param> public ItemEventArgs(ContentItem item) { AffectedItem = item; }
public static bool IsExpired(this ContentItem item) { return(item.Expires.HasValue && item.Expires.Value < Utility.CurrentTime()); }
// Content Extensions public static bool IsPublished(this ContentItem item) { return(item.Published.HasValue && item.Published <= Utility.CurrentTime()); }