2 namespace TYPO3\CMS\IndexedSearch\ViewHelpers
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 * Page browser for indexed search, and only useful here, as the
19 * so this is a cleaner "pi_browsebox" but not a real page browser
22 * @author Benjamin Mack <benni@typo3.org>
25 class PageBrowsingViewHelper
extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
30 protected $prefixId = 'tx_indexedsearch';
33 * Main render function
35 * @param int $maximumNumberOfResultPages The number of page links shown
36 * @param int $numberOfResults Total number of results
37 * @param int $resultsPerPage Number of results per page
38 * @param int $currentPage The current page starting with 0
39 * @param string|NULL $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!
40 * @return string The content
42 public function render($maximumNumberOfResultPages, $numberOfResults, $resultsPerPage, $currentPage = 0, $freeIndexUid = NULL) {
43 $pageCount = (int)ceil($numberOfResults / $resultsPerPage);
44 // only show the result browser if more than one page is needed
45 if ($pageCount === 1) {
49 // Check if $currentPage is in range
50 $currentPage = \TYPO3\CMS\Core\Utility\MathUtility
::forceIntegerInRange($currentPage, 0, $pageCount - 1);
54 // show on all pages after the 1st one
55 if ($currentPage > 0) {
56 $label = \TYPO3\CMS\Extbase\Utility\LocalizationUtility
::translate('displayResults.previous', 'IndexedSearch');
57 $content .= '<li>' . $this->makecurrentPageSelector_link($label, $currentPage - 1, $freeIndexUid) . '</li>';
59 // Check if $maximumNumberOfResultPages is in range
60 $maximumNumberOfResultPages = \TYPO3\CMS\Core\Utility\MathUtility
::forceIntegerInRange($maximumNumberOfResultPages, 1, $pageCount, 10);
61 // Assume $currentPage is in the middle and calculate the index limits of the result page listing
62 $minPage = $currentPage - (int)floor($maximumNumberOfResultPages / 2);
63 $maxPage = $minPage +
$maximumNumberOfResultPages - 1;
64 // Check if the indexes are within the page limits
68 } elseif ($maxPage >= $pageCount) {
69 $minPage -= $maxPage - $pageCount +
1;
70 $maxPage = $pageCount - 1;
72 $pageLabel = \TYPO3\CMS\Extbase\Utility\LocalizationUtility
::translate('displayResults.page', 'IndexedSearch');
73 for ($a = $minPage; $a <= $maxPage; $a++
) {
74 $label = trim($pageLabel . ' ' . ($a +
1));
75 $label = $this->makecurrentPageSelector_link($label, $a, $freeIndexUid);
76 if ($a === $currentPage) {
77 $content .= '<li class="tx-indexedsearch-browselist-currentPage"><strong>' . $label . '</strong></li>';
79 $content .= '<li>' . $label . '</li>';
83 if ($currentPage < $pageCount - 1) {
84 $label = \TYPO3\CMS\Extbase\Utility\LocalizationUtility
::translate('displayResults.next', 'IndexedSearch');
85 $content .= '<li>' . $this->makecurrentPageSelector_link($label, ($currentPage +
1), $freeIndexUid) . '</li>';
87 return '<ul class="tx-indexedsearch-browsebox">' . $content . '</ul>';
91 * Used to make the link for the result-browser.
92 * Notice how the links must resubmit the form after setting the new currentPage-value in a hidden formfield.
94 * @param string $str String to wrap in <a> tag
95 * @param int $p currentPage value
96 * @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!
97 * @return string Input string wrapped in <a> tag with onclick event attribute set.
99 public function makecurrentPageSelector_link($str, $p, $freeIndexUid) {
100 $onclick = 'document.getElementById(\'' . $this->prefixId
. '_pointer\').value=\'' . $p . '\';';
101 if ($freeIndexUid !== NULL) {
102 $onclick .= 'document.getElementById(\'' . $this->prefixId
. '_freeIndexUid\').value=\'' . rawurlencode($freeIndexUid) . '\';';
104 $onclick .= 'document.getElementById(\'' . $this->prefixId
. '\').submit();return false;';
105 return '<a href="#" onclick="' . htmlspecialchars($onclick) . '">' . $str . '</a>';