Beispiel #1
0
        public ActionResult RecentlyAddedProductsRss(CatalogSearchQuery query)
        {
            // TODO: (mc) find a more prominent place for the "NewProducts" link (may be in main menu?)
            var protocol           = _services.WebHelper.IsCurrentConnectionSecured() ? "https" : "http";
            var selfLink           = Url.RouteUrl("RecentlyAddedProductsRSS", null, protocol);
            var recentProductsLink = Url.RouteUrl("RecentlyAddedProducts", null, protocol);

            var title = "{0} - {1}".FormatInvariant(_services.StoreContext.CurrentStore.Name, T("RSS.RecentlyAddedProducts"));

            var feed = new SmartSyndicationFeed(new Uri(recentProductsLink), title, T("RSS.InformationAboutProducts"));

            feed.AddNamespaces(true);
            feed.Init(selfLink, _services.WorkContext.WorkingLanguage);

            if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0)
            {
                return(new RssActionResult {
                    Feed = feed
                });
            }

            var items = new List <SyndicationItem>();

            query = query.SortBy(ProductSortingEnum.CreatedOn).Slice(0, _catalogSettings.RecentlyAddedProductsNumber);
            var result = _catalogSearchService.Search(query);

            var storeUrl = _services.StoreContext.CurrentStore.Url;

            foreach (var product in result.Hits)
            {
                string productUrl = Url.RouteUrl("Product", new { SeName = product.GetSeName() }, "http");
                if (productUrl.HasValue())
                {
                    var item = feed.CreateItem(
                        product.GetLocalized(x => x.Name),
                        product.GetLocalized(x => x.ShortDescription),
                        productUrl,
                        product.CreatedOnUtc,
                        product.FullDescription);

                    try
                    {
                        // we add only the first picture
                        var picture = product.ProductPictures.OrderBy(x => x.DisplayOrder).Select(x => x.Picture).FirstOrDefault();

                        if (picture != null)
                        {
                            feed.AddEnclosue(item, picture, _pictureService.GetPictureUrl(picture, _mediaSettings.ProductDetailsPictureSize, false, storeUrl));
                        }
                    }
                    catch { }

                    items.Add(item);
                }
            }

            feed.Items = items;

            _services.DisplayControl.AnnounceRange(result.Hits);

            return(new RssActionResult {
                Feed = feed
            });
        }
Beispiel #2
0
        public ActionResult RecentlyAddedProductsRss(CatalogSearchQuery query)
        {
            // TODO: (mc) find a more prominent place for the "NewProducts" link (may be in main menu?)
            var protocol           = Services.WebHelper.IsCurrentConnectionSecured() ? "https" : "http";
            var selfLink           = Url.RouteUrl("RecentlyAddedProductsRSS", null, protocol);
            var recentProductsLink = Url.RouteUrl("RecentlyAddedProducts", null, protocol);

            var title = "{0} - {1}".FormatInvariant(Services.StoreContext.CurrentStore.Name, T("RSS.RecentlyAddedProducts"));

            var feed = new SmartSyndicationFeed(new Uri(recentProductsLink), title, T("RSS.InformationAboutProducts"));

            feed.AddNamespaces(true);
            feed.Init(selfLink, Services.WorkContext.WorkingLanguage);

            if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0)
            {
                return(new RssActionResult {
                    Feed = feed
                });
            }

            var items = new List <SyndicationItem>();

            query.Sorting.Clear();
            query = query
                    .BuildFacetMap(false)
                    .SortBy(ProductSortingEnum.CreatedOn)
                    .Slice(0, _catalogSettings.RecentlyAddedProductsNumber);

            var result   = _catalogSearchService.Search(query);
            var storeUrl = Services.StoreService.GetHost(Services.StoreContext.CurrentStore);

            // Prefetching.
            var fileIds = result.Hits
                          .Select(x => x.MainPictureId ?? 0)
                          .Where(x => x != 0)
                          .Distinct()
                          .ToArray();
            var files = Services.MediaService.GetFilesByIds(fileIds).ToDictionarySafe(x => x.Id);

            foreach (var product in result.Hits)
            {
                var productUrl = Url.RouteUrl("Product", new { SeName = product.GetSeName() }, protocol);
                if (productUrl.HasValue())
                {
                    var content = product.GetLocalized(x => x.FullDescription).Value;

                    if (content.HasValue())
                    {
                        content = WebHelper.MakeAllUrlsAbsolute(content, Request);
                    }

                    var item = feed.CreateItem(
                        product.GetLocalized(x => x.Name),
                        product.GetLocalized(x => x.ShortDescription),
                        productUrl,
                        product.CreatedOnUtc,
                        content);

                    try
                    {
                        // We add only the first media file.
                        if (files.TryGetValue(product.MainPictureId ?? 0, out var file))
                        {
                            var url = Services.MediaService.GetUrl(file, _mediaSettings.ProductDetailsPictureSize, storeUrl, false);
                            feed.AddEnclosure(item, file.File, url);
                        }
                    }
                    catch { }

                    items.Add(item);
                }
            }

            feed.Items = items;

            Services.DisplayControl.AnnounceRange(result.Hits);

            return(new RssActionResult {
                Feed = feed
            });
        }