public void AddsSelectedRenderings()
        {
            ID[] ids;
            var  layoutDefinition = new LayoutDefinition()
            {
                Devices = new ArrayList()
                {
                    new DeviceDefinition()
                }
            };

            using (var db = GetDatabase(out ids))
            {
                var item = db.GetItem("/sitecore/home/page");
                var args = new GetPlaceholderRenderingsArgs("placeholder", layoutDefinition.ToXml(), db.Database)
                {
                    PlaceholderRenderings = new List <Item>()
                };
                var ctx = new PlaceholderSettingsRuleContext(args, item);

                var action = new AddRenderingAction <PlaceholderSettingsRuleContext>()
                {
                    RenderingItemIds = string.Join("|", ids.Select(x => x.ToString()))
                };

                action.Apply(ctx);

                args.PlaceholderRenderings.Should().HaveSameCount(ids);
            }
        }
        public void AddsAllRenderingsFromFolder()
        {
            ID[] renderingIds;
            ID[] folderIds;
            var  layoutDefinition = new LayoutDefinition()
            {
                Devices = new ArrayList()
                {
                    new DeviceDefinition()
                }
            };

            using (var db = GetDatabase(out renderingIds, out folderIds))
            {
                var item = db.GetItem("/sitecore/home/page");
                var args = new GetPlaceholderRenderingsArgs("placeholder", layoutDefinition.ToXml(), db.Database)
                {
                    PlaceholderRenderings = new List <Item>()
                };
                var ctx = new PlaceholderSettingsRuleContext(args, item);

                var action = new AddRenderingFolderAction <PlaceholderSettingsRuleContext>()
                {
                    RenderingFolderItemId = folderIds[0].ToString()
                };

                action.Apply(ctx);

                args.PlaceholderRenderings.Select(x => x.ID)
                .Should().HaveCount(2)
                .And.Contain(renderingIds.Take(2));
            }
        }
        private void ProcessIndividualPlaceholder(Item placeholderItem, GetPlaceholderRenderingsArgs args)
        {
            PlaceholderSettingsRuleContext context = this.EvaluatePlaceholderRules(placeholderItem, args.PlaceholderKey);
            List <Item> smallList = new List <Item>();

            if (context.AllowSelectedControls)
            {
                // Add any additional renderings to the args.PlaceholderRenderings list
                smallList = this.GetRenderings(placeholderItem, out allowedControlsSpecified);
                if (smallList.Count > 0)
                {
                    string infoMessage = "\n\tContextItem: " + GetContextItem(args.PlaceholderKey).Paths.Path +
                                         "\n\tSmart Placeholder Item: " + placeholderItem.Paths.Path +
                                         "\n\tKey: " + args.PlaceholderKey +
                                         "\n\tRenderings: " + smallList.Select(i => i.Paths.Path).Aggregate((i, j) => i + "; " + j);
                    Log.Info("Smart Placeholder Settings: Renderings Added" + infoMessage, this);

                    args.PlaceholderRenderings.AddRange((IEnumerable <Item>)smallList);
                    if (allowedControlsSpecified)
                    {
                        args.Options.ShowTree = false;
                    }
                }
            }
            else if (context.DisallowSelectedControls)
            {
                // Create list of disallowed renderings to remove at the end.
                smallList = this.GetRenderings(placeholderItem, out allowedControlsSpecified);
                if (smallList.Count > 0)
                {
                    disAllowList.AddRange((IEnumerable <Item>)smallList);
                }
            }
        }
        public void ClearsAllRenderings()
        {
            ID[] ids;
            var  layoutDefinition = new LayoutDefinition()
            {
                Devices = new ArrayList()
                {
                    new DeviceDefinition()
                }
            };

            using (var db = GetDatabase(out ids))
            {
                var item = db.GetItem("/sitecore/home/page");
                var args = new GetPlaceholderRenderingsArgs("placeholder", layoutDefinition.ToXml(), db.Database)
                {
                    PlaceholderRenderings = db.Database.SelectItems("//Layouts/*").ToList()
                };

                args.PlaceholderRenderings.Should().NotBeEmpty();

                var ctx = new PlaceholderSettingsRuleContext(args, item);

                var action = new ClearRenderingsAction <PlaceholderSettingsRuleContext>();

                action.Apply(ctx);

                args.PlaceholderRenderings.Should().BeEmpty();
            }
        }
Beispiel #5
0
        /// <summary>
        /// Overrides the base class implementation and adds execution of rules to determine allowed renderings.
        /// </summary>
        protected override List <Sitecore.Data.Items.Item> GetRenderings(Sitecore.Data.Items.Item placeholderItem, out bool allowedControlsSpecified)
        {
            // Get the initial list of renderings from the base implementation.
            var list = base.GetRenderings(placeholderItem, out allowedControlsSpecified);

            // Get the rules from the placeholder item.
            string rulesXml = placeholderItem[Constants.RulesFieldId];

            if (string.IsNullOrWhiteSpace(rulesXml))
            {
                return(list);
            }

            // Parse the rules.
            var parsedRules = RuleFactory.ParseRules <PlaceholderSettingsRuleContext>(placeholderItem.Database, rulesXml);

            // Construct the context.
            PlaceholderSettingsRuleContext context = new PlaceholderSettingsRuleContext();

            context.Item = placeholderItem;
            context.AllowedRenderingItems = list;
            context.DeviceId         = DeviceId;
            context.PlaceholderKey   = PlaceholderKey;
            context.ContentDatabase  = ContentDatabase;
            context.LayoutDefinition = LayoutDefinition;

            // Execute the rules.
            RuleList <PlaceholderSettingsRuleContext> rules = new RuleList <PlaceholderSettingsRuleContext>();

            rules.Name = placeholderItem.Paths.Path;
            rules.AddRange(parsedRules.Rules);
            rules.Run(context);

            // Did the rules specify if the selection tree should be displayed?
            if (context.DisplaySelectionTree.HasValue)
            {
                allowedControlsSpecified = !context.DisplaySelectionTree.Value;
            }
            // If not, only display the tree if there are no allowed renderings.
            else
            {
                allowedControlsSpecified = context.AllowedRenderingItems.Count > 0;
            }

            // Return the list.
            return(context.AllowedRenderingItems);
        }
        private PlaceholderSettingsRuleContext EvaluateRules(Item placeholder)
        {
            PlaceholderSettingsRuleContext ruleContext = new PlaceholderSettingsRuleContext();

            ruleContext.Item = _contextItem;
            foreach (Rule <PlaceholderSettingsRuleContext> rule in RuleFactory.GetRules <PlaceholderSettingsRuleContext>(new[] { placeholder }, AllowedModulesRulesField).Rules)
            {
                if (rule.Condition != null)
                {
                    var passed = rule.Evaluate(ruleContext);
                    if (passed)
                    {
                        rule.Execute(ruleContext);
                    }
                }
            }

            return(ruleContext);
        }
        private PlaceholderSettingsRuleContext EvaluatePlaceholderRules(Item placeholder, string placeholderKey)
        {
            PlaceholderSettingsRuleContext context = new PlaceholderSettingsRuleContext();

            context.Item = this.GetContextItem(placeholderKey);

            foreach (
                Rule <PlaceholderSettingsRuleContext> rule in
                RuleFactory.GetRules <PlaceholderSettingsRuleContext>(new[] { placeholder }, Allowed_Controls_Rules_Field).Rules)
            {
                if (rule.Condition != null)
                {
                    var passed = rule.Evaluate(context);
                    if (passed)
                    {
                        rule.Execute(context);
                    }
                }
            }

            return(context);
        }
        /// <summary>
        /// Overrides the base class implementation and adds execution of rules to determine allowed renderings.
        /// </summary>
        protected override List<Sitecore.Data.Items.Item> GetRenderings(Sitecore.Data.Items.Item placeholderItem, out bool allowedControlsSpecified)
        {
            // Get the initial list of renderings from the base implementation.
            var list = base.GetRenderings(placeholderItem, out allowedControlsSpecified);

            // Get the rules from the placeholder item.
            string rulesXml = placeholderItem[Constants.RulesFieldId];
            if (string.IsNullOrWhiteSpace(rulesXml)) return list;

            // Parse the rules.
            var parsedRules = RuleFactory.ParseRules<PlaceholderSettingsRuleContext>(placeholderItem.Database, rulesXml);

            // Construct the context.
            PlaceholderSettingsRuleContext context = new PlaceholderSettingsRuleContext();
            context.Item = placeholderItem;
            context.AllowedRenderingItems = list;
            context.DeviceId = DeviceId;
            context.PlaceholderKey = PlaceholderKey;
            context.ContentDatabase = ContentDatabase;
            context.LayoutDefinition = LayoutDefinition;

            // Execute the rules.
            RuleList<PlaceholderSettingsRuleContext> rules = new RuleList<PlaceholderSettingsRuleContext>();
            rules.Name = placeholderItem.Paths.Path;
            rules.AddRange(parsedRules.Rules);
            rules.Run(context);

            // Did the rules specify if the selection tree should be displayed?
            if (context.DisplaySelectionTree.HasValue)
                allowedControlsSpecified = !context.DisplaySelectionTree.Value;
            // If not, only display the tree if there are no allowed renderings.
            else
                allowedControlsSpecified = context.AllowedRenderingItems.Count > 0;

            // Return the list.
            return context.AllowedRenderingItems;
        }
        private void ProcessPlaceholder(Item placeholderItem, GetPlaceholderRenderingsArgs args)
        {
            if (placeholderItem == null)
            {
                Log.Info("ProcessPlaceholder: placeholderItem empty, stopping execution of Rule", this);
                return;
            }
            if (args == null)
            {
                Log.Info("ProcessPlaceholder: args empty, stopping execution of Rule", this);
                return;
            }

            PlaceholderSettingsRuleContext context = null;
            List <Item> controlsToAllow            = null;
            List <Item> controlsToBlock            = null;

            try
            {
                context = this.EvaluateRules(placeholderItem);
            }
            catch (Exception e)
            {
                Log.Info(string.Format("ProcessPlaceholder: unable to EvaluateRules (placeholderItem {0}, args {1})", placeholderItem.ID, string.Join(",", args.PlaceholderRenderings.Select(r => r.ID))), this);
            }

            try
            {
                if (context != null)
                {
                    controlsToAllow = context.ControlsToAllow;
                    controlsToBlock = context.ControlsToBlock;
                    bool allowedControlsFieldSpecified = false;
                    if (allowedControlsFieldSpecified)
                    {
                        args.Options.ShowTree = false;
                    }

                    foreach (var controlToBlock in controlsToBlock)
                    {
                        if (args.PlaceholderRenderings != null && args.PlaceholderRenderings.Any())
                        {
                            args.PlaceholderRenderings.RemoveAll(c => c.ID == controlToBlock.ID);
                            Log.Info(string.Format("GetActiveModules: {0} blocked for {1}", controlToBlock.Name, _contextItem.Name), this);
                        }
                    }

                    foreach (var controlToAllow in controlsToAllow)
                    {
                        if (args.PlaceholderRenderings == null)
                        {
                            args.PlaceholderRenderings = new List <Item>();
                        }
                        args.PlaceholderRenderings.Add(controlToAllow);
                        Log.Info(string.Format("GetActiveModules: {0} allowed for {1}", controlToAllow.Name, _contextItem.Name), this);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Info(string.Format("ProcessPlaceholder: unable to process BlockedModules (placeholderItem {0}, controlsToAllow {1}, controlsToBlock {2})", placeholderItem.ID, string.Join(",", controlsToAllow.Select(r => r.ID)), string.Join(",", controlsToBlock.Select(r => r.ID))), this);
            }
        }