public virtual void LoadTokenCollection(ITokenCollection <IToken> collection) { if (collection != null) { TokenCacheUpdateTimes[collection.GetCollectionLabel()] = GetDatabase().GetItem(collection.GetBackingItemId()).Statistics.Updated; TokenCollections[collection.GetCollectionLabel()] = collection; } }
/// <summary> /// Handles if a token gets moved out of a collection. If it's a moved to a similar parent it'll be adopted. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OnItemMoved(object sender, EventArgs e) { var item = Event.ExtractParameter <Item>(e, 0); if (!item.Template.IsDerived(new ID(Constants.TokenTemplateBaseId))) { return; } var from = Event.ExtractParameter <ID>(e, 1); var toItem = item.Parent; ITokenCollection <IToken> fromCollection = null; ITokenCollection <IToken> toCollection = null; fromCollection = TokenKeeper.CurrentKeeper.GetTokenCollection <IToken>(from); toCollection = TokenKeeper.CurrentKeeper.GetTokenCollection <IToken>(toItem.ID); if (fromCollection != null && toCollection != null) { var token = fromCollection.GetTokens().FirstOrDefault(x => x.GetBackingItemId() == item.ID); if (token != null) { TokenRootPropertyChanger changer = new TokenRootPropertyChanger(fromCollection.GetCollectionLabel(), token.Token); changer.Change(toCollection.GetCollectionLabel(), token.Token); } } fromCollection?.ResetTokenCache(); toCollection?.ResetTokenCache(); }
/// <summary> /// returns the label and icon of the token collection /// </summary> /// <param name="arg"></param> /// <returns></returns> private static dynamic GetTokenLabelAndIcon(ITokenCollection <IToken> arg) { dynamic ret = new ExpandoObject(); ret.Label = arg.GetCollectionLabel(); ret.Icon = arg.SitecoreIcon; if (string.IsNullOrWhiteSpace(ret.Icon)) { ret.Icon = TokenKeeper.IsSc8 ? GetIcon("Office/32x32/package.png") : GetIcon("People/32x32/cube_green.png"); } else { ret.Icon = GetIcon(ret.Icon); } return(ret); }
/// <summary> /// checks if the token collection is valid /// </summary> /// <param name="collection"></param> /// <returns>boolean for if the token is valid</returns> private bool IsCollectionValid(ITokenCollection <IToken> collection) { if (collection.GetBackingItemId() == (ID)null) { return(true); } var item = GetDatabase().GetItem(collection.GetBackingItemId()); if (item != null && item.Statistics.Updated <= TokenCacheUpdateTimes[collection.GetCollectionLabel()]) { return(true); } return(false); }
/// <summary> /// Refreshes the token collection with what's in sitecore /// </summary> /// <param name="category"></param> /// <returns>token collection</returns> public ITokenCollection <IToken> RefreshTokenCollection(string category = null) { RefreshAutoTokens(); var tokenManagerItems = GetDatabase() .SelectItems($"fast:/sitecore/content//*[@@templateid = '{Constants.TokenRootTemplateId}']") .Union(Globals.LinkDatabase.GetReferrers(GetDatabase().GetItem(Constants.TokenRootTemplateId)) .Select(x => GetDatabase().GetItem(x.SourceItemID))); HashSet <ID> dups = new HashSet <ID>(); ITokenCollection <IToken> collection = category != null && TokenCollections.ContainsKey(category) ? TokenCollections[category] : null; foreach (Item tokenManagerItem in tokenManagerItems) { if (tokenManagerItem != null) { if (dups.Contains(tokenManagerItem.ID)) { continue; } dups.Add(tokenManagerItem.ID); Stack <Item> curItems = new Stack <Item>(); curItems.Push(tokenManagerItem); while (curItems.Any()) { Item cur = curItems.Pop(); // this means that the collection exists, it's just out of date, so we need to update it. if (collection != null) { if (collection.GetBackingItemId() == cur.ID) { ITokenCollection <IToken> col = GetCollectionFromItem(cur); RemoveCollection(category); LoadTokenCollection(col); return(col); } } else // this means that the token doesn't exist yet, lets create it. { ITokenCollection <IToken> col = GetCollectionFromItem(cur); if (col != null && col.GetCollectionLabel() == category || category == null) { LoadTokenCollection(col); if (category != null) { return(col); } } } foreach (Item child in cur.Children) { curItems.Push(child); } } } } return(null); }
/// <summary> /// returns the label and icon of the token collection /// </summary> /// <param name="arg"></param> /// <returns></returns> private static dynamic GetTokenLabelAndIcon(ITokenCollection<IToken> arg) { dynamic ret = new ExpandoObject(); ret.Label = arg.GetCollectionLabel(); ret.Icon = arg.SitecoreIcon; if (string.IsNullOrWhiteSpace(ret.Icon)) ret.Icon = "Office/32x32/package.png"; return ret; }