mirror of
https://github.com/silverstripe/silverstripe-fulltextsearch
synced 2024-10-22 12:05:29 +00:00
add default solr contentcontroller extension
piggy-back on default searchform
This commit is contained in:
parent
3a2d1792b5
commit
7b78a841b0
@ -4,3 +4,6 @@ Name: fulltextsearchconfig
|
||||
SilverStripe\ORM\DataObject:
|
||||
extensions:
|
||||
- SilverStripe\FullTextSearch\Search\Extensions\SearchUpdater_ObjectHandler
|
||||
SilverStripe\CMS\Controllers\ContentController:
|
||||
extensions:
|
||||
- SilverStripe\FullTextSearch\Solr\Control\ContentControllerExtension
|
||||
|
@ -46,7 +46,9 @@ SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater:
|
||||
|
||||
## Basic usage
|
||||
|
||||
Basic usage is a four step process:
|
||||
If you have the [CMS module](https://github.com/silverstripe/silverstripe-cms) installed, you will be able to simply add `$SearchForm` to your template to add a Solr search form. Default configuration is added via the [`ContentControllerExtension`](/src/Solr/Control/ContentControllerExtension.php) and replacement [`SearchForm`](/src/Solr/Forms/SearchForm.php)
|
||||
|
||||
Otherwise, basic usage is a four step process:
|
||||
|
||||
1). Define an index in SilverStripe (Note: The specific connector index instance - that's what defines which engine gets used)
|
||||
|
||||
|
60
src/Solr/Control/ContentControllerExtension.php
Normal file
60
src/Solr/Control/ContentControllerExtension.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\FullTextSearch\Solr\Control;
|
||||
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Core\Extension;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
use SilverStripe\Forms\FormAction;
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\FullTextSearch\Solr\Forms\SearchForm;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
|
||||
class ContentControllerExtension extends Extension
|
||||
{
|
||||
private static $allowed_actions = array(
|
||||
'SearchForm',
|
||||
'results',
|
||||
);
|
||||
|
||||
/**
|
||||
* Site search form
|
||||
*
|
||||
* @return SearchForm
|
||||
*/
|
||||
public function SearchForm()
|
||||
{
|
||||
$searchText = _t('SilverStripe\\CMS\\Search\\SearchForm.SEARCH', 'Search');
|
||||
/** @var HTTPRequest $currentRequest */
|
||||
$currentRequest = $this->owner->getRequest();
|
||||
|
||||
if ($currentRequest && $currentRequest->getVar('Search')) {
|
||||
$searchText = $currentRequest->getVar('Search');
|
||||
}
|
||||
|
||||
$fields = FieldList::create(
|
||||
TextField::create('Search', false, $searchText)
|
||||
);
|
||||
$actions = FieldList::create(
|
||||
FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go'))
|
||||
);
|
||||
return SearchForm::create($this->owner, 'SearchForm', $fields, $actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 HTTPRequest $request Request generated for this action
|
||||
*/
|
||||
public function results($data, $form, $request)
|
||||
{
|
||||
$data = [
|
||||
'Results' => $form->getResults(),
|
||||
'Query' => DBField::create_field('Text', $form->getSearchQuery()),
|
||||
'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results')
|
||||
];
|
||||
return $this->owner->customise($data)->renderWith(['Page_results', 'Page']);
|
||||
}
|
||||
}
|
98
src/Solr/Forms/SearchForm.php
Normal file
98
src/Solr/Forms/SearchForm.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\FullTextSearch\Solr\Forms;
|
||||
|
||||
use SilverStripe\Control\RequestHandler;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\FormAction;
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\FullTextSearch\Search\FullTextSearch;
|
||||
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
|
||||
use SilverStripe\FullTextSearch\Solr\SolrIndex;
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
|
||||
class SearchForm extends Form
|
||||
{
|
||||
private static $casting = array(
|
||||
'SearchQuery' => 'Text'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param RequestHandler $controller
|
||||
* @param string $name The name of the form (used in URL addressing)
|
||||
* @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized
|
||||
* if fields are added to the form.
|
||||
* @param FieldList $actions Optional, defaults to a single field named "Go".
|
||||
*/
|
||||
public function __construct(
|
||||
RequestHandler $controller = null,
|
||||
$name = 'SearchForm',
|
||||
FieldList $fields = null,
|
||||
FieldList $actions = null
|
||||
) {
|
||||
if (!$fields) {
|
||||
$fields = FieldList::create(
|
||||
TextField::create('Search', _t(__CLASS__.'.SEARCH', 'Search'))
|
||||
);
|
||||
}
|
||||
|
||||
if (!$actions) {
|
||||
$actions = FieldList::create(
|
||||
FormAction::create("results", _t(__CLASS__.'.GO', 'Go'))
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($controller, $name, $fields, $actions);
|
||||
|
||||
$this->setFormMethod('get');
|
||||
|
||||
$this->disableSecurityToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return dataObjectSet of the results using current request to get info from form.
|
||||
* Simplest implementation loops over all Solr indexes
|
||||
*
|
||||
* @return ArrayList
|
||||
*/
|
||||
public function getResults()
|
||||
{
|
||||
// Get request data from request handler
|
||||
$request = $this->getRequestHandler()->getRequest();
|
||||
|
||||
$searchTerms = $request->requestVar('Search');
|
||||
$query = SearchQuery::create()->addSearchTerm($searchTerms);
|
||||
|
||||
$indexes = FullTextSearch::get_indexes(SolrIndex::class);
|
||||
$results = ArrayList::create();
|
||||
|
||||
/** @var SolrIndex $index */
|
||||
foreach ($indexes as $index) {
|
||||
$results->merge($index->search($query)->Matches);
|
||||
}
|
||||
|
||||
// filter by permission
|
||||
if ($results) {
|
||||
/** @var DataObject $result */
|
||||
foreach ($results as $result) {
|
||||
if (!$result->canView()) {
|
||||
$results->remove($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the search query for display in a "You searched for ..." sentence.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSearchQuery()
|
||||
{
|
||||
return $this->getRequestHandler()->getRequest()->requestVar('Search');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user