2013-07-25 16:08:18 +10:00
|
|
|
|
using System;
|
2013-08-12 15:57:54 +10:00
|
|
|
|
using System.Collections.Generic;
|
2013-08-12 14:16:45 +10:00
|
|
|
|
using System.Linq;
|
2013-07-25 16:08:18 +10:00
|
|
|
|
using System.Linq.Expressions;
|
2013-08-22 15:48:32 +10:00
|
|
|
|
using System.Runtime.Serialization;
|
2013-07-25 16:08:18 +10:00
|
|
|
|
using AutoMapper;
|
2013-08-12 16:45:00 +10:00
|
|
|
|
using Newtonsoft.Json;
|
2013-05-27 01:23:49 -10:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
2013-07-25 16:08:18 +10:00
|
|
|
|
using Umbraco.Core.Models.Mapping;
|
2013-08-12 14:16:45 +10:00
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2013-08-28 17:53:31 +10:00
|
|
|
|
using Umbraco.Core.Services;
|
2013-05-27 01:23:49 -10:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2013-08-12 14:16:45 +10:00
|
|
|
|
using umbraco;
|
2013-08-12 15:57:54 +10:00
|
|
|
|
using Umbraco.Web.Routing;
|
2013-05-27 01:23:49 -10:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
2013-07-25 16:08:18 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Declares how model mappings for content
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class ContentModelMapper : MapperConfiguration
|
2013-05-27 01:23:49 -10:00
|
|
|
|
{
|
2013-07-25 16:08:18 +10:00
|
|
|
|
public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext)
|
2013-06-03 23:50:20 -10:00
|
|
|
|
{
|
2013-08-12 14:16:45 +10:00
|
|
|
|
|
2013-07-25 16:08:18 +10:00
|
|
|
|
//FROM IContent TO ContentItemDisplay
|
|
|
|
|
|
config.CreateMap<IContent, ContentItemDisplay>()
|
2013-09-03 16:35:36 +10:00
|
|
|
|
.ForMember(
|
2013-07-25 16:08:18 +10:00
|
|
|
|
dto => dto.Owner,
|
|
|
|
|
|
expression => expression.ResolveUsing<OwnerResolver<IContent>>())
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.Updator,
|
|
|
|
|
|
expression => expression.ResolveUsing<CreatorResolver>())
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.Icon,
|
|
|
|
|
|
expression => expression.MapFrom(content => content.ContentType.Icon))
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.ContentTypeAlias,
|
|
|
|
|
|
expression => expression.MapFrom(content => content.ContentType.Alias))
|
2013-08-12 14:16:45 +10:00
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.ContentTypeName,
|
|
|
|
|
|
expression => expression.MapFrom(content => content.ContentType.Name))
|
2013-07-25 16:08:18 +10:00
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.PublishDate,
|
|
|
|
|
|
expression => expression.MapFrom(content => GetPublishedDate(content, applicationContext)))
|
2013-08-12 15:57:54 +10:00
|
|
|
|
.ForMember(
|
2013-08-22 15:48:32 +10:00
|
|
|
|
dto => dto.TemplateAlias, expression => expression.MapFrom(content => content.Template.Alias))
|
2013-08-12 15:57:54 +10:00
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.Urls,
|
|
|
|
|
|
expression => expression.MapFrom(content =>
|
|
|
|
|
|
UmbracoContext.Current == null
|
|
|
|
|
|
? new[] {"Cannot generate urls without a current Umbraco Context"}
|
|
|
|
|
|
: content.GetContentUrls()))
|
2013-07-25 16:08:18 +10:00
|
|
|
|
.ForMember(display => display.Properties, expression => expression.Ignore())
|
2013-08-12 14:16:45 +10:00
|
|
|
|
.ForMember(display => display.Tabs, expression => expression.ResolveUsing<TabsAndPropertiesResolver>())
|
2013-08-22 15:48:32 +10:00
|
|
|
|
.AfterMap(MapGenericCustomProperties);
|
2013-05-27 01:23:49 -10:00
|
|
|
|
|
2013-07-25 16:08:18 +10:00
|
|
|
|
//FROM IContent TO ContentItemBasic<ContentPropertyBasic, IContent>
|
|
|
|
|
|
config.CreateMap<IContent, ContentItemBasic<ContentPropertyBasic, IContent>>()
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.Owner,
|
|
|
|
|
|
expression => expression.ResolveUsing<OwnerResolver<IContent>>())
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.Updator,
|
|
|
|
|
|
expression => expression.ResolveUsing<CreatorResolver>())
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.Icon,
|
|
|
|
|
|
expression => expression.MapFrom(content => content.ContentType.Icon))
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
dto => dto.ContentTypeAlias,
|
|
|
|
|
|
expression => expression.MapFrom(content => content.ContentType.Alias));
|
2013-06-10 16:43:42 -02:00
|
|
|
|
|
2013-07-25 16:08:18 +10:00
|
|
|
|
//FROM IContent TO ContentItemDto<IContent>
|
|
|
|
|
|
config.CreateMap<IContent, ContentItemDto<IContent>>()
|
2013-08-28 17:53:31 +10:00
|
|
|
|
.ForMember(
|
2013-07-25 16:08:18 +10:00
|
|
|
|
dto => dto.Owner,
|
|
|
|
|
|
expression => expression.ResolveUsing<OwnerResolver<IContent>>());
|
2013-06-10 16:43:42 -02:00
|
|
|
|
|
|
|
|
|
|
|
2013-07-25 16:08:18 +10:00
|
|
|
|
}
|
2013-06-10 16:43:42 -02:00
|
|
|
|
|
2013-08-22 15:48:32 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps the generic tab with custom properties for content
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <param name="display"></param>
|
|
|
|
|
|
private static void MapGenericCustomProperties(IContent content, ContentItemDisplay display)
|
|
|
|
|
|
{
|
|
|
|
|
|
//fill in the template config to be passed to the template drop down.
|
2013-09-05 15:14:28 +10:00
|
|
|
|
var templateItemConfig = new Dictionary<string, string> {{"", "Choose..."}};
|
|
|
|
|
|
foreach (var t in content.ContentType.AllowedTemplates)
|
|
|
|
|
|
{
|
|
|
|
|
|
templateItemConfig.Add(t.Alias, t.Name);
|
|
|
|
|
|
}
|
2013-10-01 21:12:32 +10:00
|
|
|
|
|
|
|
|
|
|
if (content.ContentType.IsContainer)
|
|
|
|
|
|
{
|
|
|
|
|
|
AddContainerView(display);
|
|
|
|
|
|
}
|
2013-09-30 23:33:27 +02:00
|
|
|
|
|
2013-08-22 15:48:32 +10:00
|
|
|
|
TabsAndPropertiesResolver.MapGenericProperties(
|
|
|
|
|
|
content, display,
|
|
|
|
|
|
new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}releasedate", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = ui.Text("content", "releaseDate"),
|
|
|
|
|
|
Value = display.ReleaseDate.HasValue ? display.ReleaseDate.Value.ToIsoString() : null,
|
|
|
|
|
|
View = "datepicker" //TODO: Hard coding this because the templatepicker doesn't necessarily need to be a resolvable (real) property editor
|
|
|
|
|
|
},
|
|
|
|
|
|
new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}expiredate", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = ui.Text("content", "removeDate"),
|
|
|
|
|
|
Value = display.ExpireDate.HasValue ? display.ExpireDate.Value.ToIsoString() : null,
|
|
|
|
|
|
View = "datepicker" //TODO: Hard coding this because the templatepicker doesn't necessarily need to be a resolvable (real) property editor
|
|
|
|
|
|
},
|
|
|
|
|
|
new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}template", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = "Template", //TODO: localize this?
|
|
|
|
|
|
Value = display.TemplateAlias,
|
|
|
|
|
|
View = "dropdown", //TODO: Hard coding until we make a real dropdown property editor to lookup
|
|
|
|
|
|
Config = new Dictionary<string, object>
|
|
|
|
|
|
{
|
|
|
|
|
|
{"items", templateItemConfig}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}urls", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = ui.Text("content", "urls"),
|
|
|
|
|
|
Value = string.Join(",", display.Urls),
|
|
|
|
|
|
View = "urllist" //TODO: Hard coding this because the templatepicker doesn't necessarily need to be a resolvable (real) property editor
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-01 21:12:32 +10:00
|
|
|
|
private static void AddContainerView<TPersisted>(TabbedContentItem<ContentPropertyDisplay, TPersisted> display)
|
|
|
|
|
|
where TPersisted : IContentBase
|
|
|
|
|
|
{
|
|
|
|
|
|
var listViewTab = new Tab<ContentPropertyDisplay>();
|
|
|
|
|
|
listViewTab.Alias = "containerView";
|
|
|
|
|
|
listViewTab.Label = "Content";
|
|
|
|
|
|
listViewTab.Id = 25;
|
|
|
|
|
|
listViewTab.IsActive = true;
|
|
|
|
|
|
|
|
|
|
|
|
var listViewProperties = new List<ContentPropertyDisplay>();
|
|
|
|
|
|
listViewProperties.Add(new ContentPropertyDisplay
|
|
|
|
|
|
{
|
|
|
|
|
|
Alias = string.Format("{0}containerView", Constants.PropertyEditors.InternalGenericPropertiesPrefix),
|
|
|
|
|
|
Label = ui.Text("content", "releaseDate"),
|
|
|
|
|
|
Value = null,
|
|
|
|
|
|
View = "listview",
|
|
|
|
|
|
HideLabel = true
|
|
|
|
|
|
});
|
|
|
|
|
|
listViewTab.Properties = listViewProperties;
|
|
|
|
|
|
|
|
|
|
|
|
//Is there a better way?
|
|
|
|
|
|
var tabs = new List<Tab<ContentPropertyDisplay>>();
|
|
|
|
|
|
tabs.Add(listViewTab);
|
|
|
|
|
|
tabs.AddRange(display.Tabs);
|
|
|
|
|
|
display.Tabs = tabs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-07-25 16:08:18 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the published date value for the IContent object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <param name="applicationContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static DateTime? GetPublishedDate(IContent content, ApplicationContext applicationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (content.Published)
|
|
|
|
|
|
{
|
|
|
|
|
|
return content.UpdateDate;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (content.HasPublishedVersion())
|
|
|
|
|
|
{
|
|
|
|
|
|
var published = applicationContext.Services.ContentService.GetPublishedVersion(content.Id);
|
|
|
|
|
|
return published.UpdateDate;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2013-06-10 16:43:42 -02:00
|
|
|
|
|
|
|
|
|
|
}
|
2013-07-25 16:08:18 +10:00
|
|
|
|
}
|