forked from 2sic/2sxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditTemplateFile.ascx.cs
215 lines (179 loc) · 8.65 KB
/
EditTemplateFile.ascx.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DotNetNuke.Services.Tokens;
using DotNetNuke.Web.UI.WebControls;
namespace ToSic.SexyContent
{
public partial class EditTemplateFile : SexyControlEditBase
{
//private const string AdditionalSystemTokens = "[Content:Toolbar],ContentToolbar";
//private const string ListAdditionalSystemTokens = "[List:Index],ListIndex;[List:Index1],ListIndex1;[List:Count],ListCount;[List:IsFirst],ListIsFirst;[List:IsLast],ListIsLast;[List:Alternator2],ListAlternator2;[List:Alternator3],ListAlternator3;[List:Alternator4],ListAlternator4;[List:Alternator5],ListAlternator5;[ListContent:Toolbar],ListToolbar;<repeat>...</repeat>,ListRepeat";
//private const string AdditionalSystemRazor = "@Content.Toolbar,ContentToolbar";
//private const string ListAdditionalSystemRazor = "@ListContent.Toolbar,ListToolbar";
private bool UserMayEdit
{
get
{
return UserInfo.IsSuperUser ||
(Template.Location == SexyContent.TemplateLocations.PortalFileSystem && !Template.IsRazor && UserInfo.IsInRole(PortalSettings.AdministratorRoleName));
}
}
private Template Template
{
get {
int TemplateID = int.Parse(Request.QueryString["TemplateID"]);
return Sexy.TemplateContext.GetTemplate(TemplateID);
}
}
private string TemplatePath
{
get {
return Server.MapPath(System.IO.Path.Combine(SexyContent.GetTemplatePathRoot(Template.Location, Sexy.App), Template.Path));
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!UserMayEdit)
{
btnUpdate.Visible = false;
txtTemplateContent.Enabled = false;
}
hlkCancel.NavigateUrl = DotNetNuke.Common.Globals.NavigateURL(this.TabId);
if (IsPostBack)
return;
if (Template == null)
return;
if (File.Exists(TemplatePath))
txtTemplateContent.Text = File.ReadAllText(TemplatePath);
// Get name of the current template and write it to lblTemplate
lblTemplate.Text = Template.Name;
lblTemplateLocation.Text = @"/" + TemplatePath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, String.Empty).Replace('\\','/');
lblEditTemplateFileHeading.Text = String.Format(LocalizeString("lblEditTemplateFileHeading.Text"), Template.Name);
var defaultLanguageID = Sexy.ContentContext.GetLanguageId(PortalSettings.DefaultLanguage);
var languageList = defaultLanguageID.HasValue ? new[] {defaultLanguageID.Value} : new[] { 0 };
var templateDefaults = Sexy.GetTemplateDefaults(Template.TemplateID).Where(t => t.ContentTypeID.HasValue);
string formatString;
if (Template.Type == "Token")
formatString = "[{0}:{1}]";
else
formatString = "@{0}.{1}";
foreach(var templateDefault in templateDefaults)
{
var contentTypeId = templateDefault.ContentTypeID.Value;
AddHelpForAContentType(contentTypeId, formatString, templateDefault, languageList);
}
// todo: add AppResources and AppSettings help
// add standard help
AddHelpFragments();
}
/// <summary>
/// Create a help-table showing all the tokens/placeholders for a specific content type
/// </summary>
/// <param name="contentTypeId"></param>
/// <param name="FormatString"></param>
/// <param name="TemplateDefault"></param>
/// <param name="LanguageList"></param>
private void AddHelpForAContentType(int contentTypeId, string FormatString, TemplateDefault TemplateDefault,
int[] LanguageList)
{
Eav.AttributeSet Set = Sexy.ContentContext.GetAttributeSet(contentTypeId);
var DataSource = Sexy.ContentContext.GetAttributes(Set, true).Select(a => new
{
StaticName = String.Format(FormatString, TemplateDefault.ItemType.ToString("F"), a.StaticName),
DisplayName =
(Sexy.ContentContext.GetAttributeMetaData(a.AttributesInSets.FirstOrDefault().AttributeID)).ContainsKey("Name")
? (Sexy.ContentContext.GetAttributeMetaData(a.AttributesInSets.FirstOrDefault().AttributeID))["Name"][LanguageList]
: a.StaticName + " (static)"
}).ToList();
AddFieldGrid(DataSource, TemplateDefault.ItemType.ToString("F"));
}
/// <summary>
/// Add helper infos to the editor, common tokens, razor snippets etc.
/// </summary>
private void AddHelpFragments()
{
// 2014-07-18 2dm - new
var lookupKey = Template.IsRazor ? "Razor" : "Token";
var additionalHelpers = LocalizeString("Additional" + lookupKey + "Sets.Text");
if (!Template.UseForList)
additionalHelpers = additionalHelpers.Replace("List" + lookupKey + ",", "");
foreach (var helperSet in additionalHelpers.Split(','))
{
var setText = LocalizeString(helperSet + ".List");
var hasEncodedStuff = (setText.IndexOf("»") > 0);
var splitFilter1 = hasEncodedStuff ? new string[] {"»\r\n", "»\n"} : new string[] {"\r\n", "\n"};
var splitFilter2 = hasEncodedStuff ? new string[] {"«"} : new string[] {"="};
var data =
setText.Split(splitFilter1, StringSplitOptions.None)
.Select(
d =>
new
{
StaticName = d.Split(splitFilter2, StringSplitOptions.None)[0],
DisplayName = d.Split(splitFilter2, StringSplitOptions.None)[1]
})
.ToList();
if (hasEncodedStuff)
data =
data.Select(x => new {StaticName = EncodeCode(x.StaticName), DisplayName = EncodeComment(x.DisplayName)}).ToList();
// todo: bug - cannot pass in translated title, the grid always tries to re-look it up in another resx
//var title = LocalizeString(tokenSet + ".Title");
AddFieldGrid(data, helperSet);
}
//// Add Token Help Tables
//if (!Template.IsRazor)
//{
// // 2014-07-18 2dm - removed
// // AddFieldGrid(AdditionalSystemTokens.Split(';').Select(d => new { StaticName = d.Split(',')[0], DisplayName = LocalizeString(d.Split(',')[1] + ".Text")}), "System");
// //if (Template.UseForList)
// // AddFieldGrid(ListAdditionalSystemTokens.Split(';').Select(d => new { StaticName = HttpUtility.HtmlEncode(d.Split(',')[0]), DisplayName = LocalizeString(d.Split(',')[1] + ".Text") }), "ListSystem");
//}
//// Add Razor Help Tables
//else
//{
// AddFieldGrid(AdditionalSystemRazor.Split(';').Select(d => new { StaticName = d.Split(',')[0], DisplayName = LocalizeString(d.Split(',')[1] + ".Text") }), "System");
// if(Template.UseForList)
// AddFieldGrid(ListAdditionalSystemRazor.Split(';').Select(d => new { StaticName = d.Split(',')[0], DisplayName = LocalizeString(d.Split(',')[1] + ".Text") }), "ListSystem");
//}
}
protected string EncodeCode(string original)
{
//return "<pre>" + original + "</pre>";
return Server.HtmlEncode(original)
.Replace("\r\n", "<br/>")
.Replace("\t", " ");
}
protected string EncodeComment(string original)
{
return Server.HtmlEncode(original)
.Replace("\r\n", "<br/>")
.Replace("\t", " ");
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
if(!UserMayEdit)
return;
if (File.Exists(TemplatePath))
{
File.WriteAllText(TemplatePath, txtTemplateContent.Text);
Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId));
}
}
protected void AddFieldGrid(object dataSource, string headerText)
{
var gridControl = (TemplateHelpGrid)LoadControl(Path.Combine(TemplateSourceDirectory, "TemplateHelpGrid.ascx"));
var grid = gridControl.Grid;
// DataBind the GridView with the Tokens
DnnGridBoundColumn tokenColumn = ((DnnGridBoundColumn)grid.Columns.FindByUniqueName("StaticName"));
tokenColumn.HeaderText = headerText;
grid.DataSource = dataSource;
grid.DataBind();
phGrids.Controls.Add(gridControl);
}
}
}