This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
/
ConfiguredFormConverter.cs
111 lines (84 loc) · 3.15 KB
/
ConfiguredFormConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
namespace formulate.app.Converters
{
// Namespaces.
using Helpers;
using Persistence;
using System;
using Types;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
/// <summary>
/// Converts property values to ConfiguredFormInfo.
/// </summary>
//[PropertyValueType(typeof(ConfiguredFormInfo))]
public class ConfiguredFormConverter : PropertyValueConverterBase
{
#region Properties
private IConfiguredFormPersistence ConfiguredForms { get; set; }
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
public ConfiguredFormConverter()
{
//TODO: Work around using Current.
ConfiguredForms = Current.Factory.GetInstance<IConfiguredFormPersistence>();
}
#endregion
#region Methods
/// <summary>
/// Indicates whether or not this is a converter for the specified property type.
/// </summary>
/// <param name="propertyType">
/// The property type.
/// </param>
/// <returns>
/// True, if this can convert the property type; otherwise, false.
/// </returns>
public override bool IsConverter(IPublishedPropertyType propertyType)
{
return "Formulate.ConfiguredFormPicker".InvariantEquals(propertyType.EditorAlias);
}
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object source, bool preview)
{
// Variables.
var deserialized = default(dynamic);
// Source is typically a string, but may already be deserialized (e.g.,
// when used with Umbraco grid and LeBlender).
if (source == null || source is string)
{
// Source is a string, so deserialize it.
var strSource = source as string;
strSource = string.IsNullOrWhiteSpace(strSource)
? "{id:null}"
: strSource;
deserialized = JsonHelper.Deserialize<dynamic>(strSource);
}
else
{
// Source is not a string, so it was likely already deserialized.
deserialized = source as dynamic;
}
// Variables.
var id = deserialized.id.Value as string;
var guid = string.IsNullOrWhiteSpace(id)
? null
: GuidHelper.GetGuid(id) as Guid?;
var conForm = guid.HasValue
? ConfiguredForms.Retrieve(guid.Value)
: null;
// Return configuration.
return new ConfiguredFormInfo()
{
Configuration = conForm?.Path[conForm.Path.Length - 1],
FormId = conForm?.Path[conForm.Path.Length - 2],
LayoutId = conForm?.LayoutId,
TemplateId = conForm?.TemplateId
};
}
#endregion
}
}