Passed
Pull Request — master (#216)
by Andrew
02:29
created

SearchForm::getSearchQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
The private property $casting is not used, and could be removed.
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
        $params = [
69
            'spellcheck' => 'true',
70
            'spellcheck.collate' => 'true',
71
        ];
72
73
        // Get the first index
74
        $indexClasses = FullTextSearch::get_indexes(SolrIndex::class);
75
        $indexClass = reset($indexClasses);
76
77
        /** @var SolrIndex $index */
78
        $index = $indexClass::singleton();
79
        $results = $index->search($query, -1, -1, $params);
80
81
        // filter by permission
82
        if ($results) {
0 ignored issues
show
introduced by
$results is of type SilverStripe\View\ArrayData, thus it always evaluated to true.
Loading history...
83
            foreach ($results->Matches as $match) {
84
                /** @var DataObject $match */
85
                if (!$match->canView()) {
86
                    $results->Matches->remove($match);
87
                }
88
            }
89
        }
90
        return $results;
91
    }
92
93
    /**
94
     * Get the search query for display in a "You searched for ..." sentence.
95
     *
96
     * @return string
97
     */
98
    public function getSearchQuery()
99
    {
100
        return $this->getRequestHandler()->getRequest()->requestVar('Search');
101
    }
102
}
103