Completed
Branch master (b22b71)
by Timo
13:13 queued 21s
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
33
/**
34
 * The page browser used in search result listings
35
 *
36
 */
37
class PageBrowser
38
{
39
    protected $numberOfPages;
40
    protected $pageParameterName = 'tx_solr[page]';
41
    protected $currentPage;
42
    protected $pagesBefore = 3;
43
    protected $pagesAfter = 3;
44
45
    protected $template;
46
47
    protected $configuration = array();
48
    protected $labels = array();
49
    protected $contentObject = null;
50
51
    /**
52
     * PageBrowser constructor.
53
     *
54
     * @param array $configuration
55
     * @param array $labels
56
     */
57 18
    public function __construct(array $configuration, array $labels)
58
    {
59 18
        $this->configuration = $configuration;
60 18
        $this->labels = $labels;
61
62 18
        $this->contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
63
64 18
        $this->numberOfPages = $configuration['numberOfPages'];
65 18
        $this->currentPage = $configuration['currentPage'];
66
67 18
        $this->pagesBefore = (int)$configuration['pagesBefore'];
68 18
        $this->pagesAfter = (int)$configuration['pagesAfter'];
69
70 18
        $path = $GLOBALS['TSFE']->tmpl->getFileName($configuration['templateFile']);
71 18
        if ($path !== null && file_exists($path)) {
72 18
            $this->template = file_get_contents($path);
73 18
        }
74 18
    }
75
76
    /**
77
     * Produces the page browser HTML
78
     *
79
     * @return string Generated content
80
     */
81 18
    public function render()
82
    {
83 18
        $pageBrowser = '';
84
85 18
        if ($this->numberOfPages > 1) {
86
            // Set up
87
            $markers = array(
88 2
                '###TEXT_FIRST###' => htmlspecialchars($this->labels['pagebrowser_first']),
89 2
                '###TEXT_NEXT###' => htmlspecialchars($this->labels['pagebrowser_next']),
90 2
                '###TEXT_PREV###' => htmlspecialchars($this->labels['pagebrowser_prev']),
91 2
                '###TEXT_LAST###' => htmlspecialchars($this->labels['pagebrowser_last']),
92 2
            );
93 2
            $subPartMarkers = array();
94 2
            $subPart = $this->contentObject->getSubpart($this->template,
95 2
                '###PAGE_BROWSER###');
96
97
            // First page link
98 2
            if ($this->currentPage == 0) {
99 2
                $subPartMarkers['###ACTIVE_FIRST###'] = '';
100 2
            } else {
101
                $markers['###FIRST_LINK###'] = $this->getPageLink(0);
102
                $subPartMarkers['###INACTIVE_FIRST###'] = '';
103
            }
104
105
            // Prev page link
106 2
            if ($this->currentPage == 0) {
107 2
                $subPartMarkers['###ACTIVE_PREV###'] = '';
108 2
            } else {
109
                $markers['###PREV_LINK###'] = $this->getPageLink($this->currentPage - 1);
110
                $subPartMarkers['###INACTIVE_PREV###'] = '';
111
            }
112
113
            // Next link
114 2
            if ($this->currentPage >= $this->numberOfPages - 1) {
115
                $subPartMarkers['###ACTIVE_NEXT###'] = '';
116
            } else {
117 2
                $markers['###NEXT_LINK###'] = $this->getPageLink($this->currentPage + 1);
118 2
                $subPartMarkers['###INACTIVE_NEXT###'] = '';
119
            }
120
121
            // Last link
122 2
            if ($this->currentPage == $this->numberOfPages - 1) {
123
                $subPartMarkers['###ACTIVE_LAST###'] = '';
124
            } else {
125 2
                $markers['###LAST_LINK###'] = $this->getPageLink($this->numberOfPages - 1);
126 2
                $subPartMarkers['###INACTIVE_LAST###'] = '';
127
            }
128
129
            // Page links
130 2
            $actPageLinkSubPart = trim($this->contentObject->getSubpart($subPart,
131 2
                '###CURRENT###'));
132 2
            $inactivePageLinkSubPart = trim($this->contentObject->getSubpart($subPart,
133 2
                '###PAGE###'));
134 2
            $pageLinks = '';
135 2
            $start = max($this->currentPage - $this->pagesBefore, 0);
136 2
            $end = min($this->numberOfPages,
137 2
                $this->currentPage + $this->pagesAfter + 1);
138
139 2
            for ($i = $start; $i < $end; $i++) {
140 2
                $template = ($i == $this->currentPage ? $actPageLinkSubPart : $inactivePageLinkSubPart);
141
                $localMarkers = array(
142 2
                    '###NUMBER###' => $i,
143 2
                    '###NUMBER_DISPLAY###' => $i + 1,
144 2
                    '###LINK###' => $this->getPageLink($i),
145 2
                );
146 2
                $pageLinks .= $this->contentObject->substituteMarkerArray($template,
147 2
                    $localMarkers);
148 2
            }
149 2
            $subPartMarkers['###PAGE###'] = $pageLinks;
150 2
            $subPartMarkers['###CURRENT###'] = '';
151
152
            // Less pages part
153 2
            if ($start == 0 || !$this->configuration['enableLessPages']) {
154 2
                $subPartMarkers['###LESS_PAGES###'] = '';
155 2
            }
156
157
            // More pages part
158 2
            if ($end == $this->numberOfPages || !$this->configuration['enableMorePages']) {
159
                // We have all pages covered. Remove this part.
160 2
                $subPartMarkers['###MORE_PAGES###'] = '';
161 2
            }
162
163
            // Render / substitute markers
164 2
            $pageBrowser = $this->contentObject->substituteMarkerArrayCached($subPart,
165 2
                $markers, $subPartMarkers);
166
167
            // Clean up comments
168 2
            $pageBrowser = preg_replace('/<!--\s*###.*?-->/', ' ',
169 2
                $pageBrowser);
170 2
        }
171
172 18
        return $pageBrowser;
173
    }
174
175
    /**
176
     * Generates page link. Keeps all current URL parameters except for cHash and page parameter.
177
     *
178
     * @param int $page Page number starting from 1
179
     * @return string Generated link
180
     */
181 2
    protected function getPageLink($page)
182
    {
183
        // Prepare query string. We do both urlencoded and non-encoded version
184
        // because older TYPO3 versions use un-encoded parameter names
185
        $queryConf = array(
186 2
            'exclude' => $this->pageParameterName . ',' .
187 2
                rawurlencode($this->pageParameterName) .
188 2
                ',cHash',
189 2
        );
190 2
        $additionalParams = urldecode($this->contentObject->getQueryArguments($queryConf));
191
192
        // Add page number
193 2
        if ($page > 0) {
194 2
            $additionalParams .= '&' . $this->pageParameterName . '=' . $page;
195 2
        }
196
197
        // Add extra query string from config
198 2
        $extraQueryString = trim($this->configuration['extraQueryString']);
199 2
        if (is_array($this->configuration['extraQueryString.'])) {
200
            $extraQueryString = $this->contentObject->stdWrap($extraQueryString,
201
                $this->configuration['extraQueryString.']);
202
        }
203
204 2
        if (strlen($extraQueryString) > 2 && $extraQueryString{0} == '&') {
205
            $additionalParams .= $extraQueryString;
206
        }
207
208
        // Assemble typolink configuration
209
        $conf = array(
210 2
            'parameter' => $GLOBALS['TSFE']->id,
211 2
            'additionalParams' => $additionalParams,
212 2
            'useCacheHash' => false,
213 2
        );
214
215 2
        return htmlspecialchars($this->contentObject->typoLink_URL($conf));
216
    }
217
}
218