Issues (3936)

Classes/Controller/SearchFEController.php (4 issues)

1
<?php
2
namespace EWW\Dpf\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
19
/**
20
 * Class SearchFEController
21
 * @package EWW\Dpf\Controller
22
 * @deprecated since version 4.0
23
 */
24
class SearchFEController extends \EWW\Dpf\Controller\AbstractSearchController
0 ignored issues
show
Deprecated Code introduced by
The class EWW\Dpf\Controller\AbstractSearchController has been deprecated: since version 4.0 ( Ignorable by Annotation )

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

24
class SearchFEController extends /** @scrutinizer ignore-deprecated */ \EWW\Dpf\Controller\AbstractSearchController
Loading history...
25
{
26
    /**
27
     * action
28
     * @var string
29
     */
30
    protected $action;
31
32
    /**
33
     * query
34
     * @var array
35
     */
36
    protected $query;
37
38
    /**
39
     * current page
40
     * @var int
41
     */
42
    protected $currentPage;
43
44
    /**
45
     * type
46
     * @var string
47
     */
48
    protected $type = 'object';
49
50
    /**
51
     * result list
52
     * @var array
53
     */
54
    protected $resultList;
55
56
    /**
57
     * documenTypeRepository
58
     *
59
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
60
     * @TYPO3\CMS\Extbase\Annotation\Inject
61
     */
62
    protected $documentTypeRepository;
63
64
    /**
65
     * __construct
66
     */
67
    public function __construct()
68
    {
69
        // get session data
70
        $session = $this->getSessionData('tx_dpf_frontendsearch');
71
        // get action
72
        $action = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('tx_dpf_frontendsearch')['action'];
73
        // get query
74
        $query = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('tx_dpf_frontendsearch')['query'];
75
        // get current page
76
        $currentPage = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('tx_dpf_frontendsearch')['@widget_0']['currentPage'];
77
78
        // set action
79
        if ($action == 'extendedSearch' ||
80
            ($action != 'search' && (!empty($session) && key($session) == 'extendedSearch'))) {
81
            $this->action = 'extendedSearch';
82
        } else {
83
            $this->action = 'search';
84
        }
85
86
        // set query
87
        if ((!empty($query))) {
88
            $this->query = $this->filterSafelyParameters($query);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->filterSafelyParameters($query) of type string is incompatible with the declared type array of property $query.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
            $this->currentPage = 1;
90
        } else {
91
            // restore query
92
            $this->query = (!empty($session[$this->action]['query'])) ? $session[$this->action]['query'] : array();
93
            // set current page
94
            if ((!empty($currentPage))) {
95
                $this->currentPage = MathUtility::forceIntegerInRange($currentPage, 1);
96
            } elseif (!empty($session[$this->action]['currentPage'])) {
97
                // restore current page
98
                $this->currentPage = MathUtility::forceIntegerInRange($session[$this->action]['currentPage'], 1);
99
            }
100
        }
101
    }
102
103
    /**
104
     * initializes the search action
105
     *
106
     * @return void
107
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
108
     */
109
    public function initializeSearchAction()
110
    {
111
        if ($this->action == 'extendedSearch') {
112
            $this->forward('extendedSearch');
113
        }
114
    }
115
116
    /**
117
     * action search
118
     * @return void
119
     */
120
    public function searchAction()
121
    {
122
        try {
123
            if (!empty($this->query['fulltext'])) {
124
                $query = $this->searchFulltext($this->query['fulltext']);
125
                $this->resultList = $this->getResults($query);
126
                $this->setSession();
127
                $this->viewAssign();
128
            }
129
        } catch (\Exception $exception) {
130
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
131
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
132
            $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
133
134
            $this->addFlashMessage(
135
                $message,
136
                '',
137
                $severity,
138
                true
139
            );
140
        }
141
    }
142
143
    /**
144
     * initializes the extended search action
145
     *
146
     * @return void
147
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
148
     */
149
    public function initializeExtendedSearchAction()
150
    {
151
        if ($this->action == 'search') {
152
            $this->forward('search');
153
        }
154
    }
155
156
    /**
157
     * action extendedSearch
158
     * @return void
159
     */
160
    public function extendedSearchAction()
161
    {
162
        try {
163
            $this->docTypes();
164
            if (!empty(implode('', $this->query))) {
165
                $query = $this->extendedSearch($this->query);
166
                $this->resultList = $this->getResults($query);
167
                $this->setSession();
168
                $this->viewAssign();
169
            }
170
        } catch (\Exception $exception) {
171
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
172
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
173
            $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
174
175
            $this->addFlashMessage(
176
                $message,
177
                '',
178
                $severity,
179
                true
180
            );
181
        }
182
    }
183
184
    /**
185
     * elastic searc
186
     * @param  array $query
187
     * @return array $results
188
     */
189
    private function getResults($query)
190
    {
191
        $size = $this->settings['list']['paginate']['itemsPerPage'];
192
193
        $query['body']['from'] = ($this->currentPage - 1) * $size;
194
        $query['body']['size'] = $size;
195
196
        $resultList = $this->getResultList($query, $this->type);
197
198
        return $resultList;
199
    }
200
201
    /**
202
     * action showSearchForm
203
     * dummy action for showSearchForm Action used in sidebar
204
     * @return void
205
     */
206
    public static function showSearchFormAction()
207
    {
208
209
    }
210
211
    /**
212
     * set session data
213
     * @return void
214
     */
215
    private function setSession()
216
    {
217
        $session[$this->action] = array (
0 ignored issues
show
Comprehensibility Best Practice introduced by
$session was never initialized. Although not strictly required by PHP, it is generally a good practice to add $session = array(); before regardless.
Loading history...
218
                'query'       => $this->query,
219
                'currentPage' => $this->currentPage
220
            );
221
        $this->setSessionData('tx_dpf_frontendsearch', $session);
222
    }
223
224
    /**
225
     * create docTypes
226
     * @return void
227
     */
228
    private function docTypes()
229
    {
230
        // get document types
231
        $allTypes = $this->documentTypeRepository->findAll();
232
        // add empty field
233
        $docTypeArray[0] = ' ';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$docTypeArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $docTypeArray = array(); before regardless.
Loading history...
234
        foreach ($allTypes as $key => $value) {
235
            $docTypeArray[$value->getName()] = $value->getDisplayName();
236
        }
237
        asort($docTypeArray);
238
        $this->view->assign('docTypes', $docTypeArray);
239
    }
240
241
    /**
242
     * create view
243
     * @return void
244
     */
245
    private function viewAssign()
246
    {
247
        $this->view->assign('query',       $this->query);
248
        $this->view->assign('resultList',  $this->resultList);
249
        $this->view->assign('currentPage', $this->currentPage);
250
    }
251
}
252