Example #1
0
        public override void BuildDisplay(BuildDisplayContext context)
        {
            _drivers.Invoke(driver => {
                var timedResult = _performanceMonitor.Time(() => {
                    var result  = driver.BuildDisplay(context);
                    var publish = result != null;
                    if (publish)
                    {
                        result.Apply(context);
                    }

                    return(publish);
                });

                if (timedResult.ActionResult)
                {
                    _performanceMonitor.PublishMessage(new TimelineMessage
                    {
                        Duration      = timedResult.TimerResult.Duration,
                        Offset        = timedResult.TimerResult.Offset,
                        StartTime     = timedResult.TimerResult.StartTime,
                        EventName     = "Driver Build Display",
                        EventCategory = TimelineCategories.Parts.ToGlimpseTimelineCategoryItem(),
                        EventSubText  = context.ContentPart.PartDefinition.Name
                    });

                    string stereotype;
                    if (!context.ContentItem.TypeDefinition.Settings.TryGetValue("Stereotype", out stereotype))
                    {
                        stereotype = "Content";
                    }

                    _performanceMonitor.PublishMessage(new PartMessage
                    {
                        ContentItemName       = context.ContentItem.GetContentName(),
                        ContentItemType       = context.ContentItem.ContentType,
                        ContentItemStereotype = stereotype,
                        ContentItemId         = context.ContentItem.Id,
                        Fields          = context.ContentPart.Fields,
                        ContentPartType = context.ContentPart.PartDefinition.Name,
                        Duration        = timedResult.TimerResult.Duration
                    });
                }
            }, Logger);
        }
Example #2
0
        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
            IEnumerable <LayerPart> 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
                {
                    var currentLayer     = activeLayer;
                    var layerRuleMatches = _performanceMonitor.PublishTimedAction(() => _ruleManager.Matches(currentLayer.Record.LayerRule), (r, t) => new LayerMessage
                    {
                        Active   = r,
                        Name     = currentLayer.Record.Name,
                        Rule     = currentLayer.Record.LayerRule,
                        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 occured during layer evaluation on: {0}", activeLayer.Name).Text);
                }
            }

            IEnumerable <WidgetPart> 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(global::Orchard.Core.Contents.Permissions.ViewContent, widgetPart))
                {
                    continue;
                }

                var scopedWidgetPart       = widgetPart;
                var widgetBuildDisplayTime = _performanceMonitor.Time(() => _orchardServices.ContentManager.BuildDisplay(scopedWidgetPart));
                var widgetShape            = widgetBuildDisplayTime.ActionResult;

                _performanceMonitor.PublishMessage(new WidgetMessage
                {
                    Title         = widgetPart.Title,
                    Type          = widgetPart.ContentItem.ContentType,
                    Zone          = widgetPart.Zone,
                    Layer         = widgetPart.LayerPart,
                    Position      = widgetPart.Position,
                    TechnicalName = widgetPart.Name,
                    Duration      = widgetBuildDisplayTime.TimerResult.Duration
                });

                zones[widgetPart.Record.Zone].Add(widgetShape, widgetPart.Record.Position);
            }
        }