1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers; |
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 ApacheSolrForTypo3\Solr\System\Url\UrlHelper; |
18
|
|
|
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility; |
19
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class SearchFormViewHelper |
26
|
|
|
* |
27
|
|
|
* @author Frans Saris <[email protected]> |
28
|
|
|
* @author Timo Hund <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class SearchFormViewHelper extends AbstractSolrFrontendTagBasedViewHelper |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $tagName = 'form'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var TypoScriptFrontendController |
40
|
|
|
*/ |
41
|
|
|
protected $frontendController; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
protected $escapeChildren = true; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $escapeOutput = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor |
55
|
|
|
*/ |
56
|
31 |
|
public function __construct() |
57
|
|
|
{ |
58
|
31 |
|
parent::__construct(); |
59
|
31 |
|
$this->frontendController = $GLOBALS['TSFE'] ?? null; |
60
|
31 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initialize arguments. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
28 |
|
public function initializeArguments() |
68
|
|
|
{ |
69
|
28 |
|
parent::initializeArguments(); |
70
|
28 |
|
$this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted'); |
71
|
28 |
|
$this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)', false, 'get'); |
72
|
28 |
|
$this->registerTagAttribute('name', 'string', 'Name of form'); |
73
|
28 |
|
$this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form'); |
74
|
28 |
|
$this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form'); |
75
|
28 |
|
$this->registerUniversalTagAttributes(); |
76
|
|
|
|
77
|
28 |
|
$this->registerArgument('pageUid', 'integer', 'When not set current page is used', false); |
78
|
28 |
|
$this->registerArgument('additionalFilters', 'array', 'Additional filters', false); |
79
|
28 |
|
$this->registerArgument('additionalParams', 'array', 'Query parameters to be attached to the resulting URI', false, []); |
80
|
28 |
|
$this->registerArgument('pageType', 'integer', 'Type of the target page. See typolink.parameter', false, 0); |
81
|
|
|
|
82
|
28 |
|
$this->registerArgument('noCache', 'boolean', 'Set this to disable caching for the target page. You should not need this.', false, false); |
83
|
28 |
|
$this->registerArgument('section', 'string', 'The anchor to be added to the action URI (only active if $actionUri is not set)', false, ''); |
84
|
28 |
|
$this->registerArgument('absolute', 'boolean', 'If set, the URI of the rendered link is absolute', false, false); |
85
|
28 |
|
$this->registerArgument('addQueryString', 'boolean', 'If set, the current query parameters will be kept in the URI', false, false); |
86
|
28 |
|
$this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []); |
87
|
28 |
|
$this->registerArgument('addQueryStringMethod', 'string', 'Set which parameters will be kept. Only active if $addQueryString = TRUE', false); |
88
|
28 |
|
$this->registerArgument('addSuggestUrl', 'boolean', 'Indicates if suggestUrl should be rendered or not', false, true); |
89
|
28 |
|
$this->registerArgument('suggestHeader', 'string', 'The header for the top results', false, 'Top Results'); |
90
|
28 |
|
$this->registerArgument('suggestPageType', 'integer', 'The page type that should be used for the suggest', false, 7384); |
91
|
|
|
|
92
|
28 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Render search form tag |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
31 |
|
public function render() |
100
|
|
|
{ |
101
|
31 |
|
$pageUid = $this->arguments['pageUid'] ?? null; |
102
|
31 |
|
if ($pageUid === null && !empty($this->getTypoScriptConfiguration()->getSearchTargetPage())) { |
103
|
29 |
|
$pageUid = $this->getTypoScriptConfiguration()->getSearchTargetPage(); |
104
|
|
|
} |
105
|
|
|
|
106
|
31 |
|
$uri = $this->buildUriFromPageUidAndArguments($pageUid); |
107
|
|
|
|
108
|
31 |
|
$this->tag->addAttribute('action', trim($uri)); |
109
|
31 |
|
if (($this->arguments['addSuggestUrl'] ?? null)) { |
110
|
28 |
|
$this->tag->addAttribute('data-suggest', $this->getSuggestUrl($this->arguments['additionalFilters'], $pageUid)); |
111
|
|
|
} |
112
|
31 |
|
$this->tag->addAttribute('data-suggest-header', htmlspecialchars($this->arguments['suggestHeader'] ?? '')); |
113
|
31 |
|
$this->tag->addAttribute('accept-charset', $this->frontendController->metaCharset ?? null); |
114
|
|
|
|
115
|
|
|
// Get search term |
116
|
|
|
// @extensionScannerIgnoreLine |
117
|
31 |
|
$this->getTemplateVariableContainer()->add('q', $this->getQueryString()); |
118
|
|
|
// @extensionScannerIgnoreLine |
119
|
31 |
|
$this->getTemplateVariableContainer()->add('pageUid', $pageUid); |
120
|
|
|
// @extensionScannerIgnoreLine |
121
|
31 |
|
$this->getTemplateVariableContainer()->add('languageUid', Util::getLanguageUid()); |
122
|
|
|
// @extensionScannerIgnoreLine |
123
|
31 |
|
$this->getTemplateVariableContainer()->add('existingParameters', $this->getExistingSearchParameters()); |
124
|
|
|
// @extensionScannerIgnoreLine |
125
|
31 |
|
$this->getTemplateVariableContainer()->add('addPageAndLanguageId', !$this->getIsSiteManagedSite($pageUid)); |
126
|
31 |
|
$formContent = $this->renderChildren(); |
127
|
|
|
// @extensionScannerIgnoreLine |
128
|
31 |
|
$this->getTemplateVariableContainer()->remove('addPageAndLanguageId'); |
129
|
|
|
// @extensionScannerIgnoreLine |
130
|
31 |
|
$this->getTemplateVariableContainer()->remove('q'); |
131
|
|
|
// @extensionScannerIgnoreLine |
132
|
31 |
|
$this->getTemplateVariableContainer()->remove('pageUid'); |
133
|
|
|
// @extensionScannerIgnoreLine |
134
|
31 |
|
$this->getTemplateVariableContainer()->remove('languageUid'); |
135
|
|
|
// @extensionScannerIgnoreLine |
136
|
31 |
|
$this->getTemplateVariableContainer()->remove('existingParameters'); |
137
|
|
|
|
138
|
31 |
|
$this->tag->setContent($formContent); |
139
|
|
|
|
140
|
31 |
|
return $this->tag->render(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the existing search parameters in an array |
145
|
|
|
* Returns an empty array if search.keepExistingParametersForNewSearches is not set |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
31 |
|
protected function getExistingSearchParameters() |
150
|
|
|
{ |
151
|
31 |
|
$searchParameters = []; |
152
|
31 |
|
if ($this->getTypoScriptConfiguration()->getSearchKeepExistingParametersForNewSearches()) { |
153
|
|
|
$arguments = GeneralUtility::_GPmerged($this->getTypoScriptConfiguration()->getSearchPluginNamespace()); |
154
|
|
|
unset($arguments['q'], $arguments['id'], $arguments['L']); |
155
|
|
|
$searchParameters = $this->translateSearchParametersToInputTagAttributes($arguments); |
156
|
|
|
} |
157
|
31 |
|
return $searchParameters; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Translate the multi-dimensional array of existing arguments into a flat array of name-value pairs for the input tags |
162
|
|
|
* |
163
|
|
|
* @param $arguments |
164
|
|
|
* @param string $nameAttributePrefix |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
protected function translateSearchParametersToInputTagAttributes($arguments, $nameAttributePrefix = '') |
168
|
|
|
{ |
169
|
|
|
$attributes = []; |
170
|
|
|
foreach ($arguments as $key => $value) { |
171
|
|
|
$name = $nameAttributePrefix . '[' . $key . ']'; |
172
|
|
|
if (is_array($value)) { |
173
|
|
|
$attributes = array_merge( |
174
|
|
|
$attributes, |
175
|
|
|
$this->translateSearchParametersToInputTagAttributes($value, $name) |
176
|
|
|
); |
177
|
|
|
} else { |
178
|
|
|
$attributes[$name] = $value; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return $attributes; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* When a site is managed with site management the language and the id are encoded in the path segment of the url. |
186
|
|
|
* When no speaking urls are active (e.g. with TYPO3 8 and no realurl) this information is passed as query parameter |
187
|
|
|
* and would get lost when it is only part of the query arguments in the action parameter of the form. |
188
|
|
|
* |
189
|
|
|
* @return boolean |
190
|
|
|
*/ |
191
|
28 |
|
protected function getIsSiteManagedSite($pageId) |
192
|
|
|
{ |
193
|
28 |
|
return SiteUtility::getIsSiteManagedSite($pageId); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return \TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface |
198
|
|
|
*/ |
199
|
28 |
|
protected function getTemplateVariableContainer() |
200
|
|
|
{ |
201
|
28 |
|
return $this->templateVariableContainer; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
31 |
|
protected function getQueryString() |
208
|
|
|
{ |
209
|
31 |
|
$resultSet = $this->getSearchResultSet(); |
210
|
31 |
|
if ($resultSet === null) { |
211
|
4 |
|
return ''; |
212
|
|
|
} |
213
|
27 |
|
return trim($this->getSearchResultSet()->getUsedSearchRequest()->getRawUserQuery()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param NULL|array $additionalFilters |
218
|
|
|
* @param int $pageUid |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
28 |
|
protected function getSuggestUrl($additionalFilters, $pageUid) |
222
|
|
|
{ |
223
|
28 |
|
$uriBuilder = $this->getControllerContext()->getUriBuilder(); |
|
|
|
|
224
|
28 |
|
$pluginNamespace = $this->getTypoScriptConfiguration()->getSearchPluginNamespace(); |
225
|
28 |
|
$suggestUrl = $uriBuilder->reset()->setTargetPageUid($pageUid)->setTargetPageType($this->arguments['suggestPageType'])->setArguments([$pluginNamespace => ['additionalFilters' => $additionalFilters]])->build(); |
226
|
|
|
|
227
|
|
|
/* @var UrlHelper $urlService */ |
228
|
28 |
|
$urlService = GeneralUtility::makeInstance(UrlHelper::class, $suggestUrl); |
229
|
28 |
|
$suggestUrl = $urlService->removeQueryParameter('cHash')->getUrl(); |
|
|
|
|
230
|
|
|
|
231
|
28 |
|
return $suggestUrl; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param int|null $pageUid |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
31 |
|
protected function buildUriFromPageUidAndArguments($pageUid): string |
239
|
|
|
{ |
240
|
31 |
|
$uriBuilder = $this->getControllerContext()->getUriBuilder(); |
|
|
|
|
241
|
31 |
|
$uri = $uriBuilder |
242
|
31 |
|
->reset() |
243
|
31 |
|
->setTargetPageUid($pageUid) |
|
|
|
|
244
|
31 |
|
->setTargetPageType($this->arguments['pageType'] ?? 0) |
245
|
31 |
|
->setNoCache($this->arguments['noCache'] ?? false) |
246
|
31 |
|
->setArguments($this->arguments['additionalParams'] ?? []) |
247
|
31 |
|
->setCreateAbsoluteUri($this->arguments['absolute'] ?? false) |
248
|
31 |
|
->setAddQueryString($this->arguments['addQueryString'] ?? false) |
249
|
31 |
|
->setArgumentsToBeExcludedFromQueryString($this->arguments['argumentsToBeExcludedFromQueryString'] ?? []) |
250
|
31 |
|
->setAddQueryStringMethod($this->arguments['addQueryStringMethod'] ?? '') |
251
|
31 |
|
->setSection($this->arguments['section'] ?? '') |
252
|
31 |
|
->build(); |
253
|
31 |
|
return $uri; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.