mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX: Search didn't respect searchableClasses passed to FulltextSearchable::enable()
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@111464 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
9d2550e223
commit
4965cb0f69
@ -1,38 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Extension to provide a search interface when applied to ContentController
|
||||
*
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage search
|
||||
*/
|
||||
class ContentControllerSearchExtension extends Extension {
|
||||
class ContentControllerSearchExtension extends Extension {
|
||||
static $allowed_actions = array(
|
||||
'SearchForm',
|
||||
'results',
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Site search form
|
||||
*/
|
||||
function SearchForm() {
|
||||
$searchText = _t('SearchForm.SEARCH', 'Search');
|
||||
|
||||
|
||||
if($this->owner->request) {
|
||||
$searchText = $this->owner->request->getVar('Search');
|
||||
}
|
||||
|
||||
|
||||
$fields = new FieldSet(
|
||||
new TextField('Search', false, $searchText)
|
||||
);
|
||||
$actions = new FieldSet(
|
||||
new FormAction('results', _t('SearchForm.GO', 'Go'))
|
||||
);
|
||||
return new SearchForm($this->owner, 'SearchForm', $fields, $actions);
|
||||
$form = new SearchForm($this->owner, 'SearchForm', $fields, $actions);
|
||||
$form->classesToSearch(FulltextSearchable::$searchableClasses);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process and render search results.
|
||||
*
|
||||
*
|
||||
* @param array $data The raw request data submitted by user
|
||||
* @param SearchForm $form The form instance that was submitted
|
||||
* @param SS_HTTPRequest $request Request generated for this action
|
||||
|
@ -1,13 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* Provides a simple search engine for your site based on the MySQL FULLTEXT index
|
||||
*
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage search
|
||||
*/
|
||||
class FulltextSearchable extends DataObjectDecorator {
|
||||
protected $searchFields;
|
||||
|
||||
static $searchableClasses;
|
||||
|
||||
/**
|
||||
* Enable the default configuration of MySQL full-text searching on the given data classes.
|
||||
*/
|
||||
@ -16,7 +17,7 @@ class FulltextSearchable extends DataObjectDecorator {
|
||||
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
|
||||
'File' => 'Filename,Title,Content'
|
||||
);
|
||||
|
||||
|
||||
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
|
||||
foreach($searchableClasses as $class) {
|
||||
if(isset($defaultColumns[$class])) {
|
||||
@ -25,25 +26,26 @@ class FulltextSearchable extends DataObjectDecorator {
|
||||
throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
|
||||
}
|
||||
}
|
||||
|
||||
self::$searchableClasses = $searchableClasses;
|
||||
|
||||
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
||||
}
|
||||
|
||||
|
||||
function __construct($searchFields) {
|
||||
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
|
||||
else $this->searchFields = $searchFields;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
function extraStatics($class = null, $extension = null) {
|
||||
if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
|
||||
$searchFields = $matches[1];
|
||||
|
||||
|
||||
return array(
|
||||
'indexes' => array(
|
||||
"SearchFields" => Array(
|
||||
'type'=>'fulltext',
|
||||
'name'=>'SearchFields',
|
||||
'type'=>'fulltext',
|
||||
'name'=>'SearchFields',
|
||||
'value'=> $searchFields
|
||||
),
|
||||
)
|
||||
|
@ -78,6 +78,15 @@ class SearchForm extends Form {
|
||||
$legalClasses = array_intersect($classes, array('SiteTree', 'File'));
|
||||
$this->classesToSearch = $legalClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the classes to search
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getClassesToSearch() {
|
||||
return $this->classesToSearch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return dataObjectSet of the results using $_REQUEST to get info from form.
|
||||
|
13
tests/search/ContentControllerSearchExtensionTest.php
Normal file
13
tests/search/ContentControllerSearchExtensionTest.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
class ContentControllerSearchExtensionTest extends SapphireTest {
|
||||
|
||||
function testCustomSearchFormClassesToTest() {
|
||||
FulltextSearchable::enable('File');
|
||||
|
||||
$page = new Page();
|
||||
$controller = new ContentController($page);
|
||||
$form = $controller->SearchForm();
|
||||
|
||||
$this->assertEquals(array('File'), $form->getClassesToSearch());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user