GetNextViewHelper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Events;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use FluidTYPO3\Vhs\Utility\ErrorUtility;
  5. use Opentalent\OtTemplating\Domain\Repository\EventRepository;
  6. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  7. /**
  8. * This view helper provides an array named according to the 'as' variable
  9. * and which contains the next events of the structure
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:events.getNext as="events"
  14. * organizationId="{settings.organizationId}"
  15. * limit="{settings.eventsLimit}"
  16. * period="{settings.eventsPeriod}">
  17. * <f:debug>{events}</f:debug>
  18. * </ot:events.getNext>
  19. *
  20. * @package Opentalent\OtTemplating\ViewHelpers
  21. */
  22. class GetNextViewHelper extends AbstractViewHelper {
  23. use TemplateVariableViewHelperTrait;
  24. /**
  25. * >> Required to prevent typo3 to escape the html output
  26. * @var boolean
  27. */
  28. protected $escapeOutput = false;
  29. /**
  30. * @var \Opentalent\OtTemplating\Domain\Repository\EventRepository
  31. *
  32. */
  33. protected $eventRepository;
  34. public function initializeArguments()
  35. {
  36. $this->registerArgument(
  37. 'as',
  38. 'string',
  39. 'Name of the returned array',
  40. true
  41. );
  42. $this->registerArgument(
  43. 'organizationId',
  44. 'integer',
  45. 'Id of the current structure',
  46. true
  47. );
  48. $this->registerArgument(
  49. 'limit',
  50. 'integer',
  51. 'Max number of events to return (min: 1, max: 24)',
  52. false,
  53. 5
  54. );
  55. $this->registerArgument(
  56. 'period',
  57. 'integer',
  58. 'Limit the result to the N next weeks',
  59. false,
  60. 8
  61. );
  62. $this->registerArgument(
  63. 'fromParents',
  64. 'bool',
  65. 'Get events from parents instead',
  66. false,
  67. 0
  68. );
  69. $this->registerArgument(
  70. 'fromChildren',
  71. 'bool',
  72. 'Get events from children instead',
  73. false,
  74. 0
  75. );
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function render()
  81. {
  82. // Get current settings
  83. $as = $this->arguments['as'];
  84. $organizationId = $this->arguments['organizationId'];
  85. $limit = $this->arguments['limit'];
  86. $period = $this->arguments['period'];
  87. $fromParents = $this->arguments['fromParents'];
  88. $fromChildren = $this->arguments['fromChildren'];
  89. if (!$organizationId) {
  90. ErrorUtility::throwViewHelperException('Organization id is missing');
  91. }
  92. $fromDate = new \DateTime();
  93. $toDate = null;
  94. if ($period > 0) {
  95. $nbDays = 7 * $period;
  96. $toDate = new \DateTime();
  97. $interval = new \DateInterval('P' . $nbDays . 'D');
  98. $toDate->add($interval);
  99. }
  100. if ($limit < 1) {
  101. $limit = 1; // $limit can not be lesser than 1
  102. }
  103. if ($limit > 24) {
  104. $limit = 24; // $limit can not be higher than 24
  105. }
  106. // Get next events of the structure
  107. if ($fromChildren) {
  108. // Network: Get the next events of the parent structures
  109. $events = $this->eventRepository->findChildrenByOrganizationId($organizationId, $fromDate, $toDate, $limit);
  110. } else if ($fromParents) {
  111. // Simple structure: Get the next events of the parent structures
  112. $events = $this->eventRepository->findParentsByOrganizationId($organizationId, $fromDate, $toDate, $limit);
  113. } else {
  114. $events = $this->eventRepository->findByOrganizationId($organizationId, $fromDate, $toDate, $limit);
  115. }
  116. $variables = [$as => $events];
  117. return $this->renderChildrenWithVariables($variables);
  118. }
  119. /**
  120. * @param \Opentalent\OtTemplating\Domain\Repository\EventRepository $eventRepository
  121. */
  122. public function injectEventRepository(EventRepository $eventRepository)
  123. {
  124. $this->eventRepository = $eventRepository;
  125. }
  126. }