how to make "Notify me when someone posts in this topic" to true for all users ?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 Jahre weitere
In the forum section, i want all my website members to follow/watch forum topics, in order to accomplish that i want to make "Notify me when someone posts in this topic" to true for all users.

I tried 2 methods till now :

1) in modules / ForumsPostEdit.ascx , i make checkbox to true like this:

<asp:CheckBox ID="cbSubscribe" runat="server" Checked="true" Text="<% $NopResources:Forum.NotifyWhenSomeonePostsInThisTopic %>"
                            CssClass="forumtopicoptionscheck" />

Issue: The problem with this method is that when any user creates new topic, only for that user the checkbox shows as checked but if anyone else tried to reply to that topic, the checkbox remains unchecked for that user.

2) in modules / ForumsPostEdit.ascx.cs , in code behind i made checkbox to true like this:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillDropDowns();
                BindData();
            }

            if (ForumManager.ForumEditor == EditorTypeEnum.BBCodeEditor)
            {
                LoadBBCodeEditorJS();
            }

           cbSubscribe.Checked = true;
        }


Issue: The problem with this method is, if any user decides not to follow the forum topic and uncheck the checkbox, as soon as the user post something by default he/she gets subscribed to the forum topic
13 Jahre weitere
anyone ?
13 Jahre weitere
Set the default value to true in the database and delete all parameters that reference that value.

This way, whenever the record is added, it will be set to true.
13 Jahre weitere
deccks wrote:
Set the default value to true in the database and delete all parameters that reference that value.

This way, whenever the record is added, it will be set to true.


sorry deccks, i didn't understand what value you're talking about in database ? where should i look into in database ?

And if i follow your method and if any user decides not to follow the forum topic and uncheck the checkbox, as soon as the user post something, is he/she gets subscribed to the forum topic or no ? or database value will remain true for all the users ?
13 Jahre weitere
Looks like the DB table that you need to work with is this:

CREATE TABLE [dbo].[Nop_Forums_Subscription]
(
  [SubscriptionID] [int] IDENTITY(1,1) NOT NULL,
  [SubscriptionGUID] [uniqueidentifier] NOT NULL,
  [UserID] [int] NOT NULL,
  [ForumID] [int] NOT NULL,
  [TopicID] [int] NOT NULL,
  [CreatedOn] [datetime] NOT NULL
)

In ForumsPostEdit.ascx, you would have to re-do your code to look like:

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string text = string.Empty;

                switch (ForumManager.ForumEditor)
                {
                    case EditorTypeEnum.SimpleTextBox:
                        {
                            text = txtTopicBodySimple.Text.Trim();
                        }
                        break;
                    case EditorTypeEnum.BBCodeEditor:
                        {
                            text = txtTopicBodyBBCode.Text.Trim();
                        }
                        break;
                    case EditorTypeEnum.HtmlEditor:
                        {
                            text = txtTopicBodyHtml.Content;
                        }
                        break;
                    default:
                        break;
                }
                
                string subject = txtTopicTitle.Text;
                var topicType = ForumTopicTypeEnum.Normal;
                bool subscribe = true;

                string IPAddress = string.Empty;
                if (HttpContext.Current != null && HttpContext.Current.Request != null)
                    IPAddress = HttpContext.Current.Request.UserHostAddress;

                DateTime nowDT = DateTime.Now;

                if (ForumManager.IsUserAllowedToSetTopicPriority(NopContext.Current.User))
                {
                    topicType = (ForumTopicTypeEnum)Enum.ToObject(typeof(ForumTopicTypeEnum), int.Parse(ddlPriority.SelectedItem.Value));
                }

                text = text.Trim();
                if (String.IsNullOrEmpty(text))
                    throw new NopException(GetLocaleResourceString("Forum.TextCannotBeEmpty"));

                if (this.AddTopic)
                {
                    #region Adding topic
                    var forum = ForumManager.GetForumById(this.ForumId);
                    if (forum == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainUrl());
                    }

                    if (!ForumManager.IsUserAllowedToCreateTopic(NopContext.Current.User, forum))
                    {
                        string loginURL = SEOHelper.GetLoginPageUrl(true);
                        Response.Redirect(loginURL);
                    }
                    
                    subject = subject.Trim();
                    if (String.IsNullOrEmpty(subject))
                        throw new NopException(GetLocaleResourceString("Forum.TopicSubjectCannotBeEmpty"));

                    var forumTopic = ForumManager.InsertTopic(forum.ForumId, NopContext.Current.User.CustomerId,
                        topicType, subject, 0, 0, 0, 0, null, nowDT, nowDT, true);

                    var forumPost = ForumManager.InsertPost(forumTopic.ForumTopicId, NopContext.Current.User.CustomerId,
                        text, IPAddress, nowDT, nowDT, false);

                    forumTopic = ForumManager.UpdateTopic(forumTopic.ForumTopicId, forumTopic.ForumId,
                        forumTopic.UserId, forumTopic.TopicType, forumTopic.Subject, 1,
                        0, forumPost.ForumPostId, forumTopic.UserId,
                        forumPost.CreatedOn, forumTopic.CreatedOn, nowDT);

                    //subscription
                    if (ForumManager.IsUserAllowedToSubscribe(NopContext.Current.User))
                    {
                        if (subscribe)
                        {
                            var forumSubscription = ForumManager.InsertSubscription(Guid.NewGuid(),
                                NopContext.Current.User.CustomerId, 0, forumTopic.ForumTopicId, nowDT);
                        }
                    }

                    string topicURL = SEOHelper.GetForumTopicUrl(forumTopic.ForumTopicId);
                    Response.Redirect(topicURL);
                    #endregion
                }
                else if (this.EditTopic)
                {
                    #region Editing topic
                    var forumTopic = ForumManager.GetTopicById(this.ForumTopicId);
                    if (forumTopic == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainUrl());
                    }

                    if (!ForumManager.IsUserAllowedToEditTopic(NopContext.Current.User, forumTopic))
                    {
                        string loginURL = SEOHelper.GetLoginPageUrl(true);
                        Response.Redirect(loginURL);
                    }

                    subject = subject.Trim();
                    if (String.IsNullOrEmpty(subject))
                        throw new NopException(GetLocaleResourceString("Forum.TopicSubjectCannotBeEmpty"));

                    forumTopic = ForumManager.UpdateTopic(forumTopic.ForumTopicId, forumTopic.ForumId,
                        forumTopic.UserId, topicType, subject, forumTopic.NumPosts,
                        forumTopic.Views, forumTopic.LastPostId, forumTopic.LastPostUserId,
                        forumTopic.LastPostTime, forumTopic.CreatedOn, nowDT);

                    var firstPost = forumTopic.FirstPost;
                    if (firstPost != null)
                    {
                        firstPost = ForumManager.UpdatePost(firstPost.ForumPostId, firstPost.TopicId,
                            firstPost.UserId, text, firstPost.IPAddress, firstPost.CreatedOn, nowDT);
                    }
                    else
                    {
                        //error
                        firstPost = ForumManager.InsertPost(forumTopic.ForumTopicId,
                            forumTopic.UserId, text, IPAddress, forumTopic.CreatedOn, nowDT, false);
                    }

                    //subscription
                    if (ForumManager.IsUserAllowedToSubscribe(NopContext.Current.User.CustomerId))
                    {
                        var forumSubscription = ForumManager.GetAllSubscriptions(NopContext.Current.User.CustomerId,
                            0, forumTopic.ForumTopicId, 1, 0).FirstOrDefault();
                        if (subscribe)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = ForumManager.InsertSubscription(Guid.NewGuid(),
                                    NopContext.Current.User.CustomerId, 0, forumTopic.ForumTopicId, nowDT);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                ForumManager.DeleteSubscription(forumSubscription.ForumSubscriptionId);
                            }
                        }
                    }

                    string topicURL = SEOHelper.GetForumTopicUrl(forumTopic.ForumTopicId);
                    Response.Redirect(topicURL);
                    #endregion
                }
                else if (this.AddPost)
                {
                    #region Adding post
                    var forumTopic = ForumManager.GetTopicById(this.ForumTopicId);
                    if (forumTopic == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainUrl());
                    }

                    if (!ForumManager.IsUserAllowedToCreatePost(NopContext.Current.User, forumTopic))
                    {
                        string loginURL = SEOHelper.GetLoginPageUrl(true);
                        Response.Redirect(loginURL);
                    }

                    var forumPost = ForumManager.InsertPost(this.ForumTopicId, NopContext.Current.User.CustomerId,
                        text, IPAddress, nowDT, nowDT, true);

                    //subscription
                    if (ForumManager.IsUserAllowedToSubscribe(NopContext.Current.User.CustomerId))
                    {
                        var forumSubscription = ForumManager.GetAllSubscriptions(NopContext.Current.User.CustomerId,
                            0, forumPost.TopicId, 1, 0).FirstOrDefault();
                        if (subscribe)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = ForumManager.InsertSubscription(Guid.NewGuid(),
                                    NopContext.Current.User.CustomerId, 0, forumPost.TopicId, nowDT);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                ForumManager.DeleteSubscription(forumSubscription.ForumSubscriptionId);
                            }
                        }
                    }
                    

                    int pageSize = 10;
                    if (ForumManager.PostsPageSize > 0)
                    {
                        pageSize = ForumManager.PostsPageSize;
                    }
                    int pageIndex = ForumManager.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.ForumPostId);
                    string topicURL = SEOHelper.GetForumTopicUrl(forumPost.TopicId, "p", pageIndex + 1, forumPost.ForumPostId);
                    Response.Redirect(topicURL);
                    #endregion
                }
                else if (this.EditPost)
                {
                    #region Editing post
                    var forumPost = ForumManager.GetPostById(this.ForumPostId);
                    if (forumPost == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainUrl());
                    }

                    if (!ForumManager.IsUserAllowedToEditPost(NopContext.Current.User, forumPost))
                    {
                        string loginURL = SEOHelper.GetLoginPageUrl(true);
                        Response.Redirect(loginURL);
                    }

                    forumPost = ForumManager.UpdatePost(forumPost.ForumPostId, forumPost.TopicId,
                        forumPost.UserId, text, forumPost.IPAddress, forumPost.CreatedOn, nowDT);

                    //subscription
                    if (ForumManager.IsUserAllowedToSubscribe(NopContext.Current.User.CustomerId))
                    {
                        var forumSubscription = ForumManager.GetAllSubscriptions(NopContext.Current.User.CustomerId,
                            0, forumPost.TopicId, 1, 0).FirstOrDefault();
                        if (cbSubscribe.Checked)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = ForumManager.InsertSubscription(Guid.NewGuid(),
                                    NopContext.Current.User.CustomerId, 0, forumPost.TopicId, nowDT);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                ForumManager.DeleteSubscription(forumSubscription.ForumSubscriptionId);
                            }
                        }
                    }

                    int pageSize = 10;
                    if (ForumManager.PostsPageSize > 0)
                    {
                        pageSize = ForumManager.PostsPageSize;
                    }
                    int pageIndex = ForumManager.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.ForumPostId);
                    string topicURL = SEOHelper.GetForumTopicUrl(forumPost.TopicId, "p", pageIndex + 1, forumPost.ForumPostId);
                    Response.Redirect(topicURL);
                    #endregion
                }
            }
            catch (Exception exc)
            {
                pnlError.Visible = true;
                lErrorMessage.Text = Server.HtmlEncode(exc.Message);
            }
        }
13 Jahre weitere
ok deccks i tested changed you mentioned in the code.

Same problem: If a user is posting a new topic and decides not to subscribe with the forum post, if he/she unchecks it, the code still make the user subscribe with the forum post.

Even if the user tries to un-check it by un-marking the checkbox, the moment user hits the submit button, the code make the user subscribe with the forum post (as the watch link remains "unwatched" in the forum head.

Here's my code:

1st)
checkbox tag includes -> Checked="True"




2nd)

   string subject = txtTopicTitle.Text;
                var topicType = ForumTopicTypeEnum.Normal;
                bool subscribe = true;





3rd)
var forumSubscription = ForumManager.GetAllSubscriptions(NopContext.Current.User.CustomerId,
                            0, forumPost.TopicId, 1, 0).FirstOrDefault();
                        if (cbSubscribe.Checked)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = ForumManager.InsertSubscription(Guid.NewGuid(),
                                    NopContext.Current.User.CustomerId, 0, forumPost.TopicId, nowDT);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                ForumManager.DeleteSubscription(forumSubscription.ForumSubscriptionId);
                            }
                        }
                    }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.