diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/LocalizedTaxonomyPartHandler.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/LocalizedTaxonomyPartHandler.cs index fe5a3032c..575f5d8dd 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/LocalizedTaxonomyPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/LocalizedTaxonomyPartHandler.cs @@ -38,35 +38,46 @@ namespace Orchard.Taxonomies.Handlers { public Localizer T { get; set; } private void ImportLocalizedTerms(PublishContentContext context, TaxonomyPart part) { - // When saving a Taxonomy translation I automatically move to the taxonomy any term in the corresponding language associated to the other translations + // When saving a Taxonomy translation I automatically move to the taxonomy any term + // in the corresponding language associated to the other translations bool termsMoved = false; - + // of course we require the current taxonomy be localized if (context.ContentItem.Has()) { var taxonomyCulture = context.ContentItem.As().Culture; if (taxonomyCulture != null) { - var taxonomyIds = _localizationService.GetLocalizations(context.ContentItem).Select(x => x.Id); - - foreach (var taxonomyId in taxonomyIds) { - var parentTaxonomyCulture = _taxonomyService.GetTaxonomy(taxonomyId).As().Culture; - var termIds = _taxonomyService.GetTerms(taxonomyId).Select(x => x.Id); - - foreach (var termId in termIds) { - var term = _taxonomyService.GetTerm(termId); - var parentTerm = _taxonomyExtensionsService.GetParentTerm(term); - if (term.Has()) { - var termCulture = term.As().Culture; - - if (termCulture != null && termCulture != parentTaxonomyCulture && termCulture == taxonomyCulture) { - term.TaxonomyId = context.ContentItem.Id; - term.Path = parentTerm != null ? parentTerm.As().FullPath + "/" : "/"; - if (parentTerm == null) - term.Container = context.ContentItem; - - _taxonomyExtensionsService.RegenerateAutoroute(term.ContentItem); - - termsMoved = true; + // get all localizations of the current taxonomy (except the current taxonomy itself) + var taxonomyLocalizations = _localizationService + .GetLocalizations(context.ContentItem) + .Where(lp => lp.Id != context.ContentItem.Id); + foreach (var localizationPart in taxonomyLocalizations) { + // in each localization, we should look for the terms that are already + // in the culture of the current taxonomy to move them here. + var parentCulture = localizationPart.Culture; + var terms = _taxonomyService + // all the terms in the localized taxonomy for this iteration + .GetTerms(localizationPart.Id) + .Where(t => { + // term should have a LocalizationPart for the analysis here + var lp = t.As(); + if (lp == null) { + return false; } + var tc = lp.Culture; + // the culture of the term should be the same as our current taxonomy, + // and different from its current taxonomy + return tc != null && tc != parentCulture && tc == taxonomyCulture; + }); + termsMoved = terms.Any(); + foreach (var term in terms) { + // moving a term moves its children as well. This means that we may + // have already moved a term if we moved its parent. + if (term.TaxonomyId != part.Id) { + // we use the service method to make the move, because that + // will take care of recomputing all weights + _taxonomyService.MoveTerm(part, term, null); } + // update the autoroute so it respects the rules + _taxonomyExtensionsService.RegenerateAutoroute(term.ContentItem); } } } diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs index 3c4c0b066..79be9f491 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs @@ -375,10 +375,31 @@ namespace Orchard.Taxonomies.Services { // compute new path and publish. This also computes the new weight and // recursively does the same for siblings and children of the TermPart // that was moved. + // In case we are changing the taxonomy, we will have to properly evict + // caches + if (taxonomy.Id != term.TaxonomyId) { + if (_termFamilies == null) { + _termFamilies = new Dictionary>(); + } else { + // evict the old cache for the term and all children + var oldKey = term.TaxonomyId.ToString() + (term.Path ?? string.Empty); + if (_termFamilies.ContainsKey(oldKey)) { + _termFamilies.Remove(oldKey); + } + foreach (var child in children) { + oldKey = child.TaxonomyId.ToString() + (child.Path ?? string.Empty); + if (_termFamilies.ContainsKey(oldKey)) { + _termFamilies.Remove(oldKey); + } + } + } + } + term.TaxonomyId = taxonomy.Id; term.Container = parentTerm == null ? taxonomy.ContentItem : parentTerm.ContentItem; ProcessPath(term); // process the children foreach (var child in children) { + child.TaxonomyId = taxonomy.Id; ProcessPath(child); } }