-
Notifications
You must be signed in to change notification settings - Fork 51
/
Helpers.tt
135 lines (119 loc) · 4.49 KB
/
Helpers.tt
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
<#+
/// <summary>
/// Gets the fuly qualified name of the object.
/// </summary>
/// <param name="defaultNamespace">The default namespace.</param>
/// <param name="template">The template.</param>
public static string GetFullyQualifiedName(string defaultNamespace, SitecoreItem item)
{
return GetFullyQualifiedName(defaultNamespace, item, AsClassName);
}
/// <summary>
/// Gets the fuly qualified name of the object.
/// </summary>
/// <param name="defaultNamespace">The default namespace.</param>
/// <param name="template">The template.</param>
/// <param name="nameFunc">The function to run the template name through.</param>
public static string GetFullyQualifiedName(string defaultNamespace, SitecoreItem item, Func<string, string> nameFunc)
{
return string.Concat(GetNamespace(defaultNamespace, item, true), ".", nameFunc(item.Name));
}
/// <summary>
/// Gets the calculated namespace for the template
/// </summary>
public static string GetNamespace(string defaultNamespace, SitecoreItem item, bool includeGlobal = false)
{
List<string> namespaceSegments = new List<string>();
namespaceSegments.Add(defaultNamespace);
namespaceSegments.Add(item.Namespace);
string @namespace = AsNamespace(namespaceSegments); // use an extension method in the supporting assembly
return includeGlobal ? string.Concat("global::", @namespace) : @namespace;
}
/// <summary>
/// Takes multiple namespaces ('.' delimited strings) and joins them together
/// </summary>
/// <param name="namespaces">The namespaces.</param>
public static string JoinNamespaces(params string[] namespaces)
{
// leverage an extension method in the support assembly
return AsNamespace(namespaces);
}
/// <summary>
/// Gets a list of all fields from the template.
/// <para>If desired, fields from all base templates will be included.</para>
/// </summary>
/// <param name="item">The item.</param>
/// <param name="includeBases">if set to <c>true</c> include base template's fields.</param>
public static List<SitecoreField> GetFieldsForTemplate(SitecoreTemplate item, bool includeBases)
{
List<SitecoreField> fields = new List<SitecoreField>();
// Add direct fields that aren't ignored
fields.AddRange(item.Fields.Where(f=> GetCustomProperty(f.Data, "ignore") != "true"));
if (includeBases)
{
// leverage an extenstion method to recursively select base templates, then flatten the fields down
IEnumerable<SitecoreField> baseFields = SelectRecursive(item.BaseTemplates, i=>i.BaseTemplates).SelectMany(t=>t.Fields);
// only grab base fields who aren't ignored
fields.AddRange(baseFields.Where(f=> GetCustomProperty(f.Data, "ignore") != "true"));
}
return fields;
}
/// <summary>
/// Given a sitecore field, returns the name of the property to use.
/// <para>If the field is plural, it returns a plural property name</para>
/// </summary>
/// <param name="field">The field.</param>
/// <returns>A string to use for a property representing the field</returns>
public static string GetPropertyName(SitecoreField field)
{
// has the field been configured in TDS with a custom name?
string customName = GetCustomProperty(field.Data, "name");
if (!string.IsNullOrEmpty(customName))
{
return customName;
}
bool isFieldPlural = IsFieldPlural(field);
return AsPropertyName(field.Name, isFieldPlural);
}
/// <summary>
/// Determines whether the Sitecore Field holds multiple values.
/// </summary>
/// <param name="field">The field.</param>
/// <returns>
/// <c>true</c> if the field holds multiple values; otherwise, <c>false</c>.
/// </returns>
public static bool IsFieldPlural(SitecoreField field)
{
string[] multipleValueFields = new string[]
{
"treelist",
"treelistex",
"treelist descriptive",
"checklist",
"multilist"
};
return multipleValueFields.Contains(field.Type.ToLower());
}
/// <summary>
/// Gets a custom propery from the data assuming it is querystring format
/// </summary>
/// <param name="data">A string in query string format.</param>
/// <param name="key">The key to get the value for.</param>
/// <returns>The value, or an empty string</returns>
public static string GetCustomProperty(string data, string key)
{
if (string.IsNullOrEmpty(data))
return "";
string[] strArray = data.Split(new char[] { '&' });
string keyEquals = key + "=";
int length = keyEquals.Length;
foreach (string keyValuePair in strArray)
{
if ((keyValuePair.Length > length) && keyValuePair.StartsWith(keyEquals, StringComparison.OrdinalIgnoreCase))
{
return keyValuePair.Substring(length);
}
}
return "";
}
#>