protected override DriverResult Editor(LayerPart layerPart, IUpdateModel updater, dynamic shapeHelper) { if (updater.TryUpdateModel(layerPart, Prefix, null, null)) { if (String.IsNullOrWhiteSpace(layerPart.LayerRule)) { layerPart.LayerRule = "true"; } if (_widgetsService.GetLayers() .Any(l => l.Id != layerPart.Id && String.Equals(l.Name, layerPart.Name, StringComparison.InvariantCultureIgnoreCase))) { updater.AddModelError("Name", T("A Layer with the same name already exists")); } try { _conditionManager.Matches(layerPart.LayerRule); } catch (Exception e) { updater.AddModelError("Description", T("The rule is not valid: {0}", e.Message)); } } return(Editor(layerPart, shapeHelper)); }
private int[] PopulateActiveLayers() { // Once the Condition 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 (_conditionManager.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()); }
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>().WithQueryHints(new QueryHints().ExpandParts <LayerPart>()).ForType("Layer").List(); var activeLayerIds = new List <int>(); foreach (var activeLayer in activeLayers) { // ignore the rule if it fails to execute try { var currentLayer = activeLayer; var layerRuleMatches = _glimpseService.PublishTimedAction(() => _conditionManager.Matches(currentLayer.Record.LayerRule), (r, t) => new LayerMessage { Active = r, Name = currentLayer.Record.Name, Rule = currentLayer.Record.LayerRule, EditUrl = GlimpseHelpers.AppendReturnUrl(_urlHelper.ItemAdminUrl(activeLayer), _urlHelper), Duration = t.Duration }, TimelineCategories.Layers, "Layer Evaluation", currentLayer.Record.Name).ActionResult; if (layerRuleMatches) { 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()); }
private bool EvaluateRule(string rule) { if (_evaluations.ContainsKey(rule)) { return(_evaluations[rule]); } var result = _conditionManager.Matches(rule); _evaluations[rule] = result; return(result); }
public void ProviderGetsCalledForExpression() { var result = _conditionManager.Matches("hello"); Assert.IsTrue(result); }
public void OnResultExecuting(ResultExecutingContext filterContext) { // layers and widgets should only run on a full view rendering result var viewResult = filterContext.Result as ViewResult; if (viewResult == null) { return; } var workContext = _workContextAccessor.GetContext(filterContext); if (workContext == null || workContext.Layout == null || workContext.CurrentSite == null || AdminFilter.IsApplied(filterContext.RequestContext) || !ThemeFilter.IsApplied(filterContext.RequestContext)) { return; } // Once the Rule Engine is done: // Get Layers and filter by zone and rule var activeLayers = _orchardServices.ContentManager.Query <LayerPart, LayerPartRecord>().List(); var activeLayerIds = new List <int>(); foreach (var activeLayer in activeLayers) { // ignore the rule if it fails to execute try { if (_conditionManager.Matches(activeLayer.Record.LayerRule)) { activeLayerIds.Add(activeLayer.ContentItem.Id); } } catch (Exception e) { Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text); } } var widgetParts = _widgetsService.GetWidgets(layerIds: activeLayerIds.ToArray()); // Build and add shape to zone. var zones = workContext.Layout.Zones; var defaultCulture = workContext.CurrentSite.As <SiteSettingsPart>().SiteCulture; var currentCulture = workContext.CurrentCulture; foreach (var widgetPart in widgetParts) { var commonPart = widgetPart.As <ICommonPart>(); if (commonPart == null || commonPart.Container == null) { Logger.Warning("The widget '{0}' is has no assigned layer or the layer does not exist.", widgetPart.Title); continue; } // ignore widget for different cultures var localizablePart = widgetPart.As <ILocalizableAspect>(); if (localizablePart != null) { // if localized culture is null then show if current culture is the default // this allows a user to show a content item for the default culture only if (localizablePart.Culture == null && defaultCulture != currentCulture) { continue; } // if culture is set, show only if current culture is the same if (localizablePart.Culture != null && localizablePart.Culture != currentCulture) { continue; } } // check permissions if (!_orchardServices.Authorizer.Authorize(Orchard.Core.Contents.Permissions.ViewContent, widgetPart)) { continue; } var useCache = UseCache(widgetPart, filterContext); var widgetShape = useCache ? BuildCachedWidgetShape(widgetPart, filterContext) : BuildWidgetShape(widgetPart); zones[widgetPart.Record.Zone].Add(widgetShape, widgetPart.Record.Position); } }