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 TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class SearchFormViewHelper |
22
|
|
|
* |
23
|
|
|
* @author Frans Saris <[email protected]> |
24
|
|
|
* @author Timo Hund <[email protected]> |
25
|
|
|
* @package ApacheSolrForTypo3\Solr\ViewHelpers |
26
|
|
|
*/ |
27
|
|
|
class SearchFormViewHelper extends AbstractSolrFrontendTagBasedViewHelper |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $tagName = 'form'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TypoScriptFrontendController |
37
|
|
|
*/ |
38
|
|
|
protected $frontendController; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $escapeChildren = true; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected $escapeOutput = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor |
52
|
|
|
*/ |
53
|
38 |
|
public function __construct() |
54
|
|
|
{ |
55
|
38 |
|
parent::__construct(); |
56
|
38 |
|
$this->frontendController = $GLOBALS['TSFE']; |
57
|
38 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Initialize arguments. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
35 |
|
public function initializeArguments() |
65
|
|
|
{ |
66
|
35 |
|
parent::initializeArguments(); |
67
|
35 |
|
$this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted'); |
68
|
35 |
|
$this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)', false, 'get'); |
69
|
35 |
|
$this->registerTagAttribute('name', 'string', 'Name of form'); |
70
|
35 |
|
$this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form'); |
71
|
35 |
|
$this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form'); |
72
|
35 |
|
$this->registerUniversalTagAttributes(); |
73
|
|
|
|
74
|
35 |
|
$this->registerArgument('pageUid', 'integer', 'When not set current page is used', false); |
75
|
35 |
|
$this->registerArgument('additionalFilters', 'array', 'Additional filters', false); |
76
|
35 |
|
$this->registerArgument('additionalParams', 'array', 'Query parameters to be attached to the resulting URI', false, []); |
77
|
35 |
|
$this->registerArgument('pageType', 'integer', 'Type of the target page. See typolink.parameter', false, 0); |
78
|
|
|
|
79
|
35 |
|
$this->registerArgument('noCache', 'boolean', 'Set this to disable caching for the target page. You should not need this.', false, false); |
80
|
35 |
|
$this->registerArgument('noCacheHash', 'boolean', 'Set this to supress the cHash query parameter created by TypoLink. You should not need this.', false, false); |
81
|
35 |
|
$this->registerArgument('section', 'string', 'The anchor to be added to the action URI (only active if $actionUri is not set)', false, ''); |
82
|
35 |
|
$this->registerArgument('absolute', 'boolean', 'If set, the URI of the rendered link is absolute', false, false); |
83
|
35 |
|
$this->registerArgument('addQueryString', 'boolean', 'If set, the current query parameters will be kept in the URI', false, false); |
84
|
35 |
|
$this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []); |
85
|
35 |
|
$this->registerArgument('addQueryStringMethod', 'string', 'Set which parameters will be kept. Only active if $addQueryString = TRUE', false); |
86
|
35 |
|
$this->registerArgument('addSuggestUrl', 'boolean', 'Indicates if suggestUrl should be rendered or not', false, true); |
87
|
35 |
|
$this->registerArgument('suggestHeader', 'string', 'The header for the top results', false, 'Top Results'); |
88
|
35 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Render search form tag |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
38 |
|
public function render() |
96
|
|
|
{ |
97
|
38 |
|
$pageUid = $this->arguments['pageUid']; |
98
|
38 |
|
if ($pageUid === null && !empty($this->getTypoScriptConfiguration()->getSearchTargetPage())) { |
99
|
36 |
|
$pageUid = $this->getTypoScriptConfiguration()->getSearchTargetPage(); |
100
|
|
|
} |
101
|
|
|
|
102
|
38 |
|
$uri = $this->buildUriFromPageUidAndArguments($pageUid); |
103
|
|
|
|
104
|
38 |
|
$this->tag->addAttribute('action', trim($uri)); |
105
|
38 |
|
if ($this->arguments['addSuggestUrl']) { |
106
|
35 |
|
$this->tag->addAttribute('data-suggest', $this->getSuggestEidUrl($this->arguments['additionalFilters'], $pageUid)); |
107
|
|
|
} |
108
|
38 |
|
$this->tag->addAttribute('data-suggest-header', htmlspecialchars($this->arguments['suggestHeader'])); |
109
|
38 |
|
$this->tag->addAttribute('accept-charset', $this->frontendController->metaCharset); |
110
|
|
|
|
111
|
|
|
// Get search term |
112
|
38 |
|
$this->getTemplateVariableContainer()->add('q', $this->getQueryString()); |
113
|
38 |
|
$this->getTemplateVariableContainer()->add('pageUid', $pageUid); |
114
|
38 |
|
$this->getTemplateVariableContainer()->add('languageUid', $this->frontendController->sys_language_uid); |
115
|
38 |
|
$formContent = $this->renderChildren(); |
116
|
38 |
|
$this->getTemplateVariableContainer()->remove('q'); |
117
|
38 |
|
$this->getTemplateVariableContainer()->remove('pageUid'); |
118
|
38 |
|
$this->getTemplateVariableContainer()->remove('languageUid'); |
119
|
|
|
|
120
|
38 |
|
$this->tag->setContent($formContent); |
121
|
|
|
|
122
|
38 |
|
return $this->tag->render(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface |
127
|
|
|
*/ |
128
|
35 |
|
protected function getTemplateVariableContainer() |
129
|
|
|
{ |
130
|
35 |
|
return $this->templateVariableContainer; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
38 |
|
protected function getQueryString() |
137
|
|
|
{ |
138
|
38 |
|
$resultSet = $this->getSearchResultSet(); |
139
|
38 |
|
if ($resultSet === null) { |
140
|
5 |
|
return ''; |
141
|
|
|
} |
142
|
33 |
|
return trim($this->getSearchResultSet()->getUsedSearchRequest()->getRawUserQuery()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param NULL|array $additionalFilters |
147
|
|
|
* @param int $pageUid |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
35 |
|
protected function getSuggestEidUrl($additionalFilters, $pageUid) |
151
|
|
|
{ |
152
|
35 |
|
$suggestUrl = $this->frontendController->absRefPrefix; |
153
|
|
|
|
154
|
35 |
|
$suggestUrl .= '?type=7384&id=' . $pageUid; |
155
|
|
|
|
156
|
|
|
// add filters |
157
|
35 |
|
if (!empty($additionalFilters)) { |
158
|
1 |
|
$additionalFilters = json_encode($additionalFilters); |
159
|
1 |
|
$additionalFilters = rawurlencode($additionalFilters); |
160
|
|
|
|
161
|
1 |
|
$suggestUrl .= '&filters=' . $additionalFilters; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// adds the language parameter to the suggest URL |
165
|
35 |
|
if ($this->frontendController->sys_language_uid > 0) { |
166
|
|
|
$suggestUrl .= '&L=' . $this->frontendController->sys_language_uid; |
167
|
|
|
} |
168
|
|
|
|
169
|
35 |
|
return $suggestUrl; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param int|null $pageUid |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
38 |
|
protected function buildUriFromPageUidAndArguments($pageUid): string |
177
|
|
|
{ |
178
|
38 |
|
$uriBuilder = $this->getControllerContext()->getUriBuilder(); |
179
|
38 |
|
$uri = $uriBuilder->reset()->setTargetPageUid($pageUid)->setTargetPageType($this->arguments['pageType'])->setNoCache($this->arguments['noCache'])->setUseCacheHash(!$this->arguments['noCacheHash'])->setArguments($this->arguments['additionalParams'])->setCreateAbsoluteUri($this->arguments['absolute'])->setAddQueryString($this->arguments['addQueryString'])->setArgumentsToBeExcludedFromQueryString($this->arguments['argumentsToBeExcludedFromQueryString'])->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])->setSection($this->arguments['section'])->build(); |
180
|
38 |
|
return $uri; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|