Completed
Pull Request — master (#216)
by Andrew
30:41 queued 28:07
created

SearchForm::getResults()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 0
dl 0
loc 27
rs 8.439
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\ArrayList;
14
use SilverStripe\ORM\DataObject;
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 ArrayList
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
        $indexes = FullTextSearch::get_indexes(SolrIndex::class);
69
        $results = ArrayList::create();
70
71
        /** @var SolrIndex $index */
72
        foreach ($indexes as $index) {
73
            $results->merge($index->search($query)->Matches);
74
        }
75
76
        // filter by permission
77
        if ($results) {
78
            /** @var DataObject $result */
79
            foreach ($results as $result) {
80
                if (!$result->canView()) {
81
                    $results->remove($result);
82
                }
83
            }
84
        }
85
86
        return $results;
87
    }
88
89
    /**
90
     * Get the search query for display in a "You searched for ..." sentence.
91
     *
92
     * @return string
93
     */
94
    public function getSearchQuery()
95
    {
96
        return $this->getRequestHandler()->getRequest()->requestVar('Search');
97
    }
98
}
99