public IEnumerable <ID> LoadAutoToken(AutoToken token)
        {
            using (new SecurityDisabler())
            {
                if (TokenCollections.ContainsKey(token.CollectionName))
                {
                    TokenCollections[token.CollectionName].AddOrUpdateToken(token.Token, token);
                }
                else
                {
                    TokenCollections[token.CollectionName] = new AutoTokenCollection(token);
                    TokenCollections[token.CollectionName].AddOrUpdateToken(token.Token, token);
                }
                var db = Factory.GetDatabase("core", false);
                if (db == null)
                {
                    yield break;
                }

                TokenButton tb = token.TokenButton();
                foreach (Item parent in db.DataManager.DataEngine
                         .GetItem(new ID(Constants.Core.RteParent), LanguageManager.DefaultLanguage, Sitecore.Data.Version.Latest).Axes
                         .GetDescendants().Where(x =>
                                                 x.Name == "Toolbar 1" && x.TemplateID.ToString() == "{0E0DA701-BC94-4855-A0C3-92063E64BA1F}"))
                {
                    ID buttonId = GuidUtility.GetId("tokenmanager", $"{token.CollectionName}{token.Token}{parent.ID}");

                    Item button = db.DataManager.DataEngine.GetItem(buttonId, LanguageManager.DefaultLanguage, Sitecore.Data.Version.Latest);

                    if (tb != null)
                    {
                        yield return(buttonId);

                        if (button != null)
                        {
                            if (
                                button["Click"] == $"TokenSelector{Regex.Replace(token.CollectionName, "[^A-Za-z0-9_]", "")}{Regex.Replace(token.Token, "[^A-Za-z0-9_]", "")}" &&
                                button[FieldIDs.DisplayName] == tb.Name &&
                                button["Shortcut"] == $"?Category={token.CollectionName}&Token={token.Token}" &&
                                button[FieldIDs.Sortorder] == tb.SortOrder.ToString() &&
                                button[FieldIDs.Icon] == (string.IsNullOrWhiteSpace(tb.Icon) ? token.TokenIcon : tb.Icon))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            button = db.DataManager.DataEngine.CreateItem(tb.Name, parent, new ID(Constants.Core.ButtonTemplate), buttonId);
                        }

                        button.Editing.BeginEdit();
                        button["Click"] = $"TokenSelector{Regex.Replace(token.CollectionName, "[^A-Za-z0-9_]", "")}{Regex.Replace(token.Token, "[^A-Za-z0-9_]", "")}";
                        button[FieldIDs.DisplayName] = tb.Name;
                        button["Shortcut"]           = $"?Category={token.CollectionName}&Token={token.Token}";
                        button[FieldIDs.Sortorder]   = tb.SortOrder.ToString();
                        button[FieldIDs.Icon]        = string.IsNullOrWhiteSpace(tb.Icon) ? token.TokenIcon : tb.Icon;
                        button.Editing.EndEdit(false, true);
                        button.Database.Caches.ItemCache.RemoveItem(buttonId);
                        button.Database.Caches.DataCache.RemoveItemInformation(buttonId);
                    }
                    else
                    {
                        button?.Recycle();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void LoadAutoToken(AutoToken token)
        {
            using (new SecurityDisabler())
            {
                if (TokenCollections.ContainsKey(token.CollectionName))
                {
                    TokenCollections[token.CollectionName].AddOrUpdateToken(token.Token, token);
                }
                else
                {
                    TokenCollections[token.CollectionName] = new AutoTokenCollection(token);
                    TokenCollections[token.CollectionName].AddOrUpdateToken(token.Token, token);
                }
                var db = Factory.GetDatabase("core", false);
                if (db == null)
                {
                    return;
                }

                TokenButton tb = token.TokenButton();

                ID buttonId = GuidUtility.GetId("tokenmanager", $"{token.CollectionName}{token.Token}");

                Item button = db.DataManager.DataEngine.GetItem(buttonId, LanguageManager.DefaultLanguage, Sitecore.Data.Version.Latest);

                if (tb != null)
                {
                    if (button != null)
                    {
                        if (
                            button["Click"] == $"TokenSelector{Regex.Replace(token.CollectionName, "[^A-Za-z0-9_]", "")}{Regex.Replace(token.Token, "[^A-Za-z0-9_]", "")}" &&
                            button[FieldIDs.DisplayName] == tb.Name &&
                            button["Shortcut"] == $"?Category={token.CollectionName}&Token={token.Token}" &&
                            button[FieldIDs.Sortorder] == tb.SortOrder.ToString() &&
                            button[FieldIDs.Icon] == (string.IsNullOrWhiteSpace(tb.Icon) ? token.TokenIcon : tb.Icon))
                        {
                            return;
                        }
                    }
                    else
                    {
                        Item parent = db.DataManager.DataEngine.GetItem(new ID(Constants.Core.RteParent), LanguageManager.DefaultLanguage, Sitecore.Data.Version.Latest);

                        if (parent == null)
                        {
                            return;
                        }

                        button = db.DataManager.DataEngine.CreateItem(tb.Name, parent, new ID(Constants.Core.ButtonTemplate), buttonId);
                    }

                    button.Editing.BeginEdit();
                    button["Click"] = $"TokenSelector{Regex.Replace(token.CollectionName, "[^A-Za-z0-9_]", "")}{Regex.Replace(token.Token, "[^A-Za-z0-9_]", "")}";
                    button[FieldIDs.DisplayName] = tb.Name;
                    button["Shortcut"]           = $"?Category={token.CollectionName}&Token={token.Token}";
                    button[FieldIDs.Sortorder]   = tb.SortOrder.ToString();
                    button[FieldIDs.Icon]        = string.IsNullOrWhiteSpace(tb.Icon) ? token.TokenIcon : tb.Icon;
                    button.Editing.EndEdit(false, true);
                    button.Database.Caches.ItemCache.RemoveItem(buttonId);
                    button.Database.Caches.DataCache.RemoveItemInformation(buttonId);
                }
                else if (button != null)
                {
                    button.Recycle();
                }
            }
        }