Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ public void Describe(DescribeContext context) {
Title: T("Value"),
Classes: new[] { "text medium", "tokenized" },
Description: T("Enter the value the string should be.")
)
);
),
_IgnoreIfEmptyValue: Shape.Checkbox(
Id: "IgnoreFilterIfValueIsEmpty",
Name: "IgnoreFilterIfValueIsEmpty",
Title: "Ignore filter if value is empty",
Comment thread
BenedekFarkas marked this conversation as resolved.
Outdated
Description: T("When enabled, the filter will not be applied if the provided value is or evaluates to empty."),
Value: "true"
));

f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Equals), Text = T("Is equal to").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.NotEquals), Text = T("Is not equal to").Text });
Expand All @@ -49,10 +55,6 @@ public void Describe(DescribeContext context) {
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Ends), Text = T("Ends with").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.NotEnds), Text = T("Does not end with").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.NotContains), Text = T("Does not contain").Text });
f._Operator.Add(new SelectListItem {
Value = Convert.ToString(StringOperator.ContainsAnyIfProvided),
Text = T("Contains any word (if any is provided)").Text
});

return f;
};
Expand All @@ -65,6 +67,11 @@ public static Action<IHqlExpressionFactory> GetFilterPredicate(dynamic formState
var op = (StringOperator)Enum.Parse(typeof(StringOperator), Convert.ToString(formState.Operator));
object value = Convert.ToString(formState.Value);

if (bool.TryParse(formState.IgnoreFilterIfValueIsEmpty?.ToString() ?? "", out bool ignoreIfEmpty)
&& ignoreIfEmpty
&& string.IsNullOrWhiteSpace(value as string))
return (ex) => { };

switch (op) {
case StringOperator.Equals:
return x => x.Eq(property, value);
Expand Down Expand Up @@ -92,14 +99,6 @@ public static Action<IHqlExpressionFactory> GetFilterPredicate(dynamic formState
return y => y.Not(x => x.Like(property, Convert.ToString(value), HqlMatchMode.End));
case StringOperator.NotContains:
return y => y.Not(x => x.Like(property, Convert.ToString(value), HqlMatchMode.Anywhere));
case StringOperator.ContainsAnyIfProvided:
if (string.IsNullOrWhiteSpace((string)value))
return x => x.IsNotEmpty("Id"); // basically, return every possible ContentItem
var values3 = Convert.ToString(value)
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var predicates3 = values3.Skip(1)
.Select<string, Action<IHqlExpressionFactory>>(x => y => y.Like(property, x, HqlMatchMode.Anywhere)).ToArray();
return x => x.Disjunction(y => y.Like(property, values3[0], HqlMatchMode.Anywhere), predicates3);
default:
throw new ArgumentOutOfRangeException();
}
Expand Down Expand Up @@ -130,11 +129,6 @@ public static LocalizedString DisplayFilter(string fieldName, dynamic formState,
return T("{0} does not end with '{1}'", fieldName, value);
case StringOperator.NotContains:
return T("{0} does not contain '{1}'", fieldName, value);
case StringOperator.ContainsAnyIfProvided:
return T("{0} contains any of '{1}' (or '{1}' is empty)",
fieldName,
new LocalizedString(string.Join("', '",
value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))));
default:
throw new ArgumentOutOfRangeException();
}
Expand All @@ -151,7 +145,6 @@ public enum StringOperator {
NotStarts,
Ends,
NotEnds,
NotContains,
ContainsAnyIfProvided
NotContains
}
}
}