diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs index 01cdbcd983..4d5b587917 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs @@ -39,23 +39,12 @@ namespace Umbraco.Core.Persistence.Repositories return null; //TODO Get ContentType composition according to new table - //TODO Add AllowedContentTypes - - var propertySql = new Sql(); - propertySql.Select("*"); - propertySql.From("cmsTab"); - propertySql.RightJoin("cmsPropertyType ON [cmsTab].[id] = [cmsPropertyType].[tabId]"); - propertySql.InnerJoin("cmsDataType ON [cmsPropertyType].[dataTypeId] = [cmsDataType].[nodeId]"); - propertySql.Where("[cmsPropertyType].[contentTypeId] = @Id", new { Id = id }); - - var tabDtos = Database.Fetch(new TabPropertyTypeRelator().Map, propertySql); var factory = new ContentTypeFactory(NodeObjectTypeId); var contentType = factory.BuildEntity(documentTypeDto); - var propertyFactory = new PropertyGroupFactory(id); - var propertyGroups = propertyFactory.BuildEntity(tabDtos); - contentType.PropertyGroups = new PropertyGroupCollection(propertyGroups); + contentType.AllowedContentTypes = GetAllowedContentTypeIds(id); + contentType.PropertyGroups = GetPropertyGroupCollection(id); ((ContentType)contentType).ResetDirtyProperties(); return contentType; @@ -182,9 +171,15 @@ namespace Umbraco.Core.Persistence.Repositories Database.Insert(contentTypeDto); //TODO Insert new DocumentType entries - NOTE only seems relevant as long as Templates resides in the DB? - //TODO Insert allowed Templates and DocumentTypes + //TODO Insert allowed Templates //TODO Insert ContentType composition in new table + //Insert collection of allowed content types + foreach (var allowedContentType in entity.AllowedContentTypes) + { + Database.Insert(new ContentTypeAllowedContentTypeDto {Id = entity.Id, AllowedId = allowedContentType}); + } + var propertyFactory = new PropertyGroupFactory(nodeDto.NodeId); //Insert Tabs @@ -228,6 +223,19 @@ namespace Umbraco.Core.Persistence.Repositories //Look up DocumentType entries for updating - this could possibly be a "remove all, insert all"-approach + //TODO Update new DocumentType entries - NOTE only seems relevant as long as Templates resides in the DB? + //TODO Update allowed Templates + //TODO Update ContentType composition in new table + + //Delete the allowed content type entries before adding the updated collection + Database.Delete("WHERE Id = @Id", new {Id = entity.Id}); + + //Insert collection of allowed content types + foreach (var allowedContentType in entity.AllowedContentTypes) + { + Database.Insert(new ContentTypeAllowedContentTypeDto { Id = entity.Id, AllowedId = allowedContentType }); + } + //Check Dirty properties for Tabs/Groups and PropertyTypes - insert and delete accordingly if (((ICanBeDirty)entity).IsPropertyDirty("PropertyGroups") || entity.PropertyGroups.Any(x => x.IsDirty())) { @@ -277,5 +285,32 @@ namespace Umbraco.Core.Persistence.Repositories } #endregion + + private IEnumerable GetAllowedContentTypeIds(int id) + { + var allowedContentTypesSql = new Sql(); + allowedContentTypesSql.Select("*"); + allowedContentTypesSql.From("cmsContentTypeAllowedContentType"); + allowedContentTypesSql.Where("[cmsContentTypeAllowedContentType].[Id] = @Id", new { Id = id }); + + var allowedContentTypeDtos = Database.Fetch(allowedContentTypesSql); + return allowedContentTypeDtos.Select(x => x.AllowedId).ToList(); + } + + private PropertyGroupCollection GetPropertyGroupCollection(int id) + { + var propertySql = new Sql(); + propertySql.Select("*"); + propertySql.From("cmsTab"); + propertySql.RightJoin("cmsPropertyType ON [cmsTab].[id] = [cmsPropertyType].[tabId]"); + propertySql.InnerJoin("cmsDataType ON [cmsPropertyType].[dataTypeId] = [cmsDataType].[nodeId]"); + propertySql.Where("[cmsPropertyType].[contentTypeId] = @Id", new { Id = id }); + + var tabDtos = Database.Fetch(new TabPropertyTypeRelator().Map, propertySql); + + var propertyFactory = new PropertyGroupFactory(id); + var propertyGroups = propertyFactory.BuildEntity(tabDtos); + return new PropertyGroupCollection(propertyGroups); + } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs index 8729871c59..89698d1da7 100644 --- a/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs @@ -38,21 +38,11 @@ namespace Umbraco.Core.Persistence.Repositories if (dto == null) return null; - var propertySql = new Sql(); - propertySql.Select("*"); - propertySql.From("cmsTab"); - propertySql.RightJoin("cmsPropertyType ON [cmsTab].[id] = [cmsPropertyType].[tabId]"); - propertySql.InnerJoin("cmsDataType ON [cmsPropertyType].[dataTypeId] = [cmsDataType].[nodeId]"); - propertySql.Where("[cmsPropertyType].[contentTypeId] = @Id", new { Id = id }); - - var tabDtos = Database.Fetch(new TabPropertyTypeRelator().Map, propertySql); - var factory = new MediaTypeFactory(NodeObjectTypeId); var contentType = factory.BuildEntity(dto); - - var propertyFactory = new PropertyGroupFactory(id); - var propertyGroups = propertyFactory.BuildEntity(tabDtos); - contentType.PropertyGroups = new PropertyGroupCollection(propertyGroups); + + contentType.AllowedContentTypes = GetAllowedContentTypeIds(id); + contentType.PropertyGroups = GetPropertyGroupCollection(id); ((MediaType)contentType).ResetDirtyProperties(); return contentType; @@ -170,6 +160,12 @@ namespace Umbraco.Core.Persistence.Repositories //Insert new ContentType entry Database.Insert(dto); + + //Insert collection of allowed content types + foreach (var allowedContentType in entity.AllowedContentTypes) + { + Database.Insert(new ContentTypeAllowedContentTypeDto { Id = entity.Id, AllowedId = allowedContentType }); + } } protected override void PersistUpdatedItem(IMediaType entity) @@ -188,6 +184,15 @@ namespace Umbraco.Core.Persistence.Repositories dto.PrimaryKey = dtoPk.PrimaryKey; Database.Update(dto); + //Delete the allowed content type entries before adding the updated collection + Database.Delete("WHERE Id = @Id", new { Id = entity.Id }); + + //Insert collection of allowed content types + foreach (var allowedContentType in entity.AllowedContentTypes) + { + Database.Insert(new ContentTypeAllowedContentTypeDto { Id = entity.Id, AllowedId = allowedContentType }); + } + //Check Dirty properties for Tabs/Groups and PropertyTypes - insert and delete accordingly if (((ICanBeDirty)entity).IsPropertyDirty("PropertyGroups") || entity.PropertyGroups.Any(x => x.IsDirty())) { @@ -237,5 +242,32 @@ namespace Umbraco.Core.Persistence.Repositories } #endregion + + private IEnumerable GetAllowedContentTypeIds(int id) + { + var allowedContentTypesSql = new Sql(); + allowedContentTypesSql.Select("*"); + allowedContentTypesSql.From("cmsContentTypeAllowedContentType"); + allowedContentTypesSql.Where("[cmsContentTypeAllowedContentType].[Id] = @Id", new { Id = id }); + + var allowedContentTypeDtos = Database.Fetch(allowedContentTypesSql); + return allowedContentTypeDtos.Select(x => x.AllowedId).ToList(); + } + + private PropertyGroupCollection GetPropertyGroupCollection(int id) + { + var propertySql = new Sql(); + propertySql.Select("*"); + propertySql.From("cmsTab"); + propertySql.RightJoin("cmsPropertyType ON [cmsTab].[id] = [cmsPropertyType].[tabId]"); + propertySql.InnerJoin("cmsDataType ON [cmsPropertyType].[dataTypeId] = [cmsDataType].[nodeId]"); + propertySql.Where("[cmsPropertyType].[contentTypeId] = @Id", new { Id = id }); + + var tabDtos = Database.Fetch(new TabPropertyTypeRelator().Map, propertySql); + + var propertyFactory = new PropertyGroupFactory(id); + var propertyGroups = propertyFactory.BuildEntity(tabDtos); + return new PropertyGroupCollection(propertyGroups); + } } } \ No newline at end of file