Passed
Push — master ( 151fd1...083614 )
by Timo
25:11
created

buildUriFromPageUidAndArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
21
22
23
/**
24
 * Class SearchFormViewHelper
25
 *
26
 * @author Frans Saris <[email protected]>
27
 * @author Timo Hund <[email protected]>
28
 * @package ApacheSolrForTypo3\Solr\ViewHelpers
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 39
    public function __construct()
57
    {
58 39
        parent::__construct();
59 39
        $this->frontendController = $GLOBALS['TSFE'];
60 39
    }
61
62
    /**
63
     * Initialize arguments.
64
     *
65
     * @return void
66
     */
67 36
    public function initializeArguments()
68
    {
69 36
        parent::initializeArguments();
70 36
        $this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted');
71 36
        $this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)', false, 'get');
72 36
        $this->registerTagAttribute('name', 'string', 'Name of form');
73 36
        $this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form');
74 36
        $this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form');
75 36
        $this->registerUniversalTagAttributes();
76
77 36
        $this->registerArgument('pageUid', 'integer', 'When not set current page is used', false);
78 36
        $this->registerArgument('additionalFilters', 'array', 'Additional filters', false);
79 36
        $this->registerArgument('additionalParams', 'array', 'Query parameters to be attached to the resulting URI', false, []);
80 36
        $this->registerArgument('pageType', 'integer', 'Type of the target page. See typolink.parameter', false, 0);
81
82 36
        $this->registerArgument('noCache', 'boolean', 'Set this to disable caching for the target page. You should not need this.', false, false);
83 36
        $this->registerArgument('noCacheHash', 'boolean', 'Set this to supress the cHash query parameter created by TypoLink. You should not need this.', false, false);
84 36
        $this->registerArgument('section', 'string', 'The anchor to be added to the action URI (only active if $actionUri is not set)', false, '');
85 36
        $this->registerArgument('absolute', 'boolean', 'If set, the URI of the rendered link is absolute', false, false);
86 36
        $this->registerArgument('addQueryString', 'boolean', 'If set, the current query parameters will be kept in the URI', false, false);
87 36
        $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []);
88 36
        $this->registerArgument('addQueryStringMethod', 'string', 'Set which parameters will be kept. Only active if $addQueryString = TRUE', false);
89 36
        $this->registerArgument('addSuggestUrl', 'boolean', 'Indicates if suggestUrl should be rendered or not', false, true);
90 36
        $this->registerArgument('suggestHeader', 'string', 'The header for the top results', false, 'Top Results');
91 36
        $this->registerArgument('suggestPageType', 'integer', 'The page type that should be used for the suggest', false, 7384);
92
93 36
    }
94
95
    /**
96
     * Render search form tag
97
     *
98
     * @return string
99
     */
100 39
    public function render()
101
    {
102 39
        $pageUid = $this->arguments['pageUid'];
103 39
        if ($pageUid === null && !empty($this->getTypoScriptConfiguration()->getSearchTargetPage())) {
104 37
            $pageUid = $this->getTypoScriptConfiguration()->getSearchTargetPage();
105
        }
106
107 39
        $uri = $this->buildUriFromPageUidAndArguments($pageUid);
108
109 39
        $this->tag->addAttribute('action', trim($uri));
110 39
        if ($this->arguments['addSuggestUrl']) {
111 36
            $this->tag->addAttribute('data-suggest', $this->getSuggestUrl($this->arguments['additionalFilters'], $pageUid));
112
        }
113 39
        $this->tag->addAttribute('data-suggest-header', htmlspecialchars($this->arguments['suggestHeader']));
114 39
        $this->tag->addAttribute('accept-charset', $this->frontendController->metaCharset);
115
116
        // Get search term
117 39
        $this->getTemplateVariableContainer()->add('q', $this->getQueryString());
118 39
        $this->getTemplateVariableContainer()->add('pageUid', $pageUid);
119 39
        $this->getTemplateVariableContainer()->add('languageUid', $this->frontendController->sys_language_uid);
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

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

119
        $this->getTemplateVariableContainer()->add('languageUid', /** @scrutinizer ignore-deprecated */ $this->frontendController->sys_language_uid);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
120
121 39
        $this->getTemplateVariableContainer()->add('addPageAndLanguageId', !$this->getIsSiteManagedSite($pageUid));
122 39
        $formContent = $this->renderChildren();
123 39
        $this->getTemplateVariableContainer()->remove('addPageAndLanguageId');
124 39
        $this->getTemplateVariableContainer()->remove('q');
125 39
        $this->getTemplateVariableContainer()->remove('pageUid');
126 39
        $this->getTemplateVariableContainer()->remove('languageUid');
127
128 39
        $this->tag->setContent($formContent);
129
130 39
        return $this->tag->render();
131
    }
132
133
    /**
134
     * When a site is managed with site management the language and the id are encoded in the path segment of the url.
135
     * When no speaking urls are active (e.g. with TYPO3 8 and no realurl) this information is passed as query parameter
136
     * and would get lost when it is only part of the query arguments in the action parameter of the form.
137
     *
138
     * @return boolean
139
     */
140 36
    protected function getIsSiteManagedSite($pageId)
141
    {
142 36
        return SiteUtility::getIsSiteManagedSite($pageId);
143
    }
144
145
    /**
146
     * @return \TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface
147
     */
148 36
    protected function getTemplateVariableContainer()
149
    {
150 36
        return $this->templateVariableContainer;
151
    }
152
153
    /**
154
     * @return string
155
     */
156 39
    protected function getQueryString()
157
    {
158 39
        $resultSet = $this->getSearchResultSet();
159 39
        if ($resultSet === null) {
160 5
            return '';
161
        }
162 34
        return trim($this->getSearchResultSet()->getUsedSearchRequest()->getRawUserQuery());
163
    }
164
165
    /**
166
     * @param NULL|array $additionalFilters
167
     * @param int $pageUid
168
     * @return string
169
     */
170 36
    protected function getSuggestUrl($additionalFilters, $pageUid)
171
    {
172 36
        $uriBuilder = $this->getControllerContext()->getUriBuilder();
173 36
        $pluginNamespace = $this->getTypoScriptConfiguration()->getSearchPluginNamespace();
174 36
        $suggestUrl = $uriBuilder->reset()->setTargetPageUid($pageUid)->setTargetPageType($this->arguments['suggestPageType'])->setUseCacheHash(false)->setArguments([$pluginNamespace => ['additionalFilters' => $additionalFilters]])->build();
175
176 36
        $urlService = GeneralUtility::makeInstance(UrlHelper::class, $suggestUrl);
177 36
        $suggestUrl = $urlService->removeQueryParameter('cHash')->getUrl();
178
179 36
        return $suggestUrl;
180
    }
181
182
    /**
183
     * @param int|null $pageUid
184
     * @return string
185
     */
186 39
    protected function buildUriFromPageUidAndArguments($pageUid): string
187
    {
188 39
        $uriBuilder = $this->getControllerContext()->getUriBuilder();
189 39
        $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();
190 39
        return $uri;
191
    }
192
}
193