|
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 |
|
$this->registerArgument('suggestPageType', 'integer', 'The page type that should be used for the suggest', false, 7384); |
|
89
|
|
|
|
|
90
|
35 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Render search form tag |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
38 |
|
public function render() |
|
98
|
|
|
{ |
|
99
|
38 |
|
$pageUid = $this->arguments['pageUid']; |
|
100
|
38 |
|
if ($pageUid === null && !empty($this->getTypoScriptConfiguration()->getSearchTargetPage())) { |
|
101
|
36 |
|
$pageUid = $this->getTypoScriptConfiguration()->getSearchTargetPage(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
38 |
|
$uri = $this->buildUriFromPageUidAndArguments($pageUid); |
|
105
|
|
|
|
|
106
|
38 |
|
$this->tag->addAttribute('action', trim($uri)); |
|
107
|
38 |
|
if ($this->arguments['addSuggestUrl']) { |
|
108
|
35 |
|
$this->tag->addAttribute('data-suggest', $this->getSuggestUrl($this->arguments['additionalFilters'], $pageUid)); |
|
109
|
|
|
} |
|
110
|
38 |
|
$this->tag->addAttribute('data-suggest-header', htmlspecialchars($this->arguments['suggestHeader'])); |
|
111
|
38 |
|
$this->tag->addAttribute('accept-charset', $this->frontendController->metaCharset); |
|
112
|
|
|
|
|
113
|
|
|
// Get search term |
|
114
|
38 |
|
$this->getTemplateVariableContainer()->add('q', $this->getQueryString()); |
|
115
|
38 |
|
$this->getTemplateVariableContainer()->add('pageUid', $pageUid); |
|
116
|
38 |
|
$this->getTemplateVariableContainer()->add('languageUid', $this->frontendController->sys_language_uid); |
|
117
|
38 |
|
$formContent = $this->renderChildren(); |
|
118
|
38 |
|
$this->getTemplateVariableContainer()->remove('q'); |
|
119
|
38 |
|
$this->getTemplateVariableContainer()->remove('pageUid'); |
|
120
|
38 |
|
$this->getTemplateVariableContainer()->remove('languageUid'); |
|
121
|
|
|
|
|
122
|
38 |
|
$this->tag->setContent($formContent); |
|
123
|
|
|
|
|
124
|
38 |
|
return $this->tag->render(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return \TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface |
|
129
|
|
|
*/ |
|
130
|
35 |
|
protected function getTemplateVariableContainer() |
|
131
|
|
|
{ |
|
132
|
35 |
|
return $this->templateVariableContainer; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
38 |
|
protected function getQueryString() |
|
139
|
|
|
{ |
|
140
|
38 |
|
$resultSet = $this->getSearchResultSet(); |
|
141
|
38 |
|
if ($resultSet === null) { |
|
142
|
5 |
|
return ''; |
|
143
|
|
|
} |
|
144
|
33 |
|
return trim($this->getSearchResultSet()->getUsedSearchRequest()->getRawUserQuery()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param NULL|array $additionalFilters |
|
149
|
|
|
* @param int $pageUid |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
35 |
|
protected function getSuggestUrl($additionalFilters, $pageUid) |
|
153
|
|
|
{ |
|
154
|
35 |
|
$uriBuilder = $this->getControllerContext()->getUriBuilder(); |
|
155
|
35 |
|
$pluginNamespace = $this->getTypoScriptConfiguration()->getSearchPluginNamespace(); |
|
156
|
35 |
|
$suggestUrl = $uriBuilder->reset()->setTargetPageUid($pageUid)->setTargetPageType($this->arguments['suggestPageType'])->setUseCacheHash(false)->setArguments([$pluginNamespace => ['additionalFilters' => $additionalFilters]])->build(); |
|
157
|
35 |
|
return $suggestUrl; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param int|null $pageUid |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
38 |
|
protected function buildUriFromPageUidAndArguments($pageUid): string |
|
165
|
|
|
{ |
|
166
|
38 |
|
$uriBuilder = $this->getControllerContext()->getUriBuilder(); |
|
167
|
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(); |
|
168
|
38 |
|
return $uri; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|