-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewLinks.ascx.cs
378 lines (333 loc) · 16 KB
/
ViewLinks.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// <copyright file="ViewLinks.ascx.cs" company="Engage Software">
// Engage: F3
// Copyright (c) 2004-2013
// by Engage Software ( http://www.engagesoftware.com )
// </copyright>
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify and merge copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace Engage.Dnn.F3
{
using System;
using System.Data;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using Publish;
/// <summary>
/// Allows the user to search for content in either module and to replace values once found
/// </summary>
public partial class ViewLinks : PortalModuleBase
{
/// <summary>
/// Backing field for <see cref="LowerTabId"/>
/// </summary>
private int? lowerTabId;
/// <summary>
/// Backing field for <see cref="UpperTabId"/>
/// </summary>
private int? upperTabId;
public static string ApplicationUrl
{
get
{
if (HttpContext.Current.Request.ApplicationPath == "/")
{
return string.Empty;
}
return HttpContext.Current.Request.ApplicationPath;
}
}
/// <summary>
/// Gets the lowest tab ID to search when searching Text/HTML modules.
/// </summary>
/// <value>The lowest tab ID for Text/HTML search.</value>
public int? LowerTabId
{
get
{
if (!this.lowerTabId.HasValue)
{
int tabId;
var lowerTabIdValue = (string)this.Settings["lowerTabId"];
if (int.TryParse(lowerTabIdValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out tabId))
{
this.lowerTabId = tabId;
}
}
return this.lowerTabId;
}
}
/// <summary>
/// Gets the highest tab ID to search when searching Text/HTML modules.
/// </summary>
/// <value>The highest tab ID for Text/HTML search.</value>
public int? UpperTabId
{
get
{
int tabId;
var upperTabIdValue = (string)this.Settings["upperTabId"];
if (int.TryParse(upperTabIdValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out tabId))
{
this.upperTabId = tabId;
}
return this.upperTabId;
}
}
/// <summary>
/// Gets the ID of the portal to search, or <c>null</c> to search all portals.
/// </summary>
/// <value>The portal ID or <c>null</c> for all portals.</value>
public int? SearchPortalId
{
get
{
return this.AllPortalsCheckBox.Checked ? (int?)null : this.PortalId;
}
}
protected static string CleanupText(string text, bool encodeText)
{
if (encodeText)
{
text = HttpUtility.HtmlEncode(text);
}
if (text.Length > 500)
{
return text.Substring(0, 500) + "…";
}
return text;
}
protected static string GetPublishViewLink(int itemId)
{
return ApplicationUrl + "/desktopmodules/engagepublish/itemlink.aspx?itemId=" + itemId;
}
protected static string GetTextHtmlModuleEditLink(int moduleId, int tabId)
{
return Globals.NavigateURL(tabId, "edit", "&mid=" + moduleId);
}
protected void BindPublishData(string searchValue)
{
this.PublishResultsGrid.DataSource = DataProvider.Instance.SearchPublishContent(searchValue, this.SearchPortalId);
this.PublishResultsGrid.DataBind();
// only show portal name column when we're searching multiple portals
this.PublishResultsGrid.Columns[0].Visible = !this.SearchPortalId.HasValue;
}
protected void BindTextHtmlData(string searchValue)
{
this.ResultsGrid.DataSource = DataProvider.Instance.SearchTextHtmlContent(searchValue, this.SearchPortalId, this.LowerTabId, this.UpperTabId);
this.ResultsGrid.DataBind();
// only show portal name column when we're searching multiple portals
this.ResultsGrid.Columns[0].Visible = !this.SearchPortalId.HasValue;
}
/// <summary>
/// Raises the <see cref="Control.Init"/> event.
/// </summary>
/// <param name="e">An <see cref="EventArgs"/> object that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
this.Load += this.Page_Load;
this.SearchTextHtmlButton.Click += this.SearchTextHtmlButton_Click;
this.SearchPublishButton.Click += this.SearchPublishButton_Click;
this.ReplaceTextHtmlButton.Click += this.ReplaceTextHtmlButton_Click;
this.ReplacePublishButton.Click += this.ReplacePublishButton_Click;
base.OnInit(e);
}
/// <summary>
/// Creates the new version of the Text/HTML content for the module with the given ID, with <paramref name="searchValue"/> replaced by <paramref name="replacementValue"/>.
/// </summary>
/// <param name="searchValue">The search value.</param>
/// <param name="replacementValue">The replacement value.</param>
/// <param name="moduleId">The module id.</param>
/// <param name="tabId">The tab id.</param>
/// <param name="portalId">The portal id.</param>
/// <param name="content">The content.</param>
private static void CreateNewTextHtmlVersion(string searchValue, string replacementValue, int moduleId, int tabId, int portalId, string content)
{
var htmlTextController = CreateHtmlTextController();
var workflowId = htmlTextController.GetWorkflowId(moduleId, tabId, portalId);
var htmlInfo = htmlTextController.GetTopHtmlText(moduleId, false, workflowId) ?? htmlTextController.CreateNewHtmlTextInfo();
htmlInfo.ModuleId = moduleId;
htmlInfo.Content = content.Replace(HttpUtility.HtmlEncode(searchValue), HttpUtility.HtmlEncode(replacementValue));
htmlInfo.WorkflowId = workflowId;
htmlInfo.StateId = htmlTextController.GetFirstWorkflowStateId(workflowId);
// TODO: allow direct publish
////if (canDirectlyPublish)
////{
//// htmlInfo.StateID = workflowStateController.GetNextWorkflowStateID(workflowId, htmlInfo.StateID);
////}
htmlTextController.UpdateHtmlText(htmlInfo, htmlTextController.GetMaximumVersionHistory(portalId));
}
/// <summary>
/// Creates an instance of the HTML/Text module's controller.
/// </summary>
/// <returns>A <see cref="IHtmlTextModuleController"/> instance</returns>
private static IHtmlTextModuleController CreateHtmlTextController()
{
return new HtmlTextModuleController();
}
/// <summary>
/// Handles the <see cref="Control.Load"/> event of this control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void Page_Load(object sender, EventArgs e)
{
try
{
this.AllPortalsCheckBox.Visible = this.UserInfo.IsSuperUser;
this.SearchPublishButton.Visible = this.Settings.Contains("chkEnablePublish")
&& bool.Parse(this.Settings["chkEnablePublish"].ToString());
}
catch (Exception exc)
{
// Module failed to load
Exceptions.ProcessModuleLoadException(this, exc);
}
}
/// <summary>
/// Handles the <see cref="Button.Click"/> event of the <see cref="ReplacePublishButton"/> control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void ReplacePublishButton_Click(object sender, EventArgs e)
{
try
{
var searchValue = this.SearchTextBox.Text.Trim();
var replacementValue = this.ReplacementTextBox.Text;
if (string.IsNullOrEmpty(searchValue))
{
return;
}
DataTable searchResults = DataProvider.Instance.SearchPublishContent(searchValue, this.SearchPortalId);
foreach (DataRow resultRow in searchResults.Rows)
{
var article = Article.GetArticle((int)resultRow["ItemId"], this.PortalId, true, true, true);
article.ArticleText = article.ArticleText.Replace(searchValue, replacementValue);
article.Description = article.Description.Replace(searchValue, replacementValue);
article.Save(this.UserInfo.UserID);
}
this.ReplacementResultsLabel.Text = string.Format(
CultureInfo.CurrentCulture,
Localization.GetString("replacementResults", this.LocalResourceFile),
searchValue,
replacementValue,
searchResults.Rows.Count);
this.ReplacementResultsLabel.Visible = true;
this.ResultsGrid.Visible = false;
this.PublishResultsGrid.Visible = false;
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
/// <summary>
/// Handles the <see cref="Button.Click"/> event of the <see cref="ReplaceTextHtmlButton"/> control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void ReplaceTextHtmlButton_Click(object sender, EventArgs e)
{
// Get the initial caching level
var initialCachingLevel = (int)Host.PerformanceSetting;
try
{
var searchValue = this.SearchTextBox.Text.Trim();
var replacementValue = this.ReplacementTextBox.Text;
if (string.IsNullOrEmpty(searchValue))
{
return;
}
DataTable searchResults = DataProvider.Instance.SearchUniqueTextHtmlContent(searchValue, this.SearchPortalId, this.LowerTabId, this.UpperTabId);
// Disable caching
new HostSettingsController().UpdateHostSetting("PerformanceSetting", ((int)Globals.PerformanceSettings.NoCaching).ToString(CultureInfo.InvariantCulture));
DataCache.ClearHostCache(true);
foreach (DataRow searchResultRow in searchResults.Rows)
{
var moduleId = (int)searchResultRow["ModuleID"];
var tabId = (int)searchResultRow["TabID"];
var portalId = (int)searchResultRow["PortalID"];
var content = searchResultRow["Content"].ToString();
CreateNewTextHtmlVersion(searchValue, replacementValue, moduleId, tabId, portalId, content);
}
this.ReplacementResultsLabel.Text = string.Format(
CultureInfo.CurrentCulture,
Localization.GetString("TextHtmlReplacementResults.Format", this.LocalResourceFile),
searchValue,
replacementValue,
searchResults.Rows.Count);
this.ReplacementResultsLabel.Visible = true;
this.ResultsGrid.Visible = false;
this.PublishResultsGrid.Visible = false;
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
}
finally
{
// Enable caching with
// the initial caching level
new HostSettingsController().UpdateHostSetting("PerformanceSetting", initialCachingLevel.ToString(CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Handles the <see cref="Button.Click"/> event of the <see cref="SearchPublishButton"/> control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void SearchPublishButton_Click(object sender, EventArgs e)
{
this.BindPublishData(this.SearchTextBox.Text.Trim());
this.ShowResults(true);
}
/// <summary>
/// Handles the <see cref="Button.Click"/> event of the <see cref="SearchTextHtmlButton"/> control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void SearchTextHtmlButton_Click(object sender, EventArgs e)
{
try
{
this.BindTextHtmlData(this.SearchTextBox.Text.Trim());
this.ShowResults(false);
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
/// <summary>
/// Shows the proper results grid and the <see cref="ReplacementPanel"/>, resetting its display and showing either the Text/HTML results and buttons.
/// </summary>
/// <param name="showPublish">if set to <c>true</c> shows the Engage: Publish search results and replace button, otherwise the Text/HTML results and button.</param>
private void ShowResults(bool showPublish)
{
this.ReplacementPanel.Visible = true;
this.ReplacementTextBox.Text = string.Empty;
this.ReplacementResultsLabel.Text = string.Empty;
this.ReplaceTextHtmlButton.Visible = !showPublish;
this.ReplacePublishButton.Visible = showPublish;
this.ResultsGrid.Visible = !showPublish;
this.PublishResultsGrid.Visible = showPublish;
}
}
}