-
Notifications
You must be signed in to change notification settings - Fork 83
/
SearchForm.php
109 lines (92 loc) · 3.24 KB
/
SearchForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?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\Search\Services\SearchableService;
use SilverStripe\FullTextSearch\Solr\SolrIndex;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;
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 ArrayData
*/
public function getResults()
{
// Get request data from request handler
$request = $this->getRequestHandler()->getRequest();
$searchTerms = $request->requestVar('Search');
$query = SearchQuery::create()->addSearchTerm($searchTerms);
if ($start = $request->requestVar('start')) {
$query->setStart($start);
}
$params = [
'spellcheck' => 'true',
'spellcheck.collate' => 'true',
];
// Get the first index
$indexClasses = FullTextSearch::get_indexes(SolrIndex::class);
$indexClass = reset($indexClasses);
/** @var SolrIndex $index */
$index = $indexClass::singleton();
$results = $index->search($query, -1, -1, $params);
$searchableService = SearchableService::singleton();
// filter by permission
if ($results) {
foreach ($results->Matches as $match) {
/** @var DataObject $match */
if (!$searchableService->isViewable($match)) {
$results->Matches->remove($match);
}
}
}
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');
}
}