GetNextViewHelper.php 4.5 KB

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