Passed
Push — master ( 5f60a5...391298 )
by Timo
24:49
created

SearchFormViewHelper::initializeArguments()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 21
cts 22
cp 0.9545
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
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 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 35
    public function __construct()
54
    {
55 35
        parent::__construct();
56 35
        $this->frontendController = $GLOBALS['TSFE'];
57 35
    }
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 35
    public function render()
96
    {
97 35
        $pageUid = $this->arguments['pageUid'];
98 35
        if ($pageUid === null && !empty($this->getTypoScriptConfiguration()->getSearchTargetPage())) {
99 35
            $pageUid = $this->getTypoScriptConfiguration()->getSearchTargetPage();
100
        }
101
102 35
        $uri = $this->buildUriFromArguments();
103
104 35
        $this->tag->addAttribute('action', trim($uri));
105 35
        if ($this->arguments['addSuggestUrl']) {
106 35
            $this->tag->addAttribute('data-suggest', $this->getSuggestEidUrl($this->arguments['additionalFilters'], $pageUid));
107
        }
108 35
        $this->tag->addAttribute('data-suggest-header', htmlspecialchars($this->arguments['suggestHeader']));
109 35
        $this->tag->addAttribute('accept-charset', $this->frontendController->metaCharset);
110
111
        // Get search term
112 35
        $this->templateVariableContainer->add('q', $this->getQueryString());
113 35
        $this->templateVariableContainer->add('pageUid', $pageUid);
114 35
        $this->templateVariableContainer->add('languageUid', $this->frontendController->sys_language_uid);
115 35
        $formContent = $this->renderChildren();
116 35
        $this->templateVariableContainer->remove('q');
117 35
        $this->templateVariableContainer->remove('pageUid');
118 35
        $this->templateVariableContainer->remove('languageUid');
119
120 35
        $this->tag->setContent($formContent);
121
122 35
        return $this->tag->render();
123
    }
124
125
    /**
126
     * @return string
127
     */
128 35
    protected function getQueryString()
129
    {
130 35
        $resultSet = $this->getSearchResultSet();
131 35
        if ($resultSet === null) {
132 2
            return '';
133
        }
134 33
        return trim($this->getSearchResultSet()->getUsedSearchRequest()->getRawUserQuery());
135
    }
136
137
    /**
138
     * @param NULL|array $additionalFilters
139
     * @param int $pageUid
140
     * @return string
141
     */
142 35
    protected function getSuggestEidUrl($additionalFilters, $pageUid)
143
    {
144 35
        $suggestUrl = $this->frontendController->absRefPrefix;
145
146 35
        $suggestUrl .= '?type=7384&id=' . $pageUid;
147
148
        // add filters
149 35
        if (!empty($additionalFilters)) {
150 1
            $additionalFilters = json_encode($additionalFilters);
151 1
            $additionalFilters = rawurlencode($additionalFilters);
152
153 1
            $suggestUrl .= '&filters=' . $additionalFilters;
154
        }
155
156
        // adds the language parameter to the suggest URL
157 35
        if ($this->frontendController->sys_language_uid > 0) {
158
            $suggestUrl .= '&L=' . $this->frontendController->sys_language_uid;
159
        }
160
161 35
        return $suggestUrl;
162
    }
163
164
    /**
165
     * @return string
166
     */
167 35
    protected function buildUriFromArguments(): string
168
    {
169 35
        $uriBuilder = $this->getControllerContext()->getUriBuilder();
170 35
        $uri = $uriBuilder->reset()->setTargetPageUid($this->arguments['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();
171 35
        return $uri;
172
    }
173
}
174