2018-06-04 15:47:10 +01:00
|
|
|
<?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\DataObject;
|
2018-06-07 14:13:07 +01:00
|
|
|
use SilverStripe\View\ArrayData;
|
2018-06-04 15:47:10 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2018-06-07 14:13:07 +01:00
|
|
|
* @return ArrayData
|
2018-06-04 15:47:10 +01:00
|
|
|
*/
|
|
|
|
public function getResults()
|
|
|
|
{
|
|
|
|
// Get request data from request handler
|
|
|
|
$request = $this->getRequestHandler()->getRequest();
|
|
|
|
|
|
|
|
$searchTerms = $request->requestVar('Search');
|
|
|
|
$query = SearchQuery::create()->addSearchTerm($searchTerms);
|
|
|
|
|
2018-06-07 14:13:07 +01:00
|
|
|
$params = [
|
|
|
|
'spellcheck' => 'true',
|
|
|
|
'spellcheck.collate' => 'true',
|
|
|
|
];
|
|
|
|
|
|
|
|
// Get the first index
|
|
|
|
$indexClasses = FullTextSearch::get_indexes(SolrIndex::class);
|
|
|
|
$indexClass = reset($indexClasses);
|
2018-06-04 15:47:10 +01:00
|
|
|
|
|
|
|
/** @var SolrIndex $index */
|
2018-06-07 14:13:07 +01:00
|
|
|
$index = $indexClass::singleton();
|
|
|
|
$results = $index->search($query, -1, -1, $params);
|
2018-06-04 15:47:10 +01:00
|
|
|
|
|
|
|
// filter by permission
|
|
|
|
if ($results) {
|
2018-06-07 14:13:07 +01:00
|
|
|
foreach ($results->Matches as $match) {
|
|
|
|
/** @var DataObject $match */
|
|
|
|
if (!$match->canView()) {
|
|
|
|
$results->Matches->remove($match);
|
2018-06-04 15:47:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|