[!!!][TASK] Rewrite backend modules of indexed_search
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / ViewHelpers / PageBrowsingViewHelper.php
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 * Page browser for indexed search, and only useful here, as the
18 * regular pagebrowser
19 * so this is a cleaner "pi_browsebox" but not a real page browser
20 * functionality
21 *
22 * @author Benjamin Mack <benni@typo3.org>
23 * @internal
24 */
25 class PageBrowsingViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
26
27 /**
28 * @var string
29 */
30 protected $prefixId = 'tx_indexedsearch';
31
32 /**
33 * Main render function
34 *
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
41 */
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) {
46 return '';
47 }
48
49 // Check if $currentPage is in range
50 $currentPage = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($currentPage, 0, $pageCount - 1);
51
52 $content = '';
53 // prev page
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>';
58 }
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
65 if ($minPage < 0) {
66 $maxPage -= $minPage;
67 $minPage = 0;
68 } elseif ($maxPage >= $pageCount) {
69 $minPage -= $maxPage - $pageCount + 1;
70 $maxPage = $pageCount - 1;
71 }
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>';
78 } else {
79 $content .= '<li>' . $label . '</li>';
80 }
81 }
82 // next link
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>';
86 }
87 return '<ul class="tx-indexedsearch-browsebox">' . $content . '</ul>';
88 }
89
90 /**
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.
93 *
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.
98 */
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) . '\';';
103 }
104 $onclick .= 'document.getElementById(\'' . $this->prefixId . '\').submit();return false;';
105 return '<a href="#" onclick="' . htmlspecialchars($onclick) . '">' . $str . '</a>';
106 }
107
108 }