GetNextViewHelper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Events;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  5. use Opentalent\OtCore\Domain\Repository\EventRepository;
  6. use Opentalent\OtCore\Exception\ApiRequestException;
  7. /**
  8. * This view helper provides an ApiPagedCollection 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 OtAbstractViewHelper {
  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 EventRepository
  31. *
  32. */
  33. protected $eventRepository;
  34. /**
  35. * -- This method is expected by Fluid --
  36. * Declares the viewhelper's parameters
  37. */
  38. public function initializeArguments()
  39. {
  40. $this->registerArgument(
  41. 'as',
  42. 'string',
  43. 'Name of the returned array',
  44. true
  45. );
  46. $this->registerArgument(
  47. 'organizationId',
  48. 'integer',
  49. 'Id of the current structure',
  50. true
  51. );
  52. $this->registerArgument(
  53. 'limit',
  54. 'integer',
  55. 'Max number of events to return (min: 1, max: 24)',
  56. false,
  57. 5
  58. );
  59. $this->registerArgument(
  60. 'period',
  61. 'integer',
  62. 'Limit the result to the N next weeks',
  63. false,
  64. 8
  65. );
  66. $this->registerArgument(
  67. 'fromParents',
  68. 'bool',
  69. 'Get events from parents instead',
  70. false,
  71. 0
  72. );
  73. $this->registerArgument(
  74. 'fromChildren',
  75. 'bool',
  76. 'Get events from children instead',
  77. false,
  78. 0
  79. );
  80. }
  81. /**
  82. * -- This method is expected by Fluid --
  83. * Renders the content as html
  84. *
  85. * @return string
  86. * @throws ApiRequestException
  87. * @throws \Exception
  88. */
  89. public function render()
  90. {
  91. // Get current settings
  92. $as = $this->arguments['as'];
  93. $organizationId = $this->arguments['organizationId'];
  94. $limit = $this->arguments['limit'];
  95. $period = $this->arguments['period'];
  96. $fromParents = $this->arguments['fromParents'];
  97. $fromChildren = $this->arguments['fromChildren'];
  98. if (!$organizationId) {
  99. $this->throwException('Organization id is missing');
  100. }
  101. $fromDate = new \DateTime();
  102. $toDate = null;
  103. if ($period > 0) {
  104. $nbDays = 7 * $period;
  105. $toDate = new \DateTime();
  106. $interval = new \DateInterval('P' . $nbDays . 'D');
  107. $toDate->add($interval);
  108. }
  109. if ($limit < 1) {
  110. $limit = 1; // $limit can not be lesser than 1
  111. }
  112. if ($limit > 24) {
  113. $limit = 24; // $limit can not be higher than 24
  114. }
  115. // Get next events of the structure
  116. try {
  117. if ($fromChildren) {
  118. // Network: Get the next events of the parent structures
  119. $events = $this->eventRepository->findChildrenByOrganizationId($organizationId, $fromDate, $toDate, $limit);
  120. } else if ($fromParents) {
  121. // Simple structure: Get the next events of the parent structures
  122. $events = $this->eventRepository->findParentsByOrganizationId($organizationId, $fromDate, $toDate, $limit);
  123. } else {
  124. $events = $this->eventRepository->findByOrganizationId($organizationId, $fromDate, $toDate, $limit);
  125. }
  126. } catch (ApiRequestException $e) {
  127. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  128. $events = [];
  129. }
  130. $variables = [$as => $events];
  131. return $this->renderChildrenWithVariables($variables);
  132. }
  133. /**
  134. * @param EventRepository $eventRepository
  135. */
  136. public function injectEventRepository(EventRepository $eventRepository)
  137. {
  138. $this->eventRepository = $eventRepository;
  139. }
  140. }