GetNextViewHelper.php 4.7 KB

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