/// <summary> /// Prepare the forum topic page model /// </summary> /// <param name="forumTopic">Forum topic</param> /// <param name="page">Number of forum posts page</param> /// <returns>Forum topic page model</returns> public virtual ForumTopicPageModel PrepareForumTopicPageModel(ForumTopic forumTopic, int page) { if (forumTopic == null) { throw new ArgumentNullException(nameof(forumTopic)); } //load posts var posts = _forumService.GetAllPosts(forumTopic.Id, 0, string.Empty, page - 1, _forumSettings.PostsPageSize); //prepare model var model = new ForumTopicPageModel { Id = forumTopic.Id, Subject = forumTopic.Subject, SeName = _forumService.GetTopicSeName(forumTopic), IsCustomerAllowedToEditTopic = _forumService.IsCustomerAllowedToEditTopic(_workContext.CurrentCustomer, forumTopic), IsCustomerAllowedToDeleteTopic = _forumService.IsCustomerAllowedToDeleteTopic(_workContext.CurrentCustomer, forumTopic), IsCustomerAllowedToMoveTopic = _forumService.IsCustomerAllowedToMoveTopic(_workContext.CurrentCustomer, forumTopic), IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer) }; if (model.IsCustomerAllowedToSubscribe) { model.WatchTopicText = _localizationService.GetResource("Forum.WatchTopic"); var forumTopicSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id, 0, forumTopic.Id, 0, 1).FirstOrDefault(); if (forumTopicSubscription != null) { model.WatchTopicText = _localizationService.GetResource("Forum.UnwatchTopic"); } } model.PostsPageIndex = posts.PageIndex; model.PostsPageSize = posts.PageSize; model.PostsTotalRecords = posts.TotalCount; foreach (var post in posts) { var forumPostModel = new ForumPostModel { Id = post.Id, ForumTopicId = post.TopicId, ForumTopicSeName = _forumService.GetTopicSeName(forumTopic), FormattedText = _forumService.FormatPostText(post), IsCurrentCustomerAllowedToEditPost = _forumService.IsCustomerAllowedToEditPost(_workContext.CurrentCustomer, post), IsCurrentCustomerAllowedToDeletePost = _forumService.IsCustomerAllowedToDeletePost(_workContext.CurrentCustomer, post), CustomerId = post.CustomerId, AllowViewingProfiles = _customerSettings.AllowViewingProfiles && !post.Customer.IsGuest(), CustomerName = _customerService.FormatUserName(post.Customer), IsCustomerForumModerator = post.Customer.IsForumModerator(), ShowCustomersPostCount = _forumSettings.ShowCustomersPostCount, ForumPostCount = _genericAttributeService.GetAttribute <int>(post.Customer, GSCustomerDefaults.ForumPostCountAttribute), ShowCustomersJoinDate = _customerSettings.ShowCustomersJoinDate && !post.Customer.IsGuest(), CustomerJoinDate = post.Customer.CreatedOnUtc, AllowPrivateMessages = _forumSettings.AllowPrivateMessages && !post.Customer.IsGuest(), SignaturesEnabled = _forumSettings.SignaturesEnabled, FormattedSignature = _forumService.FormatForumSignatureText(_genericAttributeService.GetAttribute <string>(post.Customer, GSCustomerDefaults.SignatureAttribute)), }; //created on string if (_forumSettings.RelativeDateTimeFormattingEnabled) { forumPostModel.PostCreatedOnStr = post.CreatedOnUtc.RelativeFormat(true, "f"); } else { forumPostModel.PostCreatedOnStr = _dateTimeHelper.ConvertToUserTime(post.CreatedOnUtc, DateTimeKind.Utc).ToString("f"); } //avatar if (_customerSettings.AllowCustomersToUploadAvatars) { forumPostModel.CustomerAvatarUrl = _pictureService.GetPictureUrl( _genericAttributeService.GetAttribute <int>(post.Customer, GSCustomerDefaults.AvatarPictureIdAttribute), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType: PictureType.Avatar); } //location forumPostModel.ShowCustomersLocation = _customerSettings.ShowCustomersLocation && !post.Customer.IsGuest(); if (_customerSettings.ShowCustomersLocation) { var countryId = _genericAttributeService.GetAttribute <int>(post.Customer, GSCustomerDefaults.CountryIdAttribute); var country = _countryService.GetCountryById(countryId); forumPostModel.CustomerLocation = country != null?_localizationService.GetLocalized(country, x => x.Name) : string.Empty; } //votes if (_forumSettings.AllowPostVoting) { forumPostModel.AllowPostVoting = true; forumPostModel.VoteCount = post.VoteCount; var postVote = _forumService.GetPostVote(post.Id, _workContext.CurrentCustomer); if (postVote != null) { forumPostModel.VoteIsUp = postVote.IsUp; } } // page number is needed for creating post link in _ForumPost partial view forumPostModel.CurrentTopicPage = page; model.ForumPostModels.Add(forumPostModel); } return(model); }
/// <summary> /// Prepare the profile posts model /// </summary> /// <param name="customer">Customer</param> /// <param name="page">Number of posts page</param> /// <returns>Profile posts model</returns> public virtual ProfilePostsModel PrepareProfilePostsModel(Customer customer, int page) { if (customer == null) { throw new ArgumentNullException(nameof(customer)); } if (page > 0) { page -= 1; } var pageSize = _forumSettings.LatestCustomerPostsPageSize; var list = _forumService.GetAllPosts(0, customer.Id, string.Empty, false, page, pageSize); var latestPosts = new List <PostsModel>(); foreach (var forumPost in list) { var posted = string.Empty; if (_forumSettings.RelativeDateTimeFormattingEnabled) { var languageCode = _workContext.WorkingLanguage.LanguageCulture; var postedAgo = forumPost.CreatedOnUtc.RelativeFormat(languageCode); posted = string.Format(_localizationService.GetResource("Common.RelativeDateTime.Past"), postedAgo); } else { posted = _dateTimeHelper.ConvertToUserTime(forumPost.CreatedOnUtc, DateTimeKind.Utc).ToString("f"); } latestPosts.Add(new PostsModel { ForumTopicId = forumPost.TopicId, ForumTopicTitle = forumPost.ForumTopic.Subject, ForumTopicSlug = _forumService.GetTopicSeName(forumPost.ForumTopic), ForumPostText = _forumService.FormatPostText(forumPost), Posted = posted }); } var pagerModel = new PagerModel { PageSize = list.PageSize, TotalRecords = list.TotalCount, PageIndex = list.PageIndex, ShowTotalSummary = false, RouteActionName = "CustomerProfilePaged", UseRouteLinks = true, RouteValues = new RouteValues { pageNumber = page, id = customer.Id } }; var model = new ProfilePostsModel { PagerModel = pagerModel, Posts = latestPosts, }; return(model); }
/// <summary> /// Prepare the forum topic page model /// </summary> /// <param name="forumTopic">Forum topic</param> /// <param name="page">Number of forum posts page</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the forum topic page model /// </returns> public virtual async Task <ForumTopicPageModel> PrepareForumTopicPageModelAsync(ForumTopic forumTopic, int page) { if (forumTopic == null) { throw new ArgumentNullException(nameof(forumTopic)); } //load posts var posts = await _forumService.GetAllPostsAsync(forumTopic.Id, 0, string.Empty, page - 1, _forumSettings.PostsPageSize); //prepare model var model = new ForumTopicPageModel { Id = forumTopic.Id, Subject = forumTopic.Subject, SeName = await _forumService.GetTopicSeNameAsync(forumTopic), IsCustomerAllowedToEditTopic = await _forumService.IsCustomerAllowedToEditTopicAsync(await _workContext.GetCurrentCustomerAsync(), forumTopic), IsCustomerAllowedToDeleteTopic = await _forumService.IsCustomerAllowedToDeleteTopicAsync(await _workContext.GetCurrentCustomerAsync(), forumTopic), IsCustomerAllowedToMoveTopic = await _forumService.IsCustomerAllowedToMoveTopicAsync(await _workContext.GetCurrentCustomerAsync(), forumTopic), IsCustomerAllowedToSubscribe = await _forumService.IsCustomerAllowedToSubscribeAsync(await _workContext.GetCurrentCustomerAsync()) }; if (model.IsCustomerAllowedToSubscribe) { model.WatchTopicText = await _localizationService.GetResourceAsync("Forum.WatchTopic"); var forumTopicSubscription = (await _forumService.GetAllSubscriptionsAsync((await _workContext.GetCurrentCustomerAsync()).Id, 0, forumTopic.Id, 0, 1)).FirstOrDefault(); if (forumTopicSubscription != null) { model.WatchTopicText = await _localizationService.GetResourceAsync("Forum.UnwatchTopic"); } } model.PostsPageIndex = posts.PageIndex; model.PostsPageSize = posts.PageSize; model.PostsTotalRecords = posts.TotalCount; foreach (var post in posts) { var customer = await _customerService.GetCustomerByIdAsync(post.CustomerId); var customerIsGuest = await _customerService.IsGuestAsync(customer); var customerIsModerator = customerIsGuest ? false : await _customerService.IsForumModeratorAsync(customer); var forumPostModel = new ForumPostModel { Id = post.Id, ForumTopicId = post.TopicId, ForumTopicSeName = await _forumService.GetTopicSeNameAsync(forumTopic), FormattedText = _forumService.FormatPostText(post), IsCurrentCustomerAllowedToEditPost = await _forumService.IsCustomerAllowedToEditPostAsync(await _workContext.GetCurrentCustomerAsync(), post), IsCurrentCustomerAllowedToDeletePost = await _forumService.IsCustomerAllowedToDeletePostAsync(await _workContext.GetCurrentCustomerAsync(), post), CustomerId = post.CustomerId, AllowViewingProfiles = _customerSettings.AllowViewingProfiles && !customerIsGuest, CustomerName = await _customerService.FormatUsernameAsync(customer), IsCustomerForumModerator = customerIsModerator, ShowCustomersPostCount = _forumSettings.ShowCustomersPostCount, ForumPostCount = await _genericAttributeService.GetAttributeAsync <Customer, int>(post.CustomerId, NopCustomerDefaults.ForumPostCountAttribute), ShowCustomersJoinDate = _customerSettings.ShowCustomersJoinDate && !customerIsGuest, CustomerJoinDate = customer?.CreatedOnUtc ?? DateTime.Now, AllowPrivateMessages = _forumSettings.AllowPrivateMessages && !customerIsGuest, SignaturesEnabled = _forumSettings.SignaturesEnabled, FormattedSignature = _forumService.FormatForumSignatureText(await _genericAttributeService.GetAttributeAsync <Customer, string>(post.CustomerId, NopCustomerDefaults.SignatureAttribute)), }; //created on string var languageCode = (await _workContext.GetWorkingLanguageAsync()).LanguageCulture; if (_forumSettings.RelativeDateTimeFormattingEnabled) { var postCreatedAgo = post.CreatedOnUtc.RelativeFormat(languageCode); forumPostModel.PostCreatedOnStr = string.Format(await _localizationService.GetResourceAsync("Common.RelativeDateTime.Past"), postCreatedAgo); } else { forumPostModel.PostCreatedOnStr = (await _dateTimeHelper.ConvertToUserTimeAsync(post.CreatedOnUtc, DateTimeKind.Utc)).ToString("f"); } //avatar if (_customerSettings.AllowCustomersToUploadAvatars) { forumPostModel.CustomerAvatarUrl = await _pictureService.GetPictureUrlAsync( await _genericAttributeService.GetAttributeAsync <Customer, int>(post.CustomerId, NopCustomerDefaults.AvatarPictureIdAttribute), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType : PictureType.Avatar); } //location forumPostModel.ShowCustomersLocation = _customerSettings.ShowCustomersLocation && !customerIsGuest; if (_customerSettings.ShowCustomersLocation) { var countryId = await _genericAttributeService.GetAttributeAsync <Customer, int>(post.CustomerId, NopCustomerDefaults.CountryIdAttribute); var country = await _countryService.GetCountryByIdAsync(countryId); forumPostModel.CustomerLocation = country != null ? await _localizationService.GetLocalizedAsync(country, x => x.Name) : string.Empty; } //votes if (_forumSettings.AllowPostVoting) { forumPostModel.AllowPostVoting = true; forumPostModel.VoteCount = post.VoteCount; var postVote = await _forumService.GetPostVoteAsync(post.Id, await _workContext.GetCurrentCustomerAsync()); if (postVote != null) { forumPostModel.VoteIsUp = postVote.IsUp; } } // page number is needed for creating post link in _ForumPost partial view forumPostModel.CurrentTopicPage = page; model.ForumPostModels.Add(forumPostModel); } return(model); }