private static MacroExpr GetMacroExpr(string originalExpression, string processedExpression, bool isLocalizationMacro) { var macroExpr = new MacroExpr(); try { // Handle security macroExpr.Expression = MacroSecurityProcessor.RemoveMacroSecurityParams(originalExpression, out macroExpr.SignedBy); macroExpr.SignatureValid = MacroSecurityProcessor.CheckMacroIntegrity(originalExpression, macroExpr.SignedBy); // Parse rule text macroExpr.RuleText = MacroRuleTree.GetRuleText(macroExpr.Expression, true, true); macroExpr.Expression = MacroRuleTree.GetRuleCondition(macroExpr.Expression, true); // Macro expression does not support anonymous signature, remove the flag if (processedExpression.EndsWith("@", StringComparison.Ordinal)) { processedExpression = processedExpression.Substring(0, processedExpression.Length - 1); } // Check syntax var expr = MacroExpression.ParseExpression(processedExpression, isLocalizationMacro); macroExpr.MembersIssues = MacroMethodValidator.Validate(expr); } catch (Exception ex) { macroExpr.Error = true; macroExpr.ErrorMessage = ex.Message + "\r\n\r\n" + ex.StackTrace; } return(macroExpr); }
protected object gridElem_OnExternalDataBound(object sender, string sourceName, object parameter) { SourcePointTypeEnum sourcePointType = SourcePointTypeEnum.Standard; GridViewRow container = null; switch (sourceName.ToLowerCSafe()) { case "label": return(ResHelper.LocalizeString(HTMLHelper.HTMLEncode(ValidationHelper.GetString(parameter, String.Empty)))); case "condition": string condition = MacroRuleTree.GetRuleText(ValidationHelper.GetString(parameter, String.Empty)); return(GetDecoratedCondition(condition)); case "allowaction": // Default case can't be moved or deleted. Same goes for condition step type case container = (GridViewRow)parameter; sourcePointType = (SourcePointTypeEnum)ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue((DataRowView)container.DataItem, "Type"), 0); if ((sourcePointType != SourcePointTypeEnum.SwitchCase) || (CurrentStepInfo.StepType == WorkflowStepTypeEnum.Condition)) { ((Control)sender).Visible = false; } break; } return(parameter); }
protected void btnSetParameter_Click(object sender, EventArgs e) { MacroRuleTree selected = GetSelected(hdnParamSelected.Value); if (selected != null) { string paramName = hdnParam.Value.ToLowerCSafe(); MacroRuleParameter param = (MacroRuleParameter)selected.Parameters[paramName]; if (param != null) { if (formElem.ValidateData()) { // Load value from the form control FormEngineUserControl ctrl = formElem.FieldControls[paramName]; if (ctrl != null) { // Get the values from form control string displayName = ctrl.ValueDisplayName; object val = ctrl.Value; string value = null; // Convert value to EN culture if (ctrl.FieldInfo.DataType == FormFieldDataTypeEnum.DateTime) { value = ValidationHelper.GetDateTime(val, DataHelper.DATETIME_NOT_SELECTED).ToString(CultureHelper.EnglishCulture); displayName = ValidationHelper.GetDateTime(displayName, DataHelper.DATETIME_NOT_SELECTED).ToString(CultureHelper.EnglishCulture); } else if (ctrl.FieldInfo.DataType == FormFieldDataTypeEnum.Decimal) { value = ValidationHelper.GetDouble(val, 0).ToString(CultureHelper.EnglishCulture); displayName = ValidationHelper.GetDouble(displayName, 0).ToString(CultureHelper.EnglishCulture); } else { value = ValidationHelper.GetString(val, ""); } if (String.IsNullOrEmpty(ctrl.ValueDisplayName)) { displayName = value; } param.Value = value; param.Text = displayName; } pnlModalProperty.Visible = false; pnlFooter.Visible = false; } else { pnlModalProperty.Visible = true; pnlFooter.Visible = true; mdlDialog.Visible = true; mdlDialog.Show(); } } } }
/// <summary> /// Loads the designer from xml. /// </summary> public void LoadFromXML(string xml) { try { ViewState["RuleTree"] = MacroRuleTree.BuildFromXML(xml); } catch { } }
private void RefreshText() { string hdnValueTrim = hdnValue.Value.Trim(); if (hdnValueTrim.StartsWithCSafe("rule(", true)) { // Empty rule designer condition is not considered as rule conditions if (hdnValueTrim.EqualsCSafe("Rule(\"\", \"<rules></rules>\")", true)) { hdnValue.Value = ""; } else { try { string ruleText = MacroRuleTree.GetRuleText(hdnValueTrim, true, true, TimeZoneTransformation); // Display rule text ltlMacro.Text = ruleText; txtMacro.Visible = false; pnlRule.Visible = true; pnlUpdate.Update(); return; } catch { // If failed to parse the rule, extract the condition MacroExpression xml = MacroExpression.ExtractParameter(hdnValueTrim, "rule", 0); if (xml != null) { string user; hdnValue.Value = MacroSecurityProcessor.RemoveMacroSecurityParams(ValidationHelper.GetString(xml.Value, ""), out user); } } } } if (string.IsNullOrEmpty(hdnValue.Value) && !string.IsNullOrEmpty(DefaultConditionText)) { ltlMacro.Text = DefaultConditionText; txtMacro.Text = ""; txtMacro.Visible = false; pnlRule.Visible = true; } else { txtMacro.Text = hdnValue.Value; hdnValue.Value = null; txtMacro.Visible = true; pnlRule.Visible = false; } pnlUpdate.Update(); }
/// <summary> /// Loads the designer from xml. /// </summary> public void LoadFromXML(string xml) { try { MacroRuleTree ruleTree = new MacroRuleTree(); ruleTree.LoadFromXml(xml); ViewState["RuleTree"] = ruleTree; } catch { } }
/// <summary> /// Renders complete rule. /// </summary> /// <param name="rule">Rule to render</param> private string GetResultHTML(MacroRuleTree rule) { StringBuilder sb = new StringBuilder(); // Append operator if (rule.Position > 0) { bool isAnd = (rule.Operator == "&&"); sb.Append("<div class=\"MacroRuleOperator\" style=\"padding-left: ", 15 * (rule.Level - 1), "px\" onclick=\"ChangeOperator('", rule.IDPath, "', '", (isAnd ? "||" : "&&"), "');\">", (isAnd ? "and" : "or"), "</div>"); } if (rule.IsLeaf) { sb.Append("<div id=\"", rule.IDPath, "\" class=\"MacroRule\" style=\"padding-left: ", 15 * (rule.Level - 1), "px\">"); // Register position to a JS hashtable (for drag and drop purposes) ScriptHelper.RegisterStartupScript(Page, typeof(string), "targetPosition" + counter, "targetPosition[" + counter++ + "] = '" + rule.Parent.IDPath + ";" + rule.Position + "';", true); sb.Append("<span id=\"ruleHandle" + rule.IDPath + "\" class=\"MacroRuleHandle\">"); string handleParams = "<span" + (rule.IsLeaf ? " onclick=\"SelectRule('" + rule.IDPath + "', $cmsj(this).parent()); return false;\"" : "") + "onmousedown=\"return false;\" onmouseover=\"ActivateBorder('ruleText" + rule.IDPath + "', 'MacroRuleText');\" onmouseout=\"DeactivateBorder('ruleText" + rule.IDPath + "', 'MacroRuleText');\">"; string text = handleParams.Replace("##ID##", "0") + HTMLHelper.HTMLEncode(rule.RuleText) + "</span>"; if (rule.Parameters != null) { int i = 1; foreach (string key in rule.Parameters.Keys) { MacroRuleParameter p = rule.Parameters[key]; string paramText = (string.IsNullOrEmpty(p.Text) ? p.DefaultText : p.Text.TrimStart('#')); paramText = MacroRuleTree.GetParameterText(paramText, true, null, p.ApplyValueTypeConversion ? p.ValueType : "text"); var parameterText = "</span><span class=\"MacroRuleParameter\" onclick=\"ChangeParamValue('" + rule.IDPath + "', " + ScriptHelper.GetString(key) + ");\">" + paramText + "</span>" + handleParams; text = Regex.Replace(text, "\\{" + key + "\\}", TextHelper.EncodeRegexSubstitutes(parameterText), CMSRegex.IgnoreCase); i++; } } bool isSelected = hdnSelected.Value.Contains(";" + rule.IDPath + ";"); sb.Append("<div id=\"ruleText", rule.IDPath, "\" class=\"MacroRuleText", (isSelected ? " RuleSelected" : ""), "\">", text, "</div>"); sb.Append("</span>"); sb.Append("</div>"); } else { foreach (MacroRuleTree child in rule.Children) { sb.Append(GetResultHTML(child)); } } return(sb.ToString()); }
/// <summary> /// Loads the from definition from selected parameter into a BasicForm control. /// </summary> /// <param name="actual">If true, data from actual hiddens are loaded</param> private void LoadFormDefinition(bool actual) { MacroRuleTree selected = GetSelected((actual ? hdnParamSelected.Value : hdnLastSelected.Value)); if (selected != null) { string paramName = (actual ? hdnParam.Value.ToLowerCSafe() : hdnLastParam.Value.ToLowerCSafe()); MacroRuleParameter param = selected.Parameters[paramName]; if (param != null) { FormInfo fi = new FormInfo(selected.RuleParameters); FormFieldInfo ffi = fi.GetFormField(paramName); if (ffi != null) { fi = new FormInfo(); fi.AddFormItem(ffi); // Add fake DisplayName field FormFieldInfo displayName = new FormFieldInfo(); displayName.Visible = false; displayName.Name = "DisplayName"; displayName.DataType = FieldDataType.Text; fi.AddFormItem(displayName); DataRow row = fi.GetDataRow().Table.NewRow(); if (ffi.AllowEmpty && String.IsNullOrEmpty(param.Value)) { if (!DataTypeManager.IsString(TypeEnum.Field, ffi.DataType)) { row[paramName] = DBNull.Value; } } else { // Convert to a proper type var val = DataTypeManager.ConvertToSystemType(TypeEnum.Field, ffi.DataType, param.Value, CultureHelper.EnglishCulture); if (val != null) { row[paramName] = val; } } formElem.DataRow = row; formElem.FormInformation = fi; formElem.ReloadData(); } } } }
protected void btnChangeOperator_Click(object sender, EventArgs e) { MacroRuleTree selected = GetSelected(hdnOpSelected.Value); if (selected != null) { selected.Operator = hdnParam.Value; if ((selected.Position == 1) && (selected.Parent != null)) { // Change operator to previous sibling if we are changing the first operator in the group // It's because if we switch those two it should have same opearators selected.Parent.Children[0].Operator = selected.Operator; } } }
private void RefreshText() { string hdnValueTrim = hdnValue.Value.Trim(); if (hdnValueTrim.StartsWithCSafe("rule(", true)) { // Empty rule designer condition is not considered as rule conditions if (hdnValueTrim.EqualsCSafe("Rule(\"\", \"<rules></rules>\")", true)) { hdnValue.Value = ""; } else { bool failToParseXml = false; string ruleText = MacroRuleTree.GetRuleText(hdnValueTrim, true, out failToParseXml); if (failToParseXml) { // If failed to parse the rule, extract the condition MacroExpression xml = MacroExpression.ExtractParameter(hdnValueTrim, "rule", 0); if (xml != null) { string user = null; hdnValue.Value = MacroResolver.RemoveMacroSecurityParams(ValidationHelper.GetString(xml.Value, ""), out user); } } else { // Display rule text ltlMacro.Text = ruleText; txtMacro.Visible = false; pnlRule.Visible = true; return; } } } txtMacro.Text = hdnValue.Value; hdnValue.Value = null; txtMacro.Visible = true; pnlRule.Visible = false; pnlUpdate.Update(); }
/// <summary> /// Gets the object from its IDPath. /// </summary> /// <param name="idPath">IDPath of the rule</param> private MacroRuleTree GetSelected(string idPath) { if (!string.IsNullOrEmpty(idPath)) { string[] parts = idPath.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries); MacroRuleTree srcGroup = RuleTree; foreach (string posStr in parts) { int pos = ValidationHelper.GetInteger(posStr, 0); if (srcGroup.Children.Count > pos) { srcGroup = srcGroup.Children[pos]; } } return(srcGroup); } return(null); }
/// <summary> /// Gets the macro expression from the given object /// </summary> /// <param name="expression">Expression</param> private static MacroExpr GetMacroExpr(string expression) { var macroExpr = new MacroExpr(); try { // Handle security macroExpr.Expression = MacroSecurityProcessor.RemoveMacroSecurityParams(expression, out macroExpr.SignedBy); macroExpr.SignatureValid = MacroSecurityProcessor.CheckMacroIntegrity(expression, macroExpr.SignedBy); // Parse rule text macroExpr.RuleText = MacroRuleTree.GetRuleText(macroExpr.Expression, true, true); macroExpr.Expression = MacroRuleTree.GetRuleCondition(macroExpr.Expression, true); } catch (Exception ex) { macroExpr.Error = true; macroExpr.ErrorMessage = ex.Message + "\r\n\r\n" + ex.StackTrace; } return(macroExpr); }
/// <summary> /// Adds a clause according to selected item. /// </summary> private void AddClause() { MacroRuleInfo rule = MacroRuleInfo.Provider.Get(ValidationHelper.GetInteger(lstRules.SelectedValue, 0)); if (rule != null) { List <MacroRuleTree> selected = GetSelected(); if (selected.Count == 1) { MacroRuleTree item = selected[0]; if (item?.Parent != null) { item.Parent.AddRule(rule, item.Position + 1); return; } } // Add the rule at the root level, when no selected item RuleTree.AddRule(rule, RuleTree.Children.Count); } }
protected object gridElem_OnExternalDataBound(object sender, string sourceName, object parameter) { switch (sourceName.ToLowerCSafe()) { case "condition": return(MacroRuleTree.GetRuleText(ValidationHelper.GetString(parameter, String.Empty))); case "type": DataRowView row = parameter as DataRowView; if (row != null) { ObjectWorkflowTriggerInfo trigger = new ObjectWorkflowTriggerInfo(row.Row); if (!string.IsNullOrEmpty(trigger.TriggerTargetObjectType)) { return(GetTriggerDescription(trigger)); } else { return(AutomationHelper.GetTriggerName(trigger.TriggerType, trigger.TriggerObjectType)); } } return(parameter); case "delete": if (!WorkflowStepInfoProvider.CanUserManageAutomationProcesses(CurrentUser, CurrentSiteName)) { ImageButton btn = (ImageButton)sender; btn.Enabled = false; btn.Attributes.Add("src", GetImageUrl("Design/Controls/UniGrid/Actions/DeleteDisabled.png")); } return(parameter); default: return(parameter); } }
protected void btnAutoIndent_Click(object sender, EventArgs e) { MacroRuleTree.RemoveBrackets(RuleTree); RuleTree.AutoIndent(); }
protected void btnAutoIndent_Click(object sender, ImageClickEventArgs e) { MacroRuleTree.RemoveBrackets(this.RuleTree); this.RuleTree.AutoIndent(); }
protected void btnClearAll_Click(object sender, EventArgs e) { RuleTree = new MacroRuleTree(); hdnSelected.Value = ""; }
/// <summary> /// Renders complete rule. /// </summary> /// <param name="rule">Rule to render</param> private string GetRuleHTML(MacroRuleTree rule) { StringBuilder sb = new StringBuilder(); // Append operator if (rule.Position > 0) { bool isAnd = (rule.Operator == "&&"); sb.Append("<div class=\"MacroRuleOperator\" style=\"padding-left: ", 15 * (rule.Level - 1), "px\" onclick=\"ChangeOperator('", rule.IDPath, "', '", (isAnd ? "||" : "&&"), "');\">", (isAnd ? "and" : "or"), "</div>"); } if (rule.IsLeaf) { sb.Append("<div id=\"", rule.IDPath, "\" class=\"MacroRule\" style=\"padding-left: ", 15 * (rule.Level - 1), "px\">"); // Register position to a JS hashtable (for drag and drop purposes) ScriptHelper.RegisterStartupScript(Page, typeof(string), "targetPosition" + counter, "targetPosition[" + counter++ + "] = '" + rule.Parent.IDPath + ";" + rule.Position + "';", true); sb.Append("<span id=\"ruleHandle" + rule.IDPath + "\" class=\"MacroRuleHandle\">"); string handleParams = "<span" + (rule.IsLeaf ? " onclick=\"SelectRule('" + rule.IDPath + "', $cmsj(this).parent()); return false;\"" : "") + "onmousedown=\"return false;\" onmouseover=\"ActivateBorder('ruleText" + rule.IDPath + "', 'MacroRuleText');\" onmouseout=\"DeactivateBorder('ruleText" + rule.IDPath + "', 'MacroRuleText');\">"; string text = handleParams.Replace("##ID##", "0") + HTMLHelper.HTMLEncode(rule.RuleText) + "</span>"; if (rule.Parameters != null) { foreach (string key in rule.Parameters.Keys) { MacroRuleParameter p = rule.Parameters[key]; string paramText = (string.IsNullOrEmpty(p.Text) ? p.DefaultText : p.Text.TrimStart('#')); paramText = MacroRuleTree.GetParameterText(paramText, true, null, p.ApplyValueTypeConversion ? p.ValueType : "text"); var parameterText = "</span><span class=\"MacroRuleParameter\" onclick=\"ChangeParamValue('" + rule.IDPath + "', " + ScriptHelper.GetString(key) + ");\">" + paramText + "</span>" + handleParams; text = Regex.Replace(text, "\\{" + key + "\\}", TextHelper.EncodeRegexSubstitutes(parameterText), CMSRegex.IgnoreCase); } } bool isSelected = hdnSelected.Value.Contains(";" + rule.IDPath + ";"); sb.Append("<div id=\"ruleText", rule.IDPath, "\" class=\"MacroRuleText", (isSelected ? " RuleSelected" : ""), "\">", text, "</div>"); sb.Append("</span>"); sb.Append("</div>"); } else { foreach (MacroRuleTree child in rule.Children) { sb.Append(GetRuleHTML(child)); } } return sb.ToString(); }
/// <summary> /// Creates new <see cref="FieldMacroRule"/> object based on inputs. /// </summary> private FieldMacroRule CreateMacroRule() { if (!IsValid()) { return(null); } MacroRuleTree main = null; FieldMacroRule fmr = null; MacroRuleInfo mri = MacroRuleInfoProvider.GetMacroRuleInfo(mSelectedRuleName); if (mri != null) { main = new MacroRuleTree(); MacroRuleTree childern = new MacroRuleTree() { RuleText = mri.MacroRuleText, RuleName = mri.MacroRuleName, RuleCondition = mri.MacroRuleCondition, Parent = main }; main.Children.Add(childern); foreach (string paramName in formProperties.Fields) { // Load value from the form control FormEngineUserControl ctrl = formProperties.FieldControls[paramName]; if (ctrl != null) { // Convert value to EN culture var dataType = ctrl.FieldInfo.DataType; var convertedValue = DataTypeManager.ConvertToSystemType(TypeEnum.Field, dataType, ctrl.Value); string value = ValidationHelper.GetString(convertedValue, "", CultureHelper.EnglishCulture); string displayName = ctrl.ValueDisplayName; if (String.IsNullOrEmpty(displayName)) { displayName = value; } MacroRuleParameter param = new MacroRuleParameter { Value = value, Text = displayName, ValueType = dataType }; childern.Parameters.Add(paramName, param); } } string macroRule = string.Format("Rule(\"{0}\", \"{1}\")", MacroElement.EscapeSpecialChars(main.GetCondition()), MacroElement.EscapeSpecialChars(main.GetXML())); if (!MacroSecurityProcessor.IsSimpleMacro(macroRule)) { // Sign complex macros macroRule = MacroSecurityProcessor.AddMacroSecurityParams(macroRule, MacroIdentityOption.FromUserInfo(MembershipContext.AuthenticatedUser)); } fmr = new FieldMacroRule(); fmr.MacroRule = string.Format("{{%{0}%}}", macroRule); fmr.ErrorMessage = txtErrorMsg.Text; } return(fmr); }
/// <summary> /// Creates new <see cref="FieldMacroRule"/> object based on inputs. /// </summary> private FieldMacroRule CreateMacroRule() { if (!IsValid()) { return null; } MacroRuleTree main = null; FieldMacroRule fmr = null; MacroRuleInfo mri = MacroRuleInfoProvider.GetMacroRuleInfo(mSelectedRuleName); if (mri != null) { main = new MacroRuleTree(); MacroRuleTree childern = new MacroRuleTree() { RuleText = mri.MacroRuleText, RuleName = mri.MacroRuleName, RuleCondition = mri.MacroRuleCondition, Parent = main }; main.Children.Add(childern); foreach (string paramName in formProperties.Fields) { // Load value from the form control FormEngineUserControl ctrl = formProperties.FieldControls[paramName]; if (ctrl != null) { // Convert value to EN culture var dataType = ctrl.FieldInfo.DataType; var convertedValue = DataTypeManager.ConvertToSystemType(TypeEnum.Field, dataType, ctrl.Value); string value = ValidationHelper.GetString(convertedValue, "", CultureHelper.EnglishCulture); string displayName = ctrl.ValueDisplayName; if (String.IsNullOrEmpty(displayName)) { displayName = value; } MacroRuleParameter param = new MacroRuleParameter { Value = value, Text = displayName, ValueType = dataType }; childern.Parameters.Add(paramName, param); } } string macroRule = string.Format("Rule(\"{0}\", \"{1}\")", MacroElement.EscapeSpecialChars(main.GetCondition()), MacroElement.EscapeSpecialChars(main.GetXML())); if (!MacroSecurityProcessor.IsSimpleMacro(macroRule)) { // Sign complex macros macroRule = MacroSecurityProcessor.AddMacroSecurityParams(macroRule, MembershipContext.AuthenticatedUser.UserName); } fmr = new FieldMacroRule(); fmr.MacroRule = string.Format("{{%{0}%}}", macroRule); fmr.ErrorMessage = txtErrorMsg.Text; } return fmr; }
/// <summary> /// Loads the from definition from selected parameter into a BasicForm control. /// </summary> /// <param name="actual">If true, data from actual hiddens are loaded</param> private void LoadFormDefinition(bool actual) { MacroRuleTree selected = GetSelected((actual ? hdnParamSelected.Value : hdnLastSelected.Value)); if (selected != null) { string paramName = (actual ? hdnParam.Value.ToLowerCSafe() : hdnLastParam.Value.ToLowerCSafe()); MacroRuleParameter param = (MacroRuleParameter)selected.Parameters[paramName]; if (param != null) { FormInfo fi = new FormInfo(selected.RuleParameters); FormFieldInfo ffi = fi.GetFormField(paramName); if (ffi != null) { fi = new FormInfo(null); fi.AddFormField(ffi); // Add fake DisplayName field FormFieldInfo displayName = new FormFieldInfo(); displayName.Visible = false; displayName.Name = "DisplayName"; displayName.DataType = FormFieldDataTypeEnum.Text; fi.AddFormField(displayName); DataRow row = fi.GetDataRow().Table.NewRow(); if (ffi.AllowEmpty && string.IsNullOrEmpty(param.Value)) { if ((ffi.DataType != FormFieldDataTypeEnum.Text) && (ffi.DataType != FormFieldDataTypeEnum.LongText)) { row[paramName] = DBNull.Value; } } else { switch (ffi.DataType) { case FormFieldDataTypeEnum.Decimal: row[paramName] = ValidationHelper.GetDouble(param.Value, 0, CultureHelper.EnglishCulture); break; case FormFieldDataTypeEnum.Boolean: row[paramName] = ValidationHelper.GetBoolean(param.Value, false); break; case FormFieldDataTypeEnum.Integer: row[paramName] = ValidationHelper.GetInteger(param.Value, 0); break; case FormFieldDataTypeEnum.DateTime: row[paramName] = ValidationHelper.GetDateTime(param.Value, DataHelper.DATETIME_NOT_SELECTED, CultureHelper.EnglishCulture); break; case FormFieldDataTypeEnum.GUID: Guid guid = ValidationHelper.GetGuid(param.Value, Guid.Empty); if (guid != Guid.Empty) { row[paramName] = guid; } break; default: row[paramName] = param.Value; break; } } formElem.DataRow = row; formElem.FormInformation = fi; formElem.ReloadData(); } } } }