HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/RMourik/bassol.nl/wwwroot/App_Code/CMSModules/Macros/MacroRuleEditExtender.cs
using System;
using System.Text.RegularExpressions;

using CMS;
using CMS.Base;
using CMS.DataEngine;
using CMS.ExtendedControls;
using CMS.FormControls;
using CMS.FormEngine;
using CMS.Helpers;
using CMS.MacroEngine;


[assembly: RegisterCustomClass("MacroRuleEditExtender", typeof(MacroRuleEditExtender))]

/// <summary>
/// Macro rule UIForm extender
/// </summary>
public class MacroRuleEditExtender : ControlExtender<UIForm>
{
    #region "Methods and event handlers"

    public override void OnInit()
    {
        Control.OnBeforeSave += EditForm_OnBeforeSave;
    }


    protected void EditForm_OnBeforeSave(object sender, EventArgs e)
    {
        MacroRuleInfo info = Control.EditedObject as MacroRuleInfo;
        if (info != null)
        {
            // Generate automatic fields when present in UserText
            FormEngineUserControl control = Control.FieldControls["MacroRuleText"];
            if (control != null)
            {
                string userText = ValidationHelper.GetString(control.Value, String.Empty);
                if (!string.IsNullOrEmpty(userText))
                {
                    Regex regex = RegexHelper.GetRegex("\\{[-_a-zA-Z0-9]*\\}");
                    MatchCollection match = regex.Matches(userText);
                    if (match.Count > 0)
                    {
                        FormInfo fi = new FormInfo(info.MacroRuleParameters);
                        foreach (Match m in match)
                        {
                            foreach (Capture c in m.Captures)
                            {
                                string name = c.Value.Substring(1, c.Value.Length - 2).ToLowerCSafe();
                                FormFieldInfo ffi = fi.GetFormField(name);
                                if (ffi == null)
                                {
                                    ffi = new FormFieldInfo();
                                    ffi.Name = name;
                                    ffi.DataType = FieldDataType.Text;
                                    ffi.Size = 100;
                                    ffi.FieldType = FormFieldControlTypeEnum.CustomUserControl;
                                    ffi.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "select operation");
                                    ffi.AllowEmpty = true;
                                    switch (name)
                                    {
                                        case "_is":
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, ";is");
                                            ffi.Settings["controlname"] = "MacroNegationOperator";
                                            ffi.Settings["EditText"] = "false";
                                            ffi.Settings["Options"] = ";is\r\n!;is not";
                                            break;

                                        case "_was":
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, ";was");
                                            ffi.Settings["controlname"] = "MacroNegationOperator";
                                            ffi.Settings["EditText"] = "false";
                                            ffi.Settings["Options"] = ";was\r\n!;was not";
                                            break;

                                        case "_will":
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, ";will");
                                            ffi.Settings["controlname"] = "MacroNegationOperator";
                                            ffi.Settings["EditText"] = "false";
                                            ffi.Settings["Options"] = ";will\r\n!;will not";
                                            break;

                                        case "_has":
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, ";has");
                                            ffi.Settings["controlname"] = "MacroNegationOperator";
                                            ffi.Settings["EditText"] = "false";
                                            ffi.Settings["Options"] = ";has\r\n!;does not have";
                                            break;

                                        case "_perfectum":
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, ";has");
                                            ffi.Settings["controlname"] = "MacroNegationOperator";
                                            ffi.Settings["EditText"] = "false";
                                            ffi.Settings["Options"] = ";has\r\n!;has not";
                                            break;

                                        case "_any":
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, "false;any");
                                            ffi.Settings["controlname"] = "macro_any-all_bool_selector";
                                            ffi.Settings["EditText"] = "false";
                                            ffi.Settings["Options"] = "false;any\r\ntrue;all";
                                            break;

                                        default:
                                            ffi.FieldType = FormFieldControlTypeEnum.TextBoxControl;
                                            ffi.Size = 1000;
                                            ffi.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "enter text");
                                            break;
                                    }

                                    fi.AddFormItem(ffi);
                                }
                            }
                        }
                        info.MacroRuleParameters = fi.GetXmlDefinition();
                    }
                }
            }
        }

        Control.EditedObject.SetValue("MacroRuleIsCustom", !SystemContext.DevelopmentMode);
    }

    #endregion
}