1

我有一个 ektron8.7 应用程序。我需要自定义目标内容小部件(添加新的 RuleTemplate)。但我不知道如何实现这一点。请任何人帮助我

请找到此链接并回答我。

4

1 回答 1

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());

于 2014-01-07T10:41:45.330 回答