/// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.DeviceLayout, "args.DeviceLayout");
            Assert.IsFalse(args.Index == -1, "index indicates no selection");
            Assert.IsTrue(args.DeviceLayout.Renderings.Count > args.Index, "index out of bounds");

            var selectedRendering   = (RenderingDefinition)args.DeviceLayout.Renderings[args.Index];
            var selectedPlaceholder = Helper.NormalizePath(selectedRendering.Placeholder);

            // If we know this rendering exposes a dynamic placeholder path, then filter by it
            if (IsDynamic(selectedRendering))
            {
                var id                     = GetDynamicId(selectedRendering) ?? string.Empty;
                var pattern                = _dynamicPlaceholders.Patterns[selectedRendering.ItemID];
                var selectedPatternFull    = $@"^{selectedPlaceholder}{pattern.Prefix}{id}{pattern.Suffix}$";
                var selectedPatternPartial = $@"^{selectedPlaceholder}{pattern.Prefix}{id}{pattern.Suffix}/";

                args.Renderings = args.DeviceLayout.Renderings.Cast <RenderingDefinition>()
                                  .Where(x => Regex.IsMatch(x.Placeholder, selectedPatternFull) ||
                                         Regex.IsMatch(x.Placeholder, selectedPatternPartial)
                                         ).ToList();
            }
            else
            {
                args.Renderings = args.DeviceLayout.Renderings.Cast <RenderingDefinition>().Where(x => x.Placeholder.StartsWith(selectedPlaceholder)).ToList();
            }

            args.Renderings.Insert(0, selectedRendering);

            args.OldPlaceholderPath = selectedRendering.Placeholder;
        }
        public override void Process(CreatePrefabArgs args)
        {
            if (!ShouldRun())
            {
                return;
            }

            foreach (var r in args.Renderings)
            {
                if (!IsDynamic(r))
                {
                    continue;
                }

                var oldId = GetDynamicId(r);
                if (string.IsNullOrWhiteSpace(oldId))
                {
                    continue;
                }
                var newId = NextDynamicId(r, args);
                if (oldId == newId)
                {
                    continue;
                }

                SetDynamicId(r, newId);
                MapPlaceholderPaths(args, r, oldId, newId);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.DeviceLayout, "args.DeviceLayout");

            args.DeviceId = args.DeviceLayout.ID;
        }
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            if (!Config.Sxa.IsEnabled)
            {
                return;
            }

            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.ContextItem, "args.Database");
            Assert.IsNotNull(args.ResultProxy, "args.ResultProxy");

            var db = args.ContextItem.Database;

            using (new SecurityDisabler())
            {
                using (new EventDisabler()) // Must be silent as to not break content editor context item
                {
                    foreach (var site in SiteContextFactory.Sites)
                    {
                        // skip system sites
                        if (Helper.SystemSites.Contains(site.Name))
                        {
                            continue;
                        }

                        if (site.RootPath.Length <= 1) // skip trivial checks
                        {
                            continue;
                        }

                        var availablePrefabsItem = GetOrAddAvailablePrefabsNode(site, db);
                        if (availablePrefabsItem == null)
                        {
                            continue;
                        }

                        availablePrefabsItem.Editing.BeginEdit();
                        {
                            var newId = args.ResultProxy.ID.ToString();
                            var value = availablePrefabsItem[Templates.Sxa.AvailableRenderings.Fields.Renderings];

                            availablePrefabsItem[Templates.Sxa.AvailableRenderings.Fields.Renderings] = string.IsNullOrWhiteSpace(value)
                                ? newId
                                : $"{value}|{newId}";
                        }
                        availablePrefabsItem.Editing.EndEdit(true, true);

                        // Manually clear the cache (because we are in silent mode)
                        availablePrefabsItem.Database.Caches.DataCache.RemoveItemInformation(availablePrefabsItem.ID);
                        availablePrefabsItem.Database.Caches.ItemCache.RemoveItem(availablePrefabsItem.ID);
                    }
                    // foreach site
                    // GetOrAdd prefabs allowed renderings
                    // Add args.ResultProxy.ID to field
                }
            }
        }
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.Result, "args.Result");
            Assert.IsNotNullOrEmpty(args.Name, "args.Name");
            Assert.IsNotNull(args.DeviceId, "args.DeviceId");
            Assert.IsNotNull(args.LayoutId, "args.LayoutId");

            var prefab = args.Result;

            using (new SecurityDisabler())
            {
                using (new EventDisabler())
                {
                    // Fill Prefab
                    prefab.Editing.BeginEdit();
                    try
                    {
                        // Display Name (if input wasn't valid name)
                        if (prefab.Name != args.Name)
                        {
                            prefab[FieldIDs.DisplayName] = args.Name;
                        }

                        var prefabSharedLayoutField   = prefab.Fields[FieldIDs.LayoutField];
                        var prefabSharedLayout        = LayoutDefinition.Parse(LayoutField.GetFieldValue(prefabSharedLayoutField));
                        DeviceDefinition prefabDevice = prefabSharedLayout.GetDevice(args.DeviceId); // match device

                        // Set Layout
                        prefabDevice.Layout = args.LayoutId;

                        // Add Renderings
                        foreach (var r in args.Renderings)
                        {
                            prefabDevice.AddRendering(r);
                        }

                        LayoutField.SetFieldValue(prefabSharedLayoutField, prefabSharedLayout.ToXml());

                        prefab.Editing.EndEdit(true, true); // Must be silent as to not break content editor context item

                        // Manually clear the cache (because we are in silent mode)
                        prefab.Database.Caches.DataCache.RemoveItemInformation(prefab.ID);
                        prefab.Database.Caches.ItemCache.RemoveItem(prefab.ID);
                    }
                    catch (Exception ex)
                    {
                        prefab.Editing.CancelEdit();
                        Log.Error($"{nameof(FillPrefab)}::{nameof(Process)} - Unable to fill prefab, {args.Result.ID}, {args.Name}", ex, this);
                    }
                }
            }
        }
        public override void Process(CreatePrefabArgs args)
        {
            if (!args.Handled)
            {
                return;
            }

            using (new SecurityDisabler())  // Avoid permission issues
            {
                using (new EventDisabler()) // Must run with EventDisabler... otherwise content editor's event listeners switch context item when any new item is created... which would break our dialog
                {
                    foreach (var r in args.Renderings)
                    {
                        if (string.IsNullOrWhiteSpace(r.Datasource))
                        {
                            continue;
                        }

                        ID dataId;
                        if (!ID.TryParse(r.Datasource, out dataId))
                        {
                            continue;
                        }

                        var datasourceItem = args.ContextItem.Database.GetItem(dataId);
                        if (datasourceItem == null)
                        {
                            continue;
                        }

                        // Check if this item lives under prefab, ie. local page data
                        if (!args.ContextItem.Axes.IsAncestorOf(datasourceItem))
                        {
                            continue;
                        }

                        var destinationDataFolder = GetOrAddDatafolder(datasourceItem.Parent, args.ContextItem, args.Result);
                        if (destinationDataFolder == null)
                        {
                            continue;
                        }
                        var destItem = CopyDatasource(datasourceItem, destinationDataFolder);
                        if (destItem == null)
                        {
                            continue;
                        }

                        r.Datasource = destItem.ID.ToString();
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.Renderings, "args.Renderings");
            Assert.IsNotNull(args.OldPlaceholderPath, "args.Placeholder");
            Assert.IsNotNull(args.Placeholder, "args.Placeholder");

            var removeLength   = Sitecore.StringUtil.EnsurePrefix('/', args.OldPlaceholderPath).Length;
            var newPlaceholder = Helper.NormalizePath(args.Placeholder, false).TrimEnd('/');

            foreach (var r in args.Renderings)
            {
                r.Placeholder = r.Placeholder.Length <= removeLength ? args.Placeholder : string.Concat(newPlaceholder, r.Placeholder.Substring(removeLength));
            }
        }
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.Name, "args.Name");
            Assert.IsNotNull(args.Result, "args.Result");
            Assert.IsNotNull(args.RenderingLocation, "args.RenderingLocation");

            using (new SecurityDisabler())
            {
                using (new EventDisabler()) // Must be silent as to not break content editor context item
                {
                    // See if we already have a proxy (improper delete)
                    var proxy = args.RenderingLocation.HasChildren ? args.RenderingLocation.Children.FirstOrDefault(x => x.Name.Equals(args.Result.Name)) : null;
                    if (proxy != null)
                    {
                        args.ResultProxy = proxy;
                        return;
                    }

                    // Create new item
                    proxy = args.RenderingLocation.Add(args.Result.Name, Templates.Proxy.BranchId);

                    // For some reason branch won't add correctly in an Event Disabler or because it is a rendering item?... bug!
                    proxy.Editing.BeginEdit();
                    {
                        // Ugly, but force desired branch settings:
                        proxy.Appearance.Icon = "Office/32x32/pci_card.png";
                        proxy["Method"]       = "ProxyItem";
                        proxy["Class"]        = "Community.Foundation.Prefabs.Rendering.MethodRendering";
                        proxy["Assembly"]     = "Community.Foundation.Prefabs";

                        // Also check for display name case
                        if (args.Result.Name != args.Name)
                        {
                            proxy[FieldIDs.DisplayName] = args.Name;
                        }
                    }
                    proxy.Editing.EndEdit(true, true);

                    // Manually clear the cache (because we are in silent mode)
                    proxy.Database.Caches.DataCache.RemoveItemInformation(proxy.ID);
                    proxy.Database.Caches.ItemCache.RemoveItem(proxy.ID);

                    args.ResultProxy = proxy;
                }
            }
        }
        public virtual void MapPlaceholderPaths(CreatePrefabArgs args, RenderingDefinition rendering, string oldId, string newId)
        {
            if (!_dynamicPlaceholders.Patterns.ContainsKey(rendering.ItemID))
            {
                Log.Error($"Foundation.Prefabs::{nameof(MapDynamicPlaceholders)}:: Rendering {rendering.ItemID} identified as having dynamic placeholder, but no config mapping exists under sitecore/foundation.prefabs/dynamicPlaceholders/mappings; add config mapping", this);
                return;
            }

            var placeholderPath = Helper.NormalizePath(rendering.Placeholder);
            var pattern         = _dynamicPlaceholders.Patterns[rendering.ItemID];
            // Using Regex LookBehind and LookAhead to match replaceable value
            var placeholderPatternFull    = $@"(?<=^{placeholderPath}{pattern.Prefix}){oldId}(?={pattern.Suffix}$)"; // full value match
            var placeholderPatternPartial = $@"(?<=^{placeholderPath}{pattern.Prefix}){oldId}(?={pattern.Suffix}/)"; // partial value match - need the trailing slash to avoid false positives

            foreach (RenderingDefinition r in args.Renderings)
            {
                r.Placeholder = Regex.Replace(r.Placeholder, placeholderPatternFull, newId);
                r.Placeholder = Regex.Replace(r.Placeholder, placeholderPatternPartial, newId);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNullOrEmpty(args.Name, "args.Name");
            Assert.IsNotNull(args.PrefabLocation, "args.PrefabLocation");

            using (new SecurityDisabler())
            {
                using (new EventDisabler()) // Must be silent as to not break content editor context item
                {
                    // Get Safe Name
                    var name = ItemUtil.ProposeValidItemName(args.Name, Helper.GetSafeName(args.Name));

                    // Ensure Unique
                    var num      = 1;
                    var origName = name;
                    var db       = args.PrefabLocation.Database;
                    var path     = $"{args.PrefabLocation.Paths.FullPath}/";
                    while (db.Items[$"{path}{name}"] != null)
                    {
                        if (num == 1 && origName.Length + 3 > Settings.MaxItemNameLength)
                        {
                            origName = origName.Substring(0, Settings.MaxItemNameLength - 3);
                        }

                        num++;
                        name = $"{origName} {num:00}";
                    }
                    if (num > 1)
                    {
                        args.Name = $"{args.Name} {num:00}"; // carry to display name too
                    }

                    // Add Item
                    var prefab = args.PrefabLocation.Add(name, new TemplateID(Templates.Prefab.Id));

                    args.Result = prefab;
                }
            }
        }
Exemplo n.º 11
0
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.ContextItem, "args.Database");
            Assert.IsNotNull(args.Renderings, "args.Renderings");

            if (!args.ContextItem.HasChildren)
            {
                return;
            }

            var db       = args.ContextItem.Database;
            var basePath = args.ContextItem.Paths.FullPath;

            foreach (var r in args.Renderings)
            {
                if (string.IsNullOrWhiteSpace(r.Datasource))
                {
                    continue;
                }

                if (!r.Datasource.StartsWith("local:"))
                {
                    continue;
                }

                using (new SecurityDisabler()) // Avoid permission issues
                {
                    // Resolve "local:" format to ID
                    var dataItem = db.GetItem($"{basePath}{r.Datasource.Remove(0, "local:".Length)}");
                    if (dataItem != null)
                    {
                        r.Datasource = dataItem.ID.ToString();
                    }
                }
            }
        }
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");

            args.LayoutId = Config.Prefab.LayoutId;
        }
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");

            args.RenderingLocation = args.ContextItem.Database.GetItem(Config.Paths.Renderings);
        }
        /// <summary>
        /// Assume convention that rendering name matches prefab name
        /// Lookup item from defined global repository
        /// </summary>
        /// <param name="args"></param>
        public override void Process(CreatePrefabArgs args)
        {
            Assert.IsNotNull(args, "args");

            args.Placeholder = Config.Prefab.Placeholder;
        }
 public virtual string NextDynamicId(RenderingDefinition rendering, CreatePrefabArgs args)
 {
     return(NextDynamicId(rendering, args.Renderings).ToString());
 }