Passed
Push — master ( b39217...9ad7ee )
by Sascha
01:01
created

LinkBuilder::addUnwantedUrlParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Query;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Query;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\Util;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
32
33
/**
34
 * Link builder for queries.
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class LinkBuilder
39
{
40
41
    /**
42
     * Content object.
43
     *
44
     * @var ContentObjectRenderer
45
     */
46
    protected $contentObject;
47
48
    /**
49
     * Solr configuration.
50
     *
51
     * @var TypoScriptConfiguration
52
     */
53
    protected $solrConfiguration;
54
55
    /**
56
     * Solr query. This query's parameters are used in URL parameters.
57
     *
58
     * @var Query
59
     */
60
    protected $query = null;
61
62
    /**
63
     * URL GET parameter prefix
64
     *
65
     * @var string
66
     */
67
    protected $prefix = 'tx_solr';
68
69
    /**
70
     * Link target page ID.
71
     *
72
     * @var int
73
     */
74
    protected $linkTargetPageId;
75
76
    /**
77
     * Additional URL parameters applicable to all URLs
78
     *
79
     * @var string
80
     */
81
    protected $urlParameters = '';
82
83
    /**
84
     * Parameters we do not want to appear in the URL, usually for esthetic
85
     * reasons.
86
     *
87
     * @var array
88
     */
89
    protected $unwantedUrlParameters = ['resultsPerPage', 'page'];
90
91
    /**
92
     * Constructor.
93
     *
94
     * @param Query $query Solr query
95
     */
96 28
    public function __construct(Query $query)
97
    {
98 28
        $this->solrConfiguration = Util::getSolrConfiguration();
99 28
        $this->contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
100 28
        $this->query = $query;
101
102 28
        $targetPageUid = $this->contentObject->stdWrap(
103 28
            $this->solrConfiguration->getSearchTargetPage(),
104 28
            $this->solrConfiguration->getSearchTargetPageConfiguration()
105
        );
106 28
        $this->linkTargetPageId = $targetPageUid;
107 28
    }
108
109
    /**
110
     * Gets the target page Id for links.
111
     *
112
     * @return int Page Id links are going to point to.
113
     */
114
    public function getLinkTargetPageId()
115
    {
116
        return $this->linkTargetPageId;
117
    }
118
119
    /**
120
     * Sets the target page Id for links
121
     *
122
     * @param int $pageId Page Id links shall point to.
123
     */
124 19
    public function setLinkTargetPageId($pageId)
125
    {
126 19
        $this->linkTargetPageId = intval($pageId);
127 19
    }
128
129
    /**
130
     * Adds general URL GET parameters.
131
     *
132
     * @param string $urlParameters URL GET parameters.
133
     */
134 18
    public function addUrlParameters($urlParameters)
135
    {
136 18
        if ($urlParameters[0] != '&') {
137
            $urlParameters = '&' . $urlParameters;
138
        }
139
140 18
        $this->urlParameters .= $urlParameters;
141 18
    }
142
143
    /**
144
     * Adds an unwanted URL parameter that should get removed if found when
145
     * building URLs.
146
     *
147
     * @param string $unwantedUrlParameter URL GET parameter
148
     */
149
    public function addUnwantedUrlParameter($unwantedUrlParameter)
150
    {
151
        $this->unwantedUrlParameters[] = $unwantedUrlParameter;
152
    }
153
154
    /**
155
     * Removes an unwanted URL parameter that should not get removed if found when
156
     * building URLs.
157
     *
158
     * @param string $unwantedUrlParameter URL GET parameter
159
     */
160 17
    public function removeUnwantedUrlParameter($unwantedUrlParameter)
161
    {
162 17
        $key = array_search($unwantedUrlParameter,
163 17
            $this->unwantedUrlParameters);
164 17
        if ($key !== false) {
165 17
            unset($this->unwantedUrlParameters[$key]);
166
        }
167 17
    }
168
169
    /**
170
     * Generates a URL.
171
     *
172
     * TODO currently everything in $additionalQueryParameters is prefixed with tx_solr,
173
     * allow arbitrary parameters, too (either filter them out or introduce a new 3rd parameter)
174
     *
175
     * @param array $additionalQueryParameters Additional query parameters
176
     * @param array $typolinkOptions Typolink Options
177
     * @return string A query URL
178
     */
179 18
    public function getQueryUrl(
180
        array $additionalQueryParameters = [],
181
        array $typolinkOptions = []
182
    ) {
183 18
        $linkConfigurationOverwrite = ['returnLast' => 'url'];
184 18
        $link = $this->getQueryLink(
185 18
            '',
186
            $additionalQueryParameters,
187 18
            array_merge($typolinkOptions, $linkConfigurationOverwrite)
188
        );
189 18
        return htmlspecialchars($link);
190
    }
191
192
    /**
193
     * Generates a html link - an anchor tag.
194
     *
195
     * TODO currently everything in $additionalQueryParameters is prefixed with tx_solr,
196
     * allow arbitrary parameters, too (either filter them out or introduce a new 4th parameter)
197
     *
198
     * @param string $linkText Link Text
199
     * @param array $additionalQueryParameters Additional query parameters
200
     * @param array $typolinkOptions Typolink Options
201
     * @return string A html link
202
     */
203 19
    public function getQueryLink(
204
        $linkText,
205
        array $additionalQueryParameters = [],
206
        array $typolinkOptions = []
207
    ) {
208 19
        $queryParameters = array_merge(
209 19
            $this->getPluginParameters(),
210 19
            $additionalQueryParameters
211
        );
212 19
        $queryParameters = $this->removeUnwantedUrlParameters($queryParameters);
213
214 19
        $queryGetParameter = '';
215
216 19
        $keywords = $this->query->getKeywords();
217 19
        if (!empty($keywords)) {
218 17
            $queryGetParameter = '&q=' . $keywords;
219
        }
220
221
        $linkConfiguration = [
222 19
            'useCacheHash' => false,
223
            'no_cache' => false,
224 19
            'parameter' => $this->linkTargetPageId,
225
            'additionalParams' => $queryGetParameter
226 19
                . GeneralUtility::implodeArrayForUrl('',
227 19
                    [$this->prefix => $queryParameters], '', true)
228 19
                . $this->getUrlParameters()
229
        ];
230
231
        // merge linkConfiguration with typolinkOptions
232 19
        $linkConfiguration = array_merge($linkConfiguration, $typolinkOptions);
233
234 19
        return $this->contentObject->typoLink($linkText, $linkConfiguration);
235
    }
236
237
    /**
238
     * Gets the plugin parameters from GET and POST parameters.
239
     *
240
     * Usually known as piVars.
241
     *
242
     * @return array Array of GET and POST parameters for the extension.
243
     */
244 19
    protected function getPluginParameters()
245
    {
246 19
        $pluginParameters = GeneralUtility::_GPmerged($this->prefix);
247
248 19
        return $pluginParameters;
249
    }
250
251
    /**
252
     * Filters out unwanted parameters when building query URLs
253
     *
254
     * @param array $urlParameters An array of parameters that shall be used to build a URL.
255
     * @return array Array with wanted parameters only, ready to be used for URL building.
256
     */
257 19
    public function removeUnwantedUrlParameters(array $urlParameters)
258
    {
259 19
        foreach ($this->unwantedUrlParameters as $unwantedUrlParameter) {
260 19
            unset($urlParameters[$unwantedUrlParameter]);
261
        }
262
263 19
        return $urlParameters;
264
    }
265
266
    /**
267
     * Gets general URL GET parameters.
268
     *
269
     * @return string Additional URL GET parameters.
270
     */
271 19
    public function getUrlParameters()
272
    {
273 19
        $urlParameters = $this->urlParameters;
274
275 19
        if (!empty($urlParameters) && $urlParameters[0] != '&') {
276
            $urlParameters = '&' . $urlParameters;
277
        }
278
279 19
        return $urlParameters;
280
    }
281
282
    /**
283
     * Sets general URL GET parameters.
284
     *
285
     * @param string $urlParameters URL GET parameters.
286
     */
287
    public function setUrlParameters($urlParameters)
288
    {
289
        $this->urlParameters = $urlParameters;
290
    }
291
}
292