Make the culture filter on search results an option.

Add checkbox in admin.

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-06-17 16:41:10 -07:00
parent 40769d6f2a
commit ae82ccc865
8 changed files with 55 additions and 5 deletions

View File

@@ -1,7 +1,11 @@
using System.Web.Mvc;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Search.Services;
using Orchard.Search.ViewModels;
using Orchard.Settings;
using Orchard.Search.Models;
namespace Orchard.Search.Controllers {
[ValidateInput(false)]
public class SearchController : Controller {
@@ -13,11 +17,13 @@ namespace Orchard.Search.Controllers {
_contentManager = contentManager;
}
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ActionResult Index(string q, int page = 1, int pageSize = 10) {
var searchViewModel = new SearchViewModel {
Query = q,
DefaultPageSize = 10, // <- yeah, I know :|
PageOfResults = _searchService.Query(q, page, pageSize, searchHit => new SearchResultViewModel {
PageOfResults = _searchService.Query(q, page, pageSize, CurrentSite.As<SearchSettings>().Record.FilterCulture, searchHit => new SearchResultViewModel {
Content = _contentManager.BuildDisplayModel(_contentManager.Get(searchHit.ContentItemId), "SummaryForSearch"),
SearchHit = searchHit
})

View File

@@ -0,0 +1,15 @@
using JetBrains.Annotations;
using Orchard.Search.Models;
using Orchard.Data;
using Orchard.ContentManagement.Handlers;
namespace Orchard.Search.Handlers {
[UsedImplicitly]
public class SearchSettingsHandler : ContentHandler {
public SearchSettingsHandler(IRepository<SearchSettingsRecord> repository) {
Filters.Add(new ActivatingFilter<SearchSettings>("site"));
Filters.Add(StorageFilter.For(repository));
Filters.Add(new TemplateFilterForRecord<SearchSettingsRecord>("CommentSettings", "Parts/Search.SiteSettings"));
}
}
}

View File

@@ -0,0 +1,6 @@
using Orchard.ContentManagement;
namespace Orchard.Search.Models {
public class SearchSettings : ContentPart<SearchSettingsRecord> {
}
}

View File

@@ -0,0 +1,7 @@
using Orchard.ContentManagement.Records;
namespace Orchard.Search.Models {
public class SearchSettingsRecord : ContentPartRecord {
public virtual bool FilterCulture { get; set; }
}
}

View File

@@ -67,7 +67,10 @@
<ItemGroup>
<Compile Include="Controllers\SearchController.cs" />
<Compile Include="Filters\SearchFilter.cs" />
<Compile Include="Models\SearchSettings.cs" />
<Compile Include="Models\SearchSettingsRecord.cs" />
<Compile Include="Routes.cs" />
<Compile Include="Handlers\SearchSettingsHandler.cs" />
<Compile Include="Services\ISearchService.cs" />
<Compile Include="Services\SearchService.cs" />
<Compile Include="ViewModels\SearchResultViewModel.cs" />
@@ -84,6 +87,7 @@
<Content Include="Module.txt" />
<Content Include="Styles\admin.css" />
<Content Include="Styles\search.css" />
<Content Include="Views\EditorTemplates\Parts\Search.SiteSettings.ascx" />
<Content Include="Views\SearchForm.ascx" />
<Content Include="Views\Search\Index.ascx" />
<Content Include="Views\Web.config" />

View File

@@ -5,7 +5,7 @@ using Orchard.Indexing;
namespace Orchard.Search.Services {
public interface ISearchService : IDependency {
bool HasIndexToManage { get; }
IPageOfItems<T> Query<T>(string query, int skip, int? take, Func<ISearchHit, T> shapeResult);
IPageOfItems<T> Query<T>(string query, int skip, int? take, bool filterCulture, Func<ISearchHit, T> shapeResult);
void RebuildIndex();
void UpdateIndex();
DateTime GetIndexUpdatedUtc();

View File

@@ -33,18 +33,20 @@ namespace Orchard.Search.Services
get { return _indexManager.HasIndexProvider(); }
}
IPageOfItems<T> ISearchService.Query<T>(string query, int page, int? pageSize, Func<ISearchHit, T> shapeResult) {
IPageOfItems<T> ISearchService.Query<T>(string query, int page, int? pageSize, bool filterCulture, Func<ISearchHit, T> shapeResult) {
if (string.IsNullOrWhiteSpace(query) || !_indexManager.HasIndexProvider())
return null;
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
.Parse(new[] {"title", "body"}, query);
if ( filterCulture ) {
var culture = _cultureManager.GetSiteCulture();
// use LCID as the text representation gets analyzed by the query parser
searchBuilder
.WithField("culture", CultureInfo.GetCultureInfo(culture).LCID);
}
var totalCount = searchBuilder.Count();
if (pageSize != null)

View File

@@ -0,0 +1,10 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<SearchSettingsRecord>" %>
<%@ Import Namespace="Orchard.Search.Models"%>
<fieldset>
<legend><%: T("Search")%></legend>
<div>
<%: Html.EditorFor(m => m.FilterCulture) %>
<label class="forcheckbox" for="SearchSettings_FilterCulture"><%: T("Search results must be fitered for the current culture")%></label>
<%: Html.ValidationMessage("FilterCulture", "*")%>
</div>
</fieldset>