示例#1
0
        /// <summary>
        /// Updates the topic
        /// </summary>
        /// <param name="ForumTopicID">The forum topic identifier</param>
        /// <param name="ForumID">The forum identifier</param>
        /// <param name="UserID">The user identifier</param>
        /// <param name="TopicType">The topic type</param>
        /// <param name="Subject">The subject</param>
        /// <param name="NumPosts">The number of posts</param>
        /// <param name="Views">The number of views</param>
        /// <param name="LastPostID">The last post identifier</param>
        /// <param name="LastPostUserID">The last post user identifier</param>
        /// <param name="LastPostTime">The last post date and time</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Topic</returns>
        public static ForumTopic UpdateTopic(int ForumTopicID, int ForumID, int UserID,
            ForumTopicTypeEnum TopicType, string Subject,
            int NumPosts, int Views, int LastPostID,
            int LastPostUserID, DateTime? LastPostTime,
            DateTime CreatedOn, DateTime UpdatedOn)
        {
            CreatedOn = DateTimeHelper.ConvertToUtcTime(CreatedOn);
            UpdatedOn = DateTimeHelper.ConvertToUtcTime(UpdatedOn);

            if (Subject == null)
                Subject = string.Empty;

            Subject = Subject.Trim();

            if (String.IsNullOrEmpty(Subject))
                throw new NopException("Topic subject cannot be empty");

            if (ForumManager.TopicSubjectMaxLength > 0)
            {
                if (Subject.Length > ForumManager.TopicSubjectMaxLength)
                    Subject = Subject.Substring(0, ForumManager.TopicSubjectMaxLength);
            }

            DBForumTopic dbItem = DBProviderManager<DBForumProvider>.Provider.UpdateTopic(ForumTopicID, ForumID, UserID,
                (int)TopicType, Subject, NumPosts, Views, LastPostID,
                LastPostUserID, LastPostTime, CreatedOn, UpdatedOn);

            ForumTopic forumTopic = DBMapping(dbItem);

            if (ForumManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(FORUMGROUP_PATTERN_KEY);
                NopCache.RemoveByPattern(FORUM_PATTERN_KEY);
            }

            return forumTopic;
        }
        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.Value;
                }
                break;

                default:
                    break;
                }

                string             subject   = txtTopicTitle.Text;
                ForumTopicTypeEnum topicType = ForumTopicTypeEnum.Normal;
                bool subscribe = cbSubscribe.Checked;

                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
                    Forum forum = ForumManager.GetForumByID(this.ForumID);
                    if (forum == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainURL());
                    }

                    if (!ForumManager.IsUserAllowedToCreateTopic(NopContext.Current.User, forum))
                    {
                        string loginURL = CommonHelper.GetLoginPageURL(true);
                        Response.Redirect(loginURL);
                    }

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

                    ForumTopic forumTopic = ForumManager.InsertTopic(forum.ForumID, NopContext.Current.User.CustomerID,
                                                                     topicType, subject, 0, 0, 0, 0, null, nowDT, nowDT, true);

                    ForumPost 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)
                        {
                            ForumSubscription 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
                    ForumTopic forumTopic = ForumManager.GetTopicByID(this.ForumTopicID);
                    if (forumTopic == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainURL());
                    }

                    if (!ForumManager.IsUserAllowedToEditTopic(NopContext.Current.User, forumTopic))
                    {
                        string loginURL = CommonHelper.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);

                    ForumPost 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))
                    {
                        ForumSubscription 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
                    ForumTopic forumTopic = ForumManager.GetTopicByID(this.ForumTopicID);
                    if (forumTopic == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainURL());
                    }

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

                    ForumPost forumPost = ForumManager.InsertPost(this.ForumTopicID, NopContext.Current.User.CustomerID,
                                                                  text, IPAddress, nowDT, nowDT, true);

                    //subscription
                    if (ForumManager.IsUserAllowedToSubscribe(NopContext.Current.User.CustomerID))
                    {
                        ForumSubscription 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
                    ForumPost forumPost = ForumManager.GetPostByID(this.ForumPostID);
                    if (forumPost == null)
                    {
                        Response.Redirect(SEOHelper.GetForumMainURL());
                    }

                    if (!ForumManager.IsUserAllowedToEditPost(NopContext.Current.User, forumPost))
                    {
                        string loginURL = CommonHelper.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))
                    {
                        ForumSubscription 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
                }
            }
            catch (Exception exc)
            {
                pnlError.Visible   = true;
                lErrorMessage.Text = Server.HtmlEncode(exc.Message);
            }
        }
示例#3
0
        /// <summary>
        /// Inserts a topic
        /// </summary>
        /// <param name="ForumID">The forum identifier</param>
        /// <param name="UserID">The user identifier</param>
        /// <param name="TopicType">The topic type</param>
        /// <param name="Subject">The subject</param>
        /// <param name="NumPosts">The number of posts</param>
        /// <param name="Views">The number of views</param>
        /// <param name="LastPostID">The last post identifier</param>
        /// <param name="LastPostUserID">The last post user identifier</param>
        /// <param name="LastPostTime">The last post date and time</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <param name="SendNotifications">A value indicating whether to send notifications to users</param>
        /// <returns>Topic</returns>
        public static ForumTopic InsertTopic(int ForumID, int UserID,
            ForumTopicTypeEnum TopicType, string Subject,
            int NumPosts, int Views, int LastPostID,
            int LastPostUserID, DateTime? LastPostTime,
            DateTime CreatedOn, DateTime UpdatedOn, bool SendNotifications)
        {
            CreatedOn = DateTimeHelper.ConvertToUtcTime(CreatedOn);
            UpdatedOn = DateTimeHelper.ConvertToUtcTime(UpdatedOn);

            if (Subject == null)
                Subject = string.Empty;

            Subject = Subject.Trim();

            if (String.IsNullOrEmpty(Subject))
                throw new NopException("Topic subject cannot be empty");

            if (ForumManager.TopicSubjectMaxLength > 0)
            {
                if (Subject.Length > ForumManager.TopicSubjectMaxLength)
                    Subject = Subject.Substring(0, ForumManager.TopicSubjectMaxLength);
            }

            DBForumTopic dbItem = DBProviderManager<DBForumProvider>.Provider.InsertTopic(ForumID, UserID,
                (int)TopicType, Subject, NumPosts, Views, LastPostID,
                LastPostUserID, LastPostTime, CreatedOn, UpdatedOn);

            ForumTopic forumTopic = DBMapping(dbItem);

            if (ForumManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(FORUMGROUP_PATTERN_KEY);
                NopCache.RemoveByPattern(FORUM_PATTERN_KEY);
            }

            if (SendNotifications)
            {
                Forum forum = forumTopic.Forum;
                ForumSubscriptionCollection subscriptions = GetAllSubscriptions(0, forum.ForumID, 0, int.MaxValue, 0);
                
                foreach (ForumSubscription subscription in subscriptions)
                {
                    if (subscription.UserID == UserID)
                        continue;

                    MessageManager.SendNewForumTopicMessage(subscription.User, forumTopic, forum, NopContext.Current.WorkingLanguage.LanguageID);
                }
            }

            return forumTopic;
        }