WithPageViewHelper.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Request;
  3. use Closure;
  4. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  5. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  6. /**
  7. * Returns the current url with a 'page=' argument set up or updated
  8. * Warning: this viewhelper will also add a 'no_cache=1' argument to the query
  9. *
  10. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  11. *
  12. * {ot:request.withPage(page: 1)}
  13. *
  14. * @package Opentalent\OtTemplating\ViewHelpers
  15. */
  16. class WithPageViewHelper extends OtAbstractViewHelper {
  17. /**
  18. * -- This method is expected by Fluid --
  19. * Declares the viewhelper's parameters
  20. */
  21. public function initializeArguments()
  22. {
  23. $this->registerArgument('page',
  24. 'int',
  25. "The page's number",
  26. true);
  27. $this->registerArgument('nocache',
  28. 'int',
  29. "If true, adds a no_cache parameter to the query",
  30. false,
  31. 1
  32. );
  33. }
  34. /**
  35. * -- This method is expected by Fluid --
  36. * Renders the content as html
  37. *
  38. * @param array $arguments
  39. * @param Closure $renderChildrenClosure
  40. * @param RenderingContextInterface $renderingContext
  41. * @return string
  42. */
  43. public static function renderStatic(
  44. array $arguments,
  45. Closure $renderChildrenClosure,
  46. RenderingContextInterface $renderingContext
  47. ) {
  48. $page = $arguments['page'];
  49. $nocache = $arguments['nocache'];
  50. $request = $GLOBALS['TYPO3_REQUEST'];
  51. $uri = $request->getUri();
  52. $query = $uri->getQuery();
  53. if (preg_match("/.*page=\d+.*/", $query)) {
  54. $query = preg_replace(
  55. "/page=\d+/",
  56. "page=" . $page,
  57. $query
  58. );
  59. } elseif ($query != '') {
  60. $query .= "&page=" . $page;
  61. } else {
  62. $query .= "page=" . $page;
  63. }
  64. $query = preg_replace("/&no_cache=1/", "", $query);
  65. if ($nocache) {
  66. $query .= '&no_cache=1';
  67. }
  68. return (string)$uri->withQuery($query);
  69. }
  70. }