This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
UmbracoPickerAttribute.cs
155 lines (138 loc) · 5.87 KB
/
UmbracoPickerAttribute.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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Security;
namespace Our.Umbraco.Ditto
{
/// <summary>
/// A picker processor for handling the various types of Umbraco pickers
/// </summary>
public class UmbracoPickerAttribute : DittoProcessorAttribute
{
/// <summary>
/// Processes the value.
/// </summary>
/// <returns>
/// The <see cref="object" /> representing the processed value.
/// </returns>
public override object ProcessValue()
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (this.Context == null || this.Context.PropertyDescriptor == null || this.Value.IsNullOrEmptyString())
{
return Enumerable.Empty<object>();
}
// Single IPublishedContent
IPublishedContent content = this.Value as IPublishedContent;
if (content != null)
{
return content;
}
// ReSharper disable once PossibleNullReferenceException
var type = this.Value.GetType();
// Multiple IPublishedContent
if (type.IsEnumerableOfType(typeof(IPublishedContent)))
{
return (IEnumerable<IPublishedContent>)this.Value;
}
int[] nodeIds = { };
// First try enumerable strings, ints.
if (type.IsGenericType || type.IsArray)
{
if (type.IsEnumerableOfType(typeof(string)))
{
int n;
nodeIds = ((IEnumerable<string>)this.Value)
.Select(x => int.TryParse(x, NumberStyles.Any, this.Context.Culture, out n) ? n : -1)
.ToArray();
}
if (type.IsEnumerableOfType(typeof(int)))
{
nodeIds = ((IEnumerable<int>)this.Value).ToArray();
}
}
// Now csv strings.
if (!nodeIds.Any())
{
var s = this.Value as string ?? this.Value.ToString();
if (!string.IsNullOrWhiteSpace(s))
{
int n;
nodeIds = XmlHelper.CouldItBeXml(s)
? s.GetXmlIds()
: s.ToDelimitedList()
.Select(x => int.TryParse(x, NumberStyles.Any, this.Context.Culture, out n) ? n : -1)
.Where(x => x > 0)
.ToArray();
}
}
if (nodeIds.Any())
{
var umbracoContext = UmbracoContext.Current;
var membershipHelper = UmbracoPickerHelper.GetMembershipHelper(umbracoContext);
var objectType = UmbracoObjectTypes.Unknown;
var multiPicker = new List<IPublishedContent>();
// Oh so ugly if you let Resharper do this.
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var nodeId in nodeIds)
{
var item = this.GetPublishedContent(nodeId, ref objectType, UmbracoObjectTypes.Document, umbracoContext.ContentCache.GetById)
?? this.GetPublishedContent(nodeId, ref objectType, UmbracoObjectTypes.Media, umbracoContext.MediaCache.GetById)
?? this.GetPublishedContent(nodeId, ref objectType, UmbracoObjectTypes.Member, membershipHelper.GetById);
if (item != null)
{
multiPicker.Add(item);
}
}
return multiPicker;
}
return null;
}
/// <summary>
/// Attempt to get an <see cref="IPublishedContent"/> instance based on id and object type.
/// </summary>
/// <param name="nodeId">The content node ID</param>
/// <param name="actual">The type of content being requested</param>
/// <param name="expected">The type of content expected/supported by <paramref name="typedMethod"/></param>
/// <param name="typedMethod">A function to fetch content of type <paramref name="expected"/></param>
/// <returns>
/// The requested content, or null if either it does not exist or <paramref name="actual"/> does not
/// match <paramref name="expected"/>
/// </returns>
private IPublishedContent GetPublishedContent(int nodeId, ref UmbracoObjectTypes actual, UmbracoObjectTypes expected, Func<int, IPublishedContent> typedMethod)
{
// Is the given type supported by the typed method.
if (actual != UmbracoObjectTypes.Unknown && actual != expected)
{
return null;
}
// Attempt to get the content
var content = typedMethod(nodeId);
if (content != null)
{
// If we find the content, assign the expected type to the actual type so we don't have to
// keep looking for other types of content.
actual = expected;
}
return content;
}
}
/// <summary>
/// A helper for UmbracoPicker processor.
/// </summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed.")]
internal static class UmbracoPickerHelper
{
/// <summary>
/// Gets the MembershipHelper for the UmbracoPicker processor.
/// </summary>
internal static Func<UmbracoContext, MembershipHelper> GetMembershipHelper = (ctx) => UmbracoContext.Current != null
? new MembershipHelper(ctx)
: null;
}
}