PaginationViewHelper.php 2.9 KB

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