helpcenter/Web/Search.aspx
2019-01-23 12:03:00 +03:00

150 lines
5.8 KiB
Plaintext

<%@ Page Title="" Language="C#" MasterPageFile="~/Masters/HelpCenter.master" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="Newtonsoft.Json" %>
<%@ Import Namespace="Newtonsoft.Json.Linq" %>
<%@ Register Namespace="TeamLab.Controls" Assembly="__Code" TagPrefix="cc" %>
<%@ Import Namespace="System.Linq" %>
<%@ Assembly Name="GoogleSearchAPI" %>
<script runat="server">
public class WebResult : Google.API.Search.IWebResult
{
public string CacheUrl { get; set; }
public string Content { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string VisibleUrl { get; set; }
}
protected System.Collections.Generic.IEnumerable<Google.API.Search.IWebResult> Results { get; set; }
protected Regex Highlite;
class SearchResultComparer : System.Collections.Generic.IEqualityComparer<Google.API.Search.IWebResult>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(Google.API.Search.IWebResult x, Google.API.Search.IWebResult y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.Url == y.Url;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(Google.API.Search.IWebResult result)
{
//Check whether the object is null
if (Object.ReferenceEquals(result, null)) return 0;
//Calculate the hash code for the product.
return result.Url.GetHashCode();
}
}
private void Page_Load(object sender, System.EventArgs e)
{
var query = Request["text"];
if (!string.IsNullOrEmpty(query))
{
try
{
Highlite = new Regex(query.ToLowerInvariant(), RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
var url = String.Format(ConfigurationManager.AppSettings["google.search.url"] + "?cx={0}&key={1}&hl={2}&q={3}",
HttpUtility.HtmlEncode(ConfigurationManager.AppSettings["google.search.cx"]),
HttpUtility.HtmlEncode(ConfigurationManager.AppSettings["google.search.key"]),
HttpUtility.HtmlEncode(LanguageProvider.GetCurrentCulture().TwoLetterISOLanguageName),
HttpUtility.HtmlEncode(query));
using (var webClient = new WebClient())
{
webClient.Encoding = Encoding.UTF8;
var response = webClient.DownloadString(url);
var jsonObj = JObject.Parse(response);
var items = jsonObj.Value<JArray>("items");
if (items != null)
{
Results = items.Select(itemObj => new WebResult
{
Content = itemObj.Value<string>("snippet"),
Title = itemObj.Value<string>("title"),
Url = itemObj.Value<string>("link")
}).Distinct(new SearchResultComparer());
}
}
}
catch (Exception ex)
{
log4net.LogManager.GetLogger("ASC").Error(ex);
}
}
else
{
Results = new Google.API.Search.IWebResult[0];
}
}
private string SelectAddress(string language)
{
return language.Equals("en", StringComparison.OrdinalIgnoreCase) ? "" : (language + "/");
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
<noindex>
<%--set current menu--%>
<div class="InnerPage search">
<div class="undertop">
<div class="Breads">
<cc:LocalizeContent runat="Server" ControlName="~/Controls/Help/VariousControls/TopControls/TopHelpCenter/TopHelpCenter.ascx"/>
<cc:LocalizeContent runat="Server" ControlName="~/Controls/Help/VariousControls/TopControls/SearchResultsTop/SearchResultsTop.ascx"/>
</div>
</div>
<div class="innerblue">
<div class="description searchpage">
<cc:LocalizeContent runat="Server" ControlName="~/Controls/Help/VariousControls/SearchLine/SearchLine.ascx" />
<div class="menuleft">
<cc:LocalizeContent runat="Server" ControlName="~/Controls/Help/VariousControls/SearchLine/SearchLine.ascx"/>
<cc:LocalizeContent runat="Server" ControlName="~/Controls/Help/VariousControls/HelpLinks/HelpLinks.ascx"/>
</div>
<cc:LocalizeContent runat="Server" ControlName="~/Controls/Help/Search.ascx" />
<div class="searchResult">
<%if (Results == null || Results.Count() == 0)
{ %>
<h1><%=HttpUtility.HtmlEncode(((Page as BasePage).HelpCenter as HelpCenterModel).NoResults)%></h1>
<%}
else
{ %>
<ol>
<%foreach (var result in Results)
{ %>
<li>
<h2><a href="<%=result.Url%>"><%=HttpUtility.HtmlEncode(result.Title) %></a></h2>
<p><%=Highlite.Replace(HttpUtility.HtmlEncode(result.Content), new MatchEvaluator(x => string.Format("<span>{0}</span>", x.Value)))%></p>
</li>
<%} %>
</ol>
<%} %>
</div>
</div>
<div class="clearFix"></div>
</div>
</div>
</noindex>
</asp:Content>