Use checkboxes in Site Settings to select terms to include in search

--HG--
branch : dev
This commit is contained in:
Sébastien Ros
2010-09-03 18:48:50 -07:00
parent b5ed08edea
commit 0c2023b0bd
8 changed files with 90 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ using Orchard.Localization;
namespace Orchard.Email.Drivers {
// We define a specific driver instead of using a TemplateFilterForRecord, because we need the ;odel to be the part and not the record.
// We define a specific driver instead of using a TemplateFilterForRecord, because we need the model to be the part and not the record.
// Thus the encryption/decryption will be done when accessing the part's property
public class SmtpSettingsPartDriver : ContentPartDriver<SmtpSettingsPart> {
@@ -17,13 +17,13 @@ namespace Orchard.Email.Drivers {
protected override string Prefix { get { return "SmtpSettings"; } }
protected override DriverResult Editor(SmtpSettingsPart termPart) {
return ContentPartTemplate(termPart, "Parts/Smtp.SiteSettings");
protected override DriverResult Editor(SmtpSettingsPart part) {
return ContentPartTemplate(part, "Parts/Smtp.SiteSettings");
}
protected override DriverResult Editor(SmtpSettingsPart termPart, IUpdateModel updater) {
updater.TryUpdateModel(termPart, Prefix, null, null);
return Editor(termPart);
protected override DriverResult Editor(SmtpSettingsPart part, IUpdateModel updater) {
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part);
}
}
}

View File

@@ -24,7 +24,7 @@ namespace Orchard.Search.Controllers {
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ActionResult Index(string q, int page = 1, int pageSize = 10) {
var searchFields = CurrentSite.As<SearchSettingsPart>().Record.SearchedFields.Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
var searchFields = CurrentSite.As<SearchSettingsPart>().SearchedFields;
var searchHits = _searchService.Query(q, page, pageSize,
CurrentSite.As<SearchSettingsPart>().Record.FilterCulture,

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Indexing;
using Orchard.Localization;
using Orchard.Search.Models;
using Orchard.Search.ViewModels;
namespace Orchard.Search.Drivers {
public class SearchSettingsPartDriver : ContentPartDriver<SearchSettingsPart> {
private const string SearchIndexName = "Search";
private readonly IIndexManager _indexManager;
public SearchSettingsPartDriver(IIndexManager indexManager) {
_indexManager = indexManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
protected override string Prefix { get { return "SearchSettings"; } }
protected override DriverResult Editor(SearchSettingsPart part) {
var model = new SearchSettingsViewModel();
var searchedFields = part.SearchedFields;
if (_indexManager.HasIndexProvider()) {
model.Entries = new List<SearchSettingsEntry>();
foreach (var field in _indexManager.GetSearchIndexProvider().GetFields(SearchIndexName)) {
model.Entries.Add(new SearchSettingsEntry { Field = field, Selected = searchedFields.Contains(field) });
}
}
return ContentPartTemplate(model, "Parts/Search.SiteSettings");
}
protected override DriverResult Editor(SearchSettingsPart part, IUpdateModel updater) {
var model = new SearchSettingsViewModel();
if(updater.TryUpdateModel(model, Prefix, null, null)) {
part.SearchedFields = model.Entries.Where(e => e.Selected).Select(e => e.Field).ToArray();
}
return Editor(part);
}
}
}

View File

@@ -9,7 +9,6 @@ namespace Orchard.Search.Handlers {
public SearchSettingsPartHandler(IRepository<SearchSettingsPartRecord> repository) {
Filters.Add(new ActivatingFilter<SearchSettingsPart>("Site"));
Filters.Add(StorageFilter.For(repository));
Filters.Add(new TemplateFilterForRecord<SearchSettingsPartRecord>("CommentSettings", "Parts/Search.SiteSettings"));
}
}
}

View File

@@ -1,6 +1,11 @@
using Orchard.ContentManagement;
using System;
using Orchard.ContentManagement;
namespace Orchard.Search.Models {
public class SearchSettingsPart : ContentPart<SearchSettingsPartRecord> {
public string[] SearchedFields {
get { return Record.SearchedFields.Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries); }
set { Record.SearchedFields = String.Join(", ", value); }
}
}
}

View File

@@ -67,6 +67,7 @@
<ItemGroup>
<Compile Include="Controllers\SearchController.cs" />
<Compile Include="DataMigrations\SearchDataMigration.cs" />
<Compile Include="Drivers\SearchSettingsPartDriver.cs" />
<Compile Include="Filters\SearchFilter.cs" />
<Compile Include="Models\SearchSettingsPart.cs" />
<Compile Include="Models\SearchSettingsPartRecord.cs" />
@@ -74,6 +75,7 @@
<Compile Include="Handlers\SearchSettingsPartHandler.cs" />
<Compile Include="Services\ISearchService.cs" />
<Compile Include="Services\SearchService.cs" />
<Compile Include="ViewModels\SearchSettingsViewModel.cs" />
<Compile Include="ViewModels\SearchResultViewModel.cs" />
<Compile Include="ViewModels\SearchViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -0,0 +1,14 @@
using Orchard.Mvc.ViewModels;
using System.Collections.Generic;
namespace Orchard.Search.ViewModels {
public class SearchSettingsViewModel : BaseViewModel {
public IList<SearchSettingsEntry> Entries { get; set; }
}
public class SearchSettingsEntry {
public string Field { get; set; }
public bool Selected { get; set; }
public int Weight { get; set; }
}
}

View File

@@ -1,5 +1,5 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<SearchSettingsPartRecord>" %>
<%@ Import Namespace="Orchard.Search.Models"%>
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<SearchSettingsViewModel>" %>
<%@ Import Namespace="Orchard.Search.ViewModels"%>
<fieldset>
<legend><%: T("Search")%></legend>
<%-- TODO: (sebros) Implementation details after Alpha
@@ -10,8 +10,14 @@
</div>
--%>
<div>
<label for="SearchSettings_SearchedFields"><%: T("Searched fields")%></label>
<%: Html.EditorFor(m => m.SearchedFields)%>
<%: Html.ValidationMessage("SearchedFields", "*")%>
<%
for(var i=0; i<Model.Entries.Count; i++) {
var j = i;%>
<input type="checkbox" value="true" <% if(Model.Entries[j].Selected ) { %> checked="checked" <% } %>
name="<%:Html.FieldNameFor(m => m.Entries[j].Selected)%>" id="<%:Html.FieldIdFor(m => m.Entries[j].Selected)%>"/>
<%: Html.HiddenFor(m => m.Entries[j].Field)%>
<label class="forcheckbox" for="<%:Html.FieldIdFor(m => m.Entries[j].Selected)%>"><%: Model.Entries[j].Field%></label>
<%
}%>
</div>
</fieldset>