Esempio n. 1
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);
            // get topic widgets from STATIC cache
            var topicWidgets = _services.Cache.Get(allTopicsCacheKey, () =>
            {
                using (var scope = new DbContextScope(forceNoTracking: true))
                {
                    var allTopicWidgets = _topicService.GetAllTopics(storeId).Where(x => x.RenderAsWidget).ToList();
                    var stubs           = allTopicWidgets
                                          .Select(t => new TopicWidgetStub
                    {
                        Id          = t.Id,
                        Bordered    = t.WidgetBordered,
                        WrapContent = !t.WidgetWrapContent.HasValue || t.WidgetWrapContent.Value,
                        ShowTitle   = t.WidgetShowTitle,
                        SystemName  = t.SystemName.SanitizeHtmlId(),
                        Title       = t.GetLocalized(x => t.Title),
                        TitleTag    = t.TitleTag,
                        Body        = t.GetLocalized(x => t.Body),
                        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,
                                          Title       = String.IsNullOrEmpty(widget.Title) ? "div" : widget.Title,
                                          TitleTag    = widget.TitleTag ?? "h3",
                                          Html        = widget.Body
                                      } }
                                }
                            };
                            map.Add(zone, routeInfo);
                        }
                    }
                }

                return(map);

                #region Obsolete
                //var result = from t in topicWidgets
                //			 where t.WidgetZones.Contains(widgetZone, StringComparer.InvariantCultureIgnoreCase)
                //			 orderby t.Priority
                //			 select new WidgetRouteInfo
                //			 {
                //				 ControllerName = "Topic",
                //				 ActionName = "TopicWidget",
                //				 RouteValues = new RouteValueDictionary()
                //				 {
                //					{"Namespaces", "SmartStore.Web.Controllers"},
                //					{"area", null},
                //					{"widgetZone", widgetZone},
                //					{"model", new TopicWidgetModel
                //					{
                //						Id = t.Id,
                //						SystemName = t.SystemName,
                //						ShowTitle = t.ShowTitle,
                //						IsBordered = t.Bordered,
                //						Title = t.Title,
                //						Html = t.Body
                //					} }
                //				 }
                //			 };

                //return result.ToList();
                #endregion
            });

            if (topicsByZone.ContainsKey(widgetZone.ToLower()))
            {
                var zoneWidgets = topicsByZone[widgetZone.ToLower()];
                foreach (var topicWidget in zoneWidgets)
                {
                    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
        }
Esempio n. 2
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
        }