📄 searchresultsview.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
namespace CommunityServer.Discussions.Controls
{
// *********************************************************************
// SearchView
//
/// <summary>
/// This server control is used to display search options and search results
/// </summary>
///
// ********************************************************************/
public class SearchResultsView : TemplatedWebControl
{
#region Constructor and Member Variables
CSContext csContext = CSContext.Current;
string skinFilename = "View-SearchResults.ascx";
string searchQuery;
//string[] searchForums;
//string[] searchUsers;
Label searchFor;
TextBox searchTextTop;
TextBox searchTextBottom;
Button searchButtonTop;
Button searchButtonBottom;
Repeater searchRepeater;
CurrentPage resultTotal;
Literal searchDuration;
Pager pager;
HtmlGenericControl validatorTop;
HtmlGenericControl validatorBottom;
StatusMessage formStatus;
Panel searchResultsDisplay;
// *********************************************************************
// SearchView
//
/// <summary>
/// The constructor simply checks for a ForumID value passed in via the
/// HTTP POST or GET.
/// properties.
/// </summary>
///
// ********************************************************************/
public SearchResultsView()
{
// Assign a default template name
this.ExternalSkinFileName = skinFilename;
//searchForums = ForumsToSearchDecode ( csContext.Context.Request.QueryString["f"] ) ;
}
/// <summary>
/// If not a page back, calls DataBind()
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
if ( !Page.IsPostBack )
{
this.DataBind();
}
base.OnLoad (e);
}
#endregion
public static string[] ForumsToSearchDecode (string stringToDecode)
{
Hashtable forums = Forums.GetForums(CSContext.Current, false, true);
UTF8Encoding decoder = new UTF8Encoding();
string[] filteredForumsToSearch = null;
ArrayList verifiedForumList = new ArrayList();
// Convert the parameters from a base64 encoded string and then
// into a string[]
//
if (stringToDecode != string.Empty)
{
// Attempt to decode the string
try
{
byte[] forumList = Convert.FromBase64String( stringToDecode );
filteredForumsToSearch = decoder.GetString( forumList ).Split(',');
}
catch
{
filteredForumsToSearch = null;
}
}
// Get a verified list of only the forums the user has access to
// if our forum string[] is null we get all the forums the user
// has access to; otherwise the list is checked to ensure we only
// get forums the user is allowed to see
//
if (filteredForumsToSearch == null)
{
foreach (Forum f in forums.Values)
{
if(f.ForumType != ForumType.Deleted && f.ForumType != ForumType.Reporting && f.IsSearchable)
verifiedForumList.Add(f.SectionID.ToString());
}
}
else
{
foreach (string forumid in filteredForumsToSearch)
{
if (forums[ int.Parse(forumid) ] != null)
verifiedForumList.Add(forumid);
}
}
return (string[]) verifiedForumList.ToArray(typeof(string));
}
#region Render
// *********************************************************************
// AttachChildControls
//
/// <summary>
/// Initializes the user control loaded in CreateChildControls. Initialization
/// consists of finding well known control names and wiring up any necessary events.
/// </summary>
///
// ********************************************************************/
protected override void AttachChildControls()
{
if (!Page.IsPostBack) {
searchQuery = csContext.Context.Request.QueryString["q"];
//searchUsers = CommunityServer.Search.UsersToDecode (csContext.Context.Request.QueryString["u"] );
}
// Find all the controls
//
searchFor = (Label) FindControl("ForumDescription");
searchTextTop = (TextBox) FindControl("SearchTextTop");
searchTextBottom = (TextBox) FindControl("SearchTextBottom");
searchButtonTop = FindControl("SearchButtonTop") as Button;
searchButtonBottom = FindControl("SearchButtonBottom") as Button;
searchButtonTop.Text = ResourceManager.GetString("Search");
searchButtonBottom.Text = ResourceManager.GetString("Search");
searchRepeater = FindControl("SearchRepeater") as Repeater;
pager = (Pager) FindControl("Pager");
resultTotal = (CurrentPage) FindControl("TotalResults");
resultTotal.TextFormat = ResourceManager.GetString("SearchResults_TotalResults");
searchDuration = (Literal) FindControl("SearchDuration");
searchDuration.Text = ResourceManager.GetString("SearchResults_SearchDuration");
formStatus = FindControl("formStatus") as StatusMessage;
validatorTop = (HtmlGenericControl) FindControl("SearchBoxValidatorTop");
validatorBottom = (HtmlGenericControl) FindControl("SearchBoxValidatorBottom");
searchResultsDisplay = FindControl("SearchResultsDisplay") as Panel;
((Label) FindControl("ForumName")).Text = ResourceManager.GetString("SearchViewSimple_Title");
// Set up the OnItemCommand for the DataList
//
searchRepeater.ItemCommand += new RepeaterCommandEventHandler(Display_Item);
searchRepeater.ItemDataBound += new RepeaterItemEventHandler( SearchRepeater_ItemDataBound );
searchButtonTop.Click += new EventHandler(Search_Click);
searchButtonBottom.Click += new EventHandler(Search_Click);
// Set the pager size
//
pager.PageSize = CSContext.Current.SiteSettings.SearchPostsPerPage;
// Set the values in our textboxes
//
searchTextTop.Text = searchQuery;
searchTextBottom.Text = searchQuery;
}
#endregion
public void Search_Click (Object sender, EventArgs e)
{
bool isSearchTextInTopTextBox = true;
// Which search text is being used?
//
if ( ((Button) sender).ID != "SearchButtonTop" )
isSearchTextInTopTextBox = false;
// Set the text to the correct values
//
if (isSearchTextInTopTextBox)
{
if(searchTextTop.Text.Trim() == string.Empty)
{
validatorTop.Visible = true;
return;
}
else
{
validatorTop.Visible = false;
searchTextBottom.Text = searchTextTop.Text;
}
}
else
{
if(searchTextBottom.Text.Trim() == string.Empty)
{
validatorBottom.Visible = true;
return;
}
else
{
validatorBottom.Visible = false;
searchTextTop.Text = searchTextBottom.Text;
}
}
HttpContext.Current.Response.Redirect(Globals.GetSiteUrls().SearchForText(searchTextTop.Text));
}
public override void DataBind()
{
base.DataBind();
SearchResultSet searchResult = null;
int resultCountUpper, resultCountLower;
// Set the "search for" text
//
searchFor.Text = string.Format(ResourceManager.GetString("SearchViewSimple_SearchFor"), Globals.HtmlEncode(searchTextTop.Text));
// Get the search results
//
SearchQuery query = new SearchQuery();
query.PageIndex = pager.PageIndex;
query.PageSize = pager.PageSize;
query.UserID = CSContext.Current.User.UserID;
query.SectionsToSearch = ForumsToSearchDecode ( csContext.Context.Request.QueryString["f"] );
query.UsersToSearch = CommunityServer.Search.UsersToDecode (csContext.Context.Request.QueryString["u"]);
query.SearchTerms = searchTextTop.Text;
ForumSearch search = new ForumSearch();
try {
searchResult = search.GetSearchResults(query);
} catch {
searchResultsDisplay.Visible = false;
formStatus.Visible = true;
formStatus.Success = false;
formStatus.ResourceName = "SearchView_NoResults";
return;
}
// Set the TotalRecords in the pager
//
pager.TotalRecords = searchResult.TotalRecords;
// Databind
//
searchRepeater.DataSource = searchResult.Posts;
searchRepeater.DataBind();
resultCountLower = 1 + (pager.PageIndex * pager.PageSize);
// Set the upper bounds of the search results text
//
if (pager.TotalRecords > pager.PageSize)
resultCountUpper = pager.PageSize + (pager.PageSize * pager.PageIndex);
else
resultCountUpper = searchResult.TotalRecords;
// Setup the result total
resultTotal.PageIndex = pager.PageIndex;
resultTotal.TotalPages = pager.CalculateTotalPages();
resultTotal.TotalRecords = pager.TotalRecords;
if (searchResult.SearchDuration < 1)
searchDuration.Text = string.Format(searchDuration.Text, "< 1");
else
searchDuration.Text = string.Format(searchDuration.Text, searchResult.SearchDuration);
}
public void Display_Item (Object sender, RepeaterCommandEventArgs e)
{
HttpContext.Current.Response.Write( e.CommandArgument );
DataBind();
}
/// <summary>
/// Added to handle Username displaying
/// </summary>
protected void SearchRepeater_ItemDataBound (Object Sender, RepeaterItemEventArgs e) {
ForumPost post = e.Item.DataItem as ForumPost;
if (post == null)
return;
bool isAnonymousPostingEnabled = post.IsAnonymousPost &&
CSContext.Current.SiteSettings.EnableUserPostingAsAnonymous &&
post.Forum.EnableAnonymousPostingForUsers;
switch (e.Item.ItemType) {
case ListItemType.Item:
case ListItemType.AlternatingItem:
Label userName = e.Item.FindControl( "UsernameLink" ) as Label;
if (userName != null) {
userName.Text = Formatter.FormatUsername( post.User.UserID, post.User.Username, isAnonymousPostingEnabled, false );
}
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -