Skip to content

Instantly share code, notes, and snippets.

@5up3rman
Last active October 12, 2016 12:57
Show Gist options
  • Save 5up3rman/419dc66f06607a2c494397b44bd2bc6a to your computer and use it in GitHub Desktop.
Save 5up3rman/419dc66f06607a2c494397b44bd2bc6a to your computer and use it in GitHub Desktop.
Rule Context and Processor
[Serializable]
public class ContentEditorRuleContext : RuleContext, ISerializable
{
public List<IMappedItem> MappedItems { get; set; } = new List<IMappedItem>();
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
}
public class ContentEditorRulesProcessor
{
private Item item { get; set; }
public List<IMappedItem> MappedItems { get; set; } = new List<IMappedItem>();
public ContentEditorRulesProcessor(Item currentItem)
{
item = currentItem;
if (item == null || !item.Paths.IsContentItem) return;
ProcessRules();
}
private void ProcessRules()
{
var ruleContext = new ContentEditorRuleContext { Item = item };
// Get the Rules Folder
var rulesFolder = Factory.GetDatabase(RulesConstants.MasterDb).GetItem(RulesConstants.CeRulesFolder);
if (rulesFolder == null) return;
var rules = RuleFactory.GetRules<ContentEditorRuleContext>(rulesFolder, "Rule");
rules.Run(ruleContext);
// Set the MappedItems to the ruleContext.MappedItems which are a collection of items gathered from the Actions
MappedItems = ruleContext.MappedItems;
ruleContext.Abort();
}
}
public struct RulesConstants
{
public const string MasterDb = "master";
public const string CeRulesFolder = "{55F26A63-D476-498F-817B-2FCDE203DB8A}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment