diff --git a/src/Umbraco.Core/Services/IContentTypeServiceBase.cs b/src/Umbraco.Core/Services/IContentTypeServiceBase.cs index b86494adb5..51e5d756eb 100644 --- a/src/Umbraco.Core/Services/IContentTypeServiceBase.cs +++ b/src/Umbraco.Core/Services/IContentTypeServiceBase.cs @@ -25,7 +25,7 @@ namespace Umbraco.Core.Services /// /// Gets a content type. /// - TItem Get(int id); + new TItem Get(int id); /// /// Gets a content type. @@ -40,6 +40,7 @@ namespace Umbraco.Core.Services int Count(); IEnumerable GetAll(params int[] ids); + IEnumerable GetAll(IEnumerable ids); IEnumerable GetDescendants(int id, bool andSelf); // parent-child axis IEnumerable GetComposedOf(int id); // composition axis diff --git a/src/Umbraco.Core/Services/IDataTypeService.cs b/src/Umbraco.Core/Services/IDataTypeService.cs index 77bf509498..3ebfa95bfb 100644 --- a/src/Umbraco.Core/Services/IDataTypeService.cs +++ b/src/Umbraco.Core/Services/IDataTypeService.cs @@ -15,7 +15,7 @@ namespace Umbraco.Core.Services /// /// /// - IReadOnlyDictionary> FindUsages(int id); + IReadOnlyDictionary> GetReferences(int id); Attempt> CreateContainer(int parentId, string name, int userId = Constants.Security.SuperUserId); Attempt SaveContainer(EntityContainer container, int userId = Constants.Security.SuperUserId); diff --git a/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs b/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs index 6ac8e1404a..705a876d83 100644 --- a/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs +++ b/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs @@ -252,12 +252,12 @@ namespace Umbraco.Core.Services.Implement } } - public IEnumerable GetAll(params Guid[] ids) + public IEnumerable GetAll(IEnumerable ids) { using (var scope = ScopeProvider.CreateScope(autoComplete: true)) { scope.ReadLock(ReadLockIds); - return Repository.GetMany(ids); + return Repository.GetMany(ids.ToArray()); } } diff --git a/src/Umbraco.Core/Services/Implement/DataTypeService.cs b/src/Umbraco.Core/Services/Implement/DataTypeService.cs index e06c49cf44..5a93fb91b1 100644 --- a/src/Umbraco.Core/Services/Implement/DataTypeService.cs +++ b/src/Umbraco.Core/Services/Implement/DataTypeService.cs @@ -466,7 +466,7 @@ namespace Umbraco.Core.Services.Implement } } - public IReadOnlyDictionary> FindUsages(int id) + public IReadOnlyDictionary> GetReferences(int id) { using (var scope = ScopeProvider.CreateScope(autoComplete:true)) { diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs index 3600db32de..dad3ec4fc0 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs @@ -330,7 +330,7 @@ namespace Umbraco.Tests.TestHelpers throw new NotImplementedException(); } - public IReadOnlyDictionary> FindUsages(int id) + public IReadOnlyDictionary> GetReferences(int id) { throw new NotImplementedException(); } diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/datatype.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/datatype.resource.js index f20debc9c3..6400f7386f 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/datatype.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/datatype.resource.js @@ -45,108 +45,26 @@ function dataTypeResource($q, $http, umbDataFormatter, umbRequestHelper) { /** * @ngdoc method - * @name umbraco.resources.dataTypeResource#getRelations + * @name umbraco.resources.dataTypeResource#getReferences * @methodOf umbraco.resources.dataTypeResource * * @description * Retrieves relations of a given data type. * - * ##usage - *
-         * dataTypeResource.getRelation(1234)
-         *    .then(function(relations) {
-         *        alert('its gone!');
-         *    });
-         * 
- * * @param {Int} id id of datatype to retrieve relations for * @returns {Promise} resourcePromise object. * */ - getRelations: function (id) { - - return $q((resolve, reject) => { - - -var fakeData = { - "documentTypes": [ - { - "name": "Home", - "alias": "home", - "icon": "icon-home", - "udi": "document-type://B6C7EFA7-1D03-46BF-B6D3-9187EF4CD176", - "id": 1234, - "properties": [ - { - "alias": "bodyText", - "name": "Body Text" - }, - { - "alias": "description", - "name": "Description" - } - ] - }, - { - "name": "News", - "alias": "news", - "icon": "icon-newspaper", - "udi": "document-type://FAB4F78E-530E-4F5D-959A-82441958460C", - "id": 9876, - "properties": [ - { - "alias": "article", - "name": "Article" - } - ] - } - ], - "mediaTypes": [ - { - "name": "Image", - "alias": "image", - "icon": "icon-umb-media", - "udi": "media-type://488152AF-0C03-4F2D-9DE0-FDECC80212AD", - "id": 9999, - "properties": [ - { - "alias": "altText", - "name": "Alt Text" - } - ] - } - ], - "memberTypes": [ - { - "name": "Member", - "alias": "member", - "icon": "icon-people-alt-2", - "udi": "member-type://93773B7C-2D83-4529-92D9-B9759220384C", - "id": 7777, - "properties": [ - { - "alias": "bio", - "name": "Bio" - } - ] - } - ] -}; - - resolve(fakeData); - }); - /* - - TODO: get real data from server! + getReferences: function (id) { return umbRequestHelper.resourcePromise( $http.get( umbRequestHelper.getApiUrl( "dataTypeApiBaseUrl", - "GetRelations", - [{ id: id }])), - "Failed to retrieve relations for data type of id " + id); - */ + "GetReferences", + { id: id })), + "Failed to retrieve usages for data type of id " + id); + }, /** diff --git a/src/Umbraco.Web.UI.Client/src/views/datatypes/views/datatype.relations.controller.js b/src/Umbraco.Web.UI.Client/src/views/datatypes/views/datatype.relations.controller.js index 83d6e4a379..db97053064 100644 --- a/src/Umbraco.Web.UI.Client/src/views/datatypes/views/datatype.relations.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/datatypes/views/datatype.relations.controller.js @@ -7,26 +7,26 @@ * The controller for the relations view of the datatype editor */ function DataTypeRelationsController($scope, $routeParams, dataTypeResource) { - + var vm = this; vm.relations = {}; vm.hasRelations = false; - + vm.view = {}; vm.view.loading = true; //we are editing so get the content item from the server - dataTypeResource.getRelations($routeParams.id) - .then(function(data) { + dataTypeResource.getReferences($routeParams.id) + .then(function (data) { - vm.view.loading = false; - vm.relations = data; + vm.view.loading = false; + vm.relations = data; - vm.hasRelations = vm.relations.documentTypes.length > 0 || vm.relations.mediaTypes.length > 0 || vm.relations.memberTypes.length > 0; + vm.hasRelations = vm.relations.documentTypes.length > 0 || vm.relations.mediaTypes.length > 0 || vm.relations.memberTypes.length > 0; - }); + }); diff --git a/src/Umbraco.Web/Editors/DataTypeController.cs b/src/Umbraco.Web/Editors/DataTypeController.cs index 30c4162252..ca261b3043 100644 --- a/src/Umbraco.Web/Editors/DataTypeController.cs +++ b/src/Umbraco.Web/Editors/DataTypeController.cs @@ -283,6 +283,59 @@ namespace Umbraco.Web.Editors : Request.CreateNotificationValidationErrorResponse(result.Exception.Message); } + /// + /// Returns the references (usages) for the data type + /// + /// + /// + public DataTypeReferences GetReferences(int id) + { + var result = new DataTypeReferences(); + var usages = Services.DataTypeService.GetReferences(id); + + foreach(var groupOfEntityType in usages.GroupBy(x => x.Key.EntityType)) + { + //get all the GUIDs for the content types to find + var guidsAndPropertyAliases = groupOfEntityType.ToDictionary(i => ((GuidUdi)i.Key).Guid, i => i.Value); + + if (groupOfEntityType.Key == ObjectTypes.GetUdiType(UmbracoObjectTypes.DocumentType)) + result.DocumentTypes = GetContentTypeUsages(Services.ContentTypeService.GetAll(guidsAndPropertyAliases.Keys), guidsAndPropertyAliases); + else if (groupOfEntityType.Key == ObjectTypes.GetUdiType(UmbracoObjectTypes.MediaType)) + result.MediaTypes = GetContentTypeUsages(Services.MediaTypeService.GetAll(guidsAndPropertyAliases.Keys), guidsAndPropertyAliases); + else if (groupOfEntityType.Key == ObjectTypes.GetUdiType(UmbracoObjectTypes.MemberType)) + result.MemberTypes = GetContentTypeUsages(Services.MemberTypeService.GetAll(guidsAndPropertyAliases.Keys), guidsAndPropertyAliases); + } + + return result; + } + + /// + /// Maps the found content types and usages to the resulting model + /// + /// + /// + /// + private IEnumerable GetContentTypeUsages( + IEnumerable cts, + IReadOnlyDictionary> usages) + { + return cts.Select(x => new DataTypeReferences.ContentTypeReferences + { + Key = x.Key, + Alias = x.Alias, + Icon = x.Icon, + Name = x.Name, + Udi = new GuidUdi(ObjectTypes.GetUdiType(UmbracoObjectTypes.DocumentType), x.Key), + //only select matching properties + Properties = x.PropertyTypes.Where(p => usages[x.Key].InvariantContains(p.Alias)) + .Select(p => new DataTypeReferences.ContentTypeReferences.PropertyTypeReferences + { + Alias = p.Alias, + Name = p.Name + }) + }); + } + #region ReadOnly actions to return basic data - allow access for: content ,media, members, settings, developer /// /// Gets the content json for all data types diff --git a/src/Umbraco.Web/Models/ContentEditing/DataTypeReferences.cs b/src/Umbraco.Web/Models/ContentEditing/DataTypeReferences.cs new file mode 100644 index 0000000000..dd042660a2 --- /dev/null +++ b/src/Umbraco.Web/Models/ContentEditing/DataTypeReferences.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; + +namespace Umbraco.Web.Models.ContentEditing +{ + [DataContract(Name = "dataTypeUsages", Namespace = "")] + public class DataTypeReferences + { + [DataMember(Name = "documentTypes")] + public IEnumerable DocumentTypes { get; set; } = Enumerable.Empty(); + + [DataMember(Name = "mediaTypes")] + public IEnumerable MediaTypes { get; set; } = Enumerable.Empty(); + + [DataMember(Name = "memberTypes")] + public IEnumerable MemberTypes { get; set; } = Enumerable.Empty(); + + [DataContract(Name = "contentType", Namespace = "")] + public class ContentTypeReferences : EntityBasic + { + [DataMember(Name = "properties")] + public object Properties { get; set; } + + [DataContract(Name = "property", Namespace = "")] + public class PropertyTypeReferences + { + [DataMember(Name = "name")] + public string Name { get; set; } + + [DataMember(Name = "alias")] + public string Alias { get; set; } + } + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index a0ce64943a..f908fc7c30 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -212,6 +212,7 @@ +