SearchOnSearch.cs :  » Bloggers » BlogEngine.NET » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » Controls » SearchOnSearch.cs
#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using BlogEngine.Core;

#endregion

namespace Controls{
  /// <summary>
  /// If the visitor arrives through a search engine, this control
  /// will display an in-site search result based on the same search term.
  /// </summary>
  public class SearchOnSearch : Control
  {
    #region Class Data

    private static Regex _rxSearchTerm = null;

    #endregion


    #region Class Constructor

    static SearchOnSearch()
    {
      // Matches the query string parameter "q" and its value.  Does not match if "q" is blank.
      _rxSearchTerm = new Regex("[?&]q=([^&#]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
    }

    #endregion

    #region Properties

    private int _MaxResults;
    /// <summary>
    /// Gets or sets the maximum numbers of results to display.
    /// </summary>
    public int MaxResults
    {
      get { return _MaxResults; }
      set { _MaxResults = value; }
    }

    private string _Headline;
    /// <summary>
    /// Gets or sets the text of the headline.
    /// </summary>
    public string Headline
    {
      get { return _Headline; }
      set { _Headline = value; }
    }

    private string _Text;
    /// <summary>
    /// Gets or sets the text displayed below the headline.
    /// </summary>
    public string Text
    {
      get { return _Text; }
      set { _Text = value; }
    }

    #endregion

    #region Methods

    /// <summary>
    /// Checks the referrer to see if it qualifies as a search.
    /// </summary>
    private string Html()
    {
      if (Context.Request.UrlReferrer != null && !Context.Request.UrlReferrer.ToString().Contains(Utils.AbsoluteWebRoot.ToString()) && IsSearch)
      {
        string referrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLowerInvariant();
        string searchTerm = GetSearchTerm(referrer);
        List<IPublishable> items = Search.Hits(searchTerm, false);
        if (items.Count == 0)
          return null;

        return WriteHtml(items, searchTerm);
      }

      return null;
    }

    /// <summary>
    /// Writes the search results as HTML.
    /// </summary>
    private string WriteHtml(List<IPublishable> items, string searchTerm)
    {
      int results = MaxResults < items.Count ? MaxResults : items.Count;
      StringBuilder sb = new StringBuilder();
      sb.Append("<div id=\"searchonsearch\">");
      sb.AppendFormat("<h3>{0} '{1}'</h3>", Headline, HttpUtility.HtmlEncode(HttpUtility.UrlDecode(searchTerm)));
      sb.AppendFormat("<p>{0}</p>", Text);
      sb.Append("<ol>");

      for (int i = 0; i < results; i++)
      {
        sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", items[i].RelativeLink, items[i].Title);
      }

      sb.Append("</ol>");
      sb.Append("</div>");

      return sb.ToString();
    }

    /// <summary>
    /// Retrieves the search term from the specified referrer string.
    /// </summary>
    private string GetSearchTerm(string referrer)
    {
      string term = string.Empty;
      Match match = _rxSearchTerm.Match(referrer);

      if (match.Success)
      {
        term = match.Groups[1].Value;
      }

      return term.Replace("+", " ");
    }

    /// <summary>
    /// Checks the referrer to see if it is from a search engine.
    /// </summary>
    private bool IsSearch
    {
      get
      {
        string referrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLowerInvariant();
        return _rxSearchTerm.IsMatch(referrer);
      }
    }

    #endregion

    /// <summary>
    /// Renders the control as a script tag.
    /// </summary>
    public override void RenderControl(HtmlTextWriter writer)
    {
      string html = Html();
      if (html != null)
        writer.Write(html);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.