This repository has been archived by the owner on Jun 18, 2018. It is now read-only.
/
PublishedPropertyTypeExtensions.cs
127 lines (107 loc) · 5.97 KB
/
PublishedPropertyTypeExtensions.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Our.Umbraco.NestedContent.Helpers;
using Our.Umbraco.NestedContent.Models;
using Our.Umbraco.NestedContent.PropertyEditors;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
namespace Our.Umbraco.NestedContent.Extensions
{
internal static class PublishedPropertyTypeExtensions
{
public static bool IsNestedContentProperty(this PublishedPropertyType publishedProperty)
{
return publishedProperty.PropertyEditorAlias.InvariantEquals(NestedContentPropertyEditor.PropertyEditorAlias);
}
public static bool IsSingleNestedContentProperty(this PublishedPropertyType publishedProperty)
{
if (!publishedProperty.IsNestedContentProperty())
{
return false;
}
var preValueCollection = NestedContentHelper.GetPreValuesCollectionByDataTypeId(publishedProperty.DataTypeId);
var preValueDictionary = preValueCollection.PreValuesAsDictionary.ToDictionary(x => x.Key, x => x.Value.Value);
int minItems, maxItems;
return preValueDictionary.ContainsKey("minItems") &&
int.TryParse(preValueDictionary["minItems"], out minItems) && minItems == 1
&& preValueDictionary.ContainsKey("maxItems") &&
int.TryParse(preValueDictionary["maxItems"], out maxItems) && maxItems == 1;
}
public static object ConvertPropertyToNestedContent(this PublishedPropertyType propertyType, object source, bool preview)
{
using (DisposableTimer.DebugDuration<PublishedPropertyType>(string.Format("ConvertPropertyToNestedContent ({0})", propertyType.DataTypeId)))
{
if (source != null && !source.ToString().IsNullOrWhiteSpace())
{
var rawValue = JsonConvert.DeserializeObject<List<object>>(source.ToString());
var processedValue = new List<IPublishedContent>();
var preValueCollection = NestedContentHelper.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId);
var preValueDictionary = preValueCollection.PreValuesAsDictionary.ToDictionary(x => x.Key, x => x.Value.Value);
for (var i = 0; i < rawValue.Count; i++)
{
var item = (JObject)rawValue[i];
// Convert from old style (v.0.1.1) data format if necessary
// - Please note: This call has virtually no impact on rendering performance for new style (>v0.1.1).
// Even so, this should be removed eventually, when it's safe to assume that there is
// no longer any need for conversion.
NestedContentHelper.ConvertItemValueFromV011(item, propertyType.DataTypeId, ref preValueCollection);
var contentTypeAlias = NestedContentHelper.GetContentTypeAliasFromItem(item);
if (string.IsNullOrEmpty(contentTypeAlias))
{
continue;
}
var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias);
if (publishedContentType == null)
{
continue;
}
var propValues = item.ToObject<Dictionary<string, object>>();
var properties = new List<IPublishedProperty>();
foreach (var jProp in propValues)
{
var propType = publishedContentType.GetPropertyType(jProp.Key);
if (propType != null)
{
properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview));
}
}
// Parse out the name manually
object nameObj = null;
if (propValues.TryGetValue("name", out nameObj))
{
// Do nothing, we just want to parse out the name if we can
}
// Get the current request node we are embedded in
var pcr = UmbracoContext.Current == null ? null : UmbracoContext.Current.PublishedContentRequest;
var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;
// Create the model based on our implementation of IPublishedContent
IPublishedContent content = new DetachedPublishedContent(
nameObj == null ? null : nameObj.ToString(),
publishedContentType,
properties.ToArray(),
containerNode,
i,
preview);
if (PublishedContentModelFactoryResolver.HasCurrent && PublishedContentModelFactoryResolver.Current.HasValue)
{
// Let the current model factory create a typed model to wrap our model
content = PublishedContentModelFactoryResolver.Current.Factory.CreateModel(content);
}
// Add the (typed) model as a result
processedValue.Add(content);
}
if (propertyType.IsSingleNestedContentProperty())
{
return processedValue.FirstOrDefault();
}
return processedValue;
}
}
return null;
}
}
}