/// <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; }
private static ForumPost DBMapping(DBForumPost dbItem) { if (dbItem == null) return null; ForumPost item = new ForumPost(); item.ForumPostID = dbItem.ForumPostID; item.TopicID = dbItem.TopicID; item.UserID = dbItem.UserID; item.Text = dbItem.Text; item.IPAddress = dbItem.IPAddress; item.CreatedOn = dbItem.CreatedOn; item.UpdatedOn = dbItem.UpdatedOn; return item; }
/// <summary> /// Sends a forum subscription message to a customer /// </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="languageId">Message language identifier</param> /// <returns>Queued email identifier</returns> public int SendNewForumPostMessage(Customer customer, ForumPost forumPost, ForumTopic forumTopic, Forum forum, int languageId) { if (customer == null) throw new ArgumentNullException("customer"); string templateName = "Forums.NewForumPost"; LocalizedMessageTemplate localizedMessageTemplate = this.GetLocalizedMessageTemplate(templateName, languageId); if(localizedMessageTemplate == null || !localizedMessageTemplate.IsActive) return 0; var emailAccount = localizedMessageTemplate.EmailAccount; string subject = ReplaceMessageTemplateTokens(customer, forumPost, forumTopic, forum, localizedMessageTemplate.Subject); string body = ReplaceMessageTemplateTokens(customer, forumPost, 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; }
/// <summary> /// Check whether user is allowed to delete post /// </summary> /// <param name="customer">Customer</param> /// <param name="post">Topic</param> /// <returns>True if allowed, otherwise false</returns> public static bool IsUserAllowedToDeletePost(Customer customer, ForumPost post) { if (post == null) return false; if (customer == null) return false; if (customer.IsGuest) return false; if (customer.IsForumModerator) return true; if (ForumManager.AllowCustomersToDeletePosts) { bool ownPost = customer.CustomerID == post.UserID; return ownPost; } return false; }
/// <summary> /// Updates the post /// </summary> /// <param name="forumPost">The forum post</param> public void UpdatePost(ForumPost forumPost) { //validation if (forumPost == null) throw new ArgumentNullException("forumPost"); forumPost.Text = CommonHelper.EnsureNotNull(forumPost.Text); forumPost.Text = forumPost.Text.Trim(); if (String.IsNullOrEmpty(forumPost.Text)) throw new NopException("Text cannot be empty"); if (this.PostMaxLength > 0) { if (forumPost.Text.Length > this.PostMaxLength) forumPost.Text = forumPost.Text.Substring(0, this.PostMaxLength); } forumPost.IPAddress = CommonHelper.EnsureNotNull(forumPost.IPAddress); forumPost.IPAddress = CommonHelper.EnsureMaximumLength(forumPost.IPAddress, 100); //update post if (!_context.IsAttached(forumPost)) _context.ForumPosts.Attach(forumPost); _context.SaveChanges(); //clear cache if (this.CacheEnabled) { _cacheManager.RemoveByPattern(FORUMGROUP_PATTERN_KEY); _cacheManager.RemoveByPattern(FORUM_PATTERN_KEY); } }
/// <summary> /// Check whether user is allowed to edit post /// </summary> /// <param name="customer">Customer</param> /// <param name="post">Topic</param> /// <returns>True if allowed, otherwise false</returns> public bool IsUserAllowedToEditPost(Customer customer, ForumPost post) { if (post == null) return false; if (customer == null) return false; if (customer.IsGuest) return false; if (customer.IsForumModerator) return true; if (this.AllowCustomersToEditPosts) { bool ownPost = customer.CustomerId == post.UserId; return ownPost; } return false; }
/// <summary> /// Inserts a post /// </summary> /// <param name="forumPost">The forum post</param> /// <param name="sendNotifications">A value indicating whether to send notifications to users</param> public void InsertPost(ForumPost forumPost, bool sendNotifications) { if (forumPost == null) throw new ArgumentNullException("forumPost"); //validation logic forumPost.Text = CommonHelper.EnsureNotNull(forumPost.Text); forumPost.Text = forumPost.Text.Trim(); if (String.IsNullOrEmpty(forumPost.Text)) throw new NopException("Text cannot be empty"); if (this.PostMaxLength > 0) { if (forumPost.Text.Length > this.PostMaxLength) forumPost.Text = forumPost.Text.Substring(0, this.PostMaxLength); } forumPost.IPAddress = CommonHelper.EnsureNotNull(forumPost.IPAddress); forumPost.IPAddress = CommonHelper.EnsureMaximumLength(forumPost.IPAddress, 100); //insert forum post _context.ForumPosts.AddObject(forumPost); _context.SaveChanges(); //update stats int userId = forumPost.UserId; var forumTopic = this.GetTopicById(forumPost.TopicId); int forumId = forumTopic.ForumId; UpdateForumTopicStats(forumPost.TopicId); UpdateForumStats(forumId); UpdateUserStats(userId); //clear cache if (this.CacheEnabled) { _cacheManager.RemoveByPattern(FORUMGROUP_PATTERN_KEY); _cacheManager.RemoveByPattern(FORUM_PATTERN_KEY); } //notifications if (sendNotifications) { var forum = forumTopic.Forum; var subscriptions = GetAllSubscriptions(0, 0, forumTopic.ForumTopicId, 0, int.MaxValue); foreach (ForumSubscription subscription in subscriptions) { if (subscription.UserId == forumPost.UserId) continue; IoC.Resolve<IMessageService>().SendNewForumPostMessage(subscription.User, forumPost, 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); } }