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() (from r111464)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112905 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
0d19b45155
commit
dd1c02cd90
@ -1,38 +1,40 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Extension to provide a search interface when applied to ContentController
|
* Extension to provide a search interface when applied to ContentController
|
||||||
*
|
*
|
||||||
* @package sapphire
|
* @package sapphire
|
||||||
* @subpackage search
|
* @subpackage search
|
||||||
*/
|
*/
|
||||||
class ContentControllerSearchExtension extends Extension {
|
class ContentControllerSearchExtension extends Extension {
|
||||||
static $allowed_actions = array(
|
static $allowed_actions = array(
|
||||||
'SearchForm',
|
'SearchForm',
|
||||||
'results',
|
'results',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Site search form
|
* Site search form
|
||||||
*/
|
*/
|
||||||
function SearchForm() {
|
function SearchForm() {
|
||||||
$searchText = _t('SearchForm.SEARCH', 'Search');
|
$searchText = _t('SearchForm.SEARCH', 'Search');
|
||||||
|
|
||||||
if($this->owner->request) {
|
if($this->owner->request) {
|
||||||
$searchText = $this->owner->request->getVar('Search');
|
$searchText = $this->owner->request->getVar('Search');
|
||||||
}
|
}
|
||||||
|
|
||||||
$fields = new FieldSet(
|
$fields = new FieldSet(
|
||||||
new TextField('Search', false, $searchText)
|
new TextField('Search', false, $searchText)
|
||||||
);
|
);
|
||||||
$actions = new FieldSet(
|
$actions = new FieldSet(
|
||||||
new FormAction('results', _t('SearchForm.GO', 'Go'))
|
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.
|
* Process and render search results.
|
||||||
*
|
*
|
||||||
* @param array $data The raw request data submitted by user
|
* @param array $data The raw request data submitted by user
|
||||||
* @param SearchForm $form The form instance that was submitted
|
* @param SearchForm $form The form instance that was submitted
|
||||||
* @param SS_HTTPRequest $request Request generated for this action
|
* @param SS_HTTPRequest $request Request generated for this action
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Provides a simple search engine for your site based on the MySQL FULLTEXT index
|
* Provides a simple search engine for your site based on the MySQL FULLTEXT index
|
||||||
*
|
*
|
||||||
* @package sapphire
|
* @package sapphire
|
||||||
* @subpackage search
|
* @subpackage search
|
||||||
*/
|
*/
|
||||||
class FulltextSearchable extends DataObjectDecorator {
|
class FulltextSearchable extends DataObjectDecorator {
|
||||||
protected $searchFields;
|
protected $searchFields;
|
||||||
|
static $searchableClasses;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable the default configuration of MySQL full-text searching on the given data classes.
|
* 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',
|
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
|
||||||
'File' => 'Filename,Title,Content'
|
'File' => 'Filename,Title,Content'
|
||||||
);
|
);
|
||||||
|
|
||||||
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
|
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
|
||||||
foreach($searchableClasses as $class) {
|
foreach($searchableClasses as $class) {
|
||||||
if(isset($defaultColumns[$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'");
|
throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self::$searchableClasses = $searchableClasses;
|
||||||
|
|
||||||
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct($searchFields) {
|
function __construct($searchFields) {
|
||||||
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
|
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
|
||||||
else $this->searchFields = $searchFields;
|
else $this->searchFields = $searchFields;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
function extraStatics($class = null, $extension = null) {
|
function extraStatics($class = null, $extension = null) {
|
||||||
if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
|
if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
|
||||||
$searchFields = $matches[1];
|
$searchFields = $matches[1];
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'indexes' => array(
|
'indexes' => array(
|
||||||
"SearchFields" => Array(
|
"SearchFields" => Array(
|
||||||
'type'=>'fulltext',
|
'type'=>'fulltext',
|
||||||
'name'=>'SearchFields',
|
'name'=>'SearchFields',
|
||||||
'value'=> $searchFields
|
'value'=> $searchFields
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -78,6 +78,15 @@ class SearchForm extends Form {
|
|||||||
$legalClasses = array_intersect($classes, array('SiteTree', 'File'));
|
$legalClasses = array_intersect($classes, array('SiteTree', 'File'));
|
||||||
$this->classesToSearch = $legalClasses;
|
$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.
|
* 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