GetNextViewHelper.php 4.8 KB

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