Exemplo n.º 1
0
        public Task <IEnumerable <WidgetInvoker> > GetWidgetsAsync(string zone, object model = null)
        {
            Guard.NotEmpty(zone, nameof(zone));

            var storeId = _storeContext.CurrentStore.Id;

            #region Module Widgets

            var widgets = _widgetService.LoadActiveWidgetsByWidgetZone(zone, storeId)
                          .Select(x => x.Value.GetDisplayWidget(zone, model, storeId))
                          .Where(x => x != null);

            #endregion

            #region Topic Widgets

            // TODO: (core) DefaultWidgetSelector > Determine topic widgets
            widgets = widgets.Concat(Enumerable.Empty <WidgetInvoker>());

            #endregion

            #region Request scoped widgets (provided by IWidgetProvider)

            widgets = widgets.Concat(_widgetProvider.GetWidgets(zone));

            #endregion

            widgets = widgets
                      .Distinct()
                      .OrderBy(x => x.Prepend)
                      .ThenBy(x => x.Order);

            return(Task.FromResult(widgets));
        }
Exemplo n.º 2
0
        public IEnumerable <WidgetInvoker> GetWidgets(string zone, object model = null)
        {
            Guard.NotEmpty(zone, nameof(zone));

            #region Plugin Widgets

            // TODO: (core) DefaultWidgetSelector > Determine static plugin widgets
            var widgets = Enumerable.Empty <WidgetInvoker>();

            #endregion

            #region Topic Widgets

            // TODO: (core) DefaultWidgetSelector > Determine topic widgets
            widgets = widgets.Concat(Enumerable.Empty <WidgetInvoker>());

            #endregion

            #region Request scoped widgets (provided by IWidgetProvider)

            widgets = widgets.Concat(_widgetProvider.GetWidgets(zone));

            #endregion

            return(widgets.OrderBy(x => x.Order));
        }
Exemplo n.º 3
0
        public virtual IEnumerable <WidgetRouteInfo> GetWidgets(string widgetZone, object model)
        {
            string actionName;
            string controllerName;
            RouteValueDictionary routeValues;
            var storeId = _storeContext.CurrentStore.Id;

            #region Plugin Widgets

            var widgets = _widgetService.LoadActiveWidgetsByWidgetZone(widgetZone, storeId);
            foreach (var widget in widgets)
            {
                widget.Value.GetDisplayWidgetRoute(widgetZone, model, storeId, out actionName, out controllerName, out routeValues);

                if (actionName.HasValue() && controllerName.HasValue())
                {
                    yield return(new WidgetRouteInfo
                    {
                        ActionName = actionName,
                        ControllerName = controllerName,
                        RouteValues = routeValues
                    });
                }
            }

            #endregion

            #region Topic Widgets

            // add special "topic widgets" to the list
            var allTopicsCacheKey = string.Format(ModelCacheEventConsumer.TOPIC_WIDGET_ALL_MODEL_KEY, storeId, _workContext.WorkingLanguage.Id, _workContext.CurrentCustomer.GetRolesIdent());
            // get topic widgets from STATIC cache
            var topicWidgets = _services.Cache.Get(allTopicsCacheKey, () =>
            {
                using (var scope = new DbContextScope(forceNoTracking: true))
                {
                    var allTopicWidgets = _topicService.GetAllTopics(storeId).AlterQuery(q =>
                    {
                        return(q.Where(x => x.RenderAsWidget));
                    });

                    var stubs = allTopicWidgets
                                .Select(t =>
                    {
                        var locTitle = t.GetLocalized(x => t.Title);
                        var locBody  = t.GetLocalized(x => t.Body, detectEmptyHtml: false);

                        return(new TopicWidgetStub
                        {
                            Id = t.Id,
                            Bordered = t.WidgetBordered,
                            WrapContent = !t.WidgetWrapContent.HasValue || t.WidgetWrapContent.Value,
                            ShowTitle = t.WidgetShowTitle,
                            SystemName = t.SystemName.SanitizeHtmlId(),
                            ShortTitle = t.GetLocalized(x => x.ShortTitle),
                            Title = locTitle,
                            TitleRtl = locTitle.CurrentLanguage.Rtl,
                            Intro = t.GetLocalized(x => x.Intro),
                            Body = locBody,
                            BodyRtl = locBody.CurrentLanguage.Rtl,
                            TitleTag = t.TitleTag,
                            WidgetZones = t.GetWidgetZones().ToArray(),
                            Priority = t.Priority
                        });
                    })
                                .OrderBy(t => t.Priority)
                                .ToList();
                    return(stubs);
                }
            });

            var byZoneTopicsCacheKey = "SmartStore.TopicWidgets.ZoneMapped";
            // save widgets to zones map in request cache
            var topicsByZone = _requestCache.Get(byZoneTopicsCacheKey, () =>
            {
                var map = new Multimap <string, WidgetRouteInfo>();

                foreach (var widget in topicWidgets)
                {
                    var zones = widget.WidgetZones;
                    if (zones != null && zones.Any())
                    {
                        foreach (var zone in zones.Select(x => x.ToLower()))
                        {
                            var routeInfo = new WidgetRouteInfo
                            {
                                ControllerName = "Topic",
                                ActionName     = "TopicWidget",
                                RouteValues    = new RouteValueDictionary()
                                {
                                    { "Namespaces", "SmartStore.Web.Controllers" },
                                    { "area", null },
                                    { "widgetZone", zone },
                                    { "model", new TopicWidgetModel
                                      {
                                          Id          = widget.Id,
                                          SystemName  = widget.SystemName,
                                          WrapContent = widget.WrapContent,
                                          ShowTitle   = widget.ShowTitle,
                                          IsBordered  = widget.Bordered,
                                          ShortTitle  = widget.ShortTitle.NullEmpty(),
                                          Title       = widget.Title.NullEmpty(),
                                          TitleTag    = widget.TitleTag ?? "h3",
                                          Intro       = widget.Intro.NullEmpty(),
                                          Html        = widget.Body,
                                          HtmlRtl     = widget.BodyRtl,
                                          TitleRtl    = widget.TitleRtl
                                      } }
                                }
                            };

                            map.Add(zone, routeInfo);
                        }
                    }
                }

                return(map);
            });

            if (topicsByZone.ContainsKey(widgetZone.ToLower()))
            {
                var zoneWidgets = topicsByZone[widgetZone.ToLower()];
                foreach (var topicWidget in zoneWidgets)
                {
                    // Handle OC announcement
                    var topicWidgetModel = topicWidget.RouteValues["model"] as TopicWidgetModel;
                    if (topicWidgetModel != null)
                    {
                        _services.DisplayControl.Announce(new Topic {
                            Id = topicWidgetModel.Id
                        });
                    }

                    yield return(topicWidget);
                }
            }

            #endregion

            #region Request scoped widgets (provided by IWidgetProvider)

            var requestScopedWidgets = _widgetProvider.GetWidgets(widgetZone);
            if (requestScopedWidgets != null)
            {
                foreach (var widget in requestScopedWidgets)
                {
                    yield return(widget);
                }
            }

            #endregion
        }
Exemplo n.º 4
0
        public async Task <IEnumerable <WidgetInvoker> > GetWidgetsAsync(string zone, object model = null)
        {
            Guard.NotEmpty(zone, nameof(zone));

            var storeId = _storeContext.CurrentStore.Id;

            #region Module Widgets

            var widgets = _widgetService.LoadActiveWidgetsByWidgetZone(zone, storeId)
                          .Select(x => x.Value.GetDisplayWidget(zone, model, storeId))
                          .Where(x => x != null);

            #endregion

            #region Topic Widgets

            // Get topic widgets from STATIC cache
            var allTopicsCacheKey = string.Format(TOPIC_WIDGET_ALL_MODEL_KEY, storeId, _workContext.WorkingLanguage.Id, _workContext.CurrentCustomer.GetRolesIdent());

            var topicWidgets = await _cache.GetAsync(allTopicsCacheKey, async() =>
            {
                var allTopicWidgets = await _db.Topics
                                      .AsNoTracking()
                                      .ApplyStandardFilter(customerRoleIds: _workContext.CurrentCustomer.GetRoleIds(), storeId: storeId)
                                      .Where(x => x.RenderAsWidget)
                                      .ToListAsync();

                var stubs = allTopicWidgets
                            .Select(t =>
                {
                    var locTitle = t.GetLocalized(x => t.Title);
                    var locBody  = t.GetLocalized(x => t.Body, detectEmptyHtml: false);

                    return(new TopicWidget
                    {
                        Id = t.Id,
                        Bordered = t.WidgetBordered,
                        WrapContent = !t.WidgetWrapContent.HasValue || t.WidgetWrapContent.Value,
                        ShowTitle = t.WidgetShowTitle,
                        SystemName = t.SystemName.SanitizeHtmlId(),
                        ShortTitle = t.GetLocalized(x => x.ShortTitle),
                        Title = locTitle,
                        TitleRtl = locTitle.CurrentLanguage.Rtl,
                        Intro = t.GetLocalized(x => x.Intro),
                        Body = locBody,
                        BodyRtl = locBody.CurrentLanguage.Rtl,
                        TitleTag = t.TitleTag,
                        WidgetZones = t.GetWidgetZones().ToArray(),
                        Priority = t.Priority,
                        CookieType = t.CookieType
                    });
                })
                            .OrderBy(t => t.Priority)
                            .ToList();

                return(stubs);
            });

            // Save widgets to zones map in request cache
            var topicsByZone = _requestCache.Get(ByZoneTopicsCacheKey, () =>
            {
                var map = new Multimap <string, TopicWidgetInvoker>(StringComparer.OrdinalIgnoreCase);

                foreach (var topicWidget in topicWidgets)
                {
                    var zones = topicWidget.WidgetZones;
                    if (zones != null && zones.Any())
                    {
                        foreach (var zone in zones)
                        {
                            var topicWidgetInvoker = new TopicWidgetInvoker(topicWidget);
                            map.Add(zone, topicWidgetInvoker);
                        }
                    }
                }

                return(map);
            });

            if (topicsByZone.ContainsKey(zone))
            {
                widgets = widgets.Concat(topicsByZone[zone]);
            }

            #endregion

            #region Request scoped widgets (provided by IWidgetProvider)

            widgets = widgets.Concat(_widgetProvider.GetWidgets(zone));

            #endregion

            widgets = widgets
                      .Distinct()
                      .OrderBy(x => x.Prepend)
                      .ThenBy(x => x.Order);

            return(widgets);
        }