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