mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Merge pull request #5392 from paynecrl97/5356/abstract_active_layer_evaluation
Abstract active layer evaluation away from the Widget Filter
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
@@ -9,25 +8,24 @@ using Orchard.Logging;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Themes;
|
||||
using Orchard.UI.Admin;
|
||||
using Orchard.Widgets.Models;
|
||||
using Orchard.Widgets.Services;
|
||||
|
||||
namespace Orchard.Widgets.Filters {
|
||||
public class WidgetFilter : FilterProvider, IResultFilter {
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IRuleManager _ruleManager;
|
||||
private readonly IWidgetsService _widgetsService;
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly ILayerEvaluationService _layerEvaluationService;
|
||||
|
||||
public WidgetFilter(
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
IRuleManager ruleManager,
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
IWidgetsService widgetsService,
|
||||
IOrchardServices orchardServices) {
|
||||
IOrchardServices orchardServices,
|
||||
ILayerEvaluationService layerEvaluationService) {
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_ruleManager = ruleManager;
|
||||
_widgetsService = widgetsService;
|
||||
_orchardServices = orchardServices;
|
||||
_layerEvaluationService = layerEvaluationService;
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
@@ -51,25 +49,7 @@ namespace Orchard.Widgets.Filters {
|
||||
return;
|
||||
}
|
||||
|
||||
// Once the Rule Engine is done:
|
||||
// Get Layers and filter by zone and rule
|
||||
// NOTE: .ForType("Layer") is faster than .Query<LayerPart, LayerPartRecord>()
|
||||
IEnumerable<LayerPart> activeLayers = _orchardServices.ContentManager.Query<LayerPart>().ForType("Layer").List();
|
||||
|
||||
var activeLayerIds = new List<int>();
|
||||
foreach (var activeLayer in activeLayers) {
|
||||
// ignore the rule if it fails to execute
|
||||
try {
|
||||
if (_ruleManager.Matches(activeLayer.LayerRule)) {
|
||||
activeLayerIds.Add(activeLayer.ContentItem.Id);
|
||||
}
|
||||
}
|
||||
catch(Exception e) {
|
||||
Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<WidgetPart> widgetParts = _widgetsService.GetWidgets(layerIds: activeLayerIds.ToArray());
|
||||
var widgetParts = _widgetsService.GetWidgets(_layerEvaluationService.GetActiveLayerIds());
|
||||
|
||||
// Build and add shape to zone.
|
||||
var zones = workContext.Layout.Zones;
|
||||
|
@@ -90,6 +90,8 @@
|
||||
<Compile Include="RuleEngine\ContentDisplayedRuleProvider.cs" />
|
||||
<Compile Include="RuleEngine\RuleManager.cs" />
|
||||
<Compile Include="RuleEngine\UrlRuleProvider.cs" />
|
||||
<Compile Include="Services\DefaultLayerEvaluationService.cs" />
|
||||
<Compile Include="Services\ILayerEvaluationService.cs" />
|
||||
<Compile Include="Services\IRuleManager.cs" />
|
||||
<Compile Include="Services\IRuleProvider.cs" />
|
||||
<Compile Include="Services\IWidgetsService.cs" />
|
||||
|
@@ -20,7 +20,7 @@ namespace Orchard.Widgets.RuleEngine {
|
||||
public bool Matches(string expression) {
|
||||
var evaluator = _evaluators.FirstOrDefault();
|
||||
if (evaluator == null) {
|
||||
throw new OrchardException(T("There are currently not scripting engine enabled"));
|
||||
throw new OrchardException(T("There are currently no scripting engines enabled"));
|
||||
}
|
||||
|
||||
var result = evaluator.Evaluate(expression, new List<IGlobalMethodProvider> { new GlobalMethodProvider(this) });
|
||||
|
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Widgets.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Core.Common.Utilities;
|
||||
|
||||
namespace Orchard.Widgets.Services{
|
||||
public class DefaultLayerEvaluationService : ILayerEvaluationService {
|
||||
private readonly IRuleManager _ruleManager;
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
|
||||
private readonly LazyField<int[]> _activeLayerIDs;
|
||||
|
||||
public DefaultLayerEvaluationService(IRuleManager ruleManager, IOrchardServices orchardServices) {
|
||||
_ruleManager = ruleManager;
|
||||
_orchardServices = orchardServices;
|
||||
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
|
||||
_activeLayerIDs = new LazyField<int[]>();
|
||||
_activeLayerIDs.Loader(PopulateActiveLayers);
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves every Layer from the Content Manager and evaluates each one.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A collection of integers that represents the Ids of each active Layer
|
||||
/// </returns>
|
||||
public int[] GetActiveLayerIds() {
|
||||
return _activeLayerIDs.Value;
|
||||
}
|
||||
|
||||
private int[] PopulateActiveLayers() {
|
||||
// Once the Rule Engine is done:
|
||||
// Get Layers and filter by zone and rule
|
||||
// NOTE: .ForType("Layer") is faster than .Query<LayerPart, LayerPartRecord>()
|
||||
var activeLayers = _orchardServices.ContentManager.Query<LayerPart>().ForType("Layer").List();
|
||||
|
||||
var activeLayerIds = new List<int>();
|
||||
foreach (var activeLayer in activeLayers) {
|
||||
// ignore the rule if it fails to execute
|
||||
try {
|
||||
if (_ruleManager.Matches(activeLayer.LayerRule)) {
|
||||
activeLayerIds.Add(activeLayer.ContentItem.Id);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Logger.Warning(e, T("An error occurred during layer evaluation on: {0}", activeLayer.Name).Text);
|
||||
}
|
||||
}
|
||||
|
||||
return activeLayerIds.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
namespace Orchard.Widgets.Services
|
||||
{
|
||||
public interface ILayerEvaluationService : IDependency {
|
||||
int[] GetActiveLayerIds();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user