Search code examples
ektron

Add new RuleTemplate in the Targeted Content Widget in EKTRON CMS


I have an ektron8.7 application. I need to Customize the Targeted Content Widget (add a new RuleTemplate). But I dont know how to achieve this. Please anyone help me for this

Please find this link and answer me.


Solution

  • In root\App_Code folder just create a class file for example MyRuleTemplate.cs

    namespace Ektron.Com.Helpers
    {  
        public class MyRuleTemplate : RuleTemplate
        {
    
            private StringOperatorField _fieldOperator = null;
    
            public MyRuleTemplate ()
                : base("My First RuleTemplate")
            {
            _fieldOperator = new StringOperatorField("operator", "operator", "");
            }        
            /// <summary>
            /// Evaluating the rule template condition set in the widget
            /// </summary>
            /// <param name="rule"></param>
            /// <returns></returns>
            public override bool Evaluate(Rule rule)
            {
                //write the code for evaluate
            string op = rule.Values["operator"];
                string visitorCountry = string.Empty;
                string ruleCountry = rule.Values["name"];
    
                try
                {
                    string IP = HttpContext.Current.Request["remote_addr"];
                    if (!string.IsNullOrEmpty(HttpContext.Current.Request["ip"]))
                        IP = HttpContext.Current.Request["ip"];
                    else if (!string.IsNullOrEmpty(HttpContext.Current.Session["ipaddress"].ToString()))
                        IP = HttpContext.Current.Session["ipaddress"].ToString();
                    var userData = Ektron.Cms.UserContext.GetLocationInfo(IP);
                    visitorCountry = userData.CountryCode;
                }
                catch (Exception ex)
                {
                    Ektron.Cms.EkException.LogException(ex, System.Diagnostics.EventLogEntryType.Error);
                }
    
                return _fieldOperator.Evaluate(visitorCountry, op, ruleCountry);
            }
    
        private Dictionary<string, Field> _fields = null;
            public override Dictionary<string, Field> Fields
            {
                 get
                {
                    if (_fields == null)
                    {
                        _fields = new Dictionary<string, Field>();
                        this.AddField(_fieldOperator);
                        List<LocalizableValue> countryFields = new List<LocalizableValue>();
                        countryFields.Add(new LocalizableValue("US", "United States"));
                        countryFields.Add(new LocalizableValue("DE", "Germany"));
                        countryFields.Add(new LocalizableValue("CA", "Canada"));
                        countryFields.Add(new LocalizableValue("FR", "France"));
                        _fields.Add("name", new EnumField(new LocalizableValue("name", "name", ""), countryFields));
                    }
                    return _fields;
                }
            }
    
    
            /// <summary>
            /// Text which will be displayed in the condition
            /// </summary>
            public override string Text
            {
                get
                {
                    return "Custom Country {operator} {name}";
                }
            }
            /// <summary>
            /// Title of the rule template
            /// </summary>
            public override string Title
            {
                get
                {
                    return "Custom Country";
                }
            }
    
    
        }
    }
    

    Then, in \widgets\TargetedContent.ascx.cs inside the method AddAllRuleTemplates() just add a reference to the newly added custom rule template MyRuleTemplate as AddRuleTemplate(new MyRuleTemplate());