2013-05-27 01:23:49 -10:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
2013-06-10 16:43:42 -02:00
|
|
|
|
internal class BaseContentModelMapper
|
2013-05-27 01:23:49 -10:00
|
|
|
|
{
|
2013-06-10 16:43:42 -02:00
|
|
|
|
protected ApplicationContext ApplicationContext { get; private set; }
|
|
|
|
|
|
protected ProfileModelMapper ProfileMapper { get; private set; }
|
2013-05-27 01:23:49 -10:00
|
|
|
|
|
2013-06-10 16:43:42 -02:00
|
|
|
|
public BaseContentModelMapper(ApplicationContext applicationContext, ProfileModelMapper profileMapper)
|
2013-05-27 01:23:49 -10:00
|
|
|
|
{
|
2013-06-10 16:43:42 -02:00
|
|
|
|
ApplicationContext = applicationContext;
|
|
|
|
|
|
ProfileMapper = profileMapper;
|
2013-05-27 01:23:49 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-06-10 16:43:42 -02:00
|
|
|
|
internal ContentItemDto ToContentItemDto(IContentBase content)
|
2013-05-30 21:21:52 -10:00
|
|
|
|
{
|
2013-06-10 16:43:42 -02:00
|
|
|
|
return CreateContent<ContentItemDto, ContentPropertyDto>(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);
|
|
|
|
|
|
});
|
2013-05-30 21:21:52 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-06-10 16:43:42 -02:00
|
|
|
|
internal ContentItemBasic<ContentPropertyBasic> ToContentItemSimple(IContentBase content)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CreateContent<ContentItemBasic<ContentPropertyBasic>, ContentPropertyBasic>(content, null, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected IList<Tab<ContentPropertyDisplay>> GetTabs(IContentBase content)
|
2013-05-30 21:21:52 -10:00
|
|
|
|
{
|
|
|
|
|
|
var tabs = content.PropertyGroups.Select(propertyGroup =>
|
2013-06-10 16:43:42 -02:00
|
|
|
|
{
|
|
|
|
|
|
//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<ContentPropertyDisplay>
|
2013-05-30 21:21:52 -10:00
|
|
|
|
{
|
2013-06-10 16:43:42 -02:00
|
|
|
|
Id = propertyGroup.Id,
|
|
|
|
|
|
Alias = propertyGroup.Name,
|
|
|
|
|
|
Label = propertyGroup.Name,
|
|
|
|
|
|
Properties = displayProperties
|
|
|
|
|
|
};
|
|
|
|
|
|
}).ToList();
|
2013-05-30 21:21:52 -10:00
|
|
|
|
|
|
|
|
|
|
//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<ContentPropertyDisplay>
|
2013-06-10 16:43:42 -02:00
|
|
|
|
{
|
|
|
|
|
|
Id = 0,
|
|
|
|
|
|
Label = "Generic properties",
|
|
|
|
|
|
Alias = "Generic properties",
|
|
|
|
|
|
Properties = orphanProperties.Select(ToContentPropertyDisplay).ToArray()
|
|
|
|
|
|
});
|
2013-06-03 23:50:20 -10:00
|
|
|
|
|
2013-06-10 16:43:42 -02:00
|
|
|
|
return tabs;
|
2013-06-10 10:36:59 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-06-10 16:43:42 -02:00
|
|
|
|
protected TContent CreateContent<TContent, TContentProperty>(IContentBase content,
|
|
|
|
|
|
Action<TContent, IContentBase> contentCreatedCallback = null,
|
|
|
|
|
|
Action<TContentProperty, Property, PropertyEditor> propertyCreatedCallback = null,
|
2013-06-03 23:50:20 -10:00
|
|
|
|
bool createProperties = true)
|
|
|
|
|
|
where TContent : ContentItemBasic<TContentProperty>, new()
|
2013-06-10 10:36:59 -02:00
|
|
|
|
where TContentProperty : ContentPropertyBasic, new()
|
2013-06-03 23:50:20 -10:00
|
|
|
|
{
|
|
|
|
|
|
var result = new TContent
|
2013-06-10 16:43:42 -02:00
|
|
|
|
{
|
|
|
|
|
|
Id = content.Id,
|
|
|
|
|
|
Owner = ProfileMapper.ToBasicUser(content.GetCreatorProfile()),
|
|
|
|
|
|
|
|
|
|
|
|
ParentId = content.ParentId,
|
|
|
|
|
|
UpdateDate = content.UpdateDate,
|
|
|
|
|
|
CreateDate = content.CreateDate,
|
|
|
|
|
|
Name = content.Name
|
|
|
|
|
|
};
|
2013-06-07 01:39:53 -10:00
|
|
|
|
if (createProperties)
|
|
|
|
|
|
result.Properties = content.Properties.Select(p => CreateProperty(p, propertyCreatedCallback)).ToArray();
|
2013-06-10 16:43:42 -02:00
|
|
|
|
if (contentCreatedCallback != null)
|
2013-06-03 23:50:20 -10:00
|
|
|
|
contentCreatedCallback(result, content);
|
|
|
|
|
|
return result;
|
2013-05-27 01:23:49 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-06-10 16:43:42 -02:00
|
|
|
|
protected ContentPropertyDisplay ToContentPropertyDisplay(Property property)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CreateProperty<ContentPropertyDisplay>(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-06-03 23:50:20 -10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates the property with the basic property values mapped
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TContentProperty"></typeparam>
|
|
|
|
|
|
/// <param name="property"></param>
|
|
|
|
|
|
/// <param name="callback"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-06-10 16:43:42 -02:00
|
|
|
|
protected static TContentProperty CreateProperty<TContentProperty>(
|
|
|
|
|
|
Property property,
|
2013-06-03 23:50:20 -10:00
|
|
|
|
Action<TContentProperty, Property, PropertyEditor> callback = null)
|
2013-06-10 10:36:59 -02:00
|
|
|
|
where TContentProperty : ContentPropertyBasic, new()
|
2013-06-03 23:50:20 -10:00
|
|
|
|
{
|
|
|
|
|
|
var editor = PropertyEditorResolver.Current.GetById(property.PropertyType.DataTypeId);
|
|
|
|
|
|
if (editor == null)
|
|
|
|
|
|
{
|
2013-06-10 10:36:59 -02:00
|
|
|
|
//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");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var legacyResult = new TContentProperty
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = property.Id,
|
2013-06-10 11:15:33 -02:00
|
|
|
|
Value = property.Value.ToString(),
|
|
|
|
|
|
Alias = property.Alias
|
2013-06-10 10:36:59 -02:00
|
|
|
|
};
|
|
|
|
|
|
if (callback != null) callback(legacyResult, property, null);
|
|
|
|
|
|
return legacyResult;
|
2013-06-03 23:50:20 -10:00
|
|
|
|
}
|
|
|
|
|
|
var result = new TContentProperty
|
2013-06-10 16:43:42 -02:00
|
|
|
|
{
|
|
|
|
|
|
Id = property.Id,
|
|
|
|
|
|
Value = editor.ValueEditor.SerializeValue(property.Value),
|
|
|
|
|
|
Alias = property.Alias
|
|
|
|
|
|
};
|
2013-06-03 23:50:20 -10:00
|
|
|
|
if (callback != null) callback(result, property, editor);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2013-05-27 01:23:49 -10:00
|
|
|
|
}
|
2013-06-10 16:43:42 -02:00
|
|
|
|
|
|
|
|
|
|
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<ContentItemDisplay, ContentPropertyDisplay>(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2013-05-27 01:23:49 -10:00
|
|
|
|
}
|