‪TYPO3CMS  10.4
PageBrowsingViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
22 
30 class ‪PageBrowsingViewHelper extends AbstractTagBasedViewHelper
31 {
35  protected static ‪$prefixId = 'tx_indexedsearch';
36 
40  protected ‪$tagName = 'ul';
41 
45  public function ‪initializeArguments()
46  {
47  $this->registerArgument('maximumNumberOfResultPages', 'int', '', true);
48  $this->registerArgument('numberOfResults', 'int', '', true);
49  $this->registerArgument('resultsPerPage', 'int', '', true);
50  $this->registerArgument('currentPage', 'int', '', false, 0);
51  $this->registerArgument('freeIndexUid', 'int', '');
52  $this->registerUniversalTagAttributes();
53  }
54 
58  public function ‪render()
59  {
60  $maximumNumberOfResultPages = $this->arguments['maximumNumberOfResultPages'];
61  $numberOfResults = $this->arguments['numberOfResults'];
62  $resultsPerPage = $this->arguments['resultsPerPage'];
63  $currentPage = $this->arguments['currentPage'];
64  $freeIndexUid = $this->arguments['freeIndexUid'];
65 
66  if ($resultsPerPage <= 0) {
67  $resultsPerPage = 10;
68  }
69  $pageCount = (int)ceil($numberOfResults / $resultsPerPage);
70  // only show the result browser if more than one page is needed
71  if ($pageCount === 1) {
72  return '';
73  }
74 
75  // Check if $currentPage is in range
76  $currentPage = ‪MathUtility::forceIntegerInRange($currentPage, 0, $pageCount - 1);
77 
78  $content = '';
79  // prev page
80  // show on all pages after the 1st one
81  if ($currentPage > 0) {
82  $label = ‪LocalizationUtility::translate('displayResults.previous', 'IndexedSearch') ?? '';
83  $content .= '<li>' . $this->‪makecurrentPageSelector_link($label, $currentPage - 1, $freeIndexUid) . '</li>';
84  }
85  // Check if $maximumNumberOfResultPages is in range
86  $maximumNumberOfResultPages = ‪MathUtility::forceIntegerInRange($maximumNumberOfResultPages, 1, $pageCount, 10);
87  // Assume $currentPage is in the middle and calculate the index limits of the result page listing
88  $minPage = $currentPage - (int)floor($maximumNumberOfResultPages / 2);
89  $maxPage = $minPage + $maximumNumberOfResultPages - 1;
90  // Check if the indexes are within the page limits
91  if ($minPage < 0) {
92  $maxPage -= $minPage;
93  $minPage = 0;
94  } elseif ($maxPage >= $pageCount) {
95  $minPage -= $maxPage - $pageCount + 1;
96  $maxPage = $pageCount - 1;
97  }
98  $pageLabel = ‪LocalizationUtility::translate('displayResults.page', 'IndexedSearch');
99  for ($a = $minPage; $a <= $maxPage; $a++) {
100  $label = trim($pageLabel . ' ' . ($a + 1));
101  $label = ‪self::makecurrentPageSelector_link($label, $a, $freeIndexUid);
102  if ($a === $currentPage) {
103  $content .= '<li class="tx-indexedsearch-browselist-currentPage"><strong>' . $label . '</strong></li>';
104  } else {
105  $content .= '<li>' . $label . '</li>';
106  }
107  }
108  // next link
109  if ($currentPage < $pageCount - 1) {
110  $label = ‪LocalizationUtility::translate('displayResults.next', 'IndexedSearch') ?? '';
111  $content .= '<li>' . ‪self::makecurrentPageSelector_link($label, $currentPage + 1, $freeIndexUid) . '</li>';
112  }
113 
114  if (!$this->tag->hasAttribute('class')) {
115  $this->tag->addAttribute('class', 'tx-indexedsearch-browsebox');
116  }
117  $this->tag->setContent($content);
118  return $this->tag->render();
119  }
120 
130  protected function ‪makecurrentPageSelector_link($str, $p, $freeIndexUid)
131  {
132  $onclick = 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_pointer') . ').value=' . GeneralUtility::quoteJSvalue((string)$p) . ';';
133  if ($freeIndexUid !== null) {
134  $onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_freeIndexUid') . ').value=' . GeneralUtility::quoteJSvalue($freeIndexUid) . ';';
135  }
136  $onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId) . ').submit();return false;';
137  return '<a href="#" onclick="' . htmlspecialchars($onclick) . '">' . htmlspecialchars($str) . '</a>';
138  }
139 }
‪TYPO3\CMS\IndexedSearch\ViewHelpers\PageBrowsingViewHelper\initializeArguments
‪initializeArguments()
Definition: PageBrowsingViewHelper.php:43
‪TYPO3\CMS\IndexedSearch\ViewHelpers\PageBrowsingViewHelper\$prefixId
‪static string $prefixId
Definition: PageBrowsingViewHelper.php:34
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\IndexedSearch\ViewHelpers\PageBrowsingViewHelper\makecurrentPageSelector_link
‪string makecurrentPageSelector_link($str, $p, $freeIndexUid)
Definition: PageBrowsingViewHelper.php:128
‪TYPO3\CMS\IndexedSearch\ViewHelpers\PageBrowsingViewHelper
Definition: PageBrowsingViewHelper.php:31
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\IndexedSearch\ViewHelpers\PageBrowsingViewHelper\$tagName
‪string $tagName
Definition: PageBrowsingViewHelper.php:38
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\IndexedSearch\ViewHelpers
‪TYPO3\CMS\IndexedSearch\ViewHelpers\PageBrowsingViewHelper\render
‪render()
Definition: PageBrowsingViewHelper.php:56