/
MultipleMediaPickerPropertyConverter.cs
217 lines (195 loc) · 7.72 KB
/
MultipleMediaPickerPropertyConverter.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MultipleMediaPickerPropertyConverter.cs" company="OurUmbraco">
// Our.Umbraco
// </copyright>
// <summary>
// The multiple media picker property editor converter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Our.Umbraco.PropertyConverters
{
using System;
using System.Collections.Generic;
using System.Linq;
using Our.Umbraco.PropertyConverters.Utilities;
using global::Umbraco.Core;
using global::Umbraco.Core.Logging;
using global::Umbraco.Core.Models;
using global::Umbraco.Core.Models.PublishedContent;
using global::Umbraco.Core.PropertyEditors;
using global::Umbraco.Web;
/// <summary>
/// The multiple media picker property value converter.
/// </summary>
public class MultipleMediaPickerPropertyConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
{
/// <summary>
/// Checks if this converter can convert the property editor and registers if it can.
/// </summary>
/// <param name="propertyType">
/// The property type.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals(Constants.PropertyEditors.MultipleMediaPickerAlias);
}
/// <summary>
/// Convert the raw string into a nodeId integer array or a single integer
/// </summary>
/// <param name="propertyType">
/// The published property type.
/// </param>
/// <param name="source">
/// The value of the property
/// </param>
/// <param name="preview">
/// The preview.
/// </param>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (IsMultipleDataType(propertyType.DataTypeId))
{
var nodeIds =
source.ToString()
.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToArray();
return nodeIds;
}
var attemptConvertInt = source.TryConvertTo<int>();
if (attemptConvertInt.Success)
{
return attemptConvertInt.Result;
}
else
{
var nodeIds =
source.ToString()
.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToArray();
if (nodeIds.Length > 0)
{
var error =
string.Format(
"Data type \"{0}\" is not set to allow multiple items but appears to contain multiple items, check the setting and save the data type again",
ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(
propertyType.DataTypeId).Name);
LogHelper.Warn<MultipleMediaPickerPropertyConverter>(error);
throw new Exception(error);
}
}
return null;
}
/// <summary>
/// Convert the source nodeId into a IPublishedContent or IEnumerable of IPublishedContent (or DynamicPublishedContent) depending on data type setting
/// </summary>
/// <param name="propertyType">
/// The published property type.
/// </param>
/// <param name="source">
/// The value of the property
/// </param>
/// <param name="preview">
/// The preview.
/// </param>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null)
{
return null;
}
if (UmbracoContext.Current == null)
{
return null;
}
var dynamicInvocation = ConverterHelper.DynamicInvocation();
var umbHelper = new UmbracoHelper(UmbracoContext.Current);
if (IsMultipleDataType(propertyType.DataTypeId))
{
var nodeIds = (int[])source;
var multiMediaPicker = Enumerable.Empty<IPublishedContent>();
if (nodeIds.Length > 0)
{
multiMediaPicker = dynamicInvocation ? umbHelper.Media(nodeIds) : umbHelper.TypedMedia(nodeIds).Where(x => x != null);
}
return multiMediaPicker;
}
// single value picker
var nodeId = (int)source;
return dynamicInvocation ? umbHelper.Media(nodeId) : umbHelper.TypedMedia(nodeId);
}
/// <summary>
/// The get property cache level.
/// </summary>
/// <param name="propertyType">
/// The property type.
/// </param>
/// <param name="cacheValue">
/// The cache value.
/// </param>
/// <returns>
/// The <see cref="PropertyCacheLevel"/>.
/// </returns>
public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
{
PropertyCacheLevel returnLevel;
switch (cacheValue)
{
case PropertyCacheValue.Object:
returnLevel = ConverterHelper.ModeFixed() ? PropertyCacheLevel.ContentCache : PropertyCacheLevel.Request;
break;
case PropertyCacheValue.Source:
returnLevel = PropertyCacheLevel.Content;
break;
case PropertyCacheValue.XPath:
returnLevel = PropertyCacheLevel.Content;
break;
default:
returnLevel = PropertyCacheLevel.None;
break;
}
return returnLevel;
}
/// <summary>
/// The get property value type.
/// </summary>
/// <param name="propertyType">
/// The property type.
/// </param>
/// <returns>
/// The <see cref="Type"/>.
/// </returns>
public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
return IsMultipleDataType(propertyType.DataTypeId) ? typeof(IEnumerable<IPublishedContent>) : typeof(IPublishedContent);
}
/// <summary>
/// The is multiple data type.
/// </summary>
/// <param name="dataTypeId">
/// The data type id.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
private static bool IsMultipleDataType(int dataTypeId)
{
var dts = ApplicationContext.Current.Services.DataTypeService;
var multiPickerPreValue =
dts.GetPreValuesCollectionByDataTypeId(dataTypeId)
.PreValuesAsDictionary.FirstOrDefault(
x => string.Equals(x.Key, "multiPicker", StringComparison.InvariantCultureIgnoreCase)).Value;
return multiPickerPreValue != null && multiPickerPreValue.Value.TryConvertTo<bool>().Result;
}
}
}