Skip to content

Commit

Permalink
Merge pull request #1143 from johnhenley/dev82/tokens-like-buttons-1142
Browse files Browse the repository at this point in the history
ENH: Use token replacement for like buttons
  • Loading branch information
johnhenley authored Oct 31, 2024
2 parents e4e194b + 0fd3447 commit 3edf27a
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,13 @@
<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>
<data name="[LIKESx1].Text" xml:space="preserve">
<value>&lt;i id="af-topicview-likes-[FORUMPOST:CONTENTID]" class="fa [FORUMPOST:ISLIKED|fa-thumbs-o-up|fa-thumbs-up]" style="cursor: pointer" onclick="{0}"&gt; [FORUMPOST:LIKECOUNT]&lt;/i&gt;|&lt;i id="af-topicview-likes" class="fa [FORUMPOST:ISLIKED|fa-thumbs-o-up|fa-thumbs-up] fa-2x"&gt; [FORUMPOST:LIKECOUNT]&lt;/i&gt;</value>
</data>
<data name="[LIKESx2].Text" xml:space="preserve">
<value>&lt;i id="af-topicview-likes-[FORUMPOST:CONTENTID]" class="fa [FORUMPOST:ISLIKED|fa-thumbs-o-up|fa-thumbs-up] fa-2x" style="cursor: pointer" onclick="{0}"&gt; [FORUMPOST:LIKECOUNT]&lt;/i&gt;|&lt;i id="af-topicview-likes" class="fa [FORUMPOST:ISLIKED|fa-thumbs-o-up|fa-thumbs-up] fa-2x"&gt; [FORUMPOST:LIKECOUNT]&lt;/i&gt;</value>
</data>
<data name="[LIKESx3].Text" xml:space="preserve">
<value>&lt;i id="af-topicview-likes-[FORUMPOST:CONTENTID]" class="fa [FORUMPOST:ISLIKED|fa-thumbs-o-up|fa-thumbs-up] fa-3x" style="cursor: pointer" onclick="{0}"&gt; [FORUMPOST:LIKECOUNT]&lt;/i&gt;|&lt;i id="af-topicview-likes" class="fa [FORUMPOST:ISLIKED|fa-thumbs-o-up|fa-thumbs-up] fa-2x"&gt; [FORUMPOST:LIKECOUNT]&lt;/i&gt;</value>
</data>
</root>
18 changes: 9 additions & 9 deletions Dnn.CommunityForums/Controllers/LikeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@
// 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;
using System.Collections.Generic;
using System.Linq;

using DotNetNuke.Data;

namespace DotNetNuke.Modules.ActiveForums.Controllers
{
using System.Collections.Generic;
using System.Linq;

internal partial class LikeController : RepositoryControllerBase<DotNetNuke.Modules.ActiveForums.Entities.LikeInfo>
{
public LikeController() : base() { }

public bool GetForUser(int userId, int postId)
{
return this.Find("WHERE PostId = @0 AND UserId = @1 AND Checked = 1", postId, userId).Any();
return userId > 0 && this.Find("WHERE PostId = @0 AND UserId = @1 AND Checked = 1", postId, userId).Any();
}

public (int count, bool liked) Get(int userId, int postId)
Expand Down Expand Up @@ -82,6 +77,11 @@ public int Like(int contentId, int userId)

namespace DotNetNuke.Modules.ActiveForums
{
using System;
using System.Collections.Generic;

using DotNetNuke.Data;

[Obsolete("Deprecated in Community Forums. Scheduled removal in 09.00.00. Replace with DotNetNuke.Modules.ActiveForums.Controllers.LikeController")]
class LikesController : DotNetNuke.Modules.ActiveForums.Controllers.LikeController
{
Expand Down
32 changes: 1 addition & 31 deletions Dnn.CommunityForums/CustomControls/UserControls/TopicView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class TopicView : ForumBase
private int pageSize = 20;
private int rowCount;
private bool isTrusted;
private bool allowLikes;
private bool isSubscribedTopic;
private string defaultSort;
private string tags = string.Empty;
Expand Down Expand Up @@ -326,7 +325,6 @@ private void LoadData(int pageId)

this.topicTemplateId = Utilities.SafeConvertInt(this.drForum["TopicTemplateId"]);
this.tags = this.drForum["Tags"].ToString();
this.allowLikes = Utilities.SafeConvertBool(this.drForum["AllowLikes"]);
this.rowCount = Utilities.SafeConvertInt(this.drForum["ReplyCount"]) + 1;
this.isSubscribedTopic = new DotNetNuke.Modules.ActiveForums.Controllers.SubscriptionController().Subscribed(this.PortalId, this.ForumModuleId, this.UserId, this.ForumInfo.ForumID, this.TopicId);

Expand Down Expand Up @@ -932,6 +930,7 @@ private string ParseTopic(string sOutput)
sTopicTemplate = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyUserTokenSynonyms(new StringBuilder(sTopicTemplate), this.PortalSettings, this.MainSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale).ToString();
sTopicTemplate = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyAuthorTokenSynonyms(new StringBuilder(sTopicTemplate), this.PortalSettings, this.MainSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale).ToString();
sTopicTemplate = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyTopicTokenSynonyms(new StringBuilder(sTopicTemplate), this.PortalSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale).ToString();
sTopicTemplate = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyPostTokenSynonyms(new StringBuilder(sTopicTemplate), this.PortalSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale).ToString();
sReplyTemplate = DotNetNuke.Modules.ActiveForums.Services.Tokens.TokenReplacer.MapLegacyPostTokenSynonyms(new StringBuilder(sReplyTemplate), this.PortalSettings, this.ForumUser.UserInfo?.Profile?.PreferredLocale).ToString();

#endregion "Backward compatilbility -- remove in v10.00.00"
Expand Down Expand Up @@ -1071,35 +1070,6 @@ private string ParseContent(DataRow dr, string template, int rowcount)
sbOutput.Replace("[POSTREPLYCSS]", "afpostreply afpostreply2");
}

#region "likes"

if (this.allowLikes)
{
(int count, bool liked) likes = new DotNetNuke.Modules.ActiveForums.Controllers.LikeController().Get(this.UserId, (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId));
string image = likes.liked ? "fa-thumbs-o-up" : "fa-thumbs-up";

if (this.CanReply)
{
sbOutput = sbOutput.Replace("[LIKES]", "<i id=\"af-topicview-likes1-" + (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId) + "\" class=\"fa " + image + "\" style=\"cursor:pointer\" onclick=\"amaf_likePost(" + this.ModuleId + "," + this.ForumId + "," + (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId) + ")\" > " + likes.count.ToString() + "</i>");
sbOutput = sbOutput.Replace("[LIKESx2]", "<i id=\"af-topicview-likes2-" + (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId) + "\" class=\"fa " + image + " fa-2x\" style=\"cursor:pointer\" onclick=\"amaf_likePost(" + this.ModuleId + "," + this.ForumId + "," + (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId) + ")\" > " + likes.count.ToString() + "</i>");
sbOutput = sbOutput.Replace("[LIKESx3]", "<i id=\"af-topicview-likes3-" + (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId) + "\" class=\"fa " + image + " fa-3x\" style=\"cursor:pointer\" onclick=\"amaf_likePost(" + this.ModuleId + "," + this.ForumId + "," + (reply.ReplyId > 0 ? reply.ContentId : this.topic.ContentId) + ")\" > " + likes.count.ToString() + "</i>");
}
else
{
sbOutput = sbOutput.Replace("[LIKES]", "<i id=\"af-topicview-likes1\" class=\"fa " + image + "\" style=\"cursor:default\" > " + likes.count.ToString() + "</i>");
sbOutput = sbOutput.Replace("[LIKESx2]", "<i id=\"af-topicview-likes2\" class=\"fa " + image + " fa-2x\" style=\"cursor:default\" > " + likes.count.ToString() + "</i>");
sbOutput = sbOutput.Replace("[LIKESx3]", "<i id=\"af-topicview-likes3\" class=\"fa " + image + " fa-3x\" style=\"cursor:default\" > " + likes.count.ToString() + "</i>");
}
}
else
{
sbOutput = sbOutput.Replace("[LIKES]", string.Empty);
sbOutput = sbOutput.Replace("[LIKESx2]", string.Empty);
sbOutput = sbOutput.Replace("[LIKESx3]", string.Empty);
}

#endregion "likes"

// Poll Results
if (sOutput.Contains("[POLLRESULTS]"))
{
Expand Down
4 changes: 4 additions & 0 deletions Dnn.CommunityForums/Entities/IPostInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public interface IPostInfo : DotNetNuke.Services.Tokens.IPropertyAccess

string RawUrl { get; set; }

int LikeCount { get; }

DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo Author { get; set; }

DotNetNuke.Modules.ActiveForums.Entities.ContentInfo Content { get; set; }
Expand All @@ -58,6 +60,8 @@ public interface IPostInfo : DotNetNuke.Services.Tokens.IPropertyAccess

DotNetNuke.Modules.ActiveForums.Entities.TopicInfo Topic { get; }

bool IsLikedByUser(DotNetNuke.Modules.ActiveForums.Entities.ForumUserInfo forumUser);

DotNetNuke.Modules.ActiveForums.Enums.PostStatus GetPostStatusForUser(DotNetNuke.Modules.ActiveForums.Entities.ForumUserInfo forumUser);

string GetPostStatusCss(DotNetNuke.Modules.ActiveForums.Entities.ForumUserInfo forumUser);
Expand Down
40 changes: 39 additions & 1 deletion Dnn.CommunityForums/Entities/ReplyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public partial class ReplyInfo : DotNetNuke.Modules.ActiveForums.Entities.IPostI
{
private DotNetNuke.Modules.ActiveForums.Entities.TopicInfo topicInfo;
private DotNetNuke.Modules.ActiveForums.Entities.ContentInfo contentInfo;
private DotNetNuke.Modules.ActiveForums.Entities.ForumInfo forumInfo;
private DotNetNuke.Modules.ActiveForums.Entities.AuthorInfo author;
private int? likeCount;

[IgnoreColumn]
public int ForumId
Expand Down Expand Up @@ -111,6 +111,26 @@ internal DotNetNuke.Modules.ActiveForums.Entities.TopicInfo GetTopic()
[IgnoreColumn]
public string Summary => Content.Summary;

[IgnoreColumn]
public int LikeCount
{
get
{
if (this.likeCount == null)
{
this.likeCount =
new DotNetNuke.Modules.ActiveForums.Controllers.LikeController().Count(this.ContentId);
}

return (int)this.likeCount;
}
}

public bool IsLikedByUser(ForumUserInfo forumUser)
{
return new DotNetNuke.Modules.ActiveForums.Controllers.LikeController().GetForUser(forumUser.UserId, this.ContentId);
}

[IgnoreColumn]
public DotNetNuke.Modules.ActiveForums.Entities.ContentInfo Content
{
Expand Down Expand Up @@ -390,6 +410,24 @@ public string GetProperty(string propertyName, string format, System.Globalizati
return PropertyAccess.FormatString(slink, format);
}

case "isliked":
return !this.Forum.AllowLikes ? string.Empty : PropertyAccess.FormatString(this.IsLikedByUser(new Controllers.ForumUserController(this.ModuleId).GetByUserId(accessingUser.PortalID, accessingUser.UserID)) ? true.ToString() : string.Empty, format);
case "likecount":
return !this.Forum.AllowLikes ? string.Empty : PropertyAccess.FormatString(this.LikeCount.ToString(), format);
case "likeonclick":
{
var bReply = Controllers.PermissionController.HasPerm(this.Forum.Security.Reply,
accessingUser.PortalID,
this.Forum.ModuleId, accessingUser.UserID);
if (this.Forum.AllowLikes)
{
return PropertyAccess.FormatString(bReply ?
$"amaf_likePost({this.Forum.ModuleId},{this.Forum.ForumID},{this.ContentId})" : string.Empty,
format);
}
}

return string.Empty;
case "authorid":
return PropertyAccess.FormatString(this.Content.AuthorId.ToString(), format);
case "authorname":
Expand Down
39 changes: 39 additions & 0 deletions Dnn.CommunityForums/Entities/TopicInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public Category(int id, string name, bool selected)
private string tags = string.Empty;
private string selectedcategories;
private int? subscriberCount;
private int? likeCount;

public int TopicId { get; set; }

Expand Down Expand Up @@ -217,6 +218,26 @@ public int SubscriberCount
}
}

[IgnoreColumn]
public int LikeCount
{
get
{
if (this.likeCount == null)
{
this.likeCount =
new DotNetNuke.Modules.ActiveForums.Controllers.LikeController().Count(this.ContentId);
}

return (int)this.likeCount;
}
}

public bool IsLikedByUser(ForumUserInfo forumUser)
{
return new DotNetNuke.Modules.ActiveForums.Controllers.LikeController().GetForUser(forumUser.UserId, this.ContentId);
}

[IgnoreColumn]
public ContentInfo Content
{
Expand Down Expand Up @@ -695,6 +716,24 @@ public string GetProperty(string propertyName, string format, System.Globalizati
return PropertyAccess.FormatString(this.ViewCount.ToString(), format);
case "subscribercount":
return PropertyAccess.FormatString(this.SubscriberCount.ToString(), format);
case "isliked":
return !this.Forum.AllowLikes ? string.Empty : PropertyAccess.FormatString(this.IsLikedByUser(new Controllers.ForumUserController(this.ModuleId).GetByUserId(accessingUser.PortalID, accessingUser.UserID)) ? true.ToString() : string.Empty, format);
case "likecount":
return !this.Forum.AllowLikes ? string.Empty : PropertyAccess.FormatString(this.LikeCount.ToString(), format);
case "likeonclick":
{
var bReply = Controllers.PermissionController.HasPerm(this.Forum.Security.Reply,
accessingUser.PortalID,
this.Forum.ModuleId, accessingUser.UserID);
if (this.Forum.AllowLikes)
{
return PropertyAccess.FormatString(bReply ?
$"amaf_likePost({this.Forum.ModuleId},{this.Forum.ForumID},{this.ContentId})" : string.Empty,
format);
}
}

return string.Empty;
case "status":
{
var bRead = Controllers.PermissionController.HasPerm(this.Forum.Security.Read,
Expand Down
Loading

0 comments on commit 3edf27a

Please sign in to comment.