#19414: Fixing taxonomies renames

Work Item: 19414

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2013-07-12 11:59:40 -07:00
parent 769afd3083
commit a890164681
3 changed files with 45 additions and 12 deletions

View File

@@ -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<TaxonomyPartRecord> repository, ITaxonomyService taxonomyService) {
public TaxonomyPartHandler(
IRepository<TaxonomyPartRecord> repository,
ITaxonomyService taxonomyService,
IContentDefinitionManager contentDefinitionManager) {
string previousName = null;
Filters.Add(StorageFilter.For(repository));
OnPublished<TaxonomyPart>((context, part) => taxonomyService.CreateTermContentType(part));
OnPublished<TaxonomyPart>((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<TaxonomyFieldSettings>().Taxonomy == previousName) {
contentDefinitionManager.AlterPartDefinition(partDefinition.Name,
cfg => cfg.WithField(field.Name,
builder => builder.WithSetting("TaxonomyFieldSettings.Taxonomy", part.Name)));
}
}
}
}
}
});
OnLoading<TaxonomyPart>( (context, part) => part.TermsField.Loader(x => taxonomyService.GetTerms(part.Id)));
OnUpdating<TitlePart>((context, part) => {
// if altering the title of a taxonomy, save the name
if (part.As<TaxonomyPart>() != null) {
previousName = part.Title;
}
});
}
}
}

View File

@@ -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<TermPart> GetAllTerms();
IEnumerable<TermPart> GetTerms(int taxonomyId);

View File

@@ -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);