diff --git a/src/Umbraco.Core/Services/ContentTypeServiceExtensions.cs b/src/Umbraco.Core/Services/ContentTypeServiceExtensions.cs index 6b440c6b2c..16dc068a10 100644 --- a/src/Umbraco.Core/Services/ContentTypeServiceExtensions.cs +++ b/src/Umbraco.Core/Services/ContentTypeServiceExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Configuration; using Umbraco.Core.Models; namespace Umbraco.Core.Services @@ -15,8 +16,6 @@ namespace Umbraco.Core.Services IContentTypeComposition source, IContentTypeComposition[] allContentTypes) { - - if (source == null) throw new ArgumentNullException("source"); //below is all ported from the old doc type editor and comes with the same weaknesses /insanity / magic // note: there are many sanity checks missing here and there ;-(( @@ -25,11 +24,14 @@ namespace Umbraco.Core.Services // throw new Exception("A parent does not belong to a composition."); // find out if any content type uses this content type - var isUsing = allContentTypes.Where(x => x.ContentTypeComposition.Any(y => y.Id == source.Id)).ToArray(); - if (isUsing.Length > 0) + if (source != null) { - //if already in use a composition, do not allow any composited types - return new List(); + var isUsing = allContentTypes.Where(x => x.ContentTypeComposition.Any(y => y.Id == source.Id)).ToArray(); + if (isUsing.Length > 0) + { + //if already in use a composition, do not allow any composited types + return new List(); + } } // if it is not used then composition is possible @@ -59,9 +61,9 @@ namespace Umbraco.Core.Services // .Where(x => x != source.ParentId) // but not the parent // .Distinct() // .ToArray(); - + return list - .Where(x => x.Id != source.Id) + .Where(x => x.Id != (source != null ? source.Id : 0)) .OrderBy(x => x.Name) .ToList(); } diff --git a/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs b/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs index 1f4523661a..8e6ba92994 100644 --- a/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs +++ b/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs @@ -92,56 +92,9 @@ namespace Umbraco.Web.Editors throw new ArgumentOutOfRangeException("The entity type was not a content type"); } - // note: there are many sanity checks missing here and there ;-(( - // make sure once and for all - //if (allContentTypes.Any(x => x.ParentId > 0 && x.ContentTypeComposition.Any(y => y.Id == x.ParentId) == false)) - // throw new Exception("A parent does not belong to a composition."); + var filtered = Services.ContentTypeService.GetAvailableCompositeContentTypes(source, allContentTypes); - // find out if any content type uses this content type - var isUsing = allContentTypes.Where(x => x.ContentTypeComposition.Any(y => y.Id == contentTypeId)).ToArray(); - if (isUsing.Length > 0) - { - //if already in use a composition, do not allow any composited types - return new List(); - } - - // if it is not used then composition is possible - // hashset guarantees unicity on Id - var list = new HashSet(new DelegateEqualityComparer( - (x, y) => x.Id == y.Id, - x => x.Id)); - - // usable types are those that are top-level - var usableContentTypes = allContentTypes - .Where(x => x.ContentTypeComposition.Any() == false).ToArray(); - foreach (var x in usableContentTypes) - list.Add(x); - - // indirect types are those that we use, directly or indirectly - var indirectContentTypes = GetIndirect(source).ToArray(); - foreach (var x in indirectContentTypes) - list.Add(x); - - if (UmbracoConfig.For.UmbracoSettings().Content.EnableInheritedDocumentTypes) - { - // get the ancestorIds via the parent - var ancestorIds = new int[0]; - if (parentId > 0) - { - var parent = allContentTypes.FirstOrDefault(x => x.Id == parentId); - if (parent != null) - ancestorIds = parent.Path.Split(',').Select(int.Parse).ToArray(); - } - - // add all ancestors as compositions (since they are implicitly "compositions" by inheritance and should - // be in the list even though they can't be deselected) - foreach (var x in allContentTypes) - if (ancestorIds.Contains(x.Id)) - list.Add(x); - } - return list - .Where(x => x.Id != contentTypeId) - .OrderBy(x => x.Name) + return filtered .Select(Mapper.Map) .Select(x => { @@ -151,32 +104,6 @@ namespace Umbraco.Web.Editors .ToList(); } - private static IEnumerable GetIndirect(IContentTypeComposition ctype) - { - // hashset guarantees unicity on Id - var all = new HashSet(new DelegateEqualityComparer( - (x, y) => x.Id == y.Id, - x => x.Id)); - - var stack = new Stack(); - - if (ctype != null) - { - foreach (var x in ctype.ContentTypeComposition) - stack.Push(x); - } - - while (stack.Count > 0) - { - var x = stack.Pop(); - all.Add(x); - foreach (var y in x.ContentTypeComposition) - stack.Push(y); - } - - return all; - } - /// /// Validates the composition and adds errors to the model state if any are found then throws an error response if there are errors ///