protected void BindData() { try { string keywords = txtSearchTerm.Text.Trim(); if (!String.IsNullOrEmpty(keywords)) { //can be removed if (String.IsNullOrEmpty(keywords)) { throw new NopException(LocalizationManager.GetLocaleResourceString("Forum.SearchTermCouldNotBeEmpty")); } int searchTermMinimumLength = SettingManager.GetSettingValueInteger("Search.ForumSearchTermMinimumLength", 3); if (keywords.Length < searchTermMinimumLength) { throw new NopException(string.Format(LocalizationManager.GetLocaleResourceString("Forum.SearchTermMinimumLengthIsNCharacters"), searchTermMinimumLength)); } int forumId = 0; ForumSearchTypeEnum searchWithin = 0; int limitResultsToPrevious = 0; if (cbAdvancedSearch.Checked) { //adv search forumId = ctrlForumSelector.SelectedForumId; searchWithin = (ForumSearchTypeEnum)Convert.ToInt32(ddlSearchWithin.SelectedValue); limitResultsToPrevious = Convert.ToInt32(ddlLimitResultsToPrevious.SelectedValue); } int totalRecords = 0; int pageSize = 10; if (ForumManager.SearchResultsPageSize > 0) { pageSize = ForumManager.SearchResultsPageSize; } var forumTopics = ForumManager.GetAllTopics(forumId, 0, keywords, searchWithin, limitResultsToPrevious, pageSize, this.CurrentPageIndex, out totalRecords); if (forumTopics.Count > 0) { this.searchPager1.PageSize = pageSize; this.searchPager1.TotalRecords = totalRecords; this.searchPager1.PageIndex = this.CurrentPageIndex; this.searchPager2.PageSize = pageSize; this.searchPager2.TotalRecords = totalRecords; this.searchPager2.PageIndex = this.CurrentPageIndex; rptrSearchResults.DataSource = forumTopics; rptrSearchResults.DataBind(); } rptrSearchResults.Visible = (forumTopics.Count > 0); lblNoResults.Visible = !(rptrSearchResults.Visible); } else { rptrSearchResults.Visible = false; } } catch (Exception exc) { rptrSearchResults.Visible = false; lblError.Text = Server.HtmlEncode(exc.Message); } }
/// <summary> /// Gets all topics /// </summary> /// <param name="forumId">The forum group identifier</param> /// <param name="userId">The user identifier</param> /// <param name="keywords">Keywords</param> /// <param name="searchType">Search type</param> /// <param name="limitDays">Limit by the last number days; 0 to load all topics</param> /// <param name="pageIndex">Page index</param> /// <param name="pageSize">Page size</param> /// <returns>Topics</returns> public PagedList<ForumTopic> GetAllTopics(int forumId, int userId, string keywords, ForumSearchTypeEnum searchType, int limitDays, int pageIndex, int pageSize) { DateTime? limitDate = null; if (limitDays > 0) { limitDate = DateTime.UtcNow.AddDays(-limitDays); } var query1 = from ft in _context.ForumTopics from fp in _context.ForumPosts. Where(fp => fp.TopicId == ft.ForumTopicId).DefaultIfEmpty() where (forumId == 0 || ft.ForumId == forumId) && (userId == 0 || ft.UserId == userId) && (String.IsNullOrEmpty(keywords) || ((searchType == ForumSearchTypeEnum.All || searchType == ForumSearchTypeEnum.TopicTitlesOnly) && ft.Subject.Contains(keywords)) || ((searchType == ForumSearchTypeEnum.All || searchType == ForumSearchTypeEnum.PostTextOnly) && fp.Text.Contains(keywords))) && (!limitDate.HasValue || limitDate.Value <= ft.LastPostTime) select ft.ForumTopicId; var query2 = from ft in _context.ForumTopics where query1.Contains(ft.ForumTopicId) orderby ft.TopicTypeId descending, ft.LastPostTime descending, ft.ForumTopicId descending select ft; var forumTopics = new PagedList<ForumTopic>(query2, pageIndex, pageSize); return forumTopics; }