GetAllViewHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\ViewHelpers\OtAbstractViewHelper;
  6. use Opentalent\OtCore\Domain\Repository\EventRepository;
  7. use Opentalent\OtCore\Exception\ApiRequestException;
  8. /**
  9. * This view helper give access to an ApiPagedCollection named according to the 'as' variable
  10. * and containing all the upcoming events of the structure
  11. *
  12. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  13. *
  14. * <ot:events.getAll as="events"
  15. * organizationId="{settings.organizationId}">
  16. * <f:debug>{events}</f:debug>
  17. * </ot:events.getAll>
  18. *
  19. * @package Opentalent\OtTemplating\ViewHelpers
  20. */
  21. class GetAllViewHelper extends OtAbstractViewHelper {
  22. use TemplateVariableViewHelperTrait;
  23. /**
  24. * >> Required to prevent typo3 to escape the html output
  25. * @var boolean
  26. */
  27. protected $escapeOutput = false;
  28. /**
  29. * @var EventRepository
  30. *
  31. */
  32. protected $eventRepository;
  33. /**
  34. * -- This method is expected by Fluid --
  35. * Declares the viewhelper's parameters
  36. */
  37. public function initializeArguments()
  38. {
  39. $this->registerArgument(
  40. 'as',
  41. 'string',
  42. 'Name of the returned array',
  43. true
  44. );
  45. $this->registerArgument(
  46. 'organizationId',
  47. 'integer',
  48. 'Id of the current structure',
  49. true
  50. );
  51. $this->registerArgument(
  52. 'fromChildren',
  53. 'bool',
  54. 'Get events from children instead',
  55. false,
  56. 0
  57. );
  58. }
  59. /**
  60. * -- This method is expected by Fluid --
  61. * Renders the content as html
  62. *
  63. * @return string
  64. * @throws ApiRequestException|\Exception
  65. */
  66. public function render()
  67. {
  68. // Get current settings
  69. $as = $this->arguments['as'];
  70. $organizationId = $this->arguments['organizationId'];
  71. $fromChildren = $this->arguments['fromChildren'];
  72. $searchParams = [];
  73. if($_REQUEST['search-loc']) {
  74. $searchParams['where'] = $_REQUEST['search-loc'];
  75. }
  76. if($_REQUEST['search-name']) {
  77. $searchParams['what'] = $_REQUEST['search-name'];
  78. }
  79. if($_REQUEST['search-datestart']) {
  80. $dateStart = \DateTime::createFromFormat('d/m/Y', $_REQUEST['search-datestart']);
  81. $searchParams['datetimeStart'] = $dateStart->format('d-m-Y');
  82. }
  83. if($_REQUEST['search-dateend']) {
  84. $dateEnd = \DateTime::createFromFormat('d/m/Y', $_REQUEST['search-dateend']);
  85. $searchParams['datetimeEnd'] = $dateEnd->format('d-m-Y');
  86. }
  87. if ($fromChildren) {
  88. $searchParams['children'] = '1';
  89. }
  90. try {
  91. $events = $this->eventRepository->searchBy($organizationId, $searchParams);
  92. } catch (ApiRequestException $e) {
  93. var_dump($e);die;
  94. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  95. $events = new ApiPagedCollection(0, 0, 1, []);
  96. }
  97. $variables = [$as => $events];
  98. return $this->renderChildrenWithVariables($variables);
  99. }
  100. /**
  101. * @param EventRepository $eventRepository
  102. */
  103. public function injectEventRepository(EventRepository $eventRepository)
  104. {
  105. $this->eventRepository = $eventRepository;
  106. }
  107. }