PaginationViewHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers;
  3. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  4. use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
  5. use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper;
  6. /**
  7. *
  8. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  9. *
  10. * {ot:pagination(collection:collection)}
  11. *
  12. * @package Opentalent\OtTemplating\ViewHelpers
  13. */
  14. class PaginationViewHelper extends OtAbstractViewHelper
  15. {
  16. /**
  17. * >> Required to prevent typo3 to escape the html output
  18. * @var boolean
  19. */
  20. protected $escapeOutput = false;
  21. /**
  22. * -- This method is expected by Fluid --
  23. * Declares the viewhelper's parameters
  24. */
  25. public function initializeArguments()
  26. {
  27. $this->registerArgument(
  28. 'collection',
  29. 'object',
  30. 'The ApiPagedCollection object',
  31. true
  32. );
  33. }
  34. /**
  35. * -- This method is expected by Fluid --
  36. * Renders the content as html
  37. *
  38. * @return string Rendered tag
  39. */
  40. public function render() {
  41. $collection = $this->arguments['collection'];
  42. $lastPage = $collection->getLastPage();
  43. $currentPage = $collection->getCurrentPage();
  44. if (!$lastPage > 0) {
  45. return "";
  46. }
  47. $divWrapper = '<div class="pagination-bar">%s</div>';
  48. $goToFirst = '<a href="' . $this->getUriWithPage(1) . '" ' .
  49. 'title="' . $this->translate('go-to-first-page') . '">' .
  50. '<i class="fa fa-angle-double-left"></i>' .
  51. '</a>';
  52. $goToLast = '<a href="' . $this->getUriWithPage($lastPage) . '"' .
  53. 'title="' . $this->translate('go-to-last-page') . '">' .
  54. '<i class="fa fa-angle-double-right"></i>' .
  55. '</a>';
  56. $startAt = $currentPage > 6 ? $currentPage - 5 : 1;
  57. $endAt = $currentPage < ($lastPage - 5) ? $currentPage + 5 : $lastPage;
  58. $ul = '<ul>';
  59. for ($i = $startAt; $i <= $endAt; $i++) {
  60. $ul .= '<li class="' . ($i == $currentPage ? 'current' : '') . '">';
  61. $ul .= '<a href="' . $this->getUriWithPage($i) . '" title="' . $this->translate('go-to-page') . $i .'">' . $i . '</a>';
  62. $ul .= '</li>';
  63. }
  64. $ul .= '</ul>';
  65. return sprintf($divWrapper, $goToFirst . $ul . $goToLast);
  66. }
  67. private function getUriWithPage(int $page, bool $nocache = true) {
  68. $request = $GLOBALS['TYPO3_REQUEST'];
  69. $uri = $request->getUri();
  70. $query = $uri->getQuery();
  71. if (preg_match("/.*page=\d+.*/", $query)) {
  72. $query = preg_replace(
  73. "/page=\d+/",
  74. "page=" . $page,
  75. $query
  76. );
  77. } elseif ($query != '') {
  78. $query .= "&page=" . $page;
  79. } else {
  80. $query .= "page=" . $page;
  81. }
  82. $query = preg_replace("/&no_cache=1/", "", $query);
  83. if ($nocache) {
  84. $query .= '&no_cache=1';
  85. }
  86. return (string)$uri->withQuery($query);
  87. }
  88. }