-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLink.cs
238 lines (216 loc) · 6.97 KB
/
Link.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
namespace RJP.MultiUrlPicker.Models
{
using System;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Models;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Web.Extensions;
using Umbraco.Core.Models.PublishedContent;
public class Link
{
private readonly JToken _linkItem;
private readonly Lazy<UmbracoHelper> _umbracoHelper;
private bool _publishedContentInitialized = false;
private string _name;
private string _url;
private string _target;
private bool? _deleted;
private LinkType? _linkType;
private IPublishedContent _content;
private Udi _udi;
private int? _id;
public Link(JToken linkItem, Lazy<UmbracoHelper> umbracoHelper = null)
{
_linkItem = linkItem;
_umbracoHelper = umbracoHelper ?? new Lazy<UmbracoHelper>(() => new UmbracoHelper(UmbracoContext.Current));
}
private IPublishedContent PublishedContent
{
get
{
InitPublishedContent();
return _content;
}
}
[Obsolete("Use Udi instead")]
public int? Id
{
get
{
if (_id == null)
{
_id = _linkItem.Value<int?>("id");
if (!_id.HasValue)
{
InitPublishedContent();
}
}
return _id;
}
}
public Udi Udi
{
get
{
if (_udi == null)
{
if (!Udi.TryParse(_linkItem.Value<string>("udi"), out _udi))
{
InitPublishedContent();
}
}
return _udi;
}
}
public string Name
{
get
{
if (string.IsNullOrEmpty(_name))
{
_name = _linkItem.Value<string>("name");
}
return _name;
}
}
internal bool Deleted
{
get
{
if (_deleted == null)
{
if (Id.HasValue || Udi != null)
{
_deleted = PublishedContent == null;
}
else
{
_deleted = false;
}
}
return (bool)_deleted;
}
}
public string Url
{
get
{
if (string.IsNullOrEmpty(_url))
{
_url = PublishedContent?.Url ?? _linkItem.Value<string>("url");
var qs = _linkItem.Value<string>("querystring");
if (!string.IsNullOrWhiteSpace(qs))
{
_url += qs;
}
}
return _url;
}
}
public string Target
{
get
{
if (string.IsNullOrEmpty(_target))
{
_target = _linkItem.Value<string>("target");
}
return _target == string.Empty ? null : _target;
}
}
public LinkType Type
{
get
{
if (_linkType == null)
{
if (Udi != null)
{
if (Udi.EntityType == Constants.UdiEntityType.Media)
{
_linkType = LinkType.Media;
}
else
{
_linkType = LinkType.Content;
}
}
else
{
_linkType = LinkType.External;
}
}
return _linkType.Value;
}
}
private void InitPublishedContent()
{
if (!_publishedContentInitialized)
{
_publishedContentInitialized = true;
if (UmbracoContext.Current == null)
{
return;
}
if (Udi.TryParse(_linkItem.Value<string>("udi"), out _udi) && _udi is GuidUdi guidUdi)
{
var umbracoType = Constants.UdiEntityType.ToUmbracoObjectType(_udi.EntityType);
if (umbracoType == UmbracoObjectTypes.Media)
{
var entityService = ApplicationContext.Current.Services.EntityService;
var mediaAttempt = entityService.GetIdForKey(guidUdi.Guid, umbracoType);
if (mediaAttempt.Success)
{
_content = _umbracoHelper.Value.TypedMedia(mediaAttempt.Result);
}
}
else
{
_content = _umbracoHelper.Value.TypedContent(guidUdi.Guid);
}
_id = _content?.Id;
}
else
{
// there were no Udi so let's try the legacy way
_id = _linkItem.Value<int?>("id");
if (_id.HasValue)
{
if (_linkItem.Value<bool>("isMedia"))
{
_content = _umbracoHelper.Value.TypedMedia(_id.Value);
}
else
{
_content = _umbracoHelper.Value.TypedContent(_id.Value);
}
SetUdi();
}
}
}
}
private void SetUdi()
{
if (_content != null && _udi == null)
{
Guid? key = _content.GetKey();
if (key == Guid.Empty)
{
// if the key is Guid.Empty the model might be created by the ModelsBuilder,
// if so it, by default, derives from PublishedContentModel.
// By calling UnWrap() we get the original content, which probably implements
// IPublishedContentWithKey, so we can get the key
key = (_content as PublishedContentWrapped)?.Unwrap().GetKey();
}
if (key.HasValue && key != Guid.Empty)
{
string udiType = _content.ItemType == PublishedItemType.Media ?
Constants.UdiEntityType.Media :
Constants.UdiEntityType.Document;
_udi = Udi.Create(udiType, key.Value);
}
}
}
}
}