Passed
Pull Request — master (#1318)
by
unknown
32:46
created

SearchFormViewHelper::render()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 4
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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