-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #386 from t3solution/5.3.6
New release v5.3.6
- Loading branch information
Showing
34 changed files
with
772 additions
and
641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,176 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace T3SBS\T3sbootstrap\ViewHelpers; | ||
|
||
use TYPO3\CMS\Core\Page\AssetCollector; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Core\Utility\MathUtility; | ||
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; | ||
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | ||
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; | ||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; | ||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; | ||
|
||
/** | ||
* This file is part of the TYPO3 extension t3sbootstrap. | ||
* Page browser for indexed search, and only useful here, as the regular pagebrowser. | ||
* This is a cleaner "pi_browsebox" but not a real page browser functionality. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* @internal | ||
*/ | ||
class PageBrowsingViewHelper extends AbstractViewHelper | ||
final class PageBrowsingViewHelper extends AbstractTagBasedViewHelper | ||
{ | ||
use CompileWithRenderStatic; | ||
|
||
/** | ||
* As this ViewHelper renders HTML, the output must not be escaped. | ||
* | ||
* @var bool | ||
*/ | ||
protected $escapeOutput = false; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected static $prefixId = 'tx_indexedsearch'; | ||
|
||
/** | ||
* Initialize arguments | ||
*/ | ||
public function initializeArguments() | ||
{ | ||
parent::initializeArguments(); | ||
$this->registerArgument('maximumNumberOfResultPages', 'int', '', true); | ||
$this->registerArgument('numberOfResults', 'int', '', true); | ||
$this->registerArgument('resultsPerPage', 'int', '', true); | ||
$this->registerArgument('currentPage', 'int', '', false, 0); | ||
$this->registerArgument('freeIndexUid', 'int', ''); | ||
} | ||
|
||
|
||
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string | ||
{ | ||
$maximumNumberOfResultPages = $arguments['maximumNumberOfResultPages']; | ||
$numberOfResults = $arguments['numberOfResults']; | ||
$resultsPerPage = $arguments['resultsPerPage']; | ||
$currentPage = $arguments['currentPage']; | ||
$freeIndexUid = $arguments['freeIndexUid']; | ||
|
||
if ($resultsPerPage <= 0) { | ||
$resultsPerPage = 10; | ||
} | ||
$pageCount = (int)ceil($numberOfResults / $resultsPerPage); | ||
// only show the result browser if more than one page is needed | ||
if ($pageCount === 1) { | ||
return ''; | ||
} | ||
|
||
// Check if $currentPage is in range | ||
$currentPage = MathUtility::forceIntegerInRange($currentPage, 0, $pageCount - 1); | ||
|
||
$content = ''; | ||
// prev page | ||
// show on all pages after the 1st one | ||
if ($currentPage > 0 && !empty($freeIndexUid)) { | ||
$label = LocalizationUtility::translate('displayResults.previous', 'IndexedSearch'); | ||
$content .= '<li>' . self::makecurrentPageSelector_link((string) $label, $currentPage - 1, (string) $freeIndexUid) . '</li>'; | ||
} | ||
// Check if $maximumNumberOfResultPages is in range | ||
$maximumNumberOfResultPages = MathUtility::forceIntegerInRange($maximumNumberOfResultPages, 1, $pageCount, 10); | ||
// Assume $currentPage is in the middle and calculate the index limits of the result page listing | ||
$minPage = $currentPage - (int)floor($maximumNumberOfResultPages / 2); | ||
$maxPage = $minPage + $maximumNumberOfResultPages - 1; | ||
// Check if the indexes are within the page limits | ||
if ($minPage < 0) { | ||
$maxPage -= $minPage; | ||
$minPage = 0; | ||
} elseif ($maxPage >= $pageCount) { | ||
$minPage -= $maxPage - $pageCount + 1; | ||
$maxPage = $pageCount - 1; | ||
} | ||
$pageLabel = ''; | ||
|
||
for ($a = $minPage; $a <= $maxPage; $a++) { | ||
$label = trim($pageLabel . ' ' . ($a + 1)); | ||
if (!empty($freeIndexUid)) { | ||
$label = self::makecurrentPageSelector_link((string) $label, (int) $a, (string) $freeIndexUid); | ||
} | ||
if ($a === $currentPage) { | ||
$content .= '<li class="tx-indexedsearch-browselist-currentPage page-item"><strong>' . $label . '</strong></li>'; | ||
} else { | ||
$content .= '<li class="page-item">' . $label . '</li>'; | ||
} | ||
} | ||
|
||
|
||
// next link | ||
if ($currentPage < $pageCount - 1 && !empty($freeIndexUid)) { | ||
$label = LocalizationUtility::translate('displayResults.next', 'IndexedSearch'); | ||
$content .= '<li>' . self::makecurrentPageSelector_link((string) $label, ($currentPage + 1), (string) $freeIndexUid) . '</li>'; | ||
} | ||
return '<ul class="tx-indexedsearch-browsebox pagination">' . $content . '</ul>'; | ||
} | ||
|
||
/** | ||
* Used to make the link for the result-browser. | ||
* Notice how the links must resubmit the form after setting the new currentPage-value in a hidden formfield. | ||
* | ||
* $str String to wrap in <a> tag | ||
* $p currentPage value | ||
* $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! | ||
*/ | ||
protected static function makecurrentPageSelector_link(string $str, int $p, string $freeIndexUid): string | ||
{ | ||
$onclick = 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_pointer') . ').value=' . GeneralUtility::quoteJSvalue($p) . ';'; | ||
if ($freeIndexUid !== null) { | ||
$onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId . '_freeIndexUid') . ').value=' . GeneralUtility::quoteJSvalue($freeIndexUid) . ';'; | ||
} | ||
$onclick .= 'document.getElementById(' . GeneralUtility::quoteJSvalue(self::$prefixId) . ').submit();return false;'; | ||
return '<a class="page-link" href="#" onclick="' . htmlspecialchars($onclick) . '">' . htmlspecialchars($str) . '</a>'; | ||
} | ||
protected static string $prefixId = 'tx_indexedsearch'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $tagName = 'ul'; | ||
|
||
public function __construct(private readonly AssetCollector $assetCollector) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function initializeArguments(): void | ||
{ | ||
$this->registerArgument('maximumNumberOfResultPages', 'int', '', true); | ||
$this->registerArgument('numberOfResults', 'int', '', true); | ||
$this->registerArgument('resultsPerPage', 'int', '', true); | ||
$this->registerArgument('currentPage', 'int', '', false, 0); | ||
$this->registerArgument('freeIndexUid', 'int', ''); | ||
$this->registerUniversalTagAttributes(); | ||
} | ||
|
||
public function render(): string | ||
{ | ||
$maximumNumberOfResultPages = $this->arguments['maximumNumberOfResultPages']; | ||
$numberOfResults = $this->arguments['numberOfResults']; | ||
$resultsPerPage = $this->arguments['resultsPerPage']; | ||
$currentPage = $this->arguments['currentPage']; | ||
$freeIndexUid = $this->arguments['freeIndexUid']; | ||
|
||
if ($resultsPerPage <= 0) { | ||
$resultsPerPage = 10; | ||
} | ||
$pageCount = (int)ceil($numberOfResults / $resultsPerPage); | ||
// only show the result browser if more than one page is needed | ||
if ($pageCount === 1) { | ||
return ''; | ||
} | ||
|
||
// Check if $currentPage is in range | ||
$currentPage = MathUtility::forceIntegerInRange($currentPage, 0, $pageCount - 1); | ||
|
||
$content = ''; | ||
// prev page | ||
// show on all pages after the 1st one | ||
if ($currentPage > 0) { | ||
$label = LocalizationUtility::translate('displayResults.previous', 'IndexedSearch') ?? ''; | ||
$content .= '<li class="tx-indexedsearch-browselist-prev page-item">' . $this->makecurrentPageSelector_link($label, $currentPage - 1, $freeIndexUid) . '</li>'; | ||
} | ||
// Check if $maximumNumberOfResultPages is in range | ||
$maximumNumberOfResultPages = MathUtility::forceIntegerInRange($maximumNumberOfResultPages, 1, $pageCount, 10); | ||
// Assume $currentPage is in the middle and calculate the index limits of the result page listing | ||
$minPage = $currentPage - (int)floor($maximumNumberOfResultPages / 2); | ||
$maxPage = $minPage + $maximumNumberOfResultPages - 1; | ||
// Check if the indexes are within the page limits | ||
if ($minPage < 0) { | ||
$maxPage -= $minPage; | ||
$minPage = 0; | ||
} elseif ($maxPage >= $pageCount) { | ||
$minPage -= $maxPage - $pageCount + 1; | ||
$maxPage = $pageCount - 1; | ||
} | ||
$pageLabel = LocalizationUtility::translate('displayResults.page', 'IndexedSearch'); | ||
for ($a = $minPage; $a <= $maxPage; $a++) { | ||
$isCurrentPage = $a === $currentPage; | ||
$label = trim($pageLabel . ' ' . ($a + 1)); | ||
$label = $this->makecurrentPageSelector_link($label, $a, $freeIndexUid, $isCurrentPage); | ||
if ($isCurrentPage) { | ||
$content .= '<li class="tx-indexedsearch-browselist-currentPage page-item"><strong>' . $label . '</strong></li>'; | ||
} else { | ||
$content .= '<li class="page-item">' . $label . '</li>'; | ||
} | ||
} | ||
// next link | ||
if ($currentPage < $pageCount - 1) { | ||
$label = LocalizationUtility::translate('displayResults.next', 'IndexedSearch') ?? ''; | ||
$content .= '<li class="tx-indexedsearch-browselist-next page-item">' . $this->makecurrentPageSelector_link($label, $currentPage + 1, $freeIndexUid) . '</li>'; | ||
} | ||
|
||
if (!$this->tag->hasAttribute('class')) { | ||
$this->tag->addAttribute('class', 'tx-indexedsearch-browsebox pagination'); | ||
} | ||
|
||
$this->tag->setContent($content); | ||
|
||
return $this->tag->render(); | ||
} | ||
|
||
/** | ||
* Used to make the link for the result-browser. | ||
* Notice how the links must resubmit the form after setting the new currentPage-value in a hidden formfield. | ||
* | ||
* @param string $str String to wrap in <a> tag | ||
* @param int $p currentPage value | ||
* @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! | ||
* @param bool $isCurrentPage | ||
* @return string Input string wrapped in <a> tag with onclick event attribute set. | ||
*/ | ||
protected function makecurrentPageSelector_link($str, $p, $freeIndexUid, bool $isCurrentPage = false) | ||
{ | ||
$this->providePageSelectorJavaScript(); | ||
$attributes = [ | ||
'href' => '#', | ||
'class' => 'tx-indexedsearch-page-selector page-link', | ||
'data-prefix' => self::$prefixId, | ||
'data-pointer' => $p, | ||
'data-free-index-uid' => $freeIndexUid, | ||
]; | ||
if ($isCurrentPage) { | ||
$attributes['aria-current'] = 'page'; | ||
} | ||
return sprintf( | ||
'<a %s>%s</a>', | ||
GeneralUtility::implodeAttributes($attributes, true), | ||
htmlspecialchars($str) | ||
); | ||
} | ||
|
||
private function providePageSelectorJavaScript(): void | ||
{ | ||
if ($this->assetCollector->hasInlineJavaScript(self::class)) { | ||
return; | ||
} | ||
|
||
$this->assetCollector->addInlineJavaScript( | ||
'vanilla_indexSearch', | ||
implode(' ', [ | ||
"document.addEventListener('click', (evt) => {", | ||
"if (!evt.target.classList.contains('tx-indexedsearch-page-selector')) {", | ||
'return;', | ||
'}', | ||
'evt.preventDefault();', | ||
'var data = evt.target.dataset;', | ||
"document.getElementById(data.prefix + '_pointer').value = data.pointer;", | ||
"document.getElementById(data.prefix + '_freeIndexUid').value = data.freeIndexUid;", | ||
'document.getElementById(data.prefix).submit();', | ||
'});', | ||
]), | ||
[], | ||
['useNonce' => true], | ||
); | ||
} | ||
} |
Oops, something went wrong.