#region Copyright
//
// DotNetNukeŽ - http://www.dotnetnuke.com
// Copyright (c) 2002-2011
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
#region Usings
using System;
using System.Collections;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
using Globals = DotNetNuke.Common.Globals;
using DotNetNuke.Entities.Icons;
#endregion
namespace DotNetNuke.UI.Skins.Controls
{
public partial class Search : SkinObjectBase
{
#region "Private Members"
private const string MyFileName = "Search.ascx";
private bool _showSite = true;
private bool _showWeb = true;
private string _siteIconURL;
private string _siteText;
private string _siteToolTip;
private string _siteURL;
private string _webIconURL;
private string _webText;
private string _webToolTip;
private string _webURL;
#endregion
#region "Public Members"
///
/// Gets or sets the CSS class for the option buttons and search button
///
/// If you are using the DropDownList option then you can style the search
/// elements without requiring a custom CssClass.
public string CssClass { get; set; }
///
/// Gets or sets the visibility setting for the radio button corresponding to site based searchs.
///
/// Set this value to false to hide the "Site" radio button. This setting has no effect
/// if UseDropDownList is true.
public bool ShowSite
{
get
{
return _showSite;
}
set
{
_showSite = value;
}
}
///
/// Gets or sets the visibility setting for the radio button corresponding to web based searchs.
///
/// Set this value to false to hide the "Web" radio button. This setting has no effect
/// if UseDropDownList is true.
public bool ShowWeb
{
get
{
return _showWeb;
}
set
{
_showWeb = value;
}
}
///
/// Gets or sets the site icon URL.
///
/// The site icon URL.
/// If the SiteIconURL is not set or is an empty string then this will return a site relative URL for the
/// DnnSearch_16X16_Standard.png image in the images/search subfolder. SiteIconURL supports using
/// app relative virtual paths designated by the use of the tilde (~).
public string SiteIconURL
{
get
{
if (string.IsNullOrEmpty(_siteIconURL))
{
return IconController.IconURL("DnnSearch");
}
return _siteIconURL;
}
set
{
_siteIconURL = value;
}
}
///
/// Gets or sets the text for the "site" radio button or option list item.
///
/// The site text.
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
public string SiteText
{
get
{
if (string.IsNullOrEmpty(_siteText))
{
return Localization.GetString("Site", Localization.GetResourceFile(this, MyFileName));
}
return _siteText;
}
set
{
_siteText = value;
}
}
///
/// Gets or sets the tooltip text for the "site" radio button.
///
/// The site tool tip.
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
public string SiteToolTip
{
get
{
if (string.IsNullOrEmpty(_siteToolTip))
{
return Localization.GetString("Site.ToolTip", Localization.GetResourceFile(this, MyFileName));
}
return _siteToolTip;
}
set
{
_siteToolTip = value;
}
}
///
/// Gets or sets the URL for doing web based site searches.
///
/// The site URL.
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
/// The site URL is a template for an external search engine, which by default, uses Google.com. The siteURL should
/// include the tokens [TEXT] and [DOMAIN] to be replaced automatically by the search text and the current site domain.
public string SiteURL
{
get
{
if (string.IsNullOrEmpty(_siteURL))
{
return Localization.GetString("URL", Localization.GetResourceFile(this, MyFileName));
}
return _siteURL;
}
set
{
_siteURL = value;
}
}
///
/// Gets or sets the html for the submit button.
///
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
/// If you set the value to an hmtl img tag, then the src attribute will be made relative
/// to the current skinpath.
public string Submit { get; set; }
///
/// Gets or sets a value indicating whether to use the web search engine for site searches.
///
/// Set this value to true to perform a domain limited search using the search engine defined by SiteURL.
public bool UseWebForSite { get; set; }
///
/// Gets or sets a value indicating whether to display the site/web options using a drop down list.
///
/// If true, then the site and web options are displayed in a drop down list. If the
/// drop down list is used, then the ShowWeb and ShowSite
/// properties are not used.
public bool UseDropDownList { get; set; }
///
/// Gets or sets the web icon URL.
///
/// The web icon URL.
/// If the WebIconURL is not set or is an empty string then this will return a site relative URL for the
/// google-icon.gif image in the images/search subfolder. WebIconURL supports using
/// app relative virtual paths designated by the use of the tilde (~).
public string WebIconURL
{
get
{
if (string.IsNullOrEmpty(_webIconURL))
{
return IconController.IconURL("GoogleSearch");
}
return _webIconURL;
}
set
{
_webIconURL = value;
}
}
///
/// Gets or sets the text for the "web" radio button or option list item.
///
/// The web text.
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
public string WebText
{
get
{
if (string.IsNullOrEmpty(_webText))
{
return Localization.GetString("Web", Localization.GetResourceFile(this, MyFileName));
}
return _webText;
}
set
{
_webText = value;
}
}
///
/// Gets or sets the tooltip text for the "web" radio button.
///
/// The web tool tip.
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
public string WebToolTip
{
get
{
if (string.IsNullOrEmpty(_webToolTip))
{
return Localization.GetString("Web.ToolTip", Localization.GetResourceFile(this, MyFileName));
}
return _webToolTip;
}
set
{
_webToolTip = value;
}
}
///
/// Gets or sets the URL for doing web based searches.
///
/// The web URL.
/// If the value is not set or is an empty string, then the localized value from
/// /admin/skins/app_localresources/Search.ascx.resx localresource file is used.
/// The web URL is a template for an external search engine, which by default, uses Google.com. The WebURL should
/// include the token [TEXT] to be replaced automatically by the search text. The [DOMAIN] token, if present, will be
/// replaced by an empty string.
public string WebURL
{
get
{
if (string.IsNullOrEmpty(_webURL))
{
return Localization.GetString("URL", Localization.GetResourceFile(this, MyFileName));
}
return _webURL;
}
set
{
_webURL = value;
}
}
#endregion
#region "Private Methods"
private int GetSearchTabId()
{
int searchTabId = PortalSettings.SearchTabId;
if (searchTabId == Null.NullInteger)
{
var objModules = new ModuleController();
ArrayList arrModules = objModules.GetModulesByDefinition(PortalSettings.PortalId, "Search Results");
if (arrModules.Count > 1)
{
foreach (ModuleInfo SearchModule in arrModules)
{
if (SearchModule.CultureCode == PortalSettings.CultureCode)
{
searchTabId = SearchModule.TabID;
}
}
}
else if (arrModules.Count == 1)
{
searchTabId = ((ModuleInfo)arrModules[0]).TabID;
}
}
return searchTabId;
}
///
/// Executes the search.
///
/// The text which will be used to perform the search.
/// The type of the search. Use "S" for a site search, and "W" for a web search.
///
/// All web based searches will open in a new window, while site searches will open in the current window. A site search uses the built
/// in search engine to perform the search, while both web based search variants will use an external search engine to perform a search.
///
protected void ExecuteSearch(string searchText, string searchType)
{
int searchTabId = GetSearchTabId();
if (searchTabId == Null.NullInteger)
{
return;
}
string strURL;
if (!string.IsNullOrEmpty(searchText))
{
switch (searchType)
{
case "S":
// site
if (UseWebForSite)
{
strURL = SiteURL;
if (!string.IsNullOrEmpty(strURL))
{
strURL = strURL.Replace("[TEXT]", Server.UrlEncode(searchText));
strURL = strURL.Replace("[DOMAIN]", Request.Url.Host);
UrlUtils.OpenNewWindow(Page, GetType(), strURL);
}
}
else
{
if (Host.UseFriendlyUrls)
{
Response.Redirect(Globals.NavigateURL(searchTabId) + "?Search=" + Server.UrlEncode(searchText));
}
else
{
Response.Redirect(Globals.NavigateURL(searchTabId) + "&Search=" + Server.UrlEncode(searchText));
}
}
break;
case "W":
// web
strURL = WebURL;
if (!string.IsNullOrEmpty(strURL))
{
strURL = strURL.Replace("[TEXT]", Server.UrlEncode(searchText));
strURL = strURL.Replace("[DOMAIN]", "");
UrlUtils.OpenNewWindow(Page, GetType(), strURL);
}
break;
}
}
else
{
if (Host.UseFriendlyUrls)
{
Response.Redirect(Globals.NavigateURL(searchTabId));
}
else
{
Response.Redirect(Globals.NavigateURL(searchTabId));
}
}
}
#endregion
#region "Event Handlers"
///
/// Handles the Load event of the Page control.
///
/// The source of the event.
/// The instance containing the event data.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
cmdSearch.Click += cmdSearch_Click;
cmdSearchNew.Click += cmdSearchNew_Click;
if (!Page.IsPostBack)
{
ClientAPI.RegisterKeyCapture(txtSearch, cmdSearch, 13);
ClientAPI.RegisterKeyCapture(txtSearchNew, cmdSearchNew, 13);
if (!String.IsNullOrEmpty(Submit))
{
if (Submit.IndexOf("src=") != -1)
{
Submit = Submit.Replace("src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);
Submit = Submit.Replace("src='", "src='" + PortalSettings.ActiveTab.SkinPath);
}
}
else
{
Submit = Localization.GetString("Search", Localization.GetResourceFile(this, MyFileName));
}
cmdSearch.Text = Submit;
cmdSearchNew.Text = Submit;
if (!String.IsNullOrEmpty(CssClass))
{
WebRadioButton.CssClass = CssClass;
SiteRadioButton.CssClass = CssClass;
cmdSearch.CssClass = CssClass;
cmdSearchNew.CssClass = CssClass;
}
if (Request.QueryString["Search"] != null)
{
txtSearch.Text = Request.QueryString["Search"];
txtSearchNew.Text = Request.QueryString["Search"];
}
}
}
///
/// Handles the Click event of the cmdSearch control.
///
/// The source of the event.
/// The instance containing the event data.
/// This event is only used when UseDropDownList is false.
private void cmdSearch_Click(object sender, EventArgs e)
{
string SearchType = "S";
if (WebRadioButton.Visible)
{
if (WebRadioButton.Checked)
{
SearchType = "W";
}
}
ExecuteSearch(txtSearch.Text.Trim(), SearchType);
}
///
/// Handles the Click event of the cmdSearchNew control.
///
/// The source of the event.
/// The instance containing the event data.
/// This event is only used when UseDropDownList is true.
protected void cmdSearchNew_Click(object sender, EventArgs e)
{
ExecuteSearch(txtSearchNew.Text.Trim(), ClientAPI.GetClientVariable(Page, "SearchIconSelected"));
}
///
/// Handles the PreRender event of the Page control.
///
/// The instance containing the event data.
/// This event performs final initialization tasks for the search object UI.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ClassicSearch.Visible = !UseDropDownList;
DropDownSearch.Visible = UseDropDownList;
if (UseDropDownList)
{
//Client Variables will survive a postback so there is no reason to register them.
if (!Page.IsPostBack)
{
downArrow.AlternateText = Localization.GetString("DropDownGlyph.AltText", Localization.GetResourceFile(this, MyFileName));
downArrow.ToolTip = downArrow.AlternateText;
ClientAPI.RegisterClientVariable(Page, "SearchIconWebUrl", string.Format("url({0})", ResolveUrl(WebIconURL)), true);
ClientAPI.RegisterClientVariable(Page, "SearchIconSiteUrl", string.Format("url({0})", ResolveUrl(SiteIconURL)), true);
//We are going to use a dnn client variable to store which search option (web/site) is selected.
ClientAPI.RegisterClientVariable(Page, "SearchIconSelected", "S", true);
}
ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn);
string script = string.Format(Globals.glbScriptFormat, ResolveUrl("~/Resources/Search/Search.js"));
ClientAPI.RegisterStartUpScript(Page, "initSearch", script);
}
else
{
if (!Page.IsPostBack)
{
WebRadioButton.Visible = ShowWeb;
SiteRadioButton.Visible = ShowSite;
if (WebRadioButton.Visible)
{
WebRadioButton.Checked = true;
WebRadioButton.Text = WebText;
WebRadioButton.ToolTip = WebToolTip;
}
if (SiteRadioButton.Visible)
{
SiteRadioButton.Checked = true;
SiteRadioButton.Text = SiteText;
SiteRadioButton.ToolTip = SiteToolTip;
}
}
}
}
#endregion
}
}