GetAllViewHelper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Events;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Domain\Repository\ApiPagedCollection;
  5. use Opentalent\OtCore\Logging\OtLogger;
  6. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  7. use Opentalent\OtCore\Domain\Repository\EventRepository;
  8. use Opentalent\OtCore\Exception\ApiRequestException;
  9. /**
  10. * This view helper give access to an ApiPagedCollection named according to the 'as' variable
  11. * and containing all the upcoming events of the structure
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * <ot:events.getAll as="events"
  16. * organizationId="{settings.organizationId}">
  17. * <f:debug>{events}</f:debug>
  18. * </ot:events.getAll>
  19. *
  20. * @package Opentalent\OtTemplating\ViewHelpers
  21. */
  22. class GetAllViewHelper 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. 'fromChildren',
  54. 'bool',
  55. 'Get events from children instead',
  56. false,
  57. 0
  58. );
  59. }
  60. /**
  61. * -- This method is expected by Fluid --
  62. * Renders the content as html
  63. *
  64. * @return string
  65. * @throws ApiRequestException|\Exception
  66. */
  67. public function render()
  68. {
  69. // Get current settings
  70. $as = $this->arguments['as'];
  71. $organizationId = $this->arguments['organizationId'];
  72. $fromChildren = $this->arguments['fromChildren'];
  73. $searchParams = [];
  74. if($_REQUEST['search-loc']) {
  75. $searchParams['where'] = $_REQUEST['search-loc'];
  76. }
  77. if($_REQUEST['search-name']) {
  78. $searchParams['what'] = $_REQUEST['search-name'];
  79. }
  80. if($_REQUEST['search-datestart']) {
  81. $dateStart = \DateTime::createFromFormat('d/m/Y', $_REQUEST['search-datestart']);
  82. $searchParams['datetimeStart'] = $dateStart->format('d-m-Y');
  83. }
  84. if($_REQUEST['search-dateend']) {
  85. $dateEnd = \DateTime::createFromFormat('d/m/Y', $_REQUEST['search-dateend']);
  86. $searchParams['datetimeEnd'] = $dateEnd->format('d-m-Y');
  87. }
  88. if ($fromChildren) {
  89. $searchParams['children'] = '1';
  90. }
  91. if ($_REQUEST['page']) {
  92. $searchParams['page'] = $_REQUEST['page'];
  93. }
  94. try {
  95. $events = $this->eventRepository->searchBy($organizationId, $searchParams);
  96. } catch (ApiRequestException $e) {
  97. OtLogger::error(sprintf('API Error: %s', $e->getMessage()));
  98. $events = new ApiPagedCollection(0, 0, 1, []);
  99. }
  100. $variables = [$as => $events];
  101. return $this->renderChildrenWithVariables($variables);
  102. }
  103. /**
  104. * @param EventRepository $eventRepository
  105. */
  106. public function injectEventRepository(EventRepository $eventRepository)
  107. {
  108. $this->eventRepository = $eventRepository;
  109. }
  110. }