Esempio n. 1
0
        public ActionResult Index(int?layerId)
        {
            ExtensionDescriptor currentTheme = _siteThemeService.GetSiteTheme();

            if (currentTheme == null)
            {
                Services.Notifier.Error(T("To manage widgets you must have a theme enabled."));
                return(RedirectToAction("Index", "Admin", new { area = "Dashboard" }));
            }

            IEnumerable <LayerPart> layers = _widgetsService.GetLayers().ToList();

            if (!layers.Any())
            {
                Services.Notifier.Error(T("There are no widget layers defined. A layer will need to be added in order to add widgets to any part of the site."));
                return(RedirectToAction("AddLayer"));
            }

            LayerPart currentLayer = layerId == null
                ? layers.FirstOrDefault()
                : layers.FirstOrDefault(layer => layer.Id == layerId);

            if (currentLayer == null && layerId != null)   // Incorrect layer id passed
            {
                Services.Notifier.Error(T("Layer not found: {0}", layerId));
                return(RedirectToAction("Index"));
            }

            IEnumerable <string> allZones           = _widgetsService.GetZones();
            IEnumerable <string> currentThemesZones = _widgetsService.GetZones(currentTheme);

            string zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id);
            string zonePreviewImage     = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null;

            dynamic viewModel = Shape.ViewModel()
                                .CurrentTheme(currentTheme)
                                .CurrentLayer(currentLayer)
                                .Layers(layers)
                                .Widgets(_widgetsService.GetWidgets())
                                .Zones(currentThemesZones)
                                .OrphanZones(allZones.Except(currentThemesZones))
                                .OrphanWidgets(_widgetsService.GetOrphanedWidgets())
                                .ZonePreviewImage(zonePreviewImage);

            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
            return(View((object)viewModel));
        }
Esempio n. 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))
            {
                return;
            }

            // Once the Rule Engine is done:
            // Get Layers and filter by zone and rule
            IEnumerable <WidgetPart> widgetParts  = _widgetsService.GetWidgets();
            IEnumerable <LayerPart>  activeLayers = _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 (_ruleManager.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);
                }
            }

            // Build and add shape to zone.
            var zones = workContext.Layout.Zones;

            foreach (var widgetPart in widgetParts)
            {
                if (activeLayerIds.Contains(widgetPart.As <ICommonPart>().Container.ContentItem.Id))
                {
                    var widgetShape = _contentManager.BuildDisplay(widgetPart);
                    zones[widgetPart.Record.Zone].Add(widgetShape, widgetPart.Record.Position);
                }
            }
        }
        public override IEnumerable <TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition)
        {
            if (definition.PartDefinition.Name != "ContentWidgetsPart")
            {
                yield break;
            }

            var settings = definition.Settings.GetModel <ContentWidgetsTypePartSettings>();

            var attachedWidgetIds = IdSerializer.DeserializeIds(settings.AttachedWidgetIdsDefinition);

            var viewModel = new ContentWidgetsViewModel();

            viewModel.Widgets = (from widget in _widgetService.GetWidgets()
                                 select new ContentWidget
            {
                Id = widget.Id,
                Title = widget.Title,
                IsAttached = attachedWidgetIds.Contains(widget.Id)
            }).ToList();

            yield return(DefinitionTemplate(viewModel, "ContentWidgetsTypePartSettings", Prefix));
        }
        public IEnumerable <CachedLayerModel> GetLayers()
        {
            return(_cacheService.Get(PopulatedLayersCacheKey, () =>
            {
                //get all the layers (all layers and widget containers are individually cached because they can be invalidated independently of each other)
                var layers = _cacheService.Get(AllLayersCacheKey, () =>
                                               _orchardServices.ContentManager.Query <LayerPart, LayerPartRecord>().List().Select(p => new CachedLayerModel
                {
                    Id = p.Id,
                    Name = p.Name,
                    Rule = p.LayerRule
                })
                                               );


                //get a collection of widget containers
                var widgetContainers = _cacheService.Get(WidgetContainersCacheKey, () =>
                {
                    var allWidgets = _widgetsService.GetWidgets(layers.Select(l => l.Id).ToArray());
                    var containerIds = new List <int>();

                    foreach (var widgetPart in allWidgets)
                    {
                        var commonPart = widgetPart.As <ICommonPart>();
                        if (commonPart != null && commonPart.Container != null)
                        {
                            containerIds.Add(commonPart.Container.Id);
                        }
                    }

                    return containerIds;
                });

                //return the layers that are also in the collection of widget containers
                return layers.Where(l => widgetContainers.Contains(l.Id));
            }));
        }
        public ActionResult Index(int?layerId, string culture)
        {
            ExtensionDescriptor currentTheme = _siteThemeService.GetSiteTheme();

            if (currentTheme == null)
            {
                Services.Notifier.Error(T("To manage widgets you must have a theme enabled."));
                return(RedirectToAction("Index", "Admin", new { area = "Dashboard" }));
            }

            IEnumerable <LayerPart> layers = _widgetsService.GetLayers().OrderBy(x => x.Name).ToList();

            if (!layers.Any())
            {
                Services.Notifier.Error(T("There are no widget layers defined. A layer will need to be added in order to add widgets to any part of the site."));
                return(RedirectToAction("AddLayer"));
            }

            LayerPart currentLayer = layerId == null
                                     // look for the "Default" layer, or take the first if it doesn't exist
                ? layers.FirstOrDefault(x => x.Name == "Default") ?? layers.FirstOrDefault()
                : layers.FirstOrDefault(layer => layer.Id == layerId);

            if (currentLayer == null && layerId != null)   // Incorrect layer id passed
            {
                Services.Notifier.Error(T("Layer not found: {0}", layerId));
                return(RedirectToAction("Index"));
            }

            IEnumerable <string> allZones           = _widgetsService.GetZones();
            IEnumerable <string> currentThemesZones = _widgetsService.GetZones(currentTheme);

            string zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id);
            string zonePreviewImage     = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null;

            var widgets = _widgetsService.GetWidgets();

            if (!String.IsNullOrWhiteSpace(culture))
            {
                widgets = widgets.Where(x => {
                    if (x.Has <ILocalizableAspect>())
                    {
                        return(String.Equals(x.As <ILocalizableAspect>().Culture, culture, StringComparison.InvariantCultureIgnoreCase));
                    }

                    return(false);
                }).ToList();
            }

            var viewModel = Shape.ViewModel()
                            .CurrentTheme(currentTheme)
                            .CurrentLayer(currentLayer)
                            .CurrentCulture(culture)
                            .Layers(layers)
                            .Widgets(widgets)
                            .Zones(currentThemesZones)
                            .Cultures(_cultureManager.ListCultures())
                            .OrphanZones(allZones.Except(currentThemesZones))
                            .OrphanWidgets(_widgetsService.GetOrphanedWidgets())
                            .ZonePreviewImage(zonePreviewImage);

            return(View(viewModel));
        }
Esempio n. 6
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;
            }

            var widgetParts = _widgetsService.GetWidgets(_layerEvaluationService.GetActiveLayerIds());

            // 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}' 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 (!_tomeltServices.Authorizer.Authorize(Core.Contents.Permissions.ViewContent, widgetPart))
                {
                    continue;
                }

                var widgetShape = _tomeltServices.ContentManager.BuildDisplay(widgetPart);
                zones[widgetPart.Zone].Add(widgetShape, widgetPart.Position);
            }
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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.HttpContext);

            if (workContext == null ||
                workContext.Layout == null ||
                workContext.CurrentSite == null ||
                AdminFilter.IsApplied(filterContext.RequestContext) ||
                !ThemeFilter.IsApplied(filterContext.RequestContext))
            {
                return;
            }

            IEnumerable <WidgetPart> widgetParts = _widgetsService.GetWidgets(_layerEvaluationService.GetActiveLayerIds().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 widgetShape      = _performanceMonitor.PublishTimedAction(() => _orchardServices.ContentManager.BuildDisplay(scopedWidgetPart), (r, t) =>
                                                                              new WidgetMessage
                {
                    Title         = scopedWidgetPart.Title,
                    Type          = scopedWidgetPart.ContentItem.ContentType,
                    Zone          = scopedWidgetPart.Zone,
                    Layer         = scopedWidgetPart.LayerPart,
                    Position      = scopedWidgetPart.Position,
                    TechnicalName = scopedWidgetPart.Name,
                    Duration      = t.Duration
                }, TimelineCategories.Widgets, string.Format("Build Display ({0})", scopedWidgetPart.ContentItem.ContentType), scopedWidgetPart.Title);

                zones[widgetPart.Record.Zone].Add(widgetShape.ActionResult, widgetPart.Record.Position);
            }
        }
        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
            // 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());

            // 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(Core.Contents.Permissions.ViewContent, widgetPart))
                {
                    continue;
                }

                var widgetShape = _orchardServices.ContentManager.BuildDisplay(widgetPart);
                zones[widgetPart.Zone].Add(widgetShape, widgetPart.Position);
            }
        }
Esempio n. 10
0
        public async Task <IActionResult> GetWidgets()
        {
            WidgetsModel widgets = await widgetsService.GetWidgets();

            return(Ok(widgets));
        }