/// <summary> /// Gets move topic URL /// </summary> /// <param name="forumTopic">Forum topic</param> /// <returns>Forum URL</returns> public static string GetMoveForumTopicUrl(ForumTopic forumTopic) { if (forumTopic == null) throw new ArgumentNullException("forumTopic"); string url = string.Format("{0}Boards/MoveTopic.aspx?TopicId={1}", CommonHelper.GetStoreLocation(), forumTopic.ForumTopicId); return url.ToLowerInvariant(); }
/// <summary> /// Gets forum topic URL /// </summary> /// <param name="topic">Forum topic</param> /// <param name="queryStringProperty">Query string property</param> /// <param name="pageIndex">Page index</param> /// <returns>Forum topic URL</returns> public static string GetForumTopicUrl(ForumTopic topic, string queryStringProperty, int? pageIndex) { return GetForumTopicUrl(topic, queryStringProperty, pageIndex, null); }
/// <summary> /// Gets forum topic URL /// </summary> /// <param name="topic">Forum topic</param> /// <param name="queryStringProperty">Query string property</param> /// <param name="pageIndex">Page index</param> /// <param name="postId">Post identifier (anchor)</param> /// <returns>Forum topic URL</returns> public static string GetForumTopicUrl(ForumTopic topic, string queryStringProperty, int? pageIndex, int? postId) { if(topic == null) { throw new ArgumentNullException("topic"); } string seName = GetSEName(topic.Subject); string url2 = SEOHelper.EnableUrlRewriting ? IoC.Resolve<ISettingManager>().GetSettingValue("SEO.ForumTopic.UrlRewriteFormat") : "{0}Boards/Topic.aspx?TopicId={1}"; string url = string.Format(url2, CommonHelper.GetStoreLocation(), topic.ForumTopicId, seName); if(pageIndex.HasValue && pageIndex.Value > 1) { url = CommonHelper.ModifyQueryString(url, queryStringProperty + "=" + pageIndex.Value.ToString(), null); } if(postId.HasValue) { url += string.Format("#{0}", postId.Value); } return url.ToLowerInvariant(); }
/// <summary> /// Replaces a message template tokens /// </summary> /// <param name="customer">Customer instance</param> /// <param name="forumPost">Forum post</param> /// <param name="forumTopic">Forum topic</param> /// <param name="forum">Forum</param> /// <param name="template">Template</param> /// <returns>New template</returns> private string ReplaceMessageTemplateTokens(Customer customer, ForumPost forumPost, ForumTopic forumTopic, Forum forum, string template) { var tokens = new NameValueCollection(); tokens.Add("Store.Name", IoC.Resolve<ISettingManager>().StoreName); tokens.Add("Store.URL", IoC.Resolve<ISettingManager>().StoreUrl); tokens.Add("Store.Email", this.DefaultEmailAccount.Email); tokens.Add("Customer.Email", HttpUtility.HtmlEncode(customer.Email)); tokens.Add("Customer.Username", HttpUtility.HtmlEncode(customer.Username)); tokens.Add("Customer.FullName", HttpUtility.HtmlEncode(customer.FullName)); tokens.Add("Customer.VatNumber", HttpUtility.HtmlEncode(customer.VatNumber)); tokens.Add("Customer.VatNumberStatus", HttpUtility.HtmlEncode(customer.VatNumberStatus.ToString())); if (forumPost != null) { tokens.Add("Forums.PostAuthor", HttpUtility.HtmlEncode(forumPost.User.FormatUserName())); tokens.Add("Forums.PostBody", forumPost.FormatPostText()); } if (forumTopic != null) { tokens.Add("Forums.TopicURL", SEOHelper.GetForumTopicUrl(forumTopic)); tokens.Add("Forums.TopicName", HttpUtility.HtmlEncode(forumTopic.Subject)); } if (forum != null) { tokens.Add("Forums.ForumURL", SEOHelper.GetForumUrl(forum)); tokens.Add("Forums.ForumName", HttpUtility.HtmlEncode(forum.Name)); } foreach (string token in tokens.Keys) { template = Replace(template, String.Format(@"%{0}%", token), tokens[token]); } return template; }
/// <summary> /// Gets forum topic URL /// </summary> /// <param name="topic">Forum topic</param> /// <returns>Forum topic URL</returns> public static string GetForumTopicUrl(ForumTopic topic) { return GetForumTopicUrl(topic, "p", null); }
/// <summary> /// Check whether user is allowed to create new post /// </summary> /// <param name="customer">Customer</param> /// <param name="topic">Topic</param> /// <returns>True if allowed, otherwise false</returns> public static bool IsUserAllowedToCreatePost(Customer customer, ForumTopic topic) { if (topic == null) return false; if (customer == null) return false; if (customer.IsGuest) return false; if (customer.IsForumModerator) return true; return true; }
/// <summary> /// Sends a forum subscription message to a customer /// </summary> /// <param name="customer">Customer instance</param> /// <param name="forumTopic">Forum Topic</param> /// <param name="forum">Forum</param> /// <param name="languageId">Message language identifier</param> /// <returns>Queued email identifier</returns> public int SendNewForumTopicMessage(Customer customer, ForumTopic forumTopic, Forum forum, int languageId) { if (customer == null) throw new ArgumentNullException("customer"); string templateName = "Forums.NewForumTopic"; var localizedMessageTemplate = this.GetLocalizedMessageTemplate(templateName, languageId); if(localizedMessageTemplate == null || !localizedMessageTemplate.IsActive) return 0; var emailAccount = localizedMessageTemplate.EmailAccount; string subject = ReplaceMessageTemplateTokens(customer, null, forumTopic, forum, localizedMessageTemplate.Subject); string body = ReplaceMessageTemplateTokens(customer, null, forumTopic, forum, localizedMessageTemplate.Body); string bcc = localizedMessageTemplate.BccEmailAddresses; var from = new MailAddress(emailAccount.Email, emailAccount.DisplayName); var to = new MailAddress(customer.Email, customer.FullName); var queuedEmail = InsertQueuedEmail(5, from, to, string.Empty, bcc, subject, body, DateTime.UtcNow, 0, null, emailAccount.EmailAccountId); return queuedEmail.QueuedEmailId; }
private static ForumTopic DBMapping(DBForumTopic dbItem) { if (dbItem == null) return null; ForumTopic item = new ForumTopic(); item.ForumTopicID = dbItem.ForumTopicID; item.ForumID = dbItem.ForumID; item.UserID = dbItem.UserID; item.TopicTypeID = dbItem.TopicTypeID; item.Subject = dbItem.Subject; item.NumPosts = dbItem.NumPosts; item.Views = dbItem.Views; item.LastPostID = dbItem.LastPostID; item.LastPostUserID = dbItem.LastPostUserID; item.LastPostTime = dbItem.LastPostTime; item.CreatedOn = dbItem.CreatedOn; item.UpdatedOn = dbItem.UpdatedOn; return item; }
/// <summary> /// Check whether user is allowed to delete topic /// </summary> /// <param name="customer">Customer</param> /// <param name="topic">Topic</param> /// <returns>True if allowed, otherwise false</returns> public static bool IsUserAllowedToDeleteTopic(Customer customer, ForumTopic topic) { if (topic == null) return false; if (customer == null) return false; if (customer.IsGuest) return false; if (customer.IsForumModerator) return true; if (ForumManager.AllowCustomersToDeletePosts) { bool ownTopic = customer.CustomerID == topic.UserID; return ownTopic; } return false; }
protected string CreatePostPager(ForumTopic forumTopic) { int pageSize = 10; if (ForumManager.PostsPageSize > 0) { pageSize = ForumManager.PostsPageSize; } string queryStringParam = "p"; string result = string.Empty; int NumToDisplay = 4; int PageCount = (int)Math.Ceiling((double)forumTopic.NumPosts / pageSize); if (PageCount > 1) { if (PageCount > NumToDisplay) { result += createLink(SEOHelper.GetForumTopicUrl(forumTopic), "1"); result += " ... "; bool first = true; for (int i = (PageCount - (NumToDisplay - 1)); i < PageCount; i++) { int iPost = i + 1; if (first) first = false; else result += ", "; result += createLink(SEOHelper.GetForumTopicUrl(forumTopic, queryStringParam, iPost), iPost.ToString()); } } else { bool first = true; for (int i = 0; i < PageCount; i++) { int iPost = i + 1; if (first) first = false; else result += ", "; result += createLink(SEOHelper.GetForumTopicUrl(forumTopic, queryStringParam, iPost), iPost.ToString()); } } } return result; }
/// <summary> /// Updates the topic /// </summary> /// <param name="forumTopic">Forum topic</param> public void UpdateTopic(ForumTopic forumTopic) { if (forumTopic == null) throw new ArgumentNullException("forumTopic"); forumTopic.Subject = CommonHelper.EnsureNotNull(forumTopic.Subject); forumTopic.Subject = forumTopic.Subject.Trim(); if (String.IsNullOrEmpty(forumTopic.Subject)) throw new NopException("Topic subject cannot be empty"); if (this.TopicSubjectMaxLength > 0) { if (forumTopic.Subject.Length > this.TopicSubjectMaxLength) forumTopic.Subject = forumTopic.Subject.Substring(0, this.TopicSubjectMaxLength); } forumTopic.Subject = CommonHelper.EnsureMaximumLength(forumTopic.Subject, 450); if (!_context.IsAttached(forumTopic)) _context.ForumTopics.Attach(forumTopic); _context.SaveChanges(); if (this.CacheEnabled) { _cacheManager.RemoveByPattern(FORUMGROUP_PATTERN_KEY); _cacheManager.RemoveByPattern(FORUM_PATTERN_KEY); } }
/// <summary> /// Check whether user is allowed to edit topic /// </summary> /// <param name="customer">Customer</param> /// <param name="topic">Topic</param> /// <returns>True if allowed, otherwise false</returns> public bool IsUserAllowedToEditTopic(Customer customer, ForumTopic topic) { if (topic == null) return false; if (customer == null) return false; if (customer.IsGuest) return false; if (customer.IsForumModerator) return true; if (this.AllowCustomersToEditPosts) { bool ownTopic = customer.CustomerId == topic.UserId; return ownTopic; } return false; }
/// <summary> /// Inserts a topic /// </summary> /// <param name="forumTopic">Forum topic</param> /// <param name="sendNotifications">A value indicating whether to send notifications to users</param> public void InsertTopic(ForumTopic forumTopic, bool sendNotifications) { if (forumTopic == null) throw new ArgumentNullException("forumTopic"); //validation forumTopic.Subject = CommonHelper.EnsureNotNull(forumTopic.Subject); forumTopic.Subject = forumTopic.Subject.Trim(); if (String.IsNullOrEmpty(forumTopic.Subject)) throw new NopException("Topic subject cannot be empty"); if (this.TopicSubjectMaxLength > 0) { if (forumTopic.Subject.Length > this.TopicSubjectMaxLength) forumTopic.Subject = forumTopic.Subject.Substring(0, this.TopicSubjectMaxLength); } forumTopic.Subject = CommonHelper.EnsureMaximumLength(forumTopic.Subject, 450); //insert forum topic _context.ForumTopics.AddObject(forumTopic); _context.SaveChanges(); //update stats UpdateForumStats(forumTopic.ForumId); //cache if (this.CacheEnabled) { _cacheManager.RemoveByPattern(FORUMGROUP_PATTERN_KEY); _cacheManager.RemoveByPattern(FORUM_PATTERN_KEY); } //send notifications if (sendNotifications) { var forum = forumTopic.Forum; var subscriptions = GetAllSubscriptions(0, forum.ForumId, 0, 0, int.MaxValue); foreach (var subscription in subscriptions) { if (subscription.UserId == forumTopic.UserId) continue; IoC.Resolve<IMessageService>().SendNewForumTopicMessage(subscription.User, forumTopic, forum, NopContext.Current.WorkingLanguage.LanguageId); } } }
protected void btnSubmit_Click(object sender, EventArgs e) { try { string text = string.Empty; switch (this.ForumService.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; var topicType = ForumTopicTypeEnum.Normal; bool subscribe = cbSubscribe.Checked; string IPAddress = NopContext.Current.UserHostAddress; DateTime nowDT = DateTime.UtcNow; if (this.ForumService.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 = this.ForumService.GetForumById(this.ForumId); if (forum == null) { Response.Redirect(SEOHelper.GetForumMainUrl()); } if (!this.ForumService.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")); //forum topic var forumTopic = new ForumTopic() { ForumId = forum.ForumId, UserId = NopContext.Current.User.CustomerId, TopicTypeId = (int)topicType, Subject = subject, CreatedOn = nowDT, UpdatedOn = nowDT }; this.ForumService.InsertTopic(forumTopic, true); //forum post var forumPost = new ForumPost() { TopicId = forumTopic.ForumTopicId, UserId = NopContext.Current.User.CustomerId, Text = text, IPAddress = IPAddress, CreatedOn = nowDT, UpdatedOn = nowDT }; this.ForumService.InsertPost(forumPost, false); //update forum topic forumTopic.NumPosts = 1; forumTopic.LastPostId = forumPost.ForumPostId; forumTopic.LastPostUserId = forumPost.UserId; forumTopic.LastPostTime = forumPost.CreatedOn; forumTopic.UpdatedOn = nowDT; this.ForumService.UpdateTopic(forumTopic); //subscription if (this.ForumService.IsUserAllowedToSubscribe(NopContext.Current.User)) { if (subscribe) { var forumSubscription = new ForumSubscription() { SubscriptionGuid = Guid.NewGuid(), UserId = NopContext.Current.User.CustomerId, TopicId = forumTopic.ForumTopicId, CreatedOn = nowDT }; this.ForumService.InsertSubscription(forumSubscription); } } string topicURL = SEOHelper.GetForumTopicUrl(forumTopic); Response.Redirect(topicURL); #endregion } else if (this.EditTopic) { #region Editing topic var forumTopic = this.ForumService.GetTopicById(this.ForumTopicId); if (forumTopic == null) { Response.Redirect(SEOHelper.GetForumMainUrl()); } if (!this.ForumService.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")); //forum topic forumTopic.TopicTypeId = (int)topicType; forumTopic.Subject = subject; forumTopic.UpdatedOn = nowDT; this.ForumService.UpdateTopic(forumTopic); //forum post var firstPost = forumTopic.FirstPost; if (firstPost != null) { firstPost.Text = text; firstPost.UpdatedOn = nowDT; this.ForumService.UpdatePost(firstPost); } else { //error (not possible) firstPost = new ForumPost() { TopicId = forumTopic.ForumTopicId, UserId = forumTopic.UserId, Text = text, IPAddress = IPAddress, UpdatedOn = nowDT }; this.ForumService.InsertPost(firstPost, false); } //subscription if (this.ForumService.IsUserAllowedToSubscribe(NopContext.Current.User)) { var forumSubscription = this.ForumService.GetAllSubscriptions(NopContext.Current.User.CustomerId, 0, forumTopic.ForumTopicId, 0, 1).FirstOrDefault(); if (subscribe) { if (forumSubscription == null) { forumSubscription = new ForumSubscription() { SubscriptionGuid = Guid.NewGuid(), UserId = NopContext.Current.User.CustomerId, TopicId = forumTopic.ForumTopicId, CreatedOn = nowDT }; this.ForumService.InsertSubscription(forumSubscription); } } else { if (forumSubscription != null) { this.ForumService.DeleteSubscription(forumSubscription.ForumSubscriptionId); } } } string topicURL = SEOHelper.GetForumTopicUrl(forumTopic); Response.Redirect(topicURL); #endregion } else if (this.AddPost) { #region Adding post var forumTopic = this.ForumService.GetTopicById(this.ForumTopicId); if (forumTopic == null) { Response.Redirect(SEOHelper.GetForumMainUrl()); } if (!this.ForumService.IsUserAllowedToCreatePost(NopContext.Current.User, forumTopic)) { string loginURL = SEOHelper.GetLoginPageUrl(true); Response.Redirect(loginURL); } //forum post var forumPost = new ForumPost() { TopicId = this.ForumTopicId, UserId = NopContext.Current.User.CustomerId, Text = text, IPAddress = IPAddress, CreatedOn = nowDT, UpdatedOn = nowDT }; this.ForumService.InsertPost(forumPost, true); //subscription if (this.ForumService.IsUserAllowedToSubscribe(NopContext.Current.User)) { var forumSubscription = this.ForumService.GetAllSubscriptions(NopContext.Current.User.CustomerId, 0, forumPost.TopicId, 0, 1).FirstOrDefault(); if (subscribe) { if (forumSubscription == null) { forumSubscription = new ForumSubscription() { SubscriptionGuid = Guid.NewGuid(), UserId = NopContext.Current.User.CustomerId, TopicId = forumPost.TopicId, CreatedOn = nowDT }; this.ForumService.InsertSubscription(forumSubscription); } } else { if (forumSubscription != null) { this.ForumService.DeleteSubscription(forumSubscription.ForumSubscriptionId); } } } int pageSize = 10; if (this.ForumService.PostsPageSize > 0) { pageSize = this.ForumService.PostsPageSize; } int pageIndex = this.ForumService.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 = this.ForumService.GetPostById(this.ForumPostId); if (forumPost == null) { Response.Redirect(SEOHelper.GetForumMainUrl()); } if (!this.ForumService.IsUserAllowedToEditPost(NopContext.Current.User, forumPost)) { string loginURL = SEOHelper.GetLoginPageUrl(true); Response.Redirect(loginURL); } forumPost.Text = text; forumPost.UpdatedOn = nowDT; this.ForumService.UpdatePost(forumPost); //subscription if (this.ForumService.IsUserAllowedToSubscribe(NopContext.Current.User)) { var forumSubscription = this.ForumService.GetAllSubscriptions(NopContext.Current.User.CustomerId, 0, forumPost.TopicId, 0, 1).FirstOrDefault(); if (subscribe) { if (forumSubscription == null) { forumSubscription = new ForumSubscription() { SubscriptionGuid = Guid.NewGuid(), UserId = NopContext.Current.User.CustomerId, TopicId = forumPost.TopicId, CreatedOn = nowDT }; this.ForumService.InsertSubscription(forumSubscription); } } else { if (forumSubscription != null) { this.ForumService.DeleteSubscription(forumSubscription.ForumSubscriptionId); } } } int pageSize = 10; if (this.ForumService.PostsPageSize > 0) { pageSize = this.ForumService.PostsPageSize; } int pageIndex = this.ForumService.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); } }