diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs index d86f77168e..4d401ce6f8 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs @@ -1250,5 +1250,14 @@ WHERE cmsContentType." + aliasColumn + @" LIKE @pattern", while (aliases.Contains(test = alias + i)) i++; return test; } + + internal bool HasContainerInPath(string contentPath) + { + var ids = contentPath.Split(',').Select(int.Parse); + var sql = new Sql(@"SELECT COUNT(pk) FROM cmsContentType +INNER JOIN cmsContent ON cmsContentType.nodeId=cmsContent.contentType +WHERE cmsContent.nodeId IN (@ids) AND cmsContentType.isContainer=@isContainer", new { ids, isContainer = true }); + return Database.ExecuteScalar(sql) > 0; + } } } diff --git a/src/Umbraco.Core/Services/ContentTypeServiceBase.cs b/src/Umbraco.Core/Services/ContentTypeServiceBase.cs index df29012b90..231186b99f 100644 --- a/src/Umbraco.Core/Services/ContentTypeServiceBase.cs +++ b/src/Umbraco.Core/Services/ContentTypeServiceBase.cs @@ -5,6 +5,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services @@ -70,5 +71,15 @@ namespace Umbraco.Core.Services return toUpdate; } + + internal bool HasContainerInPath(string contentPath) + { + using (var uow = UowProvider.GetUnitOfWork()) + { + // can use same repo for both content and media + var repository = (ContentTypeRepository) RepositoryFactory.CreateContentTypeRepository(uow); + return repository.HasContainerInPath(contentPath); + } + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs index e179159e7c..2fd57b6290 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs @@ -86,31 +86,9 @@ namespace Umbraco.Web.Models.Mapping private static void AfterMap(IContent content, ContentItemDisplay display, IDataTypeService dataTypeService, ILocalizedTextService localizedText, IContentTypeService contentTypeService) { - //map the IsChildOfListView (this is actually if it is a descendant of a list view!) - //TODO: Fix this shorthand .Ancestors() lookup, at least have an overload to use the current - if (content.HasIdentity) - { - var ancesctorListView = content.Ancestors().FirstOrDefault(x => x.ContentType.IsContainer); - display.IsChildOfListView = ancesctorListView != null; - } - else - { - //it's new so it doesn't have a path, so we need to look this up by it's parent + ancestors - var parent = content.Parent(); - if (parent == null) - { - display.IsChildOfListView = false; - } - else if (parent.ContentType.IsContainer) - { - display.IsChildOfListView = true; - } - else - { - var ancesctorListView = parent.Ancestors().FirstOrDefault(x => x.ContentType.IsContainer); - display.IsChildOfListView = ancesctorListView != null; - } - } + // map the IsChildOfListView (this is actually if it is a descendant of a list view!) + var parent = content.Parent(); + display.IsChildOfListView = parent != null && (parent.ContentType.IsContainer || ((ContentTypeService) contentTypeService).HasContainerInPath(parent.Path)); //map the tree node url if (HttpContext.Current != null) diff --git a/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs b/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs index c3f9412401..cfc254b085 100644 --- a/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs @@ -41,7 +41,7 @@ namespace Umbraco.Web.Models.Mapping .ForMember(display => display.IsContainer, expression => expression.Ignore()) .ForMember(display => display.HasPublishedVersion, expression => expression.Ignore()) .ForMember(display => display.Tabs, expression => expression.ResolveUsing(new TabsAndPropertiesResolver(applicationContext.Services.TextService))) - .AfterMap((media, display) => AfterMap(media, display, applicationContext.Services.DataTypeService, applicationContext.Services.TextService, applicationContext.ProfilingLogger.Logger)); + .AfterMap((media, display) => AfterMap(media, display, applicationContext.Services.DataTypeService, applicationContext.Services.TextService, applicationContext.Services.ContentTypeService, applicationContext.ProfilingLogger.Logger)); //FROM IMedia TO ContentItemBasic config.CreateMap>() @@ -64,34 +64,12 @@ namespace Umbraco.Web.Models.Mapping .ForMember(dto => dto.HasPublishedVersion, expression => expression.Ignore()); } - private static void AfterMap(IMedia media, MediaItemDisplay display, IDataTypeService dataTypeService, ILocalizedTextService localizedText, ILogger logger) + private static void AfterMap(IMedia media, MediaItemDisplay display, IDataTypeService dataTypeService, ILocalizedTextService localizedText, IContentTypeService contentTypeService, ILogger logger) { - // Adapted from ContentModelMapper - //map the IsChildOfListView (this is actually if it is a descendant of a list view!) - //TODO: Fix this shorthand .Ancestors() lookup, at least have an overload to use the current - if (media.HasIdentity) - { - var ancesctorListView = media.Ancestors().FirstOrDefault(x => x.ContentType.IsContainer); - display.IsChildOfListView = ancesctorListView != null; - } - else - { - //it's new so it doesn't have a path, so we need to look this up by it's parent + ancestors - var parent = media.Parent(); - if (parent == null) - { - display.IsChildOfListView = false; - } - else if (parent.ContentType.IsContainer) - { - display.IsChildOfListView = true; - } - else - { - var ancesctorListView = parent.Ancestors().FirstOrDefault(x => x.ContentType.IsContainer); - display.IsChildOfListView = ancesctorListView != null; - } - } + // Adapted from ContentModelMapper + //map the IsChildOfListView (this is actually if it is a descendant of a list view!) + var parent = media.Parent(); + display.IsChildOfListView = parent != null && (parent.ContentType.IsContainer || ((ContentTypeService) contentTypeService).HasContainerInPath(parent.Path)); //map the tree node url if (HttpContext.Current != null)