1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
4
+ using System . Linq ;
5
+ using System . Web . Hosting ;
3
6
using Newtonsoft . Json ;
4
7
using Umbraco . Core ;
5
8
using Umbraco . Core . Deploy ;
@@ -16,6 +19,27 @@ public class UrlPickerValueConnector : IValueConnector
16
19
{
17
20
private readonly IEntityService _entityService ;
18
21
22
+ private readonly Version UrlPickerVersionStoringArray = new Version ( 0 , 15 , 0 , 1 ) ;
23
+
24
+ private static Lazy < Version > UrlPickerVersion
25
+ {
26
+ get
27
+ {
28
+ return new Lazy < Version > ( ( ) =>
29
+ {
30
+ // we cannot just get the assembly and get version from that, as we need to check the FileVersion
31
+ // (apparently that is different from the assembly version...)
32
+ var urlPickerAssemblyPath = HostingEnvironment . MapPath ( "~/bin/UrlPicker.dll" ) ;
33
+ if ( System . IO . File . Exists ( urlPickerAssemblyPath ) )
34
+ {
35
+ var fileVersionInfo = FileVersionInfo . GetVersionInfo ( urlPickerAssemblyPath ) ;
36
+ return new Version ( fileVersionInfo . FileVersion ) ;
37
+ }
38
+ return null ;
39
+ } ) ;
40
+ }
41
+ }
42
+
19
43
/// <summary>
20
44
/// Initializes a new instance of the <see cref="UrlPickerValueConnector"/> class.
21
45
/// </summary>
@@ -28,7 +52,7 @@ public UrlPickerValueConnector(IEntityService entityService)
28
52
}
29
53
30
54
/// <inheritdoc/>
31
- public virtual IEnumerable < string > PropertyEditorAliases => new [ ] { "Imulus.UrlPicker" } ;
55
+ public virtual IEnumerable < string > PropertyEditorAliases => new [ ] { "Imulus.UrlPicker" } ;
32
56
33
57
/// <inheritdoc/>
34
58
public string GetValue ( Property property , ICollection < ArtifactDependency > dependencies )
@@ -37,14 +61,30 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
37
61
if ( string . IsNullOrWhiteSpace ( svalue ) )
38
62
return string . Empty ;
39
63
40
- var urlPickerPropertyData = JsonConvert . DeserializeObject < IEnumerable < UrlPickerPropertyData > > ( svalue ) ;
41
- foreach ( var urlPicker in urlPickerPropertyData )
64
+ IEnumerable < UrlPickerPropertyData > urlPickerPropertyDatas ;
65
+
66
+ // If using an old version of the UrlPicker - data is serialized as a single object
67
+ // Otherwise data is serialized as an array of objects, even if only one is selected.
68
+ if ( UrlPickerVersion . Value < UrlPickerVersionStoringArray )
69
+ {
70
+ var urlPickerPropertyData = JsonConvert . DeserializeObject < UrlPickerPropertyData > ( svalue ) ;
71
+ urlPickerPropertyDatas = new List < UrlPickerPropertyData > { urlPickerPropertyData } ;
72
+ }
73
+ else
74
+ {
75
+ urlPickerPropertyDatas = JsonConvert . DeserializeObject < IEnumerable < UrlPickerPropertyData > > ( svalue )
76
+ . ToList ( ) ;
77
+ }
78
+
79
+
80
+ foreach ( var urlPicker in urlPickerPropertyDatas )
42
81
{
43
82
// If the contentId/mediaId of the TypeData is set try get the GuidUdi for the content/media and
44
83
// mark it as a dependency we need to deploy.
45
84
// We need the Guid for the content/media because the integer value could be different in the different environments.
46
85
GuidUdi contentGuidUdi ;
47
- if ( TryGetGuidUdi ( urlPicker . TypeData . ContentId , UmbracoObjectTypes . Document , Constants . UdiEntityType . Document , out contentGuidUdi ) )
86
+ if ( TryGetGuidUdi ( urlPicker . TypeData . ContentId , UmbracoObjectTypes . Document ,
87
+ Constants . UdiEntityType . Document , out contentGuidUdi ) )
48
88
{
49
89
dependencies . Add ( new ArtifactDependency ( contentGuidUdi , false , ArtifactDependencyMode . Exist ) ) ;
50
90
urlPicker . TypeData . ContentId = contentGuidUdi . Guid ;
@@ -53,14 +93,20 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
53
93
// The picker can have values set for both content and media even though only one of them is "active".
54
94
// We still need to resolve the value for both settings.
55
95
GuidUdi mediaGuidUdi ;
56
- if ( TryGetGuidUdi ( urlPicker . TypeData . MediaId , UmbracoObjectTypes . Media , Constants . UdiEntityType . Media , out mediaGuidUdi ) )
96
+ if ( TryGetGuidUdi ( urlPicker . TypeData . MediaId , UmbracoObjectTypes . Media , Constants . UdiEntityType . Media ,
97
+ out mediaGuidUdi ) )
57
98
{
58
99
dependencies . Add ( new ArtifactDependency ( mediaGuidUdi , false , ArtifactDependencyMode . Exist ) ) ;
59
100
urlPicker . TypeData . MediaId = mediaGuidUdi . Guid ;
60
101
}
61
102
}
62
-
63
- return JsonConvert . SerializeObject ( urlPickerPropertyData ) ;
103
+ // If using an old version of the UrlPicker - data is serialized as a single object
104
+ // Otherwise data is serialized as an array of objects, even if only one is selected.
105
+ if ( UrlPickerVersion . Value < UrlPickerVersionStoringArray )
106
+ {
107
+ return JsonConvert . SerializeObject ( urlPickerPropertyDatas . FirstOrDefault ( ) ) ;
108
+ }
109
+ return JsonConvert . SerializeObject ( urlPickerPropertyDatas ) ;
64
110
}
65
111
66
112
/// <inheritdoc/>
@@ -72,8 +118,21 @@ public void SetValue(IContentBase content, string alias, string value)
72
118
return ;
73
119
}
74
120
75
- var urlPickerPropertyData = JsonConvert . DeserializeObject < IEnumerable < UrlPickerPropertyData > > ( value ) ;
76
- foreach ( var urlPicker in urlPickerPropertyData )
121
+ IEnumerable < UrlPickerPropertyData > urlPickerPropertyDatas ;
122
+
123
+ // If using an old version of the UrlPicker - data is serialized as a single object
124
+ // Otherwise data is serialized as an array of objects, even if only one is selected.
125
+ if ( UrlPickerVersion . Value < UrlPickerVersionStoringArray )
126
+ {
127
+ var urlPickerPropertyData = JsonConvert . DeserializeObject < UrlPickerPropertyData > ( value ) ;
128
+ urlPickerPropertyDatas = new List < UrlPickerPropertyData > { urlPickerPropertyData } ;
129
+ }
130
+ else
131
+ {
132
+ urlPickerPropertyDatas = JsonConvert . DeserializeObject < IEnumerable < UrlPickerPropertyData > > ( value ) . ToList ( ) ;
133
+ }
134
+
135
+ foreach ( var urlPicker in urlPickerPropertyDatas )
77
136
{
78
137
// When we set the value we want to switch the Guid value of the contentId/mediaId to the integervalue
79
138
// as this is what the UrlPicker uses to lookup it's content/media
@@ -87,11 +146,21 @@ public void SetValue(IContentBase content, string alias, string value)
87
146
if ( TryGetId ( urlPicker . TypeData . MediaId , UmbracoObjectTypes . Media , out mediaId ) )
88
147
urlPicker . TypeData . MediaId = mediaId ;
89
148
}
90
-
91
- content . SetValue ( alias , JsonConvert . SerializeObject ( urlPickerPropertyData ) ) ;
149
+
150
+ // If using an old version of the UrlPicker - data is serialized as a single object
151
+ // Otherwise data is serialized as an array of objects, even if only one is selected.
152
+ if ( UrlPickerVersion . Value < UrlPickerVersionStoringArray )
153
+ {
154
+ content . SetValue ( alias , JsonConvert . SerializeObject ( urlPickerPropertyDatas . FirstOrDefault ( ) ) ) ;
155
+ }
156
+ else
157
+ {
158
+ content . SetValue ( alias , JsonConvert . SerializeObject ( urlPickerPropertyDatas ) ) ;
159
+ }
92
160
}
93
161
94
- private bool TryGetGuidUdi ( object value , UmbracoObjectTypes umbracoObjectType , string entityType , out GuidUdi udi )
162
+ private bool TryGetGuidUdi ( object value , UmbracoObjectTypes umbracoObjectType , string entityType ,
163
+ out GuidUdi udi )
95
164
{
96
165
int id ;
97
166
if ( value != null && int . TryParse ( value . ToString ( ) , out id ) )
@@ -127,10 +196,13 @@ internal class UrlPickerPropertyData
127
196
{
128
197
[ JsonProperty ( "type" ) ]
129
198
public string Type { get ; set ; }
199
+
130
200
[ JsonProperty ( "meta" ) ]
131
201
public Meta Meta { get ; set ; }
202
+
132
203
[ JsonProperty ( "typeData" ) ]
133
204
public TypeData TypeData { get ; set ; }
205
+
134
206
[ JsonProperty ( "disabled" ) ]
135
207
public bool Disabled { get ; set ; }
136
208
}
@@ -139,6 +211,7 @@ internal class Meta
139
211
{
140
212
[ JsonProperty ( "title" ) ]
141
213
public string Title { get ; set ; }
214
+
142
215
[ JsonProperty ( "newWindow" ) ]
143
216
public bool NewWindow { get ; set ; }
144
217
}
@@ -147,10 +220,12 @@ internal class TypeData
147
220
{
148
221
[ JsonProperty ( "url" ) ]
149
222
public string Url { get ; set ; }
223
+
150
224
[ JsonProperty ( "contentId" ) ]
151
225
public object ContentId { get ; set ; }
226
+
152
227
[ JsonProperty ( "mediaId" ) ]
153
228
public object MediaId { get ; set ; }
154
229
}
155
230
}
156
- }
231
+ }
0 commit comments