diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TermCountProcessor.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TermCountProcessor.cs index 80aa73cd2..4db516934 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TermCountProcessor.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TermCountProcessor.cs @@ -1,3 +1,7 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; using Orchard.ContentManagement; using Orchard.Taxonomies.Models; @@ -9,20 +13,31 @@ namespace Orchard.Taxonomies.Services { _taxonomyService = taxonomyService; } - public void Process(params int[] termPartRecordIds) { - + public void Process(IEnumerable termPartRecordIds) + { + var processedTermPartRecordIds = new List(); foreach (var id in termPartRecordIds) { - var termPart = _taxonomyService.GetTerm(id); - while (termPart != null) { - termPart.Count = (int)_taxonomyService.GetContentItemsCount(termPart); - - // compute count for the hierarchy too - if (termPart.Container != null) { - var parentTerm = termPart.Container.As(); - termPart = parentTerm; + if (!processedTermPartRecordIds.Contains(id)) { + var termPart = _taxonomyService.GetTerm(id); + if (termPart != null) { + ProcessTerm(termPart, processedTermPartRecordIds); } } } } + + private void ProcessTerm(TermPart termPart, ICollection processedTermPartRecordIds) + { + termPart.Count = (int)_taxonomyService.GetContentItemsCount(termPart); + processedTermPartRecordIds.Add(termPart.Id); + + // Look for a parent term that has not yet been processed + if (termPart.Container != null) { + var parentTerm = termPart.Container.As(); + if (parentTerm != null && !processedTermPartRecordIds.Contains(parentTerm.Id)) { + ProcessTerm(parentTerm, processedTermPartRecordIds); + } + } + } } }