Completed
Push — master ( 73adc4...3b3b37 )
by
unknown
19:12
created

PageBrowsingViewHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 108
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 8 1
B render() 0 61 10
A makecurrentPageSelector_link() 0 8 2
1
<?php
2
namespace TYPO3\CMS\IndexedSearch\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\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Core\Utility\MathUtility;
19
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21
22
/**
23
 * Page browser for indexed search, and only useful here, as the
24
 * regular pagebrowser
25
 * so this is a cleaner "pi_browsebox" but not a real page browser
26
 * functionality
27
 * @internal
28
 */
29
class PageBrowsingViewHelper extends AbstractTagBasedViewHelper
30
{
31
    /**
32
     * @var string
33
     */
34
    protected static $prefixId = 'tx_indexedsearch';
35
36
    /**
37
     * @var string
38
     */
39
    protected $tagName = 'ul';
40
41
    /**
42
     * Initialize arguments
43
     */
44
    public function initializeArguments()
45
    {
46
        $this->registerArgument('maximumNumberOfResultPages', 'int', '', true);
47
        $this->registerArgument('numberOfResults', 'int', '', true);
48
        $this->registerArgument('resultsPerPage', 'int', '', true);
49
        $this->registerArgument('currentPage', 'int', '', false, 0);
50
        $this->registerArgument('freeIndexUid', 'int', '');
51
        $this->registerUniversalTagAttributes();
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function render()
58
    {
59
        $maximumNumberOfResultPages = $this->arguments['maximumNumberOfResultPages'];
60
        $numberOfResults = $this->arguments['numberOfResults'];
61
        $resultsPerPage = $this->arguments['resultsPerPage'];
62
        $currentPage = $this->arguments['currentPage'];
63
        $freeIndexUid = $this->arguments['freeIndexUid'];
64
65
        if ($resultsPerPage <= 0) {
66
            $resultsPerPage = 10;
67
        }
68
        $pageCount = (int)ceil($numberOfResults / $resultsPerPage);
69
        // only show the result browser if more than one page is needed
70
        if ($pageCount === 1) {
71
            return '';
72
        }
73
74
        // Check if $currentPage is in range
75
        $currentPage = MathUtility::forceIntegerInRange($currentPage, 0, $pageCount - 1);
76
77
        $content = '';
78
        // prev page
79
        // show on all pages after the 1st one
80
        if ($currentPage > 0) {
81
            $label = LocalizationUtility::translate('displayResults.previous', 'IndexedSearch');
82
            $content .= '<li>' . $this->makecurrentPageSelector_link($label, $currentPage - 1, $freeIndexUid) . '</li>';
83
        }
84
        // Check if $maximumNumberOfResultPages is in range
85
        $maximumNumberOfResultPages = MathUtility::forceIntegerInRange($maximumNumberOfResultPages, 1, $pageCount, 10);
86
        // Assume $currentPage is in the middle and calculate the index limits of the result page listing
87
        $minPage = $currentPage - (int)floor($maximumNumberOfResultPages / 2);
88
        $maxPage = $minPage + $maximumNumberOfResultPages - 1;
89
        // Check if the indexes are within the page limits
90
        if ($minPage < 0) {
91
            $maxPage -= $minPage;
92
            $minPage = 0;
93
        } elseif ($maxPage >= $pageCount) {
94
            $minPage -= $maxPage - $pageCount + 1;
95
            $maxPage = $pageCount - 1;
96
        }
97
        $pageLabel = LocalizationUtility::translate('displayResults.page', 'IndexedSearch');
98
        for ($a = $minPage; $a <= $maxPage; $a++) {
99
            $label = trim($pageLabel . ' ' . ($a + 1));
100
            $label = self::makecurrentPageSelector_link($label, $a, $freeIndexUid);
0 ignored issues
show
Bug Best Practice introduced by
The method TYPO3\CMS\IndexedSearch\...rentPageSelector_link() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            /** @scrutinizer ignore-call */ 
101
            $label = self::makecurrentPageSelector_link($label, $a, $freeIndexUid);
Loading history...
101
            if ($a === $currentPage) {
102
                $content .= '<li class="tx-indexedsearch-browselist-currentPage"><strong>' . $label . '</strong></li>';
103
            } else {
104
                $content .= '<li>' . $label . '</li>';
105
            }
106
        }
107
        // next link
108
        if ($currentPage < $pageCount - 1) {
109
            $label = LocalizationUtility::translate('displayResults.next', 'IndexedSearch');
110
            $content .= '<li>' . self::makecurrentPageSelector_link($label, $currentPage + 1, $freeIndexUid) . '</li>';
111
        }
112
113
        if (!$this->tag->hasAttribute('class')) {
114
            $this->tag->addAttribute('class', 'tx-indexedsearch-browsebox');
115
        }
116
        $this->tag->setContent($content);
117
        return $this->tag->render();
118
    }
119
120
    /**
121
     * Used to make the link for the result-browser.
122
     * Notice how the links must resubmit the form after setting the new currentPage-value in a hidden formfield.
123
     *
124
     * @param string $str String to wrap in <a> tag
125
     * @param int $p currentPage value
126
     * @param string $freeIndexUid List of integers pointing to free indexing configurations to search. -1 represents no filtering, 0 represents TYPO3 pages only, any number above zero is a uid of an indexing configuration!
127
     * @return string Input string wrapped in <a> tag with onclick event attribute set.
128
     */
129
    protected function makecurrentPageSelector_link($str, $p, $freeIndexUid)
0 ignored issues
show
Coding Style introduced by
Method name "PageBrowsingViewHelper::makecurrentPageSelector_link" is not in camel caps format
Loading history...
130
    {
131
        $onclick = 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_pointer') . ').value=' . GeneralUtility::quoteJSvalue($p) . ';';
132
        if ($freeIndexUid !== null) {
0 ignored issues
show
introduced by
The condition $freeIndexUid !== null is always true.
Loading history...
133
            $onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_freeIndexUid') . ').value=' . GeneralUtility::quoteJSvalue($freeIndexUid) . ';';
134
        }
135
        $onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId) . ').submit();return false;';
136
        return '<a href="#" onclick="' . htmlspecialchars($onclick) . '">' . htmlspecialchars($str) . '</a>';
137
    }
138
}
139