-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRelationshipMacros.cs
231 lines (214 loc) · 11.4 KB
/
RelationshipMacros.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
using CMS;
using CMS.DocumentEngine;
using CMS.FormEngine.Web.UI;
using CMS.Helpers;
using CMS.MacroEngine;
using CMS.Relationships;
using RelationshipsExtended;
using System;
using CMS.DataEngine;
using CMS.SiteProvider;
using System.Linq;
using CMS.Core;
[assembly: RegisterExtension(typeof(RelationshipMacroMethods), typeof(RelationshipsExtendedMacroNamespace))]
namespace RelationshipsExtended
{
public class RelationshipMacroMethods : MacroMethodContainer
{
[MacroMethod(typeof(string), "Returns the URL to create a new page of the specified type at the specified location.", 2)]
[MacroMethodParam(0, "ClassName", typeof(string), "The class name of the page type that will be created.")]
[MacroMethodParam(1, "ParentNodeAlias", typeof(string), "The parent node alias that the page will be inserted at.")]
[MacroMethodParam(2, "CurrentCulture", typeof(string), "The document culture, will default to en-US if not provided.")]
[MacroMethodParam(3, "CurrentSiteName", typeof(string), "The Site Name selection for the Related Node Site.")]
public static object GetNewPageLink(EvaluationContext context, params object[] parameters)
{
try
{
if (parameters.Length >= 2)
{
string ClassName = ValidationHelper.GetString(parameters[0], "");
string ParentNodeAlias = ValidationHelper.GetString(parameters[1], "");
string Culture = ValidationHelper.GetString(parameters.Length > 2 ? parameters[2] : "en-US", "en-US");
string SiteName = ValidationHelper.GetString(parameters.Length > 3 ? parameters[3] : SiteContext.CurrentSiteName, SiteContext.CurrentSiteName);
string SiteDomain = "";
if(SiteName.Equals("#currentsite", StringComparison.InvariantCultureIgnoreCase))
{
SiteName = SiteContext.CurrentSiteName;
}
if(!string.IsNullOrWhiteSpace(SiteName) && !SiteName.Equals(SiteContext.CurrentSiteName, StringComparison.InvariantCultureIgnoreCase))
{
SiteDomain = (System.Web.HttpContext.Current.Request.IsSecureConnection ? "https://" : "http://") + SiteInfo.Provider.Get(SiteName).DomainName.Trim('/');
}
if (!string.IsNullOrWhiteSpace(ClassName) && !string.IsNullOrWhiteSpace(ParentNodeAlias))
{
return CacheHelper.Cache<string>(cs =>
{
int ClassID = DataClassInfoProvider.GetDataClassInfo(ClassName).ClassID;
int NodeID = new DocumentQuery().Path(ParentNodeAlias, PathTypeEnum.Single).Published(false).LatestVersion(true).CombineWithDefaultCulture().CombineWithAnyCulture().GetEnumerableTypedResult().FirstOrDefault().NodeID;
return SiteDomain+URLHelper.ResolveUrl(string.Format("~/CMSModules/Content/CMSDesk/Edit/Edit.aspx?action=new&classid={0}&parentnodeid={1}&parentculture={2}", ClassID, NodeID, Culture));
}, new CacheSettings(CacheHelper.CacheMinutes(SiteContext.CurrentSiteName), ClassName, ParentNodeAlias, Culture, SiteName));
}
}
}
catch (Exception ex)
{
Service.Resolve<IEventLogService>().LogException("RelationshipMacros", "GetNewPageLinkError", ex);
}
return "#";
}
[MacroMethod(typeof(bool), "Determines if the current relationship tab should be visible (if the current document is either on the left or right side of this relationship).", 0)]
public static object RelationshipTabIsVisible(EvaluationContext context, params object[] parameters)
{
string LeftSideMacro = ValidationHelper.GetString(UIContext.Current.Data.GetValue("IsLeftSideMacro"), "");
string RightSideMacro = ValidationHelper.GetString(UIContext.Current.Data.GetValue("IsRightSideMacro"), "");
TreeNode currentNode = CurrentNode();
if (currentNode != null)
{
var NewResolver = MacroContext.CurrentResolver.CreateChild();
NewResolver.SetNamedSourceData("CurrentDocument", currentNode);
bool IsLeft = ValidationHelper.GetBoolean(NewResolver.ResolveMacros(LeftSideMacro), false);
bool IsRight = ValidationHelper.GetBoolean(NewResolver.ResolveMacros(RightSideMacro), false);
return IsLeft || IsRight;
}
else
{
return false;
}
}
[MacroMethod(typeof(bool), "Determines if the selector should be visible (is left or is right on a switchable non adhoc relationship).", 0)]
public static object RelationshipSelectorIsVisible(EvaluationContext context, params object[] parameters)
{
TreeNode currentNode = CurrentNode();
if (currentNode != null)
{
bool AllowSwitchSides = ValidationHelper.GetBoolean(UIContext.Current.Data.GetValue("AllowSwitchSides"), false);
return IsLeft() || (!IsAdHocRelationship() && AllowSwitchSides && IsRight());
}
else
{
return false;
}
}
[MacroMethod(typeof(bool), "Determines if the Relationship Listing (Editable) should be visible (if the current document is a Left side, or is Right Side and Is not an ad-hoc relationship).", 0)]
public static object RelationshipListingEditableIsVisible(EvaluationContext context, params object[] parameters)
{
TreeNode currentNode = CurrentNode();
if (currentNode != null)
{
return (IsLeft() || (IsRight() && !IsAdHocRelationship()));
}
else
{
return false;
}
}
[MacroMethod(typeof(bool), "Determines if the Relationship Listing (View Only) should be visible (if the current document is a Left side, or is Right Side and Is not an ad-hoc relationship).", 0)]
public static object RelationshipListingReadOnlyIsVisible(EvaluationContext context, params object[] parameters)
{
if (CurrentNode() != null)
{
bool AllowSwitchSides = ValidationHelper.GetBoolean(UIContext.Current.Data.GetValue("AllowSwitchSides"), false);
return !((IsLeft() || (IsRight() && !IsAdHocRelationship())));
}
else
{
return false;
}
}
[MacroMethod(typeof(bool), "Returns if the current document is allowed on the Left side of the relationship (using the Left Side Macro).", 0)]
public static object CurrentNodeIsLeft(EvaluationContext context, params object[] parameters)
{
return IsLeft();
}
[MacroMethod(typeof(bool), "Returns if the current document is allowed on the Right side of the relationship (using the Right Side Macro).", 0)]
public static object CurrentNodeIsRight(EvaluationContext context, params object[] parameters)
{
return IsRight();
}
[MacroMethod(typeof(bool), "Returns if the current relationship (RelationshipName) is an AdHoc Relationship", 0)]
public static object IsAdHocRelationship(EvaluationContext context, params object[] parameters)
{
return IsAdHocRelationship();
}
/// <summary>
/// Determines if the current page fits the IsLeftSideMacro for the UI Page.
/// </summary>
/// <returns>True if it is a Left side relationship</returns>
private static bool IsLeft()
{
string LeftSideMacro = ValidationHelper.GetString(UIContext.Current.Data.GetValue("IsLeftSideMacro"), "");
TreeNode currentNode = CurrentNode();
if (currentNode != null)
{
var NewResolver = MacroContext.CurrentResolver.CreateChild();
NewResolver.SetNamedSourceData("CurrentDocument", currentNode);
return ValidationHelper.GetBoolean(NewResolver.ResolveMacros(LeftSideMacro), false);
}
else
{
return false;
}
}
/// <summary>
/// Determines if the current page fits the IsRightSideMacro for the UI Page.
/// </summary>
/// <returns>True if it is a Right side relationship</returns>
private static bool IsRight()
{
string LeftRightMacro = ValidationHelper.GetString(UIContext.Current.Data.GetValue("IsRightSideMacro"), "");
TreeNode currentNode = CurrentNode();
if (currentNode != null)
{
var NewResolver = MacroContext.CurrentResolver.CreateChild();
NewResolver.SetNamedSourceData("CurrentDocument", currentNode);
return ValidationHelper.GetBoolean(NewResolver.ResolveMacros(LeftRightMacro), false);
}
else
{
return false;
}
}
/// <summary>
/// Gets the current Tree Node based on the NodeID parameter passed to UI elements
/// </summary>
/// <returns>The Tree Node that the current page belongs to</returns>
private static TreeNode CurrentNode()
{
int NodeID = QueryHelper.GetInteger("NodeID", -1);
string Culture = QueryHelper.GetString("culture", "en-US");
if (NodeID > 0)
{
return CacheHelper.Cache<TreeNode>(cs =>
{
TreeNode currentNode = new DocumentQuery().WhereEquals("NodeID", NodeID).Culture(Culture).Published(false).LatestVersion(true).CombineWithDefaultCulture().CombineWithAnyCulture().GetEnumerableTypedResult().FirstOrDefault();
if (currentNode != null && cs.Cached)
{
cs.CacheDependency = CacheHelper.GetCacheDependency("nodeid|" + NodeID);
}
return currentNode;
}, new CacheSettings(CacheHelper.CacheMinutes(SiteContext.CurrentSiteName), "RelationshipMacro", "CurrentNode", NodeID, Culture));
}
else
{
return null;
}
}
/// <summary>
/// Determines if the current relationship is an AdHoc relationship based on the UI Property RelationshipName
/// </summary>
/// <returns>True if the current relationship is an ad hoc relationship</returns>
private static bool IsAdHocRelationship()
{
string RelationshipName = ValidationHelper.GetString(UIContext.Current.Data.GetValue("RelationshipName"), "");
return CacheHelper.Cache<bool>(cs =>
{
RelationshipNameInfo relationshipObj = RelationshipNameInfo.Provider.Get(RelationshipName);
if (relationshipObj != null && cs.Cached)
{
cs.CacheDependency = CacheHelper.GetCacheDependency("cms.relationshipname|byid|" + relationshipObj.RelationshipNameId);
}
return relationshipObj != null ? relationshipObj.RelationshipNameIsAdHoc : false;
}, new CacheSettings(CacheHelper.CacheMinutes(SiteContext.CurrentSiteName), "RelationshipMacro", "IsAdHocRelationship", RelationshipName));
}
}
}