Rewritten the handler to avoid fetching stuff too often from the repository. Changed the MoveTerm method to handle the case where the term is being moved to a different taxonomy (e.g. when translating). (#8295)

This commit is contained in:
Matteo Piovanelli
2019-12-05 21:31:04 +01:00
committed by Sébastien Ros
parent c13c008f74
commit 40eca06645
2 changed files with 55 additions and 23 deletions

View File

@@ -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<LocalizationPart>()) {
var taxonomyCulture = context.ContentItem.As<LocalizationPart>().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<LocalizationPart>().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<LocalizationPart>()) {
var termCulture = term.As<LocalizationPart>().Culture;
if (termCulture != null && termCulture != parentTaxonomyCulture && termCulture == taxonomyCulture) {
term.TaxonomyId = context.ContentItem.Id;
term.Path = parentTerm != null ? parentTerm.As<TermPart>().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<LocalizationPart>();
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);
}
}
}

View File

@@ -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<string, IEnumerable<TermPart>>();
} 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);
}
}