2016-11-09 11:13:35 +01:00
|
|
|
|
using System.Collections.Generic;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using System.Linq;
|
2013-12-12 14:10:03 +11:00
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
using System.Web.Routing;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using Umbraco.Core;
|
2016-01-12 19:46:05 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2016-02-11 15:54:00 +01:00
|
|
|
|
using Umbraco.Core.Logging;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Models.Mapping;
|
|
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2014-09-18 11:52:12 +10:00
|
|
|
|
using Umbraco.Core.Services;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2013-12-12 14:10:03 +11:00
|
|
|
|
using Umbraco.Web.Trees;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Declares model mappings for media.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class MediaModelMapper : MapperConfiguration
|
|
|
|
|
|
{
|
|
|
|
|
|
public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
//FROM IMedia TO MediaItemDisplay
|
|
|
|
|
|
config.CreateMap<IMedia, MediaItemDisplay>()
|
2017-01-28 01:14:24 +11:00
|
|
|
|
.ForMember(display => display.Udi, expression => expression.MapFrom(content => Udi.Create(Constants.UdiEntityType.Media, content.Key)))
|
2016-11-09 11:13:35 +01:00
|
|
|
|
.ForMember(display => display.Owner, expression => expression.ResolveUsing(new OwnerResolver<IMedia>()))
|
|
|
|
|
|
.ForMember(display => display.Icon, expression => expression.MapFrom(content => content.ContentType.Icon))
|
|
|
|
|
|
.ForMember(display => display.ContentTypeAlias, expression => expression.MapFrom(content => content.ContentType.Alias))
|
2015-10-27 18:13:18 +01:00
|
|
|
|
.ForMember(display => display.IsChildOfListView, expression => expression.Ignore())
|
2016-11-09 11:13:35 +01:00
|
|
|
|
.ForMember(display => display.Trashed, expression => expression.MapFrom(content => content.Trashed))
|
|
|
|
|
|
.ForMember(display => display.ContentTypeName, expression => expression.MapFrom(content => content.ContentType.Name))
|
2014-10-02 19:55:24 +10:00
|
|
|
|
.ForMember(display => display.Properties, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.TreeNodeUrl, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.Notifications, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.Errors, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.Published, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.Updater, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.Alias, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(display => display.IsContainer, expression => expression.Ignore())
|
2016-11-09 11:13:35 +01:00
|
|
|
|
.ForMember(display => display.HasPublishedVersion, expression => expression.Ignore())
|
2016-01-06 11:22:15 +01:00
|
|
|
|
.ForMember(display => display.Tabs, expression => expression.ResolveUsing(new TabsAndPropertiesResolver(applicationContext.Services.TextService)))
|
2017-03-23 14:07:45 +01:00
|
|
|
|
.AfterMap((media, display) => AfterMap(media, display, applicationContext.Services.DataTypeService, applicationContext.Services.TextService, applicationContext.Services.ContentTypeService, applicationContext.ProfilingLogger.Logger));
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
|
|
|
|
|
//FROM IMedia TO ContentItemBasic<ContentPropertyBasic, IMedia>
|
|
|
|
|
|
config.CreateMap<IMedia, ContentItemBasic<ContentPropertyBasic, IMedia>>()
|
2017-01-28 01:14:24 +11:00
|
|
|
|
.ForMember(display => display.Udi, expression => expression.MapFrom(content => Udi.Create(Constants.UdiEntityType.Media, content.Key)))
|
2016-11-09 11:13:35 +01:00
|
|
|
|
.ForMember(dto => dto.Owner, expression => expression.ResolveUsing(new OwnerResolver<IMedia>()))
|
|
|
|
|
|
.ForMember(dto => dto.Icon, expression => expression.MapFrom(content => content.ContentType.Icon))
|
|
|
|
|
|
.ForMember(dto => dto.Trashed, expression => expression.MapFrom(content => content.Trashed))
|
|
|
|
|
|
.ForMember(dto => dto.ContentTypeAlias, expression => expression.MapFrom(content => content.ContentType.Alias))
|
|
|
|
|
|
.ForMember(dto => dto.Published, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.Updater, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.Alias, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.HasPublishedVersion, expression => expression.Ignore());
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
|
|
|
|
|
//FROM IMedia TO ContentItemDto<IMedia>
|
|
|
|
|
|
config.CreateMap<IMedia, ContentItemDto<IMedia>>()
|
2017-01-28 01:14:24 +11:00
|
|
|
|
.ForMember(display => display.Udi, expression => expression.MapFrom(content => Udi.Create(Constants.UdiEntityType.Media, content.Key)))
|
2016-11-09 11:13:35 +01:00
|
|
|
|
.ForMember(dto => dto.Owner, expression => expression.ResolveUsing(new OwnerResolver<IMedia>()))
|
|
|
|
|
|
.ForMember(dto => dto.Published, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.Updater, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.Icon, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.Alias, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(dto => dto.HasPublishedVersion, expression => expression.Ignore());
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 14:07:45 +01:00
|
|
|
|
private static void AfterMap(IMedia media, MediaItemDisplay display, IDataTypeService dataTypeService, ILocalizedTextService localizedText, IContentTypeService contentTypeService, ILogger logger)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2017-01-30 13:18:30 +01:00
|
|
|
|
// Adapted from ContentModelMapper
|
|
|
|
|
|
//map the IsChildOfListView (this is actually if it is a descendant of a list view!)
|
2017-03-23 14:07:45 +01:00
|
|
|
|
var parent = media.Parent();
|
2017-03-28 15:55:45 +11:00
|
|
|
|
display.IsChildOfListView = parent != null && (parent.ContentType.IsContainer || contentTypeService.HasContainerInPath(parent.Path));
|
2017-01-30 13:18:30 +01:00
|
|
|
|
|
2013-12-12 14:10:03 +11:00
|
|
|
|
//map the tree node url
|
|
|
|
|
|
if (HttpContext.Current != null)
|
|
|
|
|
|
{
|
2017-01-03 11:13:38 +11:00
|
|
|
|
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
|
2013-12-12 14:10:03 +11:00
|
|
|
|
var url = urlHelper.GetUmbracoApiService<MediaTreeController>(controller => controller.GetTreeNode(display.Id.ToString(), null));
|
|
|
|
|
|
display.TreeNodeUrl = url;
|
|
|
|
|
|
}
|
2017-01-30 13:18:30 +01:00
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
if (media.ContentType.IsContainer)
|
|
|
|
|
|
{
|
2016-01-06 11:22:15 +01:00
|
|
|
|
TabsAndPropertiesResolver.AddListView(display, "media", dataTypeService, localizedText);
|
2013-11-15 16:56:51 +11:00
|
|
|
|
}
|
2017-01-30 13:18:30 +01:00
|
|
|
|
|
2016-01-06 11:22:15 +01:00
|
|
|
|
var genericProperties = new List<ContentPropertyDisplay>
|
|
|
|
|
|
{
|
|
|
|
|
|
new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}doctype", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = localizedText.Localize("content/mediatype"),
|
|
|
|
|
|
Value = localizedText.UmbracoDictionaryTranslate(display.ContentTypeName),
|
|
|
|
|
|
View = PropertyEditorResolver.Current.GetByAlias(Constants.PropertyEditors.NoEditAlias).ValueEditor.View
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2016-11-09 11:13:35 +01:00
|
|
|
|
TabsAndPropertiesResolver.MapGenericProperties(media, display, localizedText, genericProperties, properties =>
|
|
|
|
|
|
{
|
2016-11-02 21:20:03 +01:00
|
|
|
|
if (HttpContext.Current != null && UmbracoContext.Current != null && UmbracoContext.Current.Security.CurrentUser != null
|
|
|
|
|
|
&& UmbracoContext.Current.Security.CurrentUser.AllowedSections.Any(x => x.Equals(Constants.Applications.Settings)))
|
|
|
|
|
|
{
|
2016-11-09 11:13:35 +01:00
|
|
|
|
var mediaTypeLink = string.Format("#/settings/mediatypes/edit/{0}", media.ContentTypeId);
|
2016-11-02 21:20:03 +01:00
|
|
|
|
|
2016-11-09 11:13:35 +01:00
|
|
|
|
//Replace the doctype property
|
|
|
|
|
|
var docTypeProperty = properties.First(x => x.Alias == string.Format("{0}doctype", Constants.PropertyEditors.InternalGenericPropertiesPrefix));
|
|
|
|
|
|
docTypeProperty.Value = new List<object>
|
|
|
|
|
|
{
|
|
|
|
|
|
new
|
2016-11-02 21:20:03 +01:00
|
|
|
|
{
|
2016-11-09 11:13:35 +01:00
|
|
|
|
linkText = media.ContentType.Name,
|
|
|
|
|
|
url = mediaTypeLink,
|
|
|
|
|
|
target = "_self",
|
|
|
|
|
|
icon = "icon-item-arrangement"
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
docTypeProperty.View = "urllist";
|
2016-11-02 21:20:03 +01:00
|
|
|
|
}
|
2017-01-30 13:18:30 +01:00
|
|
|
|
|
|
|
|
|
|
// inject 'Link to media' as the first generic property
|
|
|
|
|
|
var links = media.GetUrls(UmbracoConfig.For.UmbracoSettings().Content, logger);
|
|
|
|
|
|
if (links.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
var link = new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}urls", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = localizedText.Localize("media/urls"),
|
|
|
|
|
|
Value = string.Join(",", links),
|
|
|
|
|
|
View = "urllist"
|
|
|
|
|
|
};
|
|
|
|
|
|
properties.Insert(0, link);
|
|
|
|
|
|
}
|
2016-11-02 21:20:03 +01:00
|
|
|
|
});
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|