在EKTRON CMS的“目标内容”小组件中添加新的RuleTemplate

时间:2013-06-04 12:55:50

标签: ektron

我有一个ektron8.7应用程序。我需要自定义Targeted Content Widget(添加一个新的RuleTemplate)。但我不知道如何实现这一目标。请有人帮我这个

请找this link并回答我。

1 个答案:

答案 0 :(得分:1)

在root \ App_Code文件夹中只需创建一个类文件,例如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";
            }
        }


    }
}

然后,在方法AddAllRuleTemplates()中的\ widgets \ TargetedContent.ascx.cs中,只需添加对新添加的自定义规则模板MyRuleTemplate的引用,作为AddRuleTemplate(new MyRuleTemplate());