|
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); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$onclick = 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_pointer') . ').value=' . GeneralUtility::quoteJSvalue($p) . ';'; |
|
132
|
|
|
if ($freeIndexUid !== null) { |
|
|
|
|
|
|
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
|
|
|
|