From 877c0b85cd71c633848a8c27b1f7bcfb9bf24344 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 10 Jun 2013 16:43:42 -0200 Subject: [PATCH] Refactors content model mapper to be re-used for media, added new base model class to deal with content similarities like media. Renamed some service methods to be consistent. --- src/Umbraco.Web.UI.Client/build.copy.bat | 4 + src/Umbraco.Web.UI.Client/build.grunt.bat | 1 + .../build/belle/js/umbraco.resources.js | 32 ++- .../src/common/resources/content.resource.js | 4 +- .../src/common/resources/media.resource.js | 28 +++ .../views/content/contentedit.controller.js | 26 +- .../src/views/media/edit.controller.js | 22 -- .../src/views/media/mediaedit.controller.js | 25 ++ src/Umbraco.Web/Editors/ContentController.cs | 2 +- src/Umbraco.Web/Editors/MediaController.cs | 26 +- .../ContentEditing/ContentItemDisplay.cs | 32 +-- .../Models/ContentEditing/MediaItemDisplay.cs | 13 + .../ContentEditing/TabbedContentItem.cs | 37 +++ .../Models/Mapping/ContentModelMapper.cs | 238 +++++++++--------- .../Models/Mapping/MediaModelMapper.cs | 97 ++----- src/Umbraco.Web/Umbraco.Web.csproj | 2 + 16 files changed, 322 insertions(+), 267 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/build.copy.bat create mode 100644 src/Umbraco.Web.UI.Client/build.grunt.bat delete mode 100644 src/Umbraco.Web.UI.Client/src/views/media/edit.controller.js create mode 100644 src/Umbraco.Web.UI.Client/src/views/media/mediaedit.controller.js create mode 100644 src/Umbraco.Web/Models/ContentEditing/MediaItemDisplay.cs create mode 100644 src/Umbraco.Web/Models/ContentEditing/TabbedContentItem.cs diff --git a/src/Umbraco.Web.UI.Client/build.copy.bat b/src/Umbraco.Web.UI.Client/build.copy.bat new file mode 100644 index 0000000000..9e86adee09 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/build.copy.bat @@ -0,0 +1,4 @@ +xcopy build\belle\js\*.* ..\Umbraco.Web.UI\Umbraco\js /Y /F /E /D +xcopy build\belle\assets\*.* ..\Umbraco.Web.UI\Umbraco\assets /Y /F /E /D +xcopy build\belle\lib\*.* ..\Umbraco.Web.UI\Umbraco\lib /Y /F /E /D +xcopy build\belle\views\*.* ..\Umbraco.Web.UI\Umbraco\views /Y /F /E /D \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/build.grunt.bat b/src/Umbraco.Web.UI.Client/build.grunt.bat new file mode 100644 index 0000000000..73fabbb6e9 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/build.grunt.bat @@ -0,0 +1 @@ +grunt.cmd build \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/build/belle/js/umbraco.resources.js b/src/Umbraco.Web.UI.Client/build/belle/js/umbraco.resources.js index 0a0a8b7895..5e13492793 100644 --- a/src/Umbraco.Web.UI.Client/build/belle/js/umbraco.resources.js +++ b/src/Umbraco.Web.UI.Client/build/belle/js/umbraco.resources.js @@ -15,7 +15,7 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) { /** internal method to get the api url */ function getContentUrl(contentId) { - return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetContent?id=" + contentId; + return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetById?id=" + contentId; } /** internal method to get the api url */ function getEmptyContentUrl(contentTypeAlias, parentId) { @@ -64,7 +64,7 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) { } return { - getContent: function (id) { + getById: function (id) { var deferred = $q.defer(); @@ -234,6 +234,10 @@ angular.module('umbraco.resources').factory('contentTypeResource', contentTypeRe **/ function mediaResource($q, $http) { + /** internal method to get the api url */ + function getMediaUrl(contentId) { + return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetById?id=" + contentId; + } /** internal method to get the api url */ function getRootMediaUrl() { return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetRootMedia"; @@ -245,6 +249,30 @@ function mediaResource($q, $http) { } return { + getById: function (id) { + + var deferred = $q.defer(); + + //go and get the data + $http.get(getMediaUrl(id)). + success(function (data, status, headers, config) { + //set the first tab to active + _.each(data.tabs, function (item) { + item.active = false; + }); + if (data.tabs.length > 0) { + data.tabs[0].active = true; + } + + deferred.resolve(data); + }). + error(function (data, status, headers, config) { + deferred.reject('Failed to retreive data for media id ' + id); + }); + + return deferred.promise; + }, + rootMedia: function () { var deferred = $q.defer(); diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js index d5cdd5ae25..98cc086be3 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js @@ -7,7 +7,7 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) { /** internal method to get the api url */ function getContentUrl(contentId) { - return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetContent?id=" + contentId; + return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetById?id=" + contentId; } /** internal method to get the api url */ function getEmptyContentUrl(contentTypeAlias, parentId) { @@ -56,7 +56,7 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) { } return { - getContent: function (id) { + getById: function (id) { var deferred = $q.defer(); diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/media.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/media.resource.js index 8c8383fd43..f56df531f0 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/media.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/media.resource.js @@ -5,6 +5,10 @@ **/ function mediaResource($q, $http) { + /** internal method to get the api url */ + function getMediaUrl(contentId) { + return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetById?id=" + contentId; + } /** internal method to get the api url */ function getRootMediaUrl() { return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetRootMedia"; @@ -16,6 +20,30 @@ function mediaResource($q, $http) { } return { + getById: function (id) { + + var deferred = $q.defer(); + + //go and get the data + $http.get(getMediaUrl(id)). + success(function (data, status, headers, config) { + //set the first tab to active + _.each(data.tabs, function (item) { + item.active = false; + }); + if (data.tabs.length > 0) { + data.tabs[0].active = true; + } + + deferred.resolve(data); + }). + error(function (data, status, headers, config) { + deferred.reject('Failed to retreive data for media id ' + id); + }); + + return deferred.promise; + }, + rootMedia: function () { var deferred = $q.defer(); diff --git a/src/Umbraco.Web.UI.Client/src/views/content/contentedit.controller.js b/src/Umbraco.Web.UI.Client/src/views/content/contentedit.controller.js index 8f8f193403..aa8ba1ff1a 100644 --- a/src/Umbraco.Web.UI.Client/src/views/content/contentedit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/content/contentedit.controller.js @@ -1,7 +1,7 @@ -angular.module("umbraco") -.controller("Umbraco.Editors.ContentEditController", +angular.module("umbraco") +.controller("Umbraco.Editors.ContentEditController", function ($scope, $routeParams, contentResource, notificationsService) { - + if ($routeParams.create) { contentResource.getContentScaffold($routeParams.id, $routeParams.doctype) @@ -10,22 +10,22 @@ angular.module("umbraco") }); } else { - contentResource.getContent($routeParams.id) + contentResource.getById($routeParams.id) .then(function (data) { $scope.content = data; }); - } - - + } + + $scope.saveAndPublish = function (cnt) { - cnt.publishDate = new Date(); - contentResource.publishContent(cnt); + cnt.publishDate = new Date(); + contentResource.publishContent(cnt); notificationsService.success("Published", "Content has been saved and published"); - }; - + }; + $scope.save = function (cnt) { - cnt.updateDate = new Date(); - contentResource.saveContent(cnt); + cnt.updateDate = new Date(); + contentResource.saveContent(cnt); notificationsService.success("Saved", "Content has been saved"); }; }); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/media/edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/media/edit.controller.js deleted file mode 100644 index 4008fe03bf..0000000000 --- a/src/Umbraco.Web.UI.Client/src/views/media/edit.controller.js +++ /dev/null @@ -1,22 +0,0 @@ -function mediaEditController($scope, $routeParams, contentResource, notificationsService) { - if ($routeParams.create) - $scope.content = contentResource.getContentScaffold($routeParams.id, $routeParams.doctype); - else - $scope.content = contentResource.getContent($routeParams.id); - - - $scope.saveAndPublish = function (cnt) { - cnt.publishDate = new Date(); - contentResource.publishContent(cnt); - notificationsService.success("Published", "Content has been saved and published"); - }; - - $scope.save = function (cnt) { - cnt.updateDate = new Date(); - contentResource.saveContent(cnt); - notificationsService.success("Saved", "Content has been saved"); - }; -} - -angular.module("umbraco") - .controller("Umbraco.Editors.MediaEditController", mediaEditController); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/media/mediaedit.controller.js b/src/Umbraco.Web.UI.Client/src/views/media/mediaedit.controller.js new file mode 100644 index 0000000000..81255adfe4 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/media/mediaedit.controller.js @@ -0,0 +1,25 @@ +function mediaEditController($scope, $routeParams, mediaResource, notificationsService) { + + if ($routeParams.create) { + + mediaResource.getContentScaffold($routeParams.id, $routeParams.doctype) + .then(function (data) { + $scope.content = data; + }); + } + else { + mediaResource.getById($routeParams.id) + .then(function (data) { + $scope.content = data; + }); + } + + $scope.save = function (cnt) { + cnt.updateDate = new Date(); + contentResource.saveContent(cnt); + notificationsService.success("Saved", "Media has been saved"); + }; +} + +angular.module("umbraco") + .controller("Umbraco.Editors.MediaEditController", mediaEditController); \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index e105eafb03..72db8d51a3 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -58,7 +58,7 @@ namespace Umbraco.Web.Editors /// /// /// - public ContentItemDisplay GetContent(int id) + public ContentItemDisplay GetById(int id) { var foundContent = Services.ContentService.GetById(id); if (foundContent == null) diff --git a/src/Umbraco.Web/Editors/MediaController.cs b/src/Umbraco.Web/Editors/MediaController.cs index a830583d27..10d896e877 100644 --- a/src/Umbraco.Web/Editors/MediaController.cs +++ b/src/Umbraco.Web/Editors/MediaController.cs @@ -1,4 +1,7 @@ using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Web.Http; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Models.Mapping; using Umbraco.Web.Mvc; @@ -43,13 +46,32 @@ namespace Umbraco.Web.Editors _mediaModelMapper = mediaModelMapper; } + /// + /// Gets the content json for the content id + /// + /// + /// + public MediaItemDisplay GetById(int id) + { + var foundContent = Services.MediaService.GetById(id); + if (foundContent == null) + { + ModelState.AddModelError("id", string.Format("media with id: {0} was not found", id)); + var errorResponse = Request.CreateErrorResponse( + HttpStatusCode.NotFound, + ModelState); + throw new HttpResponseException(errorResponse); + } + return _mediaModelMapper.ToMediaItemDisplay(foundContent); + } + /// /// Returns the root media objects /// public IEnumerable> GetRootMedia() { return Services.MediaService.GetRootMedia() - .Select(x => _mediaModelMapper.ToMediaItemSimple(x)); + .Select(x => _mediaModelMapper.ToContentItemSimple(x)); } /// @@ -58,7 +80,7 @@ namespace Umbraco.Web.Editors public IEnumerable> GetChildren(int parentId) { return Services.MediaService.GetChildren(parentId) - .Select(x => _mediaModelMapper.ToMediaItemSimple(x)); + .Select(x => _mediaModelMapper.ToContentItemSimple(x)); } } } diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs index 6bcd7f0cc3..2a68e1caf7 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs @@ -1,47 +1,17 @@ using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Runtime.Serialization; -using Newtonsoft.Json; -using System.Linq; namespace Umbraco.Web.Models.ContentEditing { - - /// /// A model representing a content item to be displayed in the back office /// - public class ContentItemDisplay : ContentItemBasic + public class ContentItemDisplay : TabbedContentItem { - public ContentItemDisplay() - { - Tabs = new List>(); - } - [DataMember(Name = "publishDate")] public DateTime? PublishDate { get; set; } - /// - /// Defines the tabs containing display properties - /// - [DataMember(Name = "tabs")] - public IEnumerable> Tabs { get; set; } - - /// - /// Override the properties property to ensure we don't serialize this - /// and to simply return the properties based on the properties in the tabs collection - /// - /// - /// This property cannot be set - /// - [JsonIgnore] - public override IEnumerable Properties - { - get { return Tabs.SelectMany(x => x.Properties); } - set { throw new NotImplementedException(); } - } - } } \ No newline at end of file diff --git a/src/Umbraco.Web/Models/ContentEditing/MediaItemDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/MediaItemDisplay.cs new file mode 100644 index 0000000000..49f1f2bd02 --- /dev/null +++ b/src/Umbraco.Web/Models/ContentEditing/MediaItemDisplay.cs @@ -0,0 +1,13 @@ +using System; +using System.Runtime.Serialization; + +namespace Umbraco.Web.Models.ContentEditing +{ + /// + /// A model representing a content item to be displayed in the back office + /// + public class MediaItemDisplay : TabbedContentItem + { + + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Models/ContentEditing/TabbedContentItem.cs b/src/Umbraco.Web/Models/ContentEditing/TabbedContentItem.cs new file mode 100644 index 0000000000..646cc58842 --- /dev/null +++ b/src/Umbraco.Web/Models/ContentEditing/TabbedContentItem.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using Newtonsoft.Json; + +namespace Umbraco.Web.Models.ContentEditing +{ + public abstract class TabbedContentItem : ContentItemBasic + where T : ContentPropertyBasic + { + protected TabbedContentItem() + { + Tabs = new List>(); + } + + /// + /// Defines the tabs containing display properties + /// + [DataMember(Name = "tabs")] + public IEnumerable> Tabs { get; set; } + + /// + /// Override the properties property to ensure we don't serialize this + /// and to simply return the properties based on the properties in the tabs collection + /// + /// + /// This property cannot be set + /// + [JsonIgnore] + public override IEnumerable Properties + { + get { return Tabs.SelectMany(x => x.Properties); } + set { throw new NotImplementedException(); } + } + } +} \ 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 f2340b5b55..e021002322 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs @@ -10,142 +10,111 @@ using Umbraco.Web.Models.ContentEditing; namespace Umbraco.Web.Models.Mapping { - internal class ContentModelMapper + internal class BaseContentModelMapper { - private readonly ApplicationContext _applicationContext; - private readonly ProfileModelMapper _profileMapper; + protected ApplicationContext ApplicationContext { get; private set; } + protected ProfileModelMapper ProfileMapper { get; private set; } - public ContentModelMapper(ApplicationContext applicationContext, ProfileModelMapper profileMapper) + public BaseContentModelMapper(ApplicationContext applicationContext, ProfileModelMapper profileMapper) { - _applicationContext = applicationContext; - _profileMapper = profileMapper; + ApplicationContext = applicationContext; + ProfileMapper = profileMapper; } - private ContentPropertyDisplay ToContentPropertyDisplay(Property property) + internal ContentItemDto ToContentItemDto(IContentBase content) { - return CreateProperty(property, (display, originalProp, propEditor) => - { - //set the display properties after mapping - display.Alias = originalProp.Alias; - display.Description = originalProp.PropertyType.Description; - display.Label = property.PropertyType.Name; - display.Config = _applicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(property.PropertyType.DataTypeDefinitionId); - display.View = propEditor.ValueEditor.View; - }); + return CreateContent(content, null, (propertyDto, originalProperty, propEditor) => + { + propertyDto.Alias = originalProperty.Alias; + propertyDto.Description = originalProperty.PropertyType.Description; + propertyDto.Label = originalProperty.PropertyType.Name; + propertyDto.DataType = ApplicationContext.Services.DataTypeService.GetDataTypeDefinitionById(originalProperty.PropertyType.DataTypeDefinitionId); + propertyDto.PropertyEditor = PropertyEditorResolver.Current.GetById(originalProperty.PropertyType.DataTypeId); + }); } - public ContentItemDisplay ToContentItemDisplay(IContent content) + internal ContentItemBasic ToContentItemSimple(IContentBase content) + { + return CreateContent, ContentPropertyBasic>(content, null, null); + } + + protected IList> GetTabs(IContentBase content) { - //create the list of tabs for properties assigned to tabs. var tabs = content.PropertyGroups.Select(propertyGroup => + { + //get the properties for the current tab + var propertiesForTab = content.GetPropertiesForGroup(propertyGroup).ToArray(); + + //convert the properties to ContentPropertyDisplay objects + var displayProperties = propertiesForTab + .Select(ToContentPropertyDisplay); + + //return the tab with the tab properties + return new Tab { - //get the properties for the current tab - var propertiesForTab = content.GetPropertiesForGroup(propertyGroup).ToArray(); - - //convert the properties to ContentPropertyDisplay objects - var displayProperties = propertiesForTab - .Select(ToContentPropertyDisplay); - - //return the tab with the tab properties - return new Tab - { - Id = propertyGroup.Id, - Alias = propertyGroup.Name, - Label = propertyGroup.Name, - Properties = displayProperties - }; - }).ToList(); + Id = propertyGroup.Id, + Alias = propertyGroup.Name, + Label = propertyGroup.Name, + Properties = displayProperties + }; + }).ToList(); //now add the generic properties tab for any properties that don't belong to a tab var orphanProperties = content.GetNonGroupedProperties(); //now add the generic properties tab tabs.Add(new Tab - { - Id = 0, - Label = "Generic properties", - Alias = "Generic properties", - Properties = orphanProperties.Select(ToContentPropertyDisplay).ToArray() - }); - - var result = CreateContent(content, (display, originalContent) => - { - //set display props after the normal properties are alraedy mapped - display.Name = originalContent.Name; - display.Tabs = tabs; - //look up the published version of this item if it is not published - if (content.Published) - { - display.PublishDate = content.UpdateDate; - } - else if (content.HasPublishedVersion()) - { - var published = _applicationContext.Services.ContentService.GetPublishedVersion(content.Id); - display.PublishDate = published.UpdateDate; - } - else - { - display.PublishDate = null; - } - - }, null, false); + { + Id = 0, + Label = "Generic properties", + Alias = "Generic properties", + Properties = orphanProperties.Select(ToContentPropertyDisplay).ToArray() + }); - return result; - } - - internal ContentItemDto ToContentItemDto(IContent content) - { - return CreateContent(content, null, (propertyDto, originalProperty, propEditor) => - { - propertyDto.Alias = originalProperty.Alias; - propertyDto.Description = originalProperty.PropertyType.Description; - propertyDto.Label = originalProperty.PropertyType.Name; - propertyDto.DataType = _applicationContext.Services.DataTypeService.GetDataTypeDefinitionById(originalProperty.PropertyType.DataTypeDefinitionId); - propertyDto.PropertyEditor = PropertyEditorResolver.Current.GetById(originalProperty.PropertyType.DataTypeId); - }); - } - - internal ContentItemBasic ToContentItemSimple(IContent content) - { - return CreateContent, ContentPropertyBasic>(content, null, null); + return tabs; } - /// - /// Creates a new content item - /// - /// - /// - /// - /// - /// - /// - /// - private TContent CreateContent(IContent content, - Action contentCreatedCallback = null, - Action propertyCreatedCallback = null, + protected TContent CreateContent(IContentBase content, + Action contentCreatedCallback = null, + Action propertyCreatedCallback = null, bool createProperties = true) where TContent : ContentItemBasic, new() where TContentProperty : ContentPropertyBasic, new() { var result = new TContent - { - Id = content.Id, - Owner = _profileMapper.ToBasicUser(content.GetCreatorProfile()), - Updator = _profileMapper.ToBasicUser(content.GetWriterProfile()), - ParentId = content.ParentId, - UpdateDate = content.UpdateDate, - CreateDate = content.CreateDate, - ContentTypeAlias = content.ContentType.Alias, - Icon = content.ContentType.Icon, - Name = content.Name - }; + { + Id = content.Id, + Owner = ProfileMapper.ToBasicUser(content.GetCreatorProfile()), + + ParentId = content.ParentId, + UpdateDate = content.UpdateDate, + CreateDate = content.CreateDate, + Name = content.Name + }; if (createProperties) result.Properties = content.Properties.Select(p => CreateProperty(p, propertyCreatedCallback)).ToArray(); - if (contentCreatedCallback != null) + if (contentCreatedCallback != null) contentCreatedCallback(result, content); return result; } + protected ContentPropertyDisplay ToContentPropertyDisplay(Property property) + { + return CreateProperty(property, (display, originalProp, propEditor) => + { + //set the display properties after mapping + display.Alias = originalProp.Alias; + display.Description = originalProp.PropertyType.Description; + display.Label = property.PropertyType.Name; + display.Config = ApplicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(property.PropertyType.DataTypeDefinitionId); + if (propEditor != null) + { + display.View = propEditor.ValueEditor.View; + } + + }); + } + /// /// Creates the property with the basic property values mapped /// @@ -153,8 +122,8 @@ namespace Umbraco.Web.Models.Mapping /// /// /// - private static TContentProperty CreateProperty( - Property property, + protected static TContentProperty CreateProperty( + Property property, Action callback = null) where TContentProperty : ContentPropertyBasic, new() { @@ -178,13 +147,58 @@ namespace Umbraco.Web.Models.Mapping return legacyResult; } var result = new TContentProperty - { - Id = property.Id, - Value = editor.ValueEditor.SerializeValue(property.Value), - Alias = property.Alias - }; + { + Id = property.Id, + Value = editor.ValueEditor.SerializeValue(property.Value), + Alias = property.Alias + }; if (callback != null) callback(result, property, editor); return result; } } + + internal class ContentModelMapper : BaseContentModelMapper + { + + public ContentModelMapper(ApplicationContext applicationContext, ProfileModelMapper profileMapper) + : base(applicationContext, profileMapper) + { + } + + public ContentItemDisplay ToContentItemDisplay(IContent content) + { + //create the list of tabs for properties assigned to tabs. + var tabs = GetTabs(content); + + var result = CreateContent(content, (display, originalContent) => + { + //fill in the rest + display.Updator = ProfileMapper.ToBasicUser(content.GetWriterProfile()); + display.ContentTypeAlias = content.ContentType.Alias; + display.Icon = content.ContentType.Icon; + + //set display props after the normal properties are alraedy mapped + display.Name = originalContent.Name; + display.Tabs = tabs; + //look up the published version of this item if it is not published + if (content.Published) + { + display.PublishDate = content.UpdateDate; + } + else if (content.HasPublishedVersion()) + { + var published = ApplicationContext.Services.ContentService.GetPublishedVersion(content.Id); + display.PublishDate = published.UpdateDate; + } + else + { + display.PublishDate = null; + } + + }, null, false); + + return result; + } + + } } diff --git a/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs b/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs index 2cedd33ad6..ee4f4c9eba 100644 --- a/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs @@ -10,98 +10,31 @@ using Umbraco.Web.Models.ContentEditing; namespace Umbraco.Web.Models.Mapping { - internal class MediaModelMapper + internal class MediaModelMapper : BaseContentModelMapper { - private readonly ApplicationContext _applicationContext; - private readonly ProfileModelMapper _profileMapper; - public MediaModelMapper(ApplicationContext applicationContext, ProfileModelMapper profileMapper) + : base(applicationContext, profileMapper) { - _applicationContext = applicationContext; - _profileMapper = profileMapper; - } - - internal ContentItemBasic ToMediaItemSimple(IMedia media) - { - return CreateMedia, ContentPropertyBasic>(media, null, null); - } - - /// - /// Creates a new content item - /// - /// - /// - /// - /// - /// - /// - /// - private TContent CreateMedia(IMedia media, - Action contentCreatedCallback = null, - Action propertyCreatedCallback = null, - bool createProperties = true) - where TContent : ContentItemBasic, new() - where TContentProperty : ContentPropertyBasic, new() - { - var result = new TContent - { - Id = media.Id, - Owner = _profileMapper.ToBasicUser(media.GetCreatorProfile()), - Updator = null, - ParentId = media.ParentId, - UpdateDate = media.UpdateDate, - CreateDate = media.CreateDate, - ContentTypeAlias = media.ContentType.Alias, - Icon = media.ContentType.Icon, - Name = media.Name - }; - if (createProperties) - result.Properties = media.Properties.Select(p => CreateProperty(p, propertyCreatedCallback)).ToArray(); - if (contentCreatedCallback != null) - contentCreatedCallback(result, media); - return result; } - /// - /// Creates the property with the basic property values mapped - /// - /// - /// - /// - /// - private static TContentProperty CreateProperty( - Property property, - Action callback = null) - where TContentProperty : ContentPropertyBasic, new() + public MediaItemDisplay ToMediaItemDisplay(IMedia media) { - var editor = PropertyEditorResolver.Current.GetById(property.PropertyType.DataTypeId); - if (editor == null) + //create the list of tabs for properties assigned to tabs. + var tabs = GetTabs(media); + + var result = CreateContent(media, (display, originalContent) => { - //TODO: Remove this check as we shouldn't support this at all! - var legacyEditor = DataTypesResolver.Current.GetById(property.PropertyType.DataTypeId); - if (legacyEditor == null) - { - throw new NullReferenceException("The property editor with id " + property.PropertyType.DataTypeId + " does not exist"); - } + //fill in the rest + display.ContentTypeAlias = media.ContentType.Alias; + display.Icon = media.ContentType.Icon; - var legacyResult = new TContentProperty - { - Id = property.Id, - Value = property.Value.ToString(), - Alias = property.Alias - }; - if (callback != null) callback(legacyResult, property, null); - return legacyResult; + //set display props after the normal properties are alraedy mapped + display.Name = originalContent.Name; + display.Tabs = tabs; + }, null, false); - } - var result = new TContentProperty - { - Id = property.Id, - Value = editor.ValueEditor.SerializeValue(property.Value), - Alias = property.Alias - }; - if (callback != null) callback(result, property, editor); return result; } + } } diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index e11b295be0..1d8392e351 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -297,7 +297,9 @@ + +