Passed
Pull Request — master (#5)
by Gordon
01:54
created

SearchPage::setSearchResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 25/3/2561
7
 * Time: 17:01 น.
8
 */
9
10
namespace Suilven\FreeTextSearch\Page;
11
12
use SilverStripe\Forms\CheckboxField;
13
use SilverStripe\Forms\DropdownField;
14
use SilverStripe\Forms\NumericField;
15
use SilverStripe\Forms\TextField;
16
use Suilven\FreeTextSearch\Indexes;
17
18
/**
19
 * Class SearchPage
20
 *
21
 * @package Suilven\FreeTextSearch\Page
22
 * @property string $IndexToSearch - the name of the index to search, defaults to SiteTree
23
 * @property int $PageSize the number of results to show on each page
24
 * @property bool $ShowAllIfEmptyQuery - show all or no results for an empty query
25
 * @property string $ShowTagCloudFor - show a tag cloud
26
 * @phpstan-ignore-next-line
27
 */
28
class SearchPage extends \Page
29
{
30
    /** @var \Suilven\FreeTextSearch\Page\SearchResults */
0 ignored issues
show
Bug introduced by
The type Suilven\FreeTextSearch\Page\SearchResults was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
    private $searchResults;
32
33
    private static $table_name = 'SearchPage';
34
35
    /** @var array database fields */
36
    private static $db = [
37
        // fields to return facets for, stored as JSON array
38
        // 'FacetFields' => 'Varchar(255)',
39
40
        // a permanent filter for this page, not user selectable.  Use case here is to restrict to likes of searching
41
        // within a specific blog only
42
        'IndexToSearch' => 'Varchar(255)',
43
44
        // page size
45
        'PageSize' => 'Int',
46
47
        // show all results if the search page has facets (optionally)
48
        'ShowAllIfEmptyQuery' => 'Boolean',
49
50
        // show all results if the search page has facets (optionally)
51
        'ShowTagCloudFor' => 'Varchar',
52
    ];
53
54
    private static $defaults = [
55
        'IndexToSearch' => 'sitetree',
56
    ];
57
58
    /**
59
     * Accessor to the search results object
60
     */
61
    public function getSearchResults(): SearchResults
62
    {
63
        return $this->searchResults;
64
    }
65
66
67
    public function setSearchResults(SearchResults $newSearchResults): void
68
    {
69
        $this->searchResults = $newSearchResults;
70
    }
71
72
73
    /**
74
     * Get the fields to facet on
75
     *
76
     * @return array<string>
77
     */
78
    public function getFacetFields(): array
79
    {
80
        $indexesService = new Indexes();
81
82
        return $indexesService->getFacetFields($this->IndexToSearch);
83
    }
84
85
86
    /**
87
     * Get the has many fields
88
     *
89
     * @return array<string>
90
     */
91
    public function getHasManyFields(): array
92
    {
93
        $indexesService = new Indexes();
94
95
        return $indexesService->getHasManyFields($this->IndexToSearch);
96
    }
97
98
99
    public function getCMSFields(): \SilverStripe\Forms\FieldList
100
    {
101
        $fields = parent::getCMSFields();
102
        $indexesService = new Indexes();
103
        $indexes = $indexesService->getIndexes();
104
        $indexNames = [];
105
        foreach ($indexes as $index) {
106
            $indexNames[$index->getName()] = $index->getName();
107
        }
108
109
        $fields->addFieldToTab('Root.Index', DropdownField::create(
110
            'IndexToSearch',
111
            'Index to Search',
112
            $indexNames
113
        ));
114
115
        $fields->addFieldToTab('Root.Index', NumericField::create('PageSize', 'Number of Results Per Page'));
116
117
        $fields->addFieldToTab('Root.Index', TextField::create(
118
            'ShowTagCloudFor',
119
            'Show a tag cloud for the named facet'
120
        ));
121
122
        $fields->addFieldToTab('Root.Index', CheckboxField::create(
123
            'ShowAllIfEmptyQuery',
124
            'By default no results are shown for an empty query.  However for facets an empty query should still ' .
125
            'provide for a drill down scenario'
126
        ));
127
128
        return $fields;
129
    }
130
}
131