Skip to content

Commit

Permalink
Merge branch '1.10.x' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Orchard.Web/Modules/Orchard.Projections/Migrations.cs
  • Loading branch information
BenedekFarkas committed Apr 17, 2024
2 parents 3f1bf7f + 3a6810e commit 38d35ef
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Orchard.Localization;

namespace Orchard.Projections.FilterEditors.Forms {

public class StringFilterForm : IFormProvider {
public const string FormName = "StringFilter";

Expand All @@ -20,51 +19,56 @@ public StringFilterForm(IShapeFactory shapeFactory) {
}

public void Describe(DescribeContext context) {
Func<IShapeFactory, object> form =
shape => {
var f = Shape.Form(
Id: "StringFilter",
_Operator: Shape.SelectList(
Id: "operator", Name: "Operator",
Title: T("Operator"),
Size: 1,
Multiple: false
),
_Value: Shape.TextBox(
Id: "value", Name: "Value",
Title: T("Value"),
Classes: new[] { "text medium", "tokenized" },
Description: T("Enter the value the string should be.")
)
);
object form(IShapeFactory shape) {
var f = Shape.Form(
Id: "StringFilter",
_Operator: Shape.SelectList(
Id: "operator", Name: "Operator",
Title: T("Operator"),
Size: 1,
Multiple: false
),
_Value: Shape.TextBox(
Id: "value", Name: "Value",
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: T("Ignore filter if value is empty"),
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 });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Contains), Text = T("Contains").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.ContainsAny), Text = T("Contains any word").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.ContainsAll), Text = T("Contains all words").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Starts), Text = T("Starts with").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.NotStarts), Text = T("Does not start with").Text });
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
});
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 });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Contains), Text = T("Contains").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.ContainsAny), Text = T("Contains any word").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.ContainsAll), Text = T("Contains all words").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.Starts), Text = T("Starts with").Text });
f._Operator.Add(new SelectListItem { Value = Convert.ToString(StringOperator.NotStarts), Text = T("Does not start with").Text });
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 });

return f;
};
return f;
}

context.Form(FormName, form);
context.Form(FormName, (Func<IShapeFactory, object>)form);

}

public static Action<IHqlExpressionFactory> GetFilterPredicate(dynamic formState, string property) {
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 +96,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 +126,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 +142,6 @@ public enum StringOperator {
NotStarts,
Ends,
NotEnds,
NotContains,
ContainsAnyIfProvided
NotContains
}
}
}
17 changes: 16 additions & 1 deletion src/Orchard.Web/Modules/Orchard.Projections/Migrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Migrations : DataMigrationImpl {
private readonly IRepository<MemberBindingRecord> _memberBindingRepository;
private readonly IRepository<LayoutRecord> _layoutRepository;
private readonly IRepository<PropertyRecord> _propertyRecordRepository;
private readonly IRepository<FilterRecord> _filterRepository;

/// <summary>
/// When upgrading from "1.10.x" branch code committed after 1.10.3 to "dev" branch code or 1.11, merge
Expand All @@ -28,10 +29,12 @@ public class Migrations : DataMigrationImpl {
public Migrations(
IRepository<MemberBindingRecord> memberBindingRepository,
IRepository<LayoutRecord> layoutRepository,
IRepository<PropertyRecord> propertyRecordRepository) {
IRepository<PropertyRecord> propertyRecordRepository,
IRepository<FilterRecord> filterRepository) {
_memberBindingRepository = memberBindingRepository;
_layoutRepository = layoutRepository;
_propertyRecordRepository = propertyRecordRepository;
_filterRepository = filterRepository;

T = NullLocalizer.Instance;
}
Expand Down Expand Up @@ -402,6 +405,18 @@ public int UpdateFrom5() {
}

public int UpdateFrom6() {
// This change was originally UpdateFrom6 on 1.10.x and UpdateFrom6 on dev: Casting a somewhat wide net, but
// filters can't be queried by the form they are using and different types of filters can (and do) use
// StringFilterForm. However, the "Operator" parameter's value being "ContainsAnyIfProvided" is very
// specific.
var formStateToReplace = "<Operator>ContainsAnyIfProvided</Operator>";
var filterRecordsToUpdate = _filterRepository.Table.Where(f => f.State.Contains(formStateToReplace)).ToList();
foreach (var filter in filterRecordsToUpdate) {
filter.State = filter.State.Replace(
formStateToReplace,
"<Operator>ContainsAny</Operator><IgnoreFilterIfValueIsEmpty>true</IgnoreFilterIfValueIsEmpty>");
}

if (IsUpgradingFromOrchard_1_10_x_Version_6) {
MigratePropertyRecordToRewriteOutputCondition();
}
Expand Down

0 comments on commit 38d35ef

Please sign in to comment.