2013-08-19 17:39:50 +10:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using AutoMapper;
|
2013-08-20 11:33:41 +10:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
2013-08-19 17:39:50 +10:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2013-08-20 11:33:41 +10:00
|
|
|
|
using Umbraco.Core.Services;
|
2013-08-19 17:39:50 +10:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class PreValueDisplayResolver : ValueResolver<IDataTypeDefinition, IEnumerable<PreValueFieldDisplay>>
|
|
|
|
|
|
{
|
2013-08-20 11:33:41 +10:00
|
|
|
|
private readonly Lazy<IDataTypeService> _dataTypeService;
|
|
|
|
|
|
|
|
|
|
|
|
public PreValueDisplayResolver(Lazy<IDataTypeService> dataTypeService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_dataTypeService = dataTypeService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-22 18:38:57 +10:00
|
|
|
|
internal IEnumerable<PreValueFieldDisplay> Convert(IDataTypeDefinition source)
|
2013-08-19 17:39:50 +10:00
|
|
|
|
{
|
2013-08-20 18:10:19 +10:00
|
|
|
|
PropertyEditor propEd = null;
|
|
|
|
|
|
if (source.ControlId != Guid.Empty)
|
2013-08-19 17:39:50 +10:00
|
|
|
|
{
|
2013-08-20 18:10:19 +10:00
|
|
|
|
propEd = PropertyEditorResolver.Current.GetById(source.ControlId);
|
|
|
|
|
|
if (propEd == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("Could not find property editor with id " + source.ControlId);
|
2013-08-22 18:38:57 +10:00
|
|
|
|
}
|
2013-08-19 17:39:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-22 18:38:57 +10:00
|
|
|
|
//set up the defaults
|
2013-08-26 19:00:04 +10:00
|
|
|
|
var dataTypeService = _dataTypeService.Value;
|
2013-08-20 11:33:41 +10:00
|
|
|
|
var preVals = dataTypeService.GetPreValuesCollectionByDataTypeId(source.Id);
|
2013-08-28 17:53:31 +10:00
|
|
|
|
IDictionary<string, object> dictionaryVals = preVals.FormatAsDictionary().ToDictionary(x => x.Key, x => (object)x.Value);
|
2013-08-22 18:38:57 +10:00
|
|
|
|
var result = Enumerable.Empty<PreValueFieldDisplay>().ToArray();
|
2013-08-20 11:33:41 +10:00
|
|
|
|
|
2013-08-22 18:38:57 +10:00
|
|
|
|
//if we have a prop editor, then format the pre-values based on it and create it's fields.
|
2013-08-20 18:10:19 +10:00
|
|
|
|
if (propEd != null)
|
|
|
|
|
|
{
|
2013-08-22 18:38:57 +10:00
|
|
|
|
result = propEd.PreValueEditor.Fields.Select(Mapper.Map<PreValueFieldDisplay>).ToArray();
|
|
|
|
|
|
dictionaryVals = propEd.PreValueEditor.FormatDataForEditor(propEd.DefaultPreValues, preVals);
|
2013-08-20 18:10:19 +10:00
|
|
|
|
}
|
2013-08-22 18:38:57 +10:00
|
|
|
|
|
2013-08-20 11:33:41 +10:00
|
|
|
|
var currentIndex = 0; //used if the collection is non-dictionary based.
|
2013-08-22 18:38:57 +10:00
|
|
|
|
|
|
|
|
|
|
//now we need to wire up the pre-values values with the actual fields defined
|
2013-08-20 11:33:41 +10:00
|
|
|
|
foreach (var field in result)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (preVals.IsDictionaryBased == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
//we'll just need to wire up the values based on the order that the pre-values are stored
|
|
|
|
|
|
var found = dictionaryVals.Any(x => x.Key.InvariantEquals(currentIndex.ToInvariantString()));
|
|
|
|
|
|
if (found == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Warn<PreValueDisplayResolver>("Could not find persisted pre-value for index " + currentIndex);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2013-08-22 18:38:57 +10:00
|
|
|
|
field.Value = dictionaryVals.Single(x => x.Key.InvariantEquals(currentIndex.ToInvariantString())).Value.ToString();
|
2013-08-20 11:33:41 +10:00
|
|
|
|
currentIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var found = dictionaryVals.Any(x => x.Key.InvariantEquals(field.Key));
|
|
|
|
|
|
if (found == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Warn<PreValueDisplayResolver>("Could not find persisted pre-value for field " + field.Key);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2013-08-23 18:10:32 +10:00
|
|
|
|
field.Value = dictionaryVals.Single(x => x.Key.InvariantEquals(field.Key)).Value;
|
2013-08-20 11:33:41 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-22 18:38:57 +10:00
|
|
|
|
|
2013-08-20 11:33:41 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2013-08-19 17:39:50 +10:00
|
|
|
|
}
|
2013-08-22 18:38:57 +10:00
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<PreValueFieldDisplay> ResolveCore(IDataTypeDefinition source)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert(source);
|
|
|
|
|
|
}
|
2013-08-19 17:39:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|