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