Esempio n. 1
0
 /// <summary>
 /// Returns an RSS feed URL specific to this tag
 /// </summary>
 /// <param name="url"></param>
 /// <param name="model"></param>
 /// <returns></returns>
 public static string ArticulateTagRssUrl(this UrlHelper url, PostsByTagModel model)
 {
     return model.TagUrl.EnsureEndsWith('/') + "rss";
 }
        internal static IEnumerable<PostsByTagModel> GetContentByTags(this UmbracoHelper helper, IMasterModel masterModel, string tagGroup, string baseUrlName)
        {
            var tags = helper.TagQuery.GetAllContentTags(tagGroup).ToArray();
            if (!tags.Any())
            {
                return Enumerable.Empty<PostsByTagModel>();
            }

            //TODO: We want to use the core for this but it's not available, this needs to be implemented: http://issues.umbraco.org/issue/U4-9290

            var appContext = helper.UmbracoContext.Application;
            var sqlSyntax = appContext.DatabaseContext.SqlSyntax;

            Func<IEnumerable<PostsByTagModel>> getResult = () =>
            {
                var taggedContent = new List<TagDto>();

                //process in groups to not exceed the max SQL params
                foreach (var tagBatch in tags.InGroupsOf(2000))
                {
                    var sql = GetTagQuery("cmsTagRelationship.nodeId, cmsTagRelationship.tagId, cmsTags.tag", masterModel, sqlSyntax)
                        .Where("tagId IN (@tagIds) AND cmsTags." + sqlSyntax.GetQuotedColumnName("group") + " = @tagGroup", new
                        {
                            tagIds = tagBatch.Select(x => x.Id).ToArray(),
                            tagGroup = tagGroup
                        });
                    taggedContent.AddRange(appContext.DatabaseContext.Database.Fetch<TagDto>(sql));
                }

                var result = new List<PostsByTagModel>();
                foreach (var groupedTags in taggedContent.GroupBy(x => x.TagId))
                {
                    //will be the same tag name for all of these tag Ids
                    var tagName = groupedTags.First().Tag;

                    var publishedContent = helper.TypedContent(groupedTags.Select(t => t.NodeId).Distinct()).WhereNotNull();

                    var model = new PostsByTagModel(
                        publishedContent.Select(c => new PostModel(c)).OrderByDescending(c => c.PublishedDate),
                        tagName,
                        masterModel.RootBlogNode.Url.EnsureEndsWith('/') + baseUrlName + "/" + tagName.ToLowerInvariant());

                    result.Add(model);
                }

                return result.OrderBy(x => x.TagName).ToArray();
            };

            #if DEBUG
            return getResult();
            #else
            //cache this result for a short amount of time
            return appContext.ApplicationCache.RuntimeCache.GetCacheItem<IEnumerable<PostsByTagModel>>(
                string.Concat(typeof(UmbracoHelperExtensions).Name, "GetContentByTags", masterModel.RootBlogNode.Id, tagGroup),
                getResult, TimeSpan.FromSeconds(30));
            #endif
        }