1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
||
291 | } |
||
292 |