Completed
Pull Request — master (#1346)
by Timo
33:05
created

SearchFormViewHelper::getSuggestEidUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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