2020-11-17 20:27:10 +01:00
using System.Collections.Generic ;
2020-09-21 09:52:58 +02:00
using Microsoft.Extensions.Logging ;
2021-02-18 11:06:02 +01:00
using Umbraco.Cms.Core.Mapping ;
using Umbraco.Cms.Core.Models.ContentEditing ;
using Umbraco.Cms.Core.PropertyEditors ;
2017-07-19 13:42:47 +02:00
2021-02-18 11:06:02 +01:00
namespace Umbraco.Cms.Core.Models.Mapping
2017-07-19 13:42:47 +02:00
{
2020-01-20 15:48:03 +01:00
public class MacroMapDefinition : IMapDefinition
2017-07-19 13:42:47 +02:00
{
2019-03-25 09:03:46 +01:00
private readonly ParameterEditorCollection _parameterEditors ;
2020-09-21 09:52:58 +02:00
private readonly ILogger < MacroMapDefinition > _logger ;
2019-03-25 09:03:46 +01:00
2020-09-21 09:52:58 +02:00
public MacroMapDefinition ( ParameterEditorCollection parameterEditors , ILogger < MacroMapDefinition > logger )
2019-03-25 09:03:46 +01:00
{
_parameterEditors = parameterEditors ;
_logger = logger ;
}
2021-04-20 19:34:18 +02:00
public void DefineMaps ( IUmbracoMapper mapper )
2019-03-25 09:03:46 +01:00
{
2019-03-26 10:39:50 +01:00
mapper . Define < IMacro , EntityBasic > ( ( source , context ) = > new EntityBasic ( ) , Map ) ;
2020-01-08 19:02:16 +01:00
mapper . Define < IMacro , MacroDisplay > ( ( source , context ) = > new MacroDisplay ( ) , Map ) ;
2019-04-08 10:05:21 +02:00
mapper . Define < IMacro , IEnumerable < MacroParameter > > ( ( source , context ) = > context . MapEnumerable < IMacroProperty , MacroParameter > ( source . Properties . Values ) ) ;
2019-03-26 10:39:50 +01:00
mapper . Define < IMacroProperty , MacroParameter > ( ( source , context ) = > new MacroParameter ( ) , Map ) ;
2019-03-25 09:03:46 +01:00
}
// Umbraco.Code.MapAll -Trashed -AdditionalData
2019-03-26 10:39:50 +01:00
private static void Map ( IMacro source , EntityBasic target , MapperContext context )
2017-07-19 13:42:47 +02:00
{
2019-03-25 09:03:46 +01:00
target . Alias = source . Alias ;
2019-06-07 17:02:55 +01:00
target . Icon = Constants . Icons . Macro ;
2019-03-25 09:03:46 +01:00
target . Id = source . Id ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
target . Udi = Udi . Create ( Constants . UdiEntityType . Macro , source . Key ) ;
}
2020-01-08 19:02:16 +01:00
private void Map ( IMacro source , MacroDisplay target , MapperContext context )
{
target . Alias = source . Alias ;
target . Icon = Constants . Icons . Macro ;
target . Id = source . Id ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
target . Udi = Udi . Create ( Constants . UdiEntityType . Macro , source . Key ) ;
target . CacheByPage = source . CacheByPage ;
target . CacheByUser = source . CacheByMember ;
target . CachePeriod = source . CacheDuration ;
target . UseInEditor = source . UseInEditor ;
target . RenderInEditor = ! source . DontRender ;
target . View = source . MacroSource ;
}
2019-03-25 09:03:46 +01:00
// Umbraco.Code.MapAll -Value
2019-03-26 10:39:50 +01:00
private void Map ( IMacroProperty source , MacroParameter target , MapperContext context )
2019-03-25 09:03:46 +01:00
{
target . Alias = source . Alias ;
target . Name = source . Name ;
target . SortOrder = source . SortOrder ;
//map the view and the config
// we need to show the deprecated ones for backwards compatibility
var paramEditor = _parameterEditors [ source . EditorAlias ] ; // TODO: include/filter deprecated?!
if ( paramEditor = = null )
{
//we'll just map this to a text box
paramEditor = _parameterEditors [ Constants . PropertyEditors . Aliases . TextBox ] ;
2020-09-16 09:58:07 +02:00
_logger . LogWarning ( "Could not resolve a parameter editor with alias {PropertyEditorAlias}, a textbox will be rendered in it's place" , source . EditorAlias ) ;
2019-03-25 09:03:46 +01:00
}
2022-02-09 13:24:35 +01:00
target . View = paramEditor ? . GetValueEditor ( ) . View ;
2019-03-25 09:03:46 +01:00
// sets the parameter configuration to be the default configuration editor's configuration,
// ie configurationEditor.DefaultConfigurationObject, prepared for the value editor, ie
// after ToValueEditor - important to use DefaultConfigurationObject here, because depending
// on editors, ToValueEditor expects the actual strongly typed configuration - not the
// dictionary thing returned by DefaultConfiguration
2022-02-09 13:24:35 +01:00
var configurationEditor = paramEditor ? . GetConfigurationEditor ( ) ;
target . Configuration = configurationEditor ? . ToValueEditor ( configurationEditor . DefaultConfigurationObject ) ;
2017-07-19 13:42:47 +02:00
}
}
2017-07-20 11:21:28 +02:00
}