[TASK] Remove t3lib_div::intInRange()
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / ViewHelpers / PageBrowsingViewHelper.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Benjamin Mack (benni@typo3.org)
6 *
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Page browser for indexed search, and only useful here, as the
30 * regular pagebrowser
31 * so this is a cleaner "pi_browsebox" but not a real page browser
32 * functionality
33 *
34 * @author Benjamin Mack <benni@typo3.org>
35 *
36 */
37 class Tx_IndexedSearch_ViewHelpers_PageBrowsingViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
38
39
40 /**
41 * main render function
42 *
43 * @param array $details an array with the browser settings
44 * @param integer $maximumNumberOfResultPages
45 * @param integer $numberOfResults
46 * @param integer $resultsPerPage
47 * @param integer $currentPage
48 * @return the content
49 */
50 public function render($maximumNumberOfResultPages, $numberOfResults, $resultsPerPage, $currentPage = 1) {
51 $maximumNumberOfResultPages = t3lib_utility_Math::forceIntegerInRange($maximumNumberOfResultPages, 1, 100, 10);
52
53 $pageCount = ceil($numberOfResults / $resultsPerPage);
54
55 $content = '';
56
57 // only show the result browser if more than one page is needed
58 if ($pageCount > 1) {
59 $currentPage = intval($currentPage);
60
61 // prev page
62 // show on all pages after the 1st one
63 if ($currentPage > 0) {
64 $label = Tx_Extbase_Utility_Localization::translate('displayResults.previous', 'indexed_search');
65 $content .= '<li>' . $this->makecurrentPageSelector_link($label, $currentPage-1, $freeIndexUid) . '</li>';
66 }
67
68 for ($a = 0; $a < $pageCount; $a++) {
69 $min = max(0, $currentPage + 1 - ceil($maximumNumberOfResultPages / 2));
70 $max = $min + $maximumNumberOfResultPages;
71 if ($max > $pageCount) {
72 $min = $min - ($max-$pageCount);
73 }
74
75 if ($a >= $min && $a < $max) {
76 $label = Tx_Extbase_Utility_Localization::translate('displayResults.page', 'indexed_search');
77 $label = trim($label . ' ' . ($a + 1));
78 $label = $this->makecurrentPageSelector_link($label, $a, $freeIndexUid);
79 if ($a == $currentPage) {
80 $content .= '<li class="tx-indexedsearch-browselist-currentPage"><strong>' . $label . '</strong></li>';
81 } else {
82 $content .= '<li>' . $label . '</li>';
83 }
84 }
85 }
86
87 // next link
88 if ($currentPage+1 < $pageCount) {
89 $label = Tx_Extbase_Utility_Localization::translate('displayResults.next', 'indexed_search');
90 $content = '<li>' . $this->makecurrentPageSelector_link($label, $currentPage + 1, $freeIndexUid) . '</li>';
91 }
92
93 $content = '<ul class="tx-indexedsearch-browsebox">' . $content . '</ul>';
94 }
95
96 return $content;
97 }
98
99
100
101
102 /**
103 * Used to make the link for the result-browser.
104 * Notice how the links must resubmit the form after setting the new currentPage-value in a hidden formfield.
105 *
106 * @param string $str String to wrap in <a> tag
107 * @param integer $p currentPage value
108 * @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!
109 * @return string Input string wrapped in <a> tag with onclick event attribute set.
110 */
111 function makecurrentPageSelector_link($str, $p, $freeIndexUid) {
112 $onclick = 'document.getElementById(\'' . $this->prefixId . '_currentPage\').value=\'' . $p . '\';' .
113 'document.getElementById(\'' . $this->prefixId . '_freeIndexUid\').value=\'' . rawurlencode($freeIndexUid) . '\';' .
114 'document.getElementById(\'' . $this->prefixId . '\').submit();return false;';
115 return '<a href="#" onclick="' . htmlspecialchars($onclick) . '">'.$str . '</a>';
116 }
117
118 }
119
120 ?>