1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Solr\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\RequestHandler; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\Form; |
8
|
|
|
use SilverStripe\Forms\FormAction; |
9
|
|
|
use SilverStripe\Forms\TextField; |
10
|
|
|
use SilverStripe\FullTextSearch\Search\FullTextSearch; |
11
|
|
|
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery; |
12
|
|
|
use SilverStripe\FullTextSearch\Search\Services\IndexableService; |
13
|
|
|
use SilverStripe\FullTextSearch\Solr\SolrIndex; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
use SilverStripe\View\ArrayData; |
16
|
|
|
|
17
|
|
|
class SearchForm extends Form |
18
|
|
|
{ |
19
|
|
|
private static $casting = array( |
|
|
|
|
20
|
|
|
'SearchQuery' => 'Text' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param RequestHandler $controller |
25
|
|
|
* @param string $name The name of the form (used in URL addressing) |
26
|
|
|
* @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized |
27
|
|
|
* if fields are added to the form. |
28
|
|
|
* @param FieldList $actions Optional, defaults to a single field named "Go". |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
RequestHandler $controller = null, |
32
|
|
|
$name = 'SearchForm', |
33
|
|
|
FieldList $fields = null, |
34
|
|
|
FieldList $actions = null |
35
|
|
|
) { |
36
|
|
|
if (!$fields) { |
37
|
|
|
$fields = FieldList::create( |
38
|
|
|
TextField::create('Search', _t(__CLASS__.'.SEARCH', 'Search')) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!$actions) { |
43
|
|
|
$actions = FieldList::create( |
44
|
|
|
FormAction::create("results", _t(__CLASS__.'.GO', 'Go')) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
49
|
|
|
|
50
|
|
|
$this->setFormMethod('get'); |
51
|
|
|
|
52
|
|
|
$this->disableSecurityToken(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return dataObjectSet of the results using current request to get info from form. |
57
|
|
|
* Simplest implementation loops over all Solr indexes |
58
|
|
|
* |
59
|
|
|
* @return ArrayData |
60
|
|
|
*/ |
61
|
|
|
public function getResults() |
62
|
|
|
{ |
63
|
|
|
// Get request data from request handler |
64
|
|
|
$request = $this->getRequestHandler()->getRequest(); |
65
|
|
|
|
66
|
|
|
$searchTerms = $request->requestVar('Search'); |
67
|
|
|
$query = SearchQuery::create()->addSearchTerm($searchTerms); |
68
|
|
|
|
69
|
|
|
if ($start = $request->requestVar('start')) { |
70
|
|
|
$query->setStart($start); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$params = [ |
74
|
|
|
'spellcheck' => 'true', |
75
|
|
|
'spellcheck.collate' => 'true', |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
// Get the first index |
79
|
|
|
$indexClasses = FullTextSearch::get_indexes(SolrIndex::class); |
80
|
|
|
$indexClass = reset($indexClasses); |
81
|
|
|
|
82
|
|
|
/** @var SolrIndex $index */ |
83
|
|
|
$index = $indexClass::singleton(); |
84
|
|
|
$results = $index->search($query, -1, -1, $params); |
85
|
|
|
|
86
|
|
|
$indexableService = IndexableService::singleton(); |
87
|
|
|
|
88
|
|
|
// filter by permission |
89
|
|
|
if ($results) { |
|
|
|
|
90
|
|
|
foreach ($results->Matches as $match) { |
91
|
|
|
/** @var DataObject $match */ |
92
|
|
|
if (!$indexableService->isIndexable($match)) { |
93
|
|
|
$results->Matches->remove($match); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return $results; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the search query for display in a "You searched for ..." sentence. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getSearchQuery() |
106
|
|
|
{ |
107
|
|
|
return $this->getRequestHandler()->getRequest()->requestVar('Search'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|