From a890164681f1747ce9ac780a518de2994626e900 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 12 Jul 2013 11:59:40 -0700 Subject: [PATCH] #19414: Fixing taxonomies renames Work Item: 19414 --HG-- branch : 1.x --- .../Handlers/TaxonomyPartHandler.cs | 47 +++++++++++++++++-- .../Services/ITaxonomyService.cs | 1 - .../Services/TaxonomyService.cs | 9 +--- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/TaxonomyPartHandler.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/TaxonomyPartHandler.cs index 6d838a056..e42cbf83a 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/TaxonomyPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Handlers/TaxonomyPartHandler.cs @@ -1,16 +1,57 @@ -using Orchard.Taxonomies.Services; +using Orchard.ContentManagement; +using Orchard.ContentManagement.MetaData; +using Orchard.Core.Title.Models; +using Orchard.Taxonomies.Fields; +using Orchard.Taxonomies.Services; using JetBrains.Annotations; using Orchard.Taxonomies.Models; using Orchard.ContentManagement.Handlers; using Orchard.Data; +using Orchard.Taxonomies.Settings; namespace Orchard.Taxonomies.Handlers { [UsedImplicitly] public class TaxonomyPartHandler : ContentHandler { - public TaxonomyPartHandler(IRepository repository, ITaxonomyService taxonomyService) { + public TaxonomyPartHandler( + IRepository repository, + ITaxonomyService taxonomyService, + IContentDefinitionManager contentDefinitionManager) { + + string previousName = null; + Filters.Add(StorageFilter.For(repository)); - OnPublished((context, part) => taxonomyService.CreateTermContentType(part)); + OnPublished((context, part) => { + taxonomyService.CreateTermContentType(part); + + if (previousName != null && part.Name != previousName) { + + // remove previous term type + contentDefinitionManager.DeleteTypeDefinition(previousName + " Term"); + + // update existing fields + foreach (var partDefinition in contentDefinitionManager.ListPartDefinitions()) { + foreach (var field in partDefinition.Fields) { + if (field.FieldDefinition.Name == typeof (TaxonomyField).Name) { + + if (field.Settings.GetModel().Taxonomy == previousName) { + contentDefinitionManager.AlterPartDefinition(partDefinition.Name, + cfg => cfg.WithField(field.Name, + builder => builder.WithSetting("TaxonomyFieldSettings.Taxonomy", part.Name))); + } + } + } + } + } + }); + OnLoading( (context, part) => part.TermsField.Loader(x => taxonomyService.GetTerms(part.Id))); + + OnUpdating((context, part) => { + // if altering the title of a taxonomy, save the name + if (part.As() != null) { + previousName = part.Title; + } + }); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/ITaxonomyService.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/ITaxonomyService.cs index fd6796228..aafbae810 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/ITaxonomyService.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/ITaxonomyService.cs @@ -11,7 +11,6 @@ namespace Orchard.Taxonomies.Services { TaxonomyPart GetTaxonomyBySlug(string slug); void CreateTermContentType(TaxonomyPart taxonomy); void DeleteTaxonomy(TaxonomyPart taxonomy); - void EditTaxonomy(TaxonomyPart taxonomy, string oldName); IEnumerable GetAllTerms(); IEnumerable GetTerms(int taxonomyId); diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs index 87244590a..137956ca7 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Orchard.Taxonomies.Fields; using Orchard.Taxonomies.Models; using Orchard.Autoroute.Models; using Orchard.ContentManagement; @@ -101,14 +102,6 @@ namespace Orchard.Taxonomies.Services { } - public void EditTaxonomy(TaxonomyPart taxonomy, string oldName) { - // Rename term definition - _contentDefinitionManager.AlterTypeDefinition(taxonomy.TermTypeName, cfg => cfg - .WithSetting("Taxonomy", taxonomy.Name) - .DisplayedAs(taxonomy.Name + " Term") - ); - } - public void DeleteTaxonomy(TaxonomyPart taxonomy) { _contentManager.Remove(taxonomy.ContentItem);