TYPO3 CMS  TYPO3_7-6
PageBrowsingViewHelper.php
Go to the documentation of this file.
1 <?php
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 
23 
32 {
36  protected static $prefixId = 'tx_indexedsearch';
37 
48  public function render($maximumNumberOfResultPages, $numberOfResults, $resultsPerPage, $currentPage = 0, $freeIndexUid = null)
49  {
50  return static::renderStatic(
51  [
52  'maximumNumberOfResultPages' => $maximumNumberOfResultPages,
53  'numberOfResults' => $numberOfResults,
54  'resultsPerPage' => $resultsPerPage,
55  'currentPage' => $currentPage,
56  'freeIndexUid' => $freeIndexUid,
57  ],
59  $this->renderingContext
60  );
61  }
62 
71  {
72  $maximumNumberOfResultPages = $arguments['maximumNumberOfResultPages'];
73  $numberOfResults = $arguments['numberOfResults'];
74  $resultsPerPage = $arguments['resultsPerPage'];
75  $currentPage = $arguments['currentPage'];
76  $freeIndexUid = $arguments['freeIndexUid'];
77 
78  if ($resultsPerPage <= 0) {
79  $resultsPerPage = 10;
80  }
81  $pageCount = (int)ceil($numberOfResults / $resultsPerPage);
82  // only show the result browser if more than one page is needed
83  if ($pageCount === 1) {
84  return '';
85  }
86 
87  // Check if $currentPage is in range
88  $currentPage = MathUtility::forceIntegerInRange($currentPage, 0, $pageCount - 1);
89 
90  $content = '';
91  // prev page
92  // show on all pages after the 1st one
93  if ($currentPage > 0) {
94  $label = LocalizationUtility::translate('displayResults.previous', 'IndexedSearch');
95  $content .= '<li>' . self::makecurrentPageSelector_link($label, $currentPage - 1, $freeIndexUid) . '</li>';
96  }
97  // Check if $maximumNumberOfResultPages is in range
98  $maximumNumberOfResultPages = MathUtility::forceIntegerInRange($maximumNumberOfResultPages, 1, $pageCount, 10);
99  // Assume $currentPage is in the middle and calculate the index limits of the result page listing
100  $minPage = $currentPage - (int)floor($maximumNumberOfResultPages / 2);
101  $maxPage = $minPage + $maximumNumberOfResultPages - 1;
102  // Check if the indexes are within the page limits
103  if ($minPage < 0) {
104  $maxPage -= $minPage;
105  $minPage = 0;
106  } elseif ($maxPage >= $pageCount) {
107  $minPage -= $maxPage - $pageCount + 1;
108  $maxPage = $pageCount - 1;
109  }
110  $pageLabel = LocalizationUtility::translate('displayResults.page', 'IndexedSearch');
111  for ($a = $minPage; $a <= $maxPage; $a++) {
112  $label = trim($pageLabel . ' ' . ($a + 1));
113  $label = self::makecurrentPageSelector_link($label, $a, $freeIndexUid);
114  if ($a === $currentPage) {
115  $content .= '<li class="tx-indexedsearch-browselist-currentPage"><strong>' . $label . '</strong></li>';
116  } else {
117  $content .= '<li>' . $label . '</li>';
118  }
119  }
120  // next link
121  if ($currentPage < $pageCount - 1) {
122  $label = LocalizationUtility::translate('displayResults.next', 'IndexedSearch');
123  $content .= '<li>' . self::makecurrentPageSelector_link($label, ($currentPage + 1), $freeIndexUid) . '</li>';
124  }
125  return '<ul class="tx-indexedsearch-browsebox">' . $content . '</ul>';
126  }
127 
137  protected static function makecurrentPageSelector_link($str, $p, $freeIndexUid)
138  {
139  $onclick = 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_pointer') . ').value=' . GeneralUtility::quoteJSvalue($p) . ';';
140  if ($freeIndexUid !== null) {
141  $onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_freeIndexUid') . ').value=' . GeneralUtility::quoteJSvalue($freeIndexUid) . ';';
142  }
143  $onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId) . ').submit();return false;';
144  return '<a href="#" onclick="' . htmlspecialchars($onclick) . '">' . $str . '</a>';
145  }
146 }
static translate($key, $extensionName, $arguments=null)
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
render($maximumNumberOfResultPages, $numberOfResults, $resultsPerPage, $currentPage=0, $freeIndexUid=null)