Passed
Push — master ( f776da...039c82 )
by Timo
24:57
created

SearchFormViewHelper::getIsSiteManagedSite()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.1971

Importance

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

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