Passed
Pull Request — master (#1249)
by
unknown
19:13
created

PageBrowser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.35%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 181
ccs 84
cts 110
cp 0.7635
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
C render() 0 93 12
B getPageLink() 0 36 5
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2008-2014 Dmitry Dulepov ([email protected])
8
 *  (c) 2015 Ingo Renner ([email protected])
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *  A copy is found in the textfile GPL.txt and important notices to the license
20
 *  from the author is found in LICENSE.txt distributed with these scripts.
21
 *
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
33
34
/**
35
 * The page browser used in search result listings
36
 *
37
 */
38
class PageBrowser
39
{
40
    protected $numberOfPages;
41
    protected $pageParameterName = 'tx_solr[page]';
42
    protected $currentPage;
43
    protected $pagesBefore = 3;
44
    protected $pagesAfter = 3;
45
46
    protected $template;
47
48
    protected $configuration = [];
49
    protected $labels = [];
50
    protected $contentObject = null;
51
52
    /**
53
     * PageBrowser constructor.
54
     *
55
     * @param array $configuration
56
     * @param array $labels
57
     */
58 18
    public function __construct(array $configuration, array $labels)
59
    {
60 18
        $this->configuration = $configuration;
61 18
        $this->labels = $labels;
62
63 18
        $this->contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
64
65 18
        $this->numberOfPages = $configuration['numberOfPages'];
66 18
        $this->currentPage = $configuration['currentPage'];
67
68 18
        $this->pagesBefore = (int)$configuration['pagesBefore'];
69 18
        $this->pagesAfter = (int)$configuration['pagesAfter'];
70
71 18
        $path = $GLOBALS['TSFE']->tmpl->getFileName($configuration['templateFile']);
72 18
        if ($path !== null && file_exists($path)) {
73 18
            $this->template = file_get_contents($path);
74 18
        }
75 18
    }
76
77
    /**
78
     * Produces the page browser HTML
79
     *
80
     * @return string Generated content
81
     */
82 18
    public function render()
83
    {
84 18
        $pageBrowser = '';
85
86 18
        if ($this->numberOfPages > 1) {
87
            // Set up
88
            $markers = [
89 2
                '###TEXT_FIRST###' => htmlspecialchars($this->labels['pagebrowser_first']),
90 2
                '###TEXT_NEXT###' => htmlspecialchars($this->labels['pagebrowser_next']),
91 2
                '###TEXT_PREV###' => htmlspecialchars($this->labels['pagebrowser_prev']),
92 2
                '###TEXT_LAST###' => htmlspecialchars($this->labels['pagebrowser_last']),
93 2
            ];
94 2
            $subPartMarkers = [];
95 2
            $subPart = $this->contentObject->getSubpart($this->template,
96 2
                '###PAGE_BROWSER###');
97
98
            // First page link
99 2
            if ($this->currentPage == 0) {
100 2
                $subPartMarkers['###ACTIVE_FIRST###'] = '';
101 2
            } else {
102
                $markers['###FIRST_LINK###'] = $this->getPageLink(0);
103
                $subPartMarkers['###INACTIVE_FIRST###'] = '';
104
            }
105
106
            // Prev page link
107 2
            if ($this->currentPage == 0) {
108 2
                $subPartMarkers['###ACTIVE_PREV###'] = '';
109 2
            } else {
110
                $markers['###PREV_LINK###'] = $this->getPageLink($this->currentPage - 1);
111
                $subPartMarkers['###INACTIVE_PREV###'] = '';
112
            }
113
114
            // Next link
115 2
            if ($this->currentPage >= $this->numberOfPages - 1) {
116
                $subPartMarkers['###ACTIVE_NEXT###'] = '';
117
            } else {
118 2
                $markers['###NEXT_LINK###'] = $this->getPageLink($this->currentPage + 1);
119 2
                $subPartMarkers['###INACTIVE_NEXT###'] = '';
120
            }
121
122
            // Last link
123 2
            if ($this->currentPage == $this->numberOfPages - 1) {
124
                $subPartMarkers['###ACTIVE_LAST###'] = '';
125
            } else {
126 2
                $markers['###LAST_LINK###'] = $this->getPageLink($this->numberOfPages - 1);
127 2
                $subPartMarkers['###INACTIVE_LAST###'] = '';
128
            }
129
130
            // Page links
131 2
            $actPageLinkSubPart = trim($this->contentObject->getSubpart($subPart,
132 2
                '###CURRENT###'));
133 2
            $inactivePageLinkSubPart = trim($this->contentObject->getSubpart($subPart,
134 2
                '###PAGE###'));
135 2
            $pageLinks = '';
136 2
            $start = max($this->currentPage - $this->pagesBefore, 0);
137 2
            $end = min($this->numberOfPages,
138 2
                $this->currentPage + $this->pagesAfter + 1);
139
140 2
            for ($i = $start; $i < $end; $i++) {
141 2
                $template = ($i == $this->currentPage ? $actPageLinkSubPart : $inactivePageLinkSubPart);
142
                $localMarkers = [
143 2
                    '###NUMBER###' => $i,
144 2
                    '###NUMBER_DISPLAY###' => $i + 1,
145 2
                    '###LINK###' => $this->getPageLink($i),
146 2
                ];
147 2
                $pageLinks .= $this->contentObject->substituteMarkerArray($template,
148 2
                    $localMarkers);
149 2
            }
150 2
            $subPartMarkers['###PAGE###'] = $pageLinks;
151 2
            $subPartMarkers['###CURRENT###'] = '';
152
153
            // Less pages part
154 2
            if ($start == 0 || !$this->configuration['enableLessPages']) {
155 2
                $subPartMarkers['###LESS_PAGES###'] = '';
156 2
            }
157
158
            // More pages part
159 2
            if ($end == $this->numberOfPages || !$this->configuration['enableMorePages']) {
160
                // We have all pages covered. Remove this part.
161 2
                $subPartMarkers['###MORE_PAGES###'] = '';
162 2
            }
163
164
            // Render / substitute markers
165 2
            $pageBrowser = $this->contentObject->substituteMarkerArrayCached($subPart,
166 2
                $markers, $subPartMarkers);
167
168
            // Clean up comments
169 2
            $pageBrowser = preg_replace('/<!--\s*###.*?-->/', ' ',
170 2
                $pageBrowser);
171 2
        }
172
173 18
        return $pageBrowser;
174
    }
175
176
    /**
177
     * Generates page link. Keeps all current URL parameters except for cHash and page parameter.
178
     *
179
     * @param int $page Page number starting from 1
180
     * @return string Generated link
181
     */
182 2
    protected function getPageLink($page)
183
    {
184
        // Prepare query string. We do both urlencoded and non-encoded version
185
        // because older TYPO3 versions use un-encoded parameter names
186
        $queryConf = [
187 2
            'exclude' => $this->pageParameterName . ',' .
188 2
                rawurlencode($this->pageParameterName) .
189 2
                ',cHash',
190 2
        ];
191 2
        $additionalParams = urldecode($this->contentObject->getQueryArguments($queryConf));
192
193
        // Add page number
194 2
        if ($page > 0) {
195 2
            $additionalParams .= '&' . $this->pageParameterName . '=' . $page;
196 2
        }
197
198
        // Add extra query string from config
199 2
        $extraQueryString = trim($this->configuration['extraQueryString']);
200 2
        if (is_array($this->configuration['extraQueryString.'])) {
201
            $extraQueryString = $this->contentObject->stdWrap($extraQueryString,
202
                $this->configuration['extraQueryString.']);
203
        }
204
205 2
        if (strlen($extraQueryString) > 2 && $extraQueryString{0} == '&') {
206
            $additionalParams .= $extraQueryString;
207
        }
208
209
        // Assemble typolink configuration
210
        $conf = [
211 2
            'parameter' => $GLOBALS['TSFE']->id,
212 2
            'additionalParams' => $additionalParams,
213 2
            'useCacheHash' => false,
214 2
        ];
215
216 2
        return htmlspecialchars($this->contentObject->typoLink_URL($conf));
217
    }
218
}
219