Completed
Push — master ( 686084...a2bb6b )
by
unknown
9s
created

SearchControllerExtension::getResultsTemplate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace CWP\Search\Extensions;
4
5
use CWP\Search\CwpSearchEngine;
6
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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...
7
use SilverStripe\CMS\Search\SearchForm;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\Core\Extension;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\FormAction;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\ORM\FieldType\DBHTMLText;
16
use SilverStripe\View\ViewableData_Customised;
17
18
/**
19
 * Adds a search form and handling method to a {@link Controller} instance, using the configured CWP search engine
20
 */
21
class SearchControllerExtension extends Extension
22
{
23
    use Configurable;
24
25
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
26
        'SearchForm',
27
        'results',
28
    ];
29
30
    /**
31
     * How many search results should be shown per-page?
32
     *
33
     * @config
34
     * @var int
35
     */
36
    private static $results_per_page = 10;
0 ignored issues
show
introduced by
The private property $results_per_page is not used, and could be removed.
Loading history...
37
38
    /**
39
     * If spelling suggestions for searches are given, enable
40
     * suggested searches to be followed immediately
41
     *
42
     * @config
43
     * @var bool
44
     */
45
    private static $search_follow_suggestions = true;
0 ignored issues
show
introduced by
The private property $search_follow_suggestions is not used, and could be removed.
Loading history...
46
47
    /**
48
     * Which classes should be queried when searching?
49
     *
50
     * @config
51
     * @var array
52
     */
53
    private static $classes_to_search = [
0 ignored issues
show
introduced by
The private property $classes_to_search is not used, and could be removed.
Loading history...
54
        [
55
            'class' => 'Page',
56
            'includeSubclasses' => true,
57
        ]
58
    ];
59
60
    /**
61
     * Site search form
62
     */
63
    public function SearchForm()
64
    {
65
        $searchText = $this->owner->getRequest()->getVar('Search');
66
67
        $fields = FieldList::create(
68
            TextField::create('Search', false, $searchText)
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            TextField::create('Search', /** @scrutinizer ignore-type */ false, $searchText)
Loading history...
Bug introduced by
'Search' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            TextField::create(/** @scrutinizer ignore-type */ 'Search', false, $searchText)
Loading history...
69
        );
70
        $actions = FieldList::create(
71
            FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go'))
72
        );
73
74
        $form = SearchForm::create($this->owner, SearchForm::class, $fields, $actions);
0 ignored issues
show
Bug introduced by
$this->owner of type object is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $form = SearchForm::create(/** @scrutinizer ignore-type */ $this->owner, SearchForm::class, $fields, $actions);
Loading history...
75
        $form->setFormAction('search/SearchForm');
76
77
        return $form;
78
    }
79
80
    /**
81
     * Get search form with _header suffix
82
     *
83
     * @return SearchForm
84
     */
85
    public function HeaderSearchForm()
86
    {
87
        return $this->SearchForm()->setTemplate('SearchForm_header');
88
    }
89
90
    /**
91
     * Process and render search results.
92
     *
93
     * @param array $data The raw request data submitted by user
94
     * @param SearchForm $form The form instance that was submitted
95
     * @param HTTPRequest $request Request generated for this action
96
     * @return DBHTMLText
97
     */
98
    public function results($data, $form, $request)
99
    {
100
        // Check parameters for terms, pagination, and if we should follow suggestions
101
        $keywords = empty($data['Search']) ? '' : $data['Search'];
102
        $start = isset($data['start']) ? $data['start'] : 0;
103
        $suggestions = isset($data['suggestions'])
104
            ? $data['suggestions']
105
            : $this->config()->get('search_follow_suggestions');
106
107
        $results = CwpSearchEngine::create()
108
            ->search(
109
                $keywords,
110
                $this->config()->get('classes_to_search'),
111
                Injector::inst()->get(CwpSearchEngine::class . '.search_index'),
112
                $this->config()->get('results_per_page'),
113
                $start,
114
                $suggestions
115
            );
116
117
        // Customise content with these results
118
        $properties = [
119
            'MetaTitle' => _t(__CLASS__ . '.MetaTitle', 'Search {keywords}', ['keywords' => $keywords]),
120
            'NoSearchResults' => _t(__CLASS__ . '.NoResult', 'Sorry, your search query did not return any results.'),
121
            'EmptySearch' => _t(__CLASS__ . '.EmptySearch', 'Search field empty, please enter your search query.'),
122
            'PdfLink' => '',
123
            'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results'),
124
        ];
125
        $this->owner->extend('updateSearchResults', $results, $properties);
126
127
        // Customise page
128
        /** @var ViewableData_Customised $response */
129
        $response = $this->owner->customise($properties);
130
        if ($results) {
131
            $response = $response
132
                ->customise($results)
133
                ->customise(['Results' => $results->getResults()]);
134
        }
135
136
        // Render
137
        $templates = $this->getResultsTemplate($request);
138
        return $response->renderWith($templates);
139
    }
140
141
    /**
142
     * Select the template to render search results with
143
     *
144
     * @param HTTPRequest $request
145
     * @return array
146
     */
147
    protected function getResultsTemplate($request)
148
    {
149
        $templates = [
150
            Page::class . '_results',
151
            Page::class
152
        ];
153
154
        if ($request->getVar('format') == 'rss') {
155
            array_unshift($templates, Page::class . '_results_rss');
156
        }
157
158
        if ($request->getVar('format') == 'atom') {
159
            array_unshift($templates, Page::class . '_results_atom');
160
        }
161
162
        return $templates;
163
    }
164
}
165