/CMSFormControls/Filters/NumberFilter.ascx.cs

https://bitbucket.org/kudutest2/kenticogit · C# · 188 lines · 128 code · 29 blank · 31 comment · 17 complexity · 4b6946375a05973ab07197085badf4cb MD5 · raw file

  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using CMS.FormControls;
  6. using CMS.GlobalHelper;
  7. using CMS.FormEngine;
  8. using CMS.EventLog;
  9. using CMS.SettingsProvider;
  10. public partial class CMSFormControls_Filters_NumberFilter : FormEngineUserControl, IFilterFormControl
  11. {
  12. protected string mOperatorFieldName = null;
  13. #region "Properties"
  14. /// <summary>
  15. /// Gets or sets value.
  16. /// </summary>
  17. public override object Value
  18. {
  19. get
  20. {
  21. return txtText.Text;
  22. }
  23. set
  24. {
  25. // Load default value on insert
  26. if ((FieldInfo != null) && (FieldInfo.DataType == FormFieldDataTypeEnum.Decimal))
  27. {
  28. txtText.Text = FormHelper.GetDoubleValueInCurrentCulture(value);
  29. }
  30. else
  31. {
  32. txtText.Text = ValidationHelper.GetString(value, null);
  33. }
  34. }
  35. }
  36. /// <summary>
  37. /// Gets name of the field for operator value. Default value is 'Operator'.
  38. /// </summary>
  39. protected string OperatorFieldName
  40. {
  41. get
  42. {
  43. if (string.IsNullOrEmpty(mOperatorFieldName))
  44. {
  45. // Get name of the field for operator value
  46. mOperatorFieldName = DataHelper.GetNotEmpty(GetValue("OperatorFieldName"), "Operator");
  47. }
  48. return mOperatorFieldName;
  49. }
  50. }
  51. /// <summary>
  52. /// Gets or sets default operator to use for the first inicialization of the control.
  53. /// </summary>
  54. public string DefaultOperator
  55. {
  56. get;
  57. set;
  58. }
  59. #endregion
  60. #region "Methods"
  61. protected void Page_Load(object sender, EventArgs e)
  62. {
  63. CheckFieldEmptiness = false;
  64. InitFilterDropDown();
  65. if (ContainsColumn(OperatorFieldName))
  66. {
  67. drpOperator.SelectedValue = ValidationHelper.GetString(this.Form.Data.GetValue(OperatorFieldName), "0");
  68. }
  69. else
  70. {
  71. // Set default operator
  72. if (!RequestHelper.IsPostBack() && (DefaultOperator != null))
  73. {
  74. drpOperator.SelectedValue = DefaultOperator;
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// Returns other values related to this form control.
  80. /// </summary>
  81. /// <returns>Returns an array where first dimension is attribute name and the second dimension is its value.</returns>
  82. public override object[,] GetOtherValues()
  83. {
  84. if (Form.Data is DataRowContainer)
  85. {
  86. if (!ContainsColumn(OperatorFieldName))
  87. {
  88. Form.DataRow.Table.Columns.Add(OperatorFieldName);
  89. }
  90. // Set properties names
  91. object[,] values = new object[3, 2];
  92. values[0, 0] = OperatorFieldName;
  93. values[0, 1] = drpOperator.SelectedValue;
  94. return values;
  95. }
  96. return null;
  97. }
  98. /// <summary>
  99. /// Initializes operator filter dropdown list.
  100. /// </summary>
  101. private void InitFilterDropDown()
  102. {
  103. if (drpOperator.Items.Count == 0)
  104. {
  105. drpOperator.Items.Add(new ListItem("=", "="));
  106. drpOperator.Items.Add(new ListItem("<>", "<>"));
  107. drpOperator.Items.Add(new ListItem("<", "<"));
  108. drpOperator.Items.Add(new ListItem("<=", "<="));
  109. drpOperator.Items.Add(new ListItem(">", ">"));
  110. drpOperator.Items.Add(new ListItem(">=", ">="));
  111. }
  112. }
  113. /// <summary>
  114. /// Gets where condition.
  115. /// </summary>
  116. public override string GetWhereCondition()
  117. {
  118. string tempVal = ValidationHelper.GetString(Value, string.Empty).Trim();
  119. string op = drpOperator.SelectedValue;
  120. // No condition
  121. if (string.IsNullOrEmpty(tempVal) || string.IsNullOrEmpty(op))
  122. {
  123. return null;
  124. }
  125. // Only number
  126. if (ValidationHelper.IsDouble(tempVal) || ValidationHelper.IsInteger(tempVal))
  127. {
  128. try
  129. {
  130. // Format where condition
  131. return string.Format(WhereConditionFormat, FieldInfo.Name, SqlHelperClass.GetSafeQueryString(tempVal, false), op);
  132. }
  133. catch (Exception ex)
  134. {
  135. // Log exception
  136. EventLogProvider.LogException("NumberFilter", "GetWhereCondition", ex);
  137. }
  138. }
  139. return null;
  140. }
  141. #endregion
  142. #region "IFilterFormControl Members"
  143. /// <summary>
  144. /// Where condition format.
  145. /// </summary>
  146. public string WhereConditionFormat
  147. {
  148. get
  149. {
  150. // Get filter where condition format or default format
  151. return DataHelper.GetNotEmpty(GetValue("WhereConditionFormat"), "[{0}] {2} {1}");
  152. }
  153. set
  154. {
  155. SetValue("WhereConditionFormat", value);
  156. }
  157. }
  158. #endregion
  159. }