Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Token replacement for email template #1135

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Dnn.CommunityForums/App_LocalResources/LegacyTokenResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@
<value>&lt;a href="{0}" class="dcf-forums-link"&gt;[RESX:ForumMain]&lt;/a&gt;</value>
</data>
<data name="[ICONLINK-LASTREAD].Text" xml:space="preserve">
<value>&lt;a href="{0}" rel="nofollow"&gt;&lt;img src="[FORUM:THEMELOCATION]/images/miniarrow_down.png" style="vertical-align:middle;" alt="[RESX:JumpToLastRead]" border="0" class="afminiarrow" /&gt;&lt;/a&gt;</value>
<value>&lt;a href="{0}" rel="nofollow"&gt;&lt;img src="[FORUM:THEMELOCATION]images/miniarrow_down.png" style="vertical-align:middle;" alt="[RESX:JumpToLastRead]" border="0" class="afminiarrow" /&gt;&lt;/a&gt;</value>
</data>
<data name="[ICONLINK-LASTREPLY].Text" xml:space="preserve">
<value>&lt;a href="{0}" rel="nofollow"&gt;&lt;img src="[FORUM:THEMELOCATION]/images/miniarrow_right.png" style="vertical-align:middle;" alt="[RESX:JumpToLastReply]" border="0" class="afminiarrow" /&gt;&lt;/a&gt;</value>
<value>&lt;a href="{0}" rel="nofollow"&gt;&lt;img src="[FORUM:THEMELOCATION]images/miniarrow_right.png" style="vertical-align:middle;" alt="[RESX:JumpToLastReply]" border="0" class="afminiarrow" /&gt;&lt;/a&gt;</value>
</data>
<data name="[ICONLOCK]-HideIcon.Text" xml:space="preserve">
<value>&amp;nbsp;&amp;nbsp;&lt;i id="af-topicsview-lock-{0}" class="fa fa-fw fa-red"&gt;&lt;/i&gt;</value>
Expand Down Expand Up @@ -393,4 +393,19 @@
<data name="[AF:TB:Unresolved].Text" xml:space="preserve">
<value>&lt;a href="{0}"&gt;&lt;i class="fa fa-question fa-fw fa-blue"&gt;&lt;/i&gt;&lt;span class="dcf-link-text"&gt;[RESX:Unresolved]&lt;/span&gt;&lt;/a&gt;</value>
</data>
<data name="[LINK].Text" xml:space="preserve">
<value>&lt;a href="{0}"&gt;{0} target="_blank"&lt;/a&gt;</value>
</data>
<data name="[AUTHORPMLINK].Text" xml:space="preserve">
<value>&lt;img class='ComposeMessage' data-recipient='{{ "id": "user-{0}", "name": "[FORUMAUTHOR:DISPLAYNAMEFORJSON]" }}' src='[FORUM:THEMELOCATION]images/icon_pm.png' alt="[RESX:SendPM]" title="[RESX:SendPM]" border="0" /&gt;</value>
</data>
<data name="[EDITAUTHORLINK].Text" xml:space="preserve">
<value>&lt;button class='af-button af-button-edituser' data-id="{0}" data-name="[FORUMAUTHOR:DISPLAYNAMEFORJSON]"&gt;[RESX:Edit]&lt;/button&gt;</value>
</data>
<data name="[EDITUSERLINK].Text" xml:space="preserve">
<value>&lt;button class='af-button af-button-edituser' data-id="{0}" data-name="[FORUMUSER:DISPLAYNAMEFORJSON]"&gt;[RESX:Edit]&lt;/button&gt;</value>
</data>
<data name="[USERPMLINK].Text" xml:space="preserve">
<value>&lt;img class='ComposeMessage' data-recipient='{{ "id": "user-{0}", "name": "[FORUMUSER:DISPLAYNAMEFORJSON]" }}' src='[FORUM:THEMELOCATION]images/icon_pm.png' alt="[RESX:SendPM]" title="[RESX:SendPM]" border="0" /&gt;</value>
</data>
</root>
50 changes: 31 additions & 19 deletions Dnn.CommunityForums/Controllers/EmailController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System.Runtime.CompilerServices;

namespace DotNetNuke.Modules.ActiveForums.Controllers
{
using System;
Expand All @@ -42,20 +44,24 @@ namespace DotNetNuke.Modules.ActiveForums.Controllers

public class EmailController
{
public static void SendEmail(int templateId, int portalId, int moduleId, int tabId, int forumId, int topicId, int replyId, string comments, DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo author)
[Obsolete("Deprecated in Community Forums. Scheduled removal in 10.00.00. Not Used.")]
public static void SendEmail(int templateId, int portalId, int moduleId, int tabId, int forumId, int topicId, int replyId, string comments, DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo author) => throw new NotImplementedException();

internal static void SendEmail(int templateId, int portalId, int moduleId, int tabId, int forumId, int topicId, int replyId, DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo author)
{
DotNetNuke.Abstractions.Portals.IPortalSettings portalSettings = Utilities.GetPortalSettings(portalId);
var sTemplate = string.Empty;
var ti = new TemplateController().Template_Get(templateId);
var subject = TemplateUtils.ParseEmailTemplate(ti.Subject, string.Empty, portalId, moduleId, tabId, forumId, topicId, replyId, string.Empty, author.AuthorId, Utilities.GetCultureInfoForUser(portalId, author.AuthorId), Utilities.GetTimeZoneOffsetForUser(portalId, author.AuthorId));
var body = TemplateUtils.ParseEmailTemplate(ti.Template, string.Empty, portalId, moduleId, tabId, forumId, topicId, replyId, string.Empty, author.AuthorId, Utilities.GetCultureInfoForUser(portalId, author.AuthorId), Utilities.GetTimeZoneOffsetForUser(portalId, author.AuthorId));
body = body.Replace("[REASON]", comments);
var subject = TemplateUtils.ParseEmailTemplate(ti.Subject, string.Empty, portalId, moduleId, tabId, forumId, topicId, replyId, author, accessingUser: author.ForumUser, topicSubscriber: false, new Services.URLNavigator().NavigationManager(), HttpContext.Current.Request.Url);
var body = TemplateUtils.ParseEmailTemplate(ti.Template, string.Empty, portalId, moduleId, tabId, forumId, topicId, replyId, author, accessingUser: author.ForumUser, topicSubscriber: false, new Services.URLNavigator().NavigationManager(), HttpContext.Current.Request.Url);
var fi = new DotNetNuke.Modules.ActiveForums.Controllers.ForumController().GetById(forumId);
var sFrom = fi.EmailAddress != string.Empty ? fi.EmailAddress : portalSettings.Email;

// Send now
var recipients = new List<string>();
recipients.Add(author.Email);
var recipients = new List<string>
{
author.Email,
};
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.Send(new DotNetNuke.Modules.ActiveForums.Entities.EmailInfo()
{
Body = body,
Expand All @@ -76,7 +82,7 @@ public static void SendEmail(int templateId, int portalId, int moduleId, int tab
[Obsolete("Deprecated in Community Forums. Scheduled removal in 09.00.00. Use SendTemplatedEmail(int templateId, int portalId, int topicId, int replyId, int moduleID, int tabID, string comments, int userId, Forum fi, List<SubscriptionInfo> subs, Uri requestUrl)")]
public static void SendTemplatedEmail(int templateId, int portalId, int topicId, int replyId, int moduleID, int tabID, string comments, int userId, Forum fi, List<DotNetNuke.Modules.ActiveForums.SubscriptionInfo> subs) => throw new NotImplementedException();

internal static void SendTemplatedEmail(int templateId, int portalId, int topicId, int replyId, int moduleID, int tabID, string comments, DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo author, DotNetNuke.Modules.ActiveForums.Entities.ForumInfo fi, List<DotNetNuke.Modules.ActiveForums.Entities.SubscriptionInfo> subs, Uri requestUrl)
internal static void SendTemplatedEmail(int templateId, int portalId, int topicId, int replyId, int moduleID, int tabID, DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo author, DotNetNuke.Modules.ActiveForums.Entities.ForumInfo fi, List<DotNetNuke.Modules.ActiveForums.Entities.SubscriptionInfo> subs, Uri requestUrl)
{
var navigationManager = (INavigationManager)new Services.URLNavigator().NavigationManager();
DotNetNuke.Abstractions.Portals.IPortalSettings portalSettings = Utilities.GetPortalSettings(portalId);
Expand All @@ -89,35 +95,41 @@ internal static void SendTemplatedEmail(int templateId, int portalId, int topicI
IEnumerable<TimeSpan> timeZoneOffsets = subs.Where(s => s.UserCulture == userCulture).Select(s => s.TimeZoneOffSet).Distinct();
foreach (TimeSpan timeZoneOffset in timeZoneOffsets)
{
string sTemplate = string.Empty;
string sFrom = fi.EmailAddress != string.Empty ? fi.EmailAddress : portalSettings.Email;

/* subject and body, etc. can now be different based on topic subscriber vs forum subscriber so process first for topic subscribers and then for forum subscribers */
var recipients = subs.Where(s => s.TimeZoneOffSet == timeZoneOffset && s.UserCulture == userCulture && s.TopicSubscriber && !string.IsNullOrEmpty(s.Email)).Select(s => s.Email).ToList();
if (recipients.Count > 0)
/* subject and body, etc. can now be different based on topic subscriber vs forum subscriber so process first for topic subscribers and then for forum subscribers;
in addition, user-specific tokens are now supported in email templates, so need to process template and send email uniquely for each user */
var topicSubscribers = subs.Where(s => s.TimeZoneOffSet == timeZoneOffset && s.UserCulture.Equals(userCulture) && s.TopicSubscriber && !string.IsNullOrEmpty(s.Email)).ToList();
foreach (var topicSubscriber in topicSubscribers)
{
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.Send(new DotNetNuke.Modules.ActiveForums.Entities.EmailInfo()
{
From = sFrom,
PortalId = portalId,
ModuleId = moduleID,
Recipients = recipients,
Subject = TemplateUtils.ParseEmailTemplate(ti.Subject, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, comments: string.Empty, author: author, userId: author.AuthorId, userCulture: userCulture, timeZoneOffset: timeZoneOffset, topicSubscriber: true, navigationManager: navigationManager, requestUrl: requestUrl),
Body = TemplateUtils.ParseEmailTemplate(ti.Template, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, comments: comments, author: author, userId: author.AuthorId, userCulture: userCulture, timeZoneOffset: timeZoneOffset, topicSubscriber: true, navigationManager: navigationManager, requestUrl: requestUrl),
Recipients = new List<string>()
{
topicSubscriber.Email,
},
Subject = Utilities.StripHTMLTag(TemplateUtils.ParseEmailTemplate(ti.Subject, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, author: author, accessingUser: topicSubscriber.User, topicSubscriber: true, navigationManager: navigationManager, requestUrl: requestUrl)),
Body = TemplateUtils.ParseEmailTemplate(ti.Template, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, author: author, accessingUser: topicSubscriber.User, topicSubscriber: true, navigationManager: navigationManager, requestUrl: requestUrl),
});
}

recipients = subs.Where(s => s.TimeZoneOffSet == timeZoneOffset && s.UserCulture == userCulture && !s.TopicSubscriber && !string.IsNullOrEmpty(s.Email)).Select(s => s.Email).ToList();
if (recipients.Count > 0)
var forumSubscribers = subs.Where(s => s.TimeZoneOffSet == timeZoneOffset && s.UserCulture.Equals(userCulture) && !s.TopicSubscriber && !string.IsNullOrEmpty(s.Email)).ToList();
foreach (var forumSubscriber in forumSubscribers)
{
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.Send(new DotNetNuke.Modules.ActiveForums.Entities.EmailInfo()
{
From = sFrom,
ModuleId = moduleID,
PortalId = portalId,
Recipients = recipients,
Subject = TemplateUtils.ParseEmailTemplate(ti.Subject, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, comments: string.Empty, author: author, userId: author.AuthorId, userCulture: userCulture, timeZoneOffset: timeZoneOffset, topicSubscriber: false, navigationManager: navigationManager, requestUrl: requestUrl),
Body = TemplateUtils.ParseEmailTemplate(ti.Template, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, comments: comments, author: author, userId: author.AuthorId, userCulture: userCulture, timeZoneOffset: timeZoneOffset, topicSubscriber: false, navigationManager: navigationManager, requestUrl: requestUrl),
Recipients = new List<string>()
{
forumSubscriber.Email,
},
Subject = Utilities.StripHTMLTag(TemplateUtils.ParseEmailTemplate(ti.Subject, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, author: author, accessingUser: forumSubscriber.User, topicSubscriber: false, navigationManager: navigationManager, requestUrl: requestUrl)),
Body = TemplateUtils.ParseEmailTemplate(ti.Template, templateName: string.Empty, portalID: portalId, moduleID: moduleID, tabID: tabID, forumID: fi.ForumID, topicId: topicId, replyId: replyId, author: author, accessingUser: forumSubscriber.User, topicSubscriber: false, navigationManager: navigationManager, requestUrl: requestUrl),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion Dnn.CommunityForums/Controllers/ReplyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public DotNetNuke.Modules.ActiveForums.Entities.ReplyInfo ApproveReply(int porta

if (forum.ModApproveTemplateId > 0 & reply.Author.AuthorId > 0)
{
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.SendEmail(forum.ModApproveTemplateId, portalId, moduleId, tabId, forumId, topicId, replyId, string.Empty, reply.Author);
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.SendEmail(forum.ModApproveTemplateId, portalId, moduleId, tabId, forumId, topicId, replyId, reply.Author);
}

DotNetNuke.Modules.ActiveForums.Controllers.ReplyController.QueueApprovedReplyAfterAction(portalId, tabId, moduleId, forum.ForumGroupId, forumId, topicId, replyId, reply.Content.AuthorId);
Expand Down
4 changes: 2 additions & 2 deletions Dnn.CommunityForums/Controllers/TopicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static DotNetNuke.Modules.ActiveForums.Entities.TopicInfo Approve(int top

if (ti.Forum.ModApproveTemplateId > 0 & ti.Author.AuthorId > 0)
{
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.SendEmail(ti.Forum.ModApproveTemplateId, ti.PortalId, ti.ModuleId, ti.Forum.TabId, ti.ForumId, topicId, 0, string.Empty, ti.Author);
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.SendEmail(ti.Forum.ModApproveTemplateId, ti.PortalId, ti.ModuleId, ti.Forum.TabId, ti.ForumId, topicId, 0, ti.Author);
}

DotNetNuke.Modules.ActiveForums.Controllers.TopicController.QueueApprovedTopicAfterAction(ti.PortalId, ti.Forum.TabId, ti.Forum.ModuleId, ti.Forum.ForumGroupId, ti.ForumId, topicId, -1, ti.Content.AuthorId);
Expand Down Expand Up @@ -182,7 +182,7 @@ public static void Move(int topicId, int newForumId)

if (oldForum.ModMoveTemplateId > 0 & ti?.Author?.AuthorId > 0)
{
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.SendEmail(oldForum.ModMoveTemplateId, ti.PortalId, ti.ModuleId, ti.Forum.TabId, forumId: ti.Forum.ForumID, ti.TopicId, -1, string.Empty, ti.Author);
DotNetNuke.Modules.ActiveForums.Controllers.EmailController.SendEmail(oldForum.ModMoveTemplateId, ti.PortalId, ti.ModuleId, ti.Forum.TabId, forumId: ti.Forum.ForumID, topicId: ti.TopicId, replyId: -1, author: ti.Author);
}

new DotNetNuke.Modules.ActiveForums.Controllers.ProcessQueueController().Add(ProcessType.UpdateForumLastUpdated, ti.PortalId, tabId: -1, moduleId: ti.ModuleId, forumGroupId: oldForum.ForumGroupId, forumId: oldForum.ForumID, topicId: topicId, replyId: -1, authorId: ti.Content.AuthorId, requestUrl: null);
Expand Down
8 changes: 6 additions & 2 deletions Dnn.CommunityForums/CustomControls/UserControls/ForumView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ public string BuildForumView()


StringBuilder stringBuilder = new StringBuilder(sTemplate);
#region "Backward compatilbility -- remove in v10.00.00"
stringBuilder = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.RemoveObsoleteTokens(stringBuilder);
stringBuilder = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyUserTokenSynonyms(stringBuilder, this.PortalSettings, this.MainSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale);
stringBuilder = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyForumTokenSynonyms(stringBuilder, this.PortalSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale);
#endregion "Backward compatilbility -- remove in v10.00.00"

stringBuilder.Replace("[JUMPTO]", "<asp:placeholder id=\"plhQuickJump\" runat=\"server\" />");
stringBuilder.Replace("[STATISTICS]", "<am:Stats id=\"amStats\" MID=\"" + this.ModuleId + "\" PID=\"" + this.PortalId.ToString() + "\" runat=\"server\" />");
stringBuilder.Replace("[WHOSONLINE]", this.MainSettings.UsersOnlineEnabled ? "<asp:placeholder id=\"plhUsersOnline\" runat=\"server\" />" : string.Empty);
Expand Down Expand Up @@ -179,7 +183,7 @@ public string BuildForumView()
string sForumTemp = TemplateUtils.GetTemplateSection(sTemplate, "[FORUMS]", "[/FORUMS]");
string tmpGroup = string.Empty;

#region "backward compatibilty - remove when removing ForumTable property"
#region "backward compatibilty - remove when removing ForumTable property"
#pragma warning disable CS0618
/* this is for backward compatibility -- remove when removing ForumTable property in 10.00.00 */
if (this.ForumTable != null)
Expand All @@ -193,7 +197,7 @@ public string BuildForumView()
this.Forums.Add(new DotNetNuke.Modules.ActiveForums.Controllers.ForumController().GetById(Utilities.SafeConvertInt(dr["ForumId"]), this.ForumModuleId));
}
}
#endregion
#endregion "backward compatibilty - remove when removing ForumTable property"

if (this.Forums == null)
{
Expand Down
Loading